diff --git a/.classpath b/.classpath index 6fcb3c7..84821fc 100644 --- a/.classpath +++ b/.classpath @@ -22,6 +22,7 @@ + diff --git a/.project b/.project index ea71595..aa90b6c 100644 --- a/.project +++ b/.project @@ -5,6 +5,11 @@ + + org.eclipse.wst.common.project.facet.core.builder + + + org.eclipse.jdt.core.javabuilder @@ -15,9 +20,17 @@ + + org.eclipse.wst.validation.validationbuilder + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature diff --git a/descriptor.xml b/descriptor.xml index bb35e25..fb7fd5c 100644 --- a/descriptor.xml +++ b/descriptor.xml @@ -24,7 +24,7 @@ - target/${build.finalName}.war + target/${build.finalName}.jar /${artifactId} diff --git a/pom.xml b/pom.xml index fbbe79e..e752367 100644 --- a/pom.xml +++ b/pom.xml @@ -77,7 +77,7 @@ target - ${distroDirectory} + . true profile.xml diff --git a/src/main/java/org/gcube/common/authorization/client/Binder.java b/src/main/java/org/gcube/common/authorization/client/Binder.java index 4519be3..15fa74f 100644 --- a/src/main/java/org/gcube/common/authorization/client/Binder.java +++ b/src/main/java/org/gcube/common/authorization/client/Binder.java @@ -12,6 +12,7 @@ import org.gcube.common.authorization.library.policies.Policy; import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; +import org.gcube.common.authorization.library.utils.AuthorizationEntryList; import org.gcube.common.authorization.library.utils.ListMapper; public class Binder { @@ -21,7 +22,7 @@ public class Binder { public static JAXBContext getContext() throws JAXBException{ if (context==null) context = JAXBContext.newInstance(ExternalServiceList.class, QualifiersList.class, AuthorizationEntry.class, ClientInfo.class, UserInfo.class, - ServiceInfo.class, Policies.class, Policy.class, ListMapper.class); + ServiceInfo.class, Policies.class, Policy.class, ListMapper.class, AuthorizationEntryList.class); return context; } 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 7f006ed..1a76ca8 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 @@ import org.gcube.common.authorization.library.policies.Policy; import org.gcube.common.authorization.library.provider.ContainerInfo; import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; +import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest; public interface AuthorizationProxy { @@ -20,6 +21,8 @@ public interface AuthorizationProxy { void setEndpoint(EndpointsContainer endpoints); AuthorizationEntry get(String token) throws ObjectNotFound, Exception; + + List get(List tokens) throws ObjectNotFound, Exception; void addPolicies(List policies) throws Exception; @@ -30,6 +33,9 @@ public interface AuthorizationProxy { String generateApiKey(String apiQualifier) throws Exception; String generateServiceToken(ServiceInfo client) throws Exception; + + List generateServiceToken(ServiceInfo client, List containerTokens) throws Exception; + String generateUserToken(UserInfo client, 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 5217be6..4f6516d 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 @@ -36,7 +36,9 @@ 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.authorization.library.utils.AuthorizationEntryList; import org.gcube.common.authorization.library.utils.ListMapper; +import org.gcube.common.authorization.library.utils.MultiServiceTokenRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,10 +57,15 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { private String getInternalEnpoint(int infrastructureHash){ AuthorizationEndpoint ae = getEndpoint(infrastructureHash); + return getInternalEnpoint(ae); + } + + private String getInternalEnpoint(AuthorizationEndpoint ae){ StringBuilder endpoint = new StringBuilder(ae.isSecureConnection()?"https://":"http://").append(ae.getHost()).append(":") .append(ae.getPort()).append("/authorization-service/gcube/service"); return endpoint.toString(); } + @Override public String generateServiceToken(ServiceInfo client) throws Exception { @@ -74,7 +81,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { 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); } @@ -94,6 +101,44 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { return Utils.addInfrastructureHashToToken(token, infrastructureHash); } + + + + @Override + public List generateServiceToken(ServiceInfo client, List containerTokens) throws Exception { + + String methodPath = "/token/service/bunch"; + + int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()); + + 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(new MultiServiceTokenRequest(containerTokens, 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"); + List tokensToReturn = new ArrayList(); + + try(InputStream stream = (InputStream)connection.getContent();){ + ListMapper entries = (ListMapper)Binder.getContext().createUnmarshaller().unmarshal(stream); + + for (String token: entries.getList()) + tokensToReturn.add(Utils.addInfrastructureHashToToken(token, infrastructureHash)); + return tokensToReturn; + } + + + } + @Override public String generateExternalServiceToken(String serviceId) throws Exception { @@ -122,7 +167,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { return Utils.addInfrastructureHashToToken(token, infrastructureHash); } - + @Override public String resolveTokenByUserAndContext(String user, String context) throws ObjectNotFound, Exception { @@ -134,7 +179,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "GET", false); - + log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found"); @@ -151,7 +196,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { return Utils.addInfrastructureHashToToken(token, infrastructureHash); } - + @Override public String generateUserToken(UserInfo client, String context) throws Exception { @@ -186,7 +231,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { return Utils.addInfrastructureHashToToken(token, infrastructureHash); } - + @Override public void setTokenRoles(String token, List roles) throws Exception { @@ -194,7 +239,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { String methodPath = String.format("/token/user/%s/roles",realToken); int infrastructureHash = Utils.getInfrastructureHashFromToken(token, endpoints.getDefaultInfrastructure()); - + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath); URL url = new URL(callUrl.toString()); @@ -202,9 +247,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "application/xml"); - - ListMapper listmapper = new ListMapper(); - listmapper.setList(roles); + + ListMapper listmapper = new ListMapper(roles); try(OutputStream os = new BufferedOutputStream(connection.getOutputStream())){ Binder.getContext().createMarshaller().marshal(listmapper, os); } @@ -212,17 +256,17 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service"); - + } - + @Override public void removeAllReleatedToken(String clientId, String context) throws Exception{ String methodPath = "/token/user"; int infrastructureHash = Utils.getInfrastructureHashfromContext(context); StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?") .append(CONTEXT_PARAM).append("=").append(context).append("&").append(CLIENT_ID_PARAM).append("=").append(clientId); - + URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "DELETE", false); //connection.setDoOutput(false); @@ -231,7 +275,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { log.debug("response code for "+callUrl.toString()+" is "+connection.getResponseCode()+" "+connection.getResponseMessage()); if (connection.getResponseCode()!=200 && connection.getResponseCode()!=204) throw new Exception("error contacting authorization service"); - + } @Override @@ -292,16 +336,16 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { QualifiersList entries = (QualifiersList)Binder.getContext().createUnmarshaller().unmarshal(stream); tokensQulifiersMap = entries.getQualifiers(); } - + if (tokensQulifiersMap!=null && !tokensQulifiersMap.isEmpty()){ Map toReturnMap = new HashMap(); for (Entry entry: tokensQulifiersMap.entrySet()) toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash)); return toReturnMap; } else return Collections.emptyMap(); - + } - + @Override /** * return a map with key external service id and value token @@ -327,14 +371,14 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { ExternalServiceList entries = (ExternalServiceList)Binder.getContext().createUnmarshaller().unmarshal(stream); externalServiceMap = entries.getExternalServiceMap(); } - + if (externalServiceMap!=null && !externalServiceMap.isEmpty()){ Map toReturnMap = new HashMap(); for (Entry entry: externalServiceMap.entrySet()) toReturnMap.put(entry.getKey(), Utils.addInfrastructureHashToToken(entry.getValue(), infrastructureHash)); return toReturnMap; } else return Collections.emptyMap(); - + } @Override @@ -424,7 +468,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { final String methodPath = "/token/"; StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHashFromToken)) - .append(methodPath).append(realToken); + .append(methodPath).append(realToken); URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "GET", false); @@ -441,6 +485,50 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { } } + @Override + public List get(List tokens) throws ObjectNotFound, Exception { + + List realTokens = new ArrayList(); + + List toReturn = new ArrayList(); + + AuthorizationEndpoint endpoint = null; + + for (String token : tokens) { + String realToken = Utils.getRealToken(token); + + if (cache.containsKey(realToken) && cache.get(realToken).isValid(endpoint.getClientCacheValidity())) + toReturn.add(cache.get(realToken).getEntry()); + else realTokens.add(realToken); + + if (endpoint==null) { + int infrastructureHashFromToken = Utils.getInfrastructureHashFromToken(token, endpoints.getDefaultInfrastructure()); + endpoint = getEndpoint(infrastructureHashFromToken); + } + } + + final String methodPath = "/resolve/?"; + + StringBuilder callUrl = new StringBuilder(getInternalEnpoint(endpoint)) + .append(methodPath); + + for (String toAppend : realTokens) + callUrl= callUrl.append("token=").append(toAppend); + + URL url = new URL(callUrl.toString()); + HttpURLConnection connection = makeRequest(url, "GET", false); + connection.setDoInput(true); + + if (connection.getResponseCode()==404) throw new ObjectNotFound("token not found"); + if (connection.getResponseCode()!=200) throw new Exception("error contacting authorization service (error code is "+connection.getResponseCode()+")"); + if (connection.getContentLengthLong()==0) return null; + + try(InputStream stream = (InputStream)connection.getContent();){ + AuthorizationEntryList entries = (AuthorizationEntryList)Binder.getContext().createUnmarshaller().unmarshal(stream); + return entries.getEntries(); + } + } + @Override public void addPolicies(List policies) throws Exception { @@ -503,7 +591,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy { final String methodPath = "/symmKey/"; StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get(), endpoints.getDefaultInfrastructure()))) - .append(methodPath); + .append(methodPath); URL url = new URL(callUrl.toString()); HttpURLConnection connection = makeRequest(url, "GET", true); 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 188ee7e..37369a0 100644 --- a/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java +++ b/src/test/java/org/gcube/common/authorizationservice/cl/CallTest.java @@ -28,7 +28,6 @@ import org.gcube.common.authorization.library.provider.UserInfo; import org.junit.Ignore; import org.junit.Test; -@Ignore public class CallTest { @Test @@ -36,12 +35,19 @@ public class CallTest { System.out.println("pred4s".hashCode() & 0xfffffff); } + + @Test + public void requestActivation() throws Exception { + SecurityTokenProvider.instance.set("b653566c-2983-4a0e-a0a9-b913636469a8-98187548"); + //ic-test.dev.int.d4science.net b653566c-2983-4a0e-a0a9-b913636469a8-98187548 80 /home/gcube/.containerxml/2-container.xml /gcube /gcube/devNext + String token = authorizationService().requestActivation(new ContainerInfo("ic-test.dev.int.d4science.net", 80), "/gcube/devNext") ; + System.out.println(token); + } + @Test public void requestUserTokenViaUserNameAndScope() throws Exception { - String token = authorizationService().resolveTokenByUserAndContext("valentina.marioli", "/gcube"); - authorizationService().setTokenRoles(token, Arrays.asList("VOManager")); - AuthorizationEntry authEntry = authorizationService().get(token); - System.out.println(authEntry.getClientInfo().toString()); + AuthorizationEntry token = authorizationService().get("d9431600-9fef-41a7-946d-a5b402de30d6-98187548"); + System.out.println(token); } @Test