diff --git a/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java b/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java index 39e0d2e..b248706 100644 --- a/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java +++ b/src/main/java/org/gcube/common/authorization/client/proxy/AuthorizationProxy.java @@ -4,13 +4,21 @@ import java.util.List; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint; public interface AuthorizationProxy { + AuthorizationEndpoint getEndpoint(); + + void setEndpoint(AuthorizationEndpoint endpoint); + String generate(String userName, List roles) throws Exception; AuthorizationEntry get(String token) throws ObjectNotFound, Exception; + String generate(String clientId, String context, List roles) + throws Exception; + /* BannedService deny(String userName, String serviceClass, String serviceName) throws Exception; diff --git a/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java b/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java index 03230c2..0abd1b8 100644 --- a/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java +++ b/src/main/java/org/gcube/common/authorization/client/proxy/DefaultAuthorizationProxy.java @@ -3,13 +3,13 @@ package org.gcube.common.authorization.client.proxy; import static org.gcube.common.authorization.client.Constants.CLIENT_ID_PARAM; import static org.gcube.common.authorization.client.Constants.CONTEXT_PARAM; import static org.gcube.common.authorization.client.Constants.ROLES_PARAM; -import static org.gcube.common.authorization.client.Constants.SERVICE_NAME; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,23 +18,39 @@ import org.gcube.common.authorization.client.Binder; import org.gcube.common.authorization.client.Constants; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint; +import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner; import org.gcube.common.encryption.StringEncrypter; -import org.gcube.common.scope.api.Environment; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.common.scope.impl.EnvironmentServiceMap; public class DefaultAuthorizationProxy implements AuthorizationProxy { private static Map cache = new HashMap(); - private static String endpoint; + + private List endpoints; public DefaultAuthorizationProxy() { - endpoint = EnvironmentServiceMap.getInfrastructuresFor(Environment.DEVELOPMENT).endpoint(SERVICE_NAME); + endpoints = AuthorizationEndpointScanner.endpoints(); + } + + private String getInternalEnpoint(){ + StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint().getHost()).append(":") + .append(getEndpoint().getPort()).append("/authorization-service/gcube/service"); + return endpoint.toString(); + + } + + @Override + public String generate(String clientId, List roles) throws Exception { + final String context = ScopeProvider.instance.get(); + + return this.generate(clientId, context , roles); + } @Override - public String generate(String clientId, List roles) throws Exception { + public String generate(String clientId, String context, List roles) throws Exception { final String methodPath = "/generate/token/"; StringBuilder rolesQueryString = new StringBuilder(); @@ -43,10 +59,10 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { rolesQueryString.append(role).append(","); rolesQueryString.deleteCharAt(rolesQueryString.lastIndexOf(",")); } - StringBuilder callUrl = new StringBuilder(endpoint).append(methodPath).append("?") + StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append("?") .append(CLIENT_ID_PARAM).append("=").append(clientId).append("&") .append(ROLES_PARAM).append("=").append(rolesQueryString).append("&") - .append(CONTEXT_PARAM).append("=").append(ScopeProvider.instance.get()); + .append(CONTEXT_PARAM).append("=").append(context); URL url = new URL(callUrl.toString()); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); @@ -66,12 +82,13 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { return StringEncrypter.getEncrypter().decrypt(encryptedToken); } - + + @Override public AuthorizationEntry get(final String token) throws ObjectNotFound, Exception{ final String methodPath = "/retrieve/"; - StringBuilder callUrl = new StringBuilder(endpoint).append(methodPath).append(token); + StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append(token); URL url = new URL(callUrl.toString()); @@ -138,4 +155,14 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { connection.setRequestMethod(method); return connection; } + + @Override + public AuthorizationEndpoint getEndpoint() { + return this.endpoints.get(0); + } + + @Override + public void setEndpoint(AuthorizationEndpoint authEndpoint) { + this.endpoints = Collections.singletonList(authEndpoint); + } } diff --git a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java index 715d676..b176974 100644 --- a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java +++ b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java @@ -2,7 +2,7 @@ package org.gcube.common.authorizationservice.cl; import static org.gcube.common.authorization.client.Constants.authorizationService; -import java.util.Arrays; +import java.util.ArrayList; import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.scope.api.ScopeProvider; @@ -23,7 +23,7 @@ public class CallTest { public void requestToken() throws Exception { ScopeProvider.instance.set("/gcube"); - String token = authorizationService().generate("fabio.sinibaldi", Arrays.asList("User")); + String token = authorizationService().generate("fabio.sinibaldi", new ArrayList()); System.out.println("token is: "+token); }