diff --git a/pom.xml b/pom.xml index a1fd050..d0ad5a5 100644 --- a/pom.xml +++ b/pom.xml @@ -29,12 +29,6 @@ [1.0.2-SNAPSHOT,2.0.0-SNAPSHOT) - - org.gcube.core - common-encryption - [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) - - org.slf4j slf4j-api 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 b248706..2d7eb75 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 @@ -12,6 +12,7 @@ public interface AuthorizationProxy { void setEndpoint(AuthorizationEndpoint endpoint); + @Deprecated String generate(String userName, List roles) throws Exception; AuthorizationEntry get(String token) throws ObjectNotFound, 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 0abd1b8..e79c3f6 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 @@ -8,7 +8,9 @@ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; +import java.net.InetAddress; import java.net.URL; +import java.net.UnknownHostException; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -22,29 +24,45 @@ 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.ScopeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DefaultAuthorizationProxy implements AuthorizationProxy { + private static Logger log = LoggerFactory.getLogger(AuthorizationProxy.class); + private static Map cache = new HashMap(); - + private List endpoints; public DefaultAuthorizationProxy() { + endpoints = AuthorizationEndpointScanner.endpoints(); + AuthorizationEndpoint ae = endpoints.get(0); + try{ + InetAddress addr = InetAddress.getByName(ae.getHost()); + if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()){ + ae.setHost("localhost"); + this.setEndpoint(ae); + } + log.debug("endpoint set to localhost"); + }catch(UnknownHostException e){ + log.warn("unknown host", e); + } } private String getInternalEnpoint(){ StringBuilder endpoint = new StringBuilder("http://").append(getEndpoint().getHost()).append(":") .append(getEndpoint().getPort()).append("/authorization-service/gcube/service"); return endpoint.toString(); - + } - + + @Deprecated @Override public String generate(String clientId, List roles) throws Exception { final String context = ScopeProvider.instance.get(); - return this.generate(clientId, context , roles); } @@ -67,8 +85,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { URL url = new URL(callUrl.toString()); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); - - + + if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); String encryptedToken= ""; try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()));){ @@ -78,21 +96,24 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { result.append(line); encryptedToken = result.toString(); } - - return StringEncrypter.getEncrypter().decrypt(encryptedToken); + + return StringEncrypter.getEncrypter().decrypt(encryptedToken, context); } - - + @Override public AuthorizationEntry get(final String token) throws ObjectNotFound, Exception{ final String methodPath = "/retrieve/"; StringBuilder callUrl = new StringBuilder(getInternalEnpoint()).append(methodPath).append(token); + log.debug("call uri "+callUrl.toString()); + URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "GET"); + log.debug("response code is "+connection.getResponseCode()); + log.debug("response message is "+connection.getResponseMessage()); + if (connection.getResponseCode()==404) throw new ObjectNotFound("token "+token+" not found"); if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); if (connection.getContentLengthLong()<=0) return null; 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 b176974..0249f80 100644 --- a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java +++ b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java @@ -13,7 +13,8 @@ public class CallTest { @Test public void call() throws Exception{ try{ - System.out.println(authorizationService().get("a00affeb-0b75-4152-a134-e5c432a9a70a")); + //devsec cec80de1-0e1a-47be-81cd-e8534753bff7 + System.out.println(authorizationService().get("870f409b-df3c-4c12-8063-6f9b0f414751")); }catch(ObjectNotFound onf){ onf.printStackTrace(); } @@ -22,8 +23,8 @@ public class CallTest { @Test public void requestToken() throws Exception { - ScopeProvider.instance.set("/gcube"); - String token = authorizationService().generate("fabio.sinibaldi", new ArrayList()); + //ScopeProvider.instance.set("/gcube/devsec"); + String token = authorizationService().generate("lucio.lelii", "/gcube", new ArrayList()); System.out.println("token is: "+token); } diff --git a/src/test/java/org/gcube/common/authorizationservice/cl/MainCall.java b/src/test/java/org/gcube/common/authorizationservice/cl/MainCall.java deleted file mode 100644 index 27c9194..0000000 --- a/src/test/java/org/gcube/common/authorizationservice/cl/MainCall.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.gcube.common.authorizationservice.cl; - -import static org.gcube.common.authorization.client.Constants.authorizationService; - -import java.util.Arrays; - -public class MainCall { - - public static void main(String... args) throws Exception{ - String token = authorizationService().generate("fabio.sinibaldi", Arrays.asList("User")); - System.out.println("token is: "+token); - } - -}