From d579a00dbacd0a7e093f4a3a52d7f12028e807d2 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 2 Feb 2017 11:01:35 +0000 Subject: [PATCH] added extension to support oAuth2 requesting applications with error handling git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/portal-auth-library@142053 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../java/org/gcube/portal/auth/AuthUtil.java | 89 +++++++------------ .../java/org/gcube/portal/auth/AppTest.java | 13 ++- 2 files changed, 36 insertions(+), 66 deletions(-) diff --git a/src/main/java/org/gcube/portal/auth/AuthUtil.java b/src/main/java/org/gcube/portal/auth/AuthUtil.java index 6490407..ac09ad1 100644 --- a/src/main/java/org/gcube/portal/auth/AuthUtil.java +++ b/src/main/java/org/gcube/portal/auth/AuthUtil.java @@ -1,17 +1,14 @@ package org.gcube.portal.auth; +import java.io.IOException; +import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.dom.DOMSource; - import org.gcube.common.portal.PortalContext; import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.ServiceEndpoint; @@ -20,9 +17,6 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.Property; import org.gcube.common.resources.gcore.utils.Group; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; @@ -42,65 +36,42 @@ public class AuthUtil { public final static String ENDPOINT_TYPE = "ServiceEndpoint"; public final static String ENDPOINT_CATEGORY = "OnlineService"; - public static List getPortalConfigurationFromIS(String clientId) throws Exception { - List toReturn = new ArrayList<>(); - String encodedAppName = URLEncoder.encode(clientId, "UTF-8").replaceAll("\\+", "%20"); - String icproxyEndPoint = PortalContext.getICProxyEndPoint(); - String callToICProxy = new StringBuilder(icproxyEndPoint) - .append("/") - .append(ENDPOINT_TYPE) - .append("/") - .append(ENDPOINT_CATEGORY) - .append("/") - .append(encodedAppName) - .toString(); - - URL pageURL = new URL(callToICProxy); - URLConnection siteConnection = (HttpURLConnection) pageURL.openConnection(); - String portalToken = PortalContext.getPortalApplicationToken(); - siteConnection.addRequestProperty(TOKEN_ATTR_NAME, portalToken); - - //parse the service endpoints - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - Document document = factory.newDocumentBuilder().parse(siteConnection.getInputStream()); - NodeList nodeList = document.getDocumentElement().getChildNodes(); - - for (int i = 0; i < nodeList.getLength(); i++) { - Node node = nodeList.item(i); - ServiceEndpoint res = Resources.unmarshal(ServiceEndpoint.class, new DOMSource(node)); - toReturn.add(res); - } - return toReturn; - } - /** * look for the clientId AccessEndpoint passes as parameter * @param clientId - * @return a RequestingApp contanining the application name, the description and the application logo URL if any, or null if non existent + * @return a RequestingApp contanining the application name, the description and the application logo URL if any, or null if non existent */ public static RequestingApp getAuthorisedApplicationInfoFromIs(String clientId) { RequestingApp toReturn = new RequestingApp(); - String infraName = PortalContext.getConfiguration().getInfrastructureName(); try { - List list = getPortalConfigurationFromIS(clientId); - if (list.size() > 1) { - _log.error("Too many Service Endpoints having name " + clientId +" in this scope having Category " + SERVICE_ENDPOINT_CATEGORY); + String encodedClientId = URLEncoder.encode(clientId, "UTF-8").replaceAll("\\+", "%20"); + String icproxyEndPoint = PortalContext.getICProxyEndPoint(); + String callToICProxy = new StringBuilder(icproxyEndPoint) + .append("/") + .append(encodedClientId) + .toString(); + + URL pageURL = new URL(callToICProxy); + URLConnection siteConnection = (HttpURLConnection) pageURL.openConnection(); + String portalToken = PortalContext.getPortalApplicationToken(); + siteConnection.addRequestProperty(TOKEN_ATTR_NAME, portalToken); + InputStream is = null; + try { + is = siteConnection.getInputStream(); + } + catch (IOException e) { + _log.warn("The requested clientId does not exist: " + encodedClientId); return null; - } else if (list.size() == 0) { - _log.warn("There is no Service Endpoint having name " + clientId +" and Category " + SERVICE_ENDPOINT_CATEGORY + " in this scope: /" + infraName); - return null; - } else { - for (ServiceEndpoint res : list) { - toReturn.setApplicationId(res.profile().name()); - Group apGroup = res.profile().accessPoints(); - AccessPoint[] accessPoints = apGroup.toArray(new AccessPoint[apGroup.size()]); - AccessPoint found = accessPoints[0]; - for (Property prop : found.properties()) { - if (prop.name().compareTo(LOGOURL_ATTR) == 0) { - toReturn.setLogoURL(prop.value()); - return toReturn; - } - } + } + ServiceEndpoint res = Resources.unmarshal(ServiceEndpoint.class, is); + toReturn.setApplicationId(res.profile().name()); + Group apGroup = res.profile().accessPoints(); + AccessPoint[] accessPoints = apGroup.toArray(new AccessPoint[apGroup.size()]); + AccessPoint found = accessPoints[0]; + for (Property prop : found.properties()) { + if (prop.name().compareTo(LOGOURL_ATTR) == 0) { + toReturn.setLogoURL(prop.value()); + return toReturn; } } } catch (Exception e) { diff --git a/src/test/java/org/gcube/portal/auth/AppTest.java b/src/test/java/org/gcube/portal/auth/AppTest.java index ba4537f..9b584d3 100644 --- a/src/test/java/org/gcube/portal/auth/AppTest.java +++ b/src/test/java/org/gcube/portal/auth/AppTest.java @@ -33,12 +33,11 @@ public class AppTest extends TestCase { * Rigourous Test :-) */ public void testApp() { - RequestingApp app = AuthUtil.getAuthorisedApplicationInfoFromIs("agINFRA+ App"); - if (app != null) { - System.out.println(app.getApplicationId()); - System.out.println(app.getLogoURL()); - - } - assertTrue( true ); +// RequestingApp app = AuthUtil.getAuthorisedApplicationInfoFromIs("c96d4477-236c-4f98-ba7d-7897991ef412"); +// if (app != null) { +// System.out.println(app.getApplicationId()); +// System.out.println(app.getLogoURL()); +// } +// assertTrue( app != null ); } }