100 lines
3.7 KiB
Java
100 lines
3.7 KiB
Java
package org.gcube.data.access.ckanconnector;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.net.URI;
|
|
|
|
import javax.servlet.ServletContext;
|
|
import javax.ws.rs.GET;
|
|
import javax.ws.rs.Path;
|
|
import javax.ws.rs.core.Context;
|
|
import javax.ws.rs.core.NewCookie;
|
|
import javax.ws.rs.core.Response;
|
|
import javax.ws.rs.core.Response.Status;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.python.core.PyLong;
|
|
import org.python.core.PyObject;
|
|
import org.python.core.PyString;
|
|
import org.python.util.PythonInterpreter;
|
|
|
|
import eu.trentorise.opendata.jackan.CkanClient;
|
|
import eu.trentorise.opendata.jackan.model.CkanUser;
|
|
|
|
@Path("connect/")
|
|
@Slf4j
|
|
public class ConnectorManager {
|
|
|
|
@Context ServletContext context;
|
|
|
|
RandomString randomString = new RandomString(12);
|
|
|
|
@GET
|
|
public Response connect() {
|
|
try{
|
|
if (AuthorizationProvider.instance.get()==null || AuthorizationProvider.instance.get().getUserName() == null ) return Response.status(Status.UNAUTHORIZED).build();
|
|
String ckanKey = context.getInitParameter("ckanKey");
|
|
String changedUserName = AuthorizationProvider.instance.get().getUserName().replace(".", "_");
|
|
int internalPort = Integer.parseInt(context.getInitParameter("internalPort"));
|
|
CkanClient ckanClient = new CkanClient("http://127.0.0.1:"+internalPort, ckanKey);
|
|
CkanUser user = null;
|
|
try{
|
|
user = ckanClient.getUser(changedUserName);
|
|
}catch(Exception e){
|
|
log.warn("user {} doesn't exist, the system will create it",AuthorizationProvider.instance.get().getUserName(), e);
|
|
}
|
|
if (user==null)
|
|
ckanClient.createUser(new CkanUser(changedUserName, AuthorizationProvider.instance.get().getUserName()+"@gcube.ckan.org" , randomString.nextString() ));
|
|
|
|
log.info("logging {} in scope {}",AuthorizationProvider.instance.get().getUserName(), ScopeProvider.instance.get());
|
|
return createResponse(changedUserName);
|
|
}catch(Exception e){
|
|
log.info("error trying to connect to CKAN",e);
|
|
return Response.serverError().entity(e.getMessage()).build();
|
|
}
|
|
}
|
|
|
|
private Response createResponse(String userName){
|
|
try{
|
|
String secret = context.getInitParameter("secret");
|
|
String hostIp = context.getInitParameter("hostIp");
|
|
String hostname = context.getInitParameter("hostname");
|
|
String fixedData = "userid_type:unicode";
|
|
PythonInterpreter interpreter = new PythonInterpreter();
|
|
interpreter.execfile(new FileInputStream(new File(this.getClass().getClassLoader().getResource("digest.py").getFile())));
|
|
PyObject someFunc = interpreter.get("calculate_digest");
|
|
long currentMillis = System.currentTimeMillis()/1000;
|
|
PyObject ret = someFunc.__call__(new PyObject[]{new PyString(hostIp), new PyLong(currentMillis), new PyString(secret),
|
|
new PyString(userName), new PyString(""), new PyString(fixedData)} );
|
|
String realResult = (String) ret.__tojava__(String.class);
|
|
|
|
String timestamp16 = Long.toString(currentMillis, 16);
|
|
|
|
/*Calendar now = Calendar.getInstance();
|
|
now.add(Calendar.YEAR, 10);
|
|
now.add(Calendar.DAY_OF_YEAR, -1);
|
|
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
|
|
String format = sdf.format(now.getTime());
|
|
*/
|
|
String cookieValue = realResult+timestamp16+userName+"!"+fixedData;
|
|
NewCookie cookie = new NewCookie("auth_tkt",
|
|
cookieValue,
|
|
"/", hostname, "", -1, false, true );
|
|
|
|
|
|
NewCookie cookieHideHeader = new NewCookie("ckan_hide_header", "true",
|
|
"/", hostname, "", -1, false, true );
|
|
|
|
return Response.seeOther(new URI("https://"+hostname))
|
|
.cookie(cookie).cookie(cookieHideHeader).build();
|
|
}catch(Exception e){
|
|
e.printStackTrace();
|
|
return Response.serverError().build();
|
|
}
|
|
}
|
|
|
|
}
|