|
|
|
@ -1,8 +1,7 @@
|
|
|
|
|
package org.gcube.portlet.user.my_vres.server;
|
|
|
|
|
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
@ -11,13 +10,6 @@ import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
|
import org.apache.http.impl.client.LaxRedirectStrategy;
|
|
|
|
|
import org.gcube.common.portal.GCubePortalConstants;
|
|
|
|
|
import org.gcube.common.portal.PortalContext;
|
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
@ -43,6 +35,8 @@ import org.slf4j.LoggerFactory;
|
|
|
|
|
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
|
|
|
|
import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
|
|
|
|
|
import net.spy.memcached.MemcachedClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The server side implementation of the RPC service.
|
|
|
|
|
* @author Massimiliano Assante - ISTI CNR
|
|
|
|
@ -62,6 +56,11 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
|
|
|
|
|
|
|
|
|
|
public static final String ADD_MORE_CATEGORY = "Add More";
|
|
|
|
|
public static final String ADD_MORE_IMAGE_PATH= "images/More.png";
|
|
|
|
|
private static final int CACHE_SECONDS_EXPIRATION = 10;
|
|
|
|
|
/**
|
|
|
|
|
* This map contains couples as <code, {qualifier-token, insert time, scope, redirect uri, client id}>
|
|
|
|
|
*/
|
|
|
|
|
private MemcachedClient entries;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getSiteLandingPagePath() {
|
|
|
|
@ -86,10 +85,9 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
|
|
|
|
|
* first method called by the UI
|
|
|
|
|
*/
|
|
|
|
|
public LinkedHashMap<String, ArrayList<VRE>> getUserVREs() {
|
|
|
|
|
//_log.trace("getInfrastructureVOs method called");
|
|
|
|
|
_log.trace("getInfrastructureVOs method called");
|
|
|
|
|
if (!isWithinPortal())
|
|
|
|
|
return getFakeVREs();
|
|
|
|
|
//return new ArrayList<VO>();
|
|
|
|
|
else
|
|
|
|
|
try {
|
|
|
|
|
PortalContext context = PortalContext.getConfiguration();
|
|
|
|
@ -106,8 +104,6 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
|
|
|
|
|
String cat = gName;
|
|
|
|
|
toReturn.put(cat, toCreate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GCubeGroup rootGroupVO = gm.getRootVO();
|
|
|
|
|
try {
|
|
|
|
|
_log.debug("root: " + rootGroupVO.getGroupName() );
|
|
|
|
@ -344,7 +340,7 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
|
|
|
|
|
return new AuthorizationBean(tempCode, state, true, null);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* we post to the auth service a temporary code to be used within seconds from the application
|
|
|
|
|
* post to the memcached cluster a temporary code to be used within 10 seconds from the application
|
|
|
|
|
* @param qToken the user qualifier token
|
|
|
|
|
* @param tempCode the temporary code
|
|
|
|
|
* @param clientId tha app id
|
|
|
|
@ -353,73 +349,23 @@ public class MyVREsServiceImpl extends RemoteServiceServlet implements MyVREsSer
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private boolean authorizeApplication(String infrastructureName, String qToken, String tempCode, String clientId, String redirectURL) {
|
|
|
|
|
|
|
|
|
|
String fullPath2oAuthService = null;
|
|
|
|
|
try {
|
|
|
|
|
fullPath2oAuthService = AuthUtil.getOAuthServiceEndPoint(infrastructureName) +
|
|
|
|
|
"/v2/push-authentication-code?gcube-token=" + qToken;
|
|
|
|
|
} catch (Exception e1) {
|
|
|
|
|
_log.error("failed to discover oauth service endpoint ");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject object = new JSONObject();
|
|
|
|
|
object.put("code", tempCode);
|
|
|
|
|
object.put("redirect_uri", redirectURL);
|
|
|
|
|
object.put("client_id", clientId);
|
|
|
|
|
try {
|
|
|
|
|
String USER_AGENT = "Mozilla/5.0";
|
|
|
|
|
URL obj = new URL(fullPath2oAuthService);
|
|
|
|
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
|
|
|
|
|
|
|
|
// Setting basic post request
|
|
|
|
|
con.setRequestMethod("POST");
|
|
|
|
|
con.setRequestProperty("User-Agent", USER_AGENT);
|
|
|
|
|
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
|
|
con.setRequestProperty("Content-Type","application/json");
|
|
|
|
|
|
|
|
|
|
String postJsonData = object.toJSONString();
|
|
|
|
|
|
|
|
|
|
con.setDoOutput(true);
|
|
|
|
|
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
|
|
|
|
wr.writeBytes(postJsonData);
|
|
|
|
|
wr.flush();
|
|
|
|
|
wr.close();
|
|
|
|
|
|
|
|
|
|
int responseCode = con.getResponseCode();
|
|
|
|
|
|
|
|
|
|
boolean redirect = false;
|
|
|
|
|
// normally, 3xx is redirect
|
|
|
|
|
int status = con.getResponseCode();
|
|
|
|
|
if (status != HttpURLConnection.HTTP_OK) {
|
|
|
|
|
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|
|
|
|
|
|| status == HttpURLConnection.HTTP_MOVED_PERM
|
|
|
|
|
|| status == HttpURLConnection.HTTP_SEE_OTHER)
|
|
|
|
|
redirect = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (redirect)
|
|
|
|
|
// get redirect url from "location" header field
|
|
|
|
|
fullPath2oAuthService = con.getHeaderField("Location");
|
|
|
|
|
|
|
|
|
|
CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
|
|
|
|
|
HttpPost httpPostRequest = new HttpPost(fullPath2oAuthService);
|
|
|
|
|
httpPostRequest.addHeader("Content-type", "application/json");
|
|
|
|
|
StringEntity params = new StringEntity(object.toJSONString(), ContentType.APPLICATION_JSON);
|
|
|
|
|
httpPostRequest.setEntity(params);
|
|
|
|
|
HttpResponse response = httpClient.execute(httpPostRequest);
|
|
|
|
|
responseCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
if (responseCode < 200 || responseCode >= 300) {
|
|
|
|
|
_log.error("error: response status line from "
|
|
|
|
|
+ fullPath2oAuthService + " was: " + responseCode);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
}catch(Exception e){
|
|
|
|
|
_log.error("Failed to perform request", e);
|
|
|
|
|
//instance the client if first time
|
|
|
|
|
if (entries == null)
|
|
|
|
|
entries = DistributedCacheClient.getInstance().getMemcachedClient();
|
|
|
|
|
//retrieve the context of the token owner
|
|
|
|
|
String context = authorizationService().get(qToken).getContext();
|
|
|
|
|
JSONObject object = new JSONObject();
|
|
|
|
|
object.put("token", qToken);
|
|
|
|
|
object.put("context", context);
|
|
|
|
|
object.put("redirect_uri", redirectURL);
|
|
|
|
|
object.put("client_id", clientId);
|
|
|
|
|
String JSONData = object.toJSONString();
|
|
|
|
|
entries.set(tempCode, CACHE_SECONDS_EXPIRATION, JSONData);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|