ready to release

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/portal-auth-library@141981 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2017-02-01 15:39:10 +00:00
parent 4b929fac59
commit 813daee320
3 changed files with 53 additions and 29 deletions

View File

@ -62,13 +62,10 @@
<artifactId>usermanagement-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<artifactId>common-gcore-resources</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>

View File

@ -1,22 +1,28 @@
package org.gcube.portal.auth;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
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;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@ -32,18 +38,40 @@ public class AuthUtil {
public final static String REDIRECT_URL = "RedirectURL";
public final static String SERVICE_ENDPOINT_CATEGORY = "OnlineService";
public final static String LOGOURL_ATTR = "Logo";
public final static String TOKEN_ATTR_NAME = "gcube-token";
public final static String ENDPOINT_TYPE = "ServiceEndpoint";
public final static String ENDPOINT_CATEGORY = "OnlineService";
public static List<ServiceEndpoint> getPortalConfigurationFromIS(String infrastructureName, String clientId)
throws Exception {
String scope = "/" + infrastructureName;
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '" + SERVICE_ENDPOINT_CATEGORY + "'");
query.addCondition("$resource/Profile/Name/text() eq '" + clientId + "'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> toReturn = client.submit(query);
ScopeProvider.instance.set(currScope);
public static List<ServiceEndpoint> getPortalConfigurationFromIS(String clientId) throws Exception {
List<ServiceEndpoint> toReturn = new ArrayList<>();
String encodedAppName = URLEncoder.encode(clientId, "UTF-8").replaceAll("\\+", "%20");
System.out.println(encodedAppName);
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;
}
@ -55,10 +83,8 @@ public class AuthUtil {
public static RequestingApp getAuthorisedApplicationInfoFromIs(String clientId) {
RequestingApp toReturn = new RequestingApp();
String infraName = PortalContext.getConfiguration().getInfrastructureName();
System.out.println("infraName="+infraName);
try {
List<ServiceEndpoint> list = getPortalConfigurationFromIS(infraName, clientId);
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);
return null;
@ -69,7 +95,7 @@ public class AuthUtil {
for (ServiceEndpoint res : list) {
toReturn.setApplicationId(res.profile().name());
Group<AccessPoint> apGroup = res.profile().accessPoints();
AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
AccessPoint[] accessPoints = apGroup.toArray(new AccessPoint[apGroup.size()]);
AccessPoint found = accessPoints[0];
for (Property prop : found.properties()) {
if (prop.name().compareTo(LOGOURL_ATTR) == 0) {
@ -96,7 +122,7 @@ public class AuthUtil {
try {
decodedURL = java.net.URLDecoder.decode(redirectionURL, "UTF-8");
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException=" + e.getMessage());
_log.error("UnsupportedEncodingException=" + e.getMessage());
return new HashMap<String, String>();
}
String[] url = decodedURL.split("\\?");

View File

@ -33,11 +33,12 @@ public class AppTest extends TestCase {
* Rigourous Test :-)
*/
public void testApp() {
RequestingApp app = AuthUtil.getAuthorisedApplicationInfoFromIs("AnAppRequiringUserAuthN");
RequestingApp app = AuthUtil.getAuthorisedApplicationInfoFromIs("agINFRA+ App");
if (app != null) {
System.out.println(app.getApplicationId());
System.out.println(app.getLogoURL());
}
//assertTrue( true );
assertTrue( true );
}
}