From f6de43b5ef85a2267727954572771b1a7a8f201c Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Mon, 16 Sep 2019 15:58:27 +0200 Subject: [PATCH] Feature #17265, provide oAuth2 service with capability to be deployed on a multi instance cluster --- .classpath | 11 -- .settings/org.eclipse.wst.common.component | 3 - pom.xml | 50 ++++++--- .../my_vres/server/MyVREsServiceImpl.java | 106 +++++------------- 4 files changed, 63 insertions(+), 107 deletions(-) diff --git a/.classpath b/.classpath index 1969db5..05c831d 100644 --- a/.classpath +++ b/.classpath @@ -22,17 +22,6 @@ - - - - - - - - - - - diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 3ff7ea6..a14104e 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -5,9 +5,6 @@ - - uses - diff --git a/pom.xml b/pom.xml index 8c53d0a..51c1037 100644 --- a/pom.xml +++ b/pom.xml @@ -1,31 +1,33 @@ - 4.0.0 maven-parent org.gcube.tools - 1.0.0 + 1.1.0-SNAPSHOT + org.gcube.portlets.user my-vres war - 2.5.0-SNAPSHOT + 2.6.0-SNAPSHOT My VREs Portlet gCube My VREs Portlet shows only the VO and VREs a user is member of, divided by category. - scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/${project.artifactId} - scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/${project.artifactId} - http://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/${project.artifactId} + scm:git:https://code-repo.d4science.org/gCubeSystem/my-vres.git + scm:git:https://code-repo.d4science.org/gCubeSystem/my-vres.git + https://code-repo.d4science.org/gCubeSystem/my-vres - 2.8.1 + 2.8.2 distro 6.2.5 1.8 @@ -77,11 +79,6 @@ json-simple compile - - org.apache.httpcomponents - httpclient - 4.5.3 - org.gcube.resources.discovery @@ -91,7 +88,6 @@ org.gcube.portal.auth portal-auth-library - [1.0.0-SNAPSHOT,) provided @@ -104,6 +100,16 @@ common-encryption provided + + org.gcube.common + authorization-client + provided + + + net.spy + spymemcached + 2.12.3 + com.liferay.portal portal-service @@ -221,6 +227,24 @@ + + org.apache.maven.plugins + maven-javadoc-plugin + + -Xdoclint:none + -Xdoclint:none + + 3.1.0 + + + generate-doc + install + + jar + + + + diff --git a/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java b/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java index 93345d6..7294c49 100644 --- a/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java +++ b/src/main/java/org/gcube/portlet/user/my_vres/server/MyVREsServiceImpl.java @@ -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 + */ + 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> getUserVREs() { - //_log.trace("getInfrastructureVOs method called"); + _log.trace("getInfrastructureVOs method called"); if (!isWithinPortal()) return getFakeVREs(); - //return new ArrayList(); 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; }