diff --git a/src/main/java/org/gcube/portal/auth/AuthUtil.java b/src/main/java/org/gcube/portal/auth/AuthUtil.java index cc1800f..3ac78fd 100644 --- a/src/main/java/org/gcube/portal/auth/AuthUtil.java +++ b/src/main/java/org/gcube/portal/auth/AuthUtil.java @@ -1,6 +1,8 @@ package org.gcube.portal.auth; +import static org.gcube.common.authorization.client.Constants.authorizationService; import static org.gcube.resources.discovery.icclient.ICFactory.clientFor; import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; +import static org.gcube.resources.discovery.icclient.ICFactory.client; import java.io.IOException; import java.io.InputStream; @@ -14,7 +16,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.portal.PortalContext; +import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.Resources; import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; @@ -43,6 +47,10 @@ public class AuthUtil { public final static String TOKEN_ATTR_NAME = "gcube-token"; public final static String ENDPOINT_TYPE = "ServiceEndpoint"; public final static String ENDPOINT_CATEGORY = "OnlineService"; + + public final static String OAUTH_ENDPOINT_CLASS = "Portal"; + public final static String OAUTH_ENDPOINT_NAME = "oauth"; + private static final String OAUTH_ENDPOINT_ENTRYNAME = "jersey-servlet"; /** * look for the clientId passes as parameter @@ -65,7 +73,7 @@ public class AuthUtil { siteConnection.addRequestProperty(TOKEN_ATTR_NAME, portalToken); InputStream is = null; try { - is = siteConnection.getInputStream(); + is = siteConnection.getInputStream(); } catch (IOException e) { _log.warn("The requested clientId does not exist: " + encodedClientId); @@ -115,58 +123,96 @@ public class AuthUtil { } return map; } + /** + *
+ * @return a qualifier token for a given user token or null
in case of problems
+ *
RequestingApp
contanining the application name, the description and the application logo URL if any, or null
if non existent
*/
- public static List+ * return the authorised redirect for the service endpoint of type OnlineService + *
+ * @param toLookFor an instance ofServiceEndpoint
+ * @return the list of authorised redirectURLs or null
+ */
+ public static List getAuthorisedRedirectURLsFromIs(ServiceEndpoint toLookFor) {
+ List autRedirectURLs = new ArrayList<>();
+ Group apGroup = toLookFor.profile().accessPoints();
+ AccessPoint[] accessPoints = (AccessPoint[]) apGroup.toArray(new AccessPoint[apGroup.size()]);
+ for (int i = 0; i < accessPoints.length; i++) {
+ if (accessPoints[i].name().compareTo(REDIRECT_URL) == 0) {
+ AccessPoint found = accessPoints[i];
+ autRedirectURLs.add(found.address());
+ }
}
return autRedirectURLs;
}
+ /**
+ * Instantiates a new gcore endpoint reader.
+ *
+ * @param scope the scope
+ * @throws Exception the exception
+ */
+ public static String getOAuthServiceEndPoint(String infrastructureName) throws Exception {
+ String scope = "/" + infrastructureName;
+ String currScope = ScopeProvider.instance.get();
+ ScopeProvider.instance.set(scope);
+
+
+ SimpleQuery query = queryFor(GCoreEndpoint.class);
+ query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",OAUTH_ENDPOINT_CLASS));
+ query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'");
+ query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",OAUTH_ENDPOINT_NAME));
+ query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+OAUTH_ENDPOINT_ENTRYNAME+"\"]/text()");
+
+
+ DiscoveryClient client = client();
+ List toReturn = client.submit(query);
+ if (toReturn == null || toReturn.isEmpty()) throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+OAUTH_ENDPOINT_NAME +", serviceClass: " +OAUTH_ENDPOINT_CLASS +", in scope: "+scope);
+
+
+ ScopeProvider.instance.set(currScope);
+ if (toReturn.size() > 0)
+ return toReturn.get(0);
+ else
+ return null;
+ }
+
}
diff --git a/src/test/java/org/gcube/portal/auth/AppTest.java b/src/test/java/org/gcube/portal/auth/AppTest.java
index 7f66e90..d03a0ec 100644
--- a/src/test/java/org/gcube/portal/auth/AppTest.java
+++ b/src/test/java/org/gcube/portal/auth/AppTest.java
@@ -2,8 +2,17 @@ package org.gcube.portal.auth;
import java.util.List;
-import org.gcube.portal.auth.AuthUtil;
-import org.gcube.portal.auth.RequestingApp;
+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.PortalContext;
+import org.gcube.common.resources.gcore.ServiceEndpoint;
+
+import com.liferay.portal.kernel.json.JSONObject;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -35,21 +44,22 @@ public class AppTest extends TestCase {
* Rigourous Test :-)
*/
public void testApp() {
-// 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 );
System.out.println("getAuthorisedRedirectURLsFromIs ... ");
try {
- List authreds = AuthUtil.getAuthorisedRedirectURLsFromIs("c96d4477-236c-4f98-ba7d-7897991ef412");
- for (String red : authreds) {
+ ServiceEndpoint authorisedApp = AuthUtil.getAuthorisedApplicationInfoFromIsICClient(PortalContext.getConfiguration().getInfrastructureName(), "c96d4477-236c-4f98-ba7d-7897991ef412");
+ List authorisedRedirectURLs = AuthUtil.getAuthorisedRedirectURLsFromIs(authorisedApp);
+
+ for (String red : authorisedRedirectURLs) {
System.out.println(red);
}
+
+ String oauthendPoint = AuthUtil.getOAuthServiceEndPoint(PortalContext.getConfiguration().getInfrastructureName());
+ System.out.println(oauthendPoint);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
+
+
}