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
This commit is contained in:
Massimiliano Assante 2017-02-02 11:01:35 +00:00
parent 29071c41d1
commit d579a00dba
2 changed files with 36 additions and 66 deletions

View File

@ -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<ServiceEndpoint> getPortalConfigurationFromIS(String clientId) throws Exception {
List<ServiceEndpoint> 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 <code>RequestingApp</code> contanining the application name, the description and the application logo URL if any, or null if non existent
* @return a <code>RequestingApp</code> contanining the application name, the description and the application logo URL if any, or <code>null</code> if non existent
*/
public static RequestingApp getAuthorisedApplicationInfoFromIs(String clientId) {
RequestingApp toReturn = new RequestingApp();
String infraName = PortalContext.getConfiguration().getInfrastructureName();
try {
List<ServiceEndpoint> 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<AccessPoint> 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<AccessPoint> 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) {

View File

@ -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 );
}
}