diff --git a/pom.xml b/pom.xml index 85b441f..d8eeda0 100644 --- a/pom.xml +++ b/pom.xml @@ -34,11 +34,6 @@ common-authorization - - org.gcube.core - common-encryption - - 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 67a1eaa..2e8e695 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 @@ -7,8 +7,9 @@ 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.policies.Policy; -import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.provider.ContainerInfo; import org.gcube.common.authorization.library.provider.ServiceInfo; +import org.gcube.common.authorization.library.provider.UserInfo; public interface AuthorizationProxy { @@ -17,13 +18,7 @@ public interface AuthorizationProxy { void setEndpoint(Map endpoints); AuthorizationEntry get(String token) throws ObjectNotFound, Exception; - - String generateToken(ClientInfo client, String context) - throws Exception; - - String requestActivation(ServiceInfo container, String context) - throws Exception; - + void addPolicies(List policies) throws Exception; void removePolicies(long ... ids) throws Exception; @@ -31,5 +26,12 @@ public interface AuthorizationProxy { List getPolicies(String context) throws Exception; String generateApiKey(String apiQualifier) throws Exception; + + String generateServiceToken(ServiceInfo client) throws Exception; + + String generateUserToken(UserInfo client, String context) + throws Exception; + + String requestActivation(ContainerInfo container, String context) 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 7525e67..ae9113f 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 @@ -23,11 +23,10 @@ import org.gcube.common.authorization.library.Policies; import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint; import org.gcube.common.authorization.library.enpoints.AuthorizationEndpointScanner; import org.gcube.common.authorization.library.policies.Policy; -import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.provider.ContainerInfo; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; -import org.gcube.common.encryption.StringEncrypter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,14 +51,44 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } @Override - public String generateToken(ClientInfo client, String context) throws Exception { + public String generateServiceToken(ServiceInfo client) throws Exception { - String methodPath = "/token/"; + String methodPath = "/token/service"; + + int infrastructureHash = getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()); - if (client instanceof UserInfo) - methodPath+="user"; - else methodPath+="service"; + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); + + URL url = new URL(callUrl.toString()); + HttpURLConnection connection = makeRequest(url, "PUT", true); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setRequestProperty("Content-type", "application/xml"); + + try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ + Binder.getContext().createMarshaller().marshal(client, os); + } + log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); + + if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); + String token= ""; + try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ + StringBuilder result = new StringBuilder(); + String line; + while((line = reader.readLine()) != null) + result.append(line); + token = result.toString(); + } + + return token; + } + + @Override + public String generateUserToken(UserInfo client, String context) throws Exception { + + String methodPath = "/token/user"; + int infrastructureHash = getInfrastructureHashfromContext(context); StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?") @@ -144,18 +173,16 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } @Override - public String requestActivation(ServiceInfo container, String context) throws Exception { - - String methodPath = "/generate/node"; - + public String requestActivation(ContainerInfo container, String context) throws Exception { + String methodPath = "/token/node"; - StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashfromContext(context))).append(methodPath).append("?") + StringBuilder callUrl = + new StringBuilder(getInternalEnpoint(getInfrastructureHashfromContext(context))).append(methodPath).append("?") .append(CONTEXT_PARAM).append("=").append(context); URL url = new URL(callUrl.toString()); - HttpURLConnection connection = (HttpURLConnection)url.openConnection(); - connection.setRequestMethod("PUT"); + HttpURLConnection connection = makeRequest(url, "PUT", false); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "application/xml"); @@ -167,16 +194,16 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { log.debug("response code is "+connection.getResponseCode()); if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - String encryptedToken= ""; + String token= ""; try(BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream)connection.getContent()))){ StringBuilder result = new StringBuilder(); String line; while((line = reader.readLine()) != null) result.append(line); - encryptedToken = result.toString(); + token = result.toString(); } - return StringEncrypter.getEncrypter().decrypt(encryptedToken, context); + return token; } @Override 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 4fad5c8..05c77b8 100644 --- a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java +++ b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java @@ -5,15 +5,13 @@ import static org.gcube.common.authorization.client.Constants.authorizationServi import java.util.ArrayList; import java.util.List; -import javax.annotation.Generated; - -import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.policies.Action; import org.gcube.common.authorization.library.policies.Policy; import org.gcube.common.authorization.library.policies.ServiceAccess; import org.gcube.common.authorization.library.policies.User2ServicePolicy; import org.gcube.common.authorization.library.policies.Users; +import org.gcube.common.authorization.library.provider.ContainerInfo; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.UserInfo; import org.junit.Test; @@ -21,8 +19,9 @@ public class CallTest { @Test - public void requestToken() throws Exception { - System.out.println(requestTestToken("/gcube")); + public void requestNodeToken() throws Exception { + String token = authorizationService().requestActivation(new ContainerInfo("dlib29.isti.cnr.it",8080), "/gcube"); + System.out.println(token); } @Test @@ -59,7 +58,7 @@ public class CallTest { private String requestTestToken(String context) throws Exception{ - return authorizationService().generateToken(new UserInfo("test.token", new ArrayList()), context); + return authorizationService().generateUserToken(new UserInfo("test.token", new ArrayList()), context); } private AuthorizationEntry resolveToken(String token) throws Exception{