This commit is contained in:
Lucio Lelii 2015-11-27 17:06:28 +00:00
parent da23122050
commit 428424f82b
3 changed files with 47 additions and 12 deletions

View File

@ -4,13 +4,21 @@ import java.util.List;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry; import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.enpoints.AuthorizationEndpoint;
public interface AuthorizationProxy { public interface AuthorizationProxy {
AuthorizationEndpoint getEndpoint();
void setEndpoint(AuthorizationEndpoint endpoint);
String generate(String userName, List<String> roles) throws Exception; String generate(String userName, List<String> roles) throws Exception;
AuthorizationEntry get(String token) throws ObjectNotFound, Exception; AuthorizationEntry get(String token) throws ObjectNotFound, Exception;
String generate(String clientId, String context, List<String> roles)
throws Exception;
/* /*
BannedService deny(String userName, String serviceClass, String serviceName) throws Exception; BannedService deny(String userName, String serviceClass, String serviceName) throws Exception;

View File

@ -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.CLIENT_ID_PARAM;
import static org.gcube.common.authorization.client.Constants.CONTEXT_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.ROLES_PARAM;
import static org.gcube.common.authorization.client.Constants.SERVICE_NAME;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound; import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry; 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.encryption.StringEncrypter;
import org.gcube.common.scope.api.Environment;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.EnvironmentServiceMap;
public class DefaultAuthorizationProxy implements AuthorizationProxy { public class DefaultAuthorizationProxy implements AuthorizationProxy {
private static Map<String, AuthorizationEntryCache> cache = new HashMap<String, AuthorizationEntryCache>(); private static Map<String, AuthorizationEntryCache> cache = new HashMap<String, AuthorizationEntryCache>();
private static String endpoint;
private List<AuthorizationEndpoint> endpoints;
public DefaultAuthorizationProxy() { 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 @Override
public String generate(String clientId, List<String> roles) throws Exception { public String generate(String clientId, List<String> roles) throws Exception {
final String context = ScopeProvider.instance.get();
return this.generate(clientId, context , roles);
}
@Override
public String generate(String clientId, String context, List<String> roles) throws Exception {
final String methodPath = "/generate/token/"; final String methodPath = "/generate/token/";
StringBuilder rolesQueryString = new StringBuilder(); StringBuilder rolesQueryString = new StringBuilder();
@ -43,10 +59,10 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
rolesQueryString.append(role).append(","); rolesQueryString.append(role).append(",");
rolesQueryString.deleteCharAt(rolesQueryString.lastIndexOf(",")); 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(CLIENT_ID_PARAM).append("=").append(clientId).append("&")
.append(ROLES_PARAM).append("=").append(rolesQueryString).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()); URL url = new URL(callUrl.toString());
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); HttpURLConnection connection = (HttpURLConnection)url.openConnection();
@ -67,11 +83,12 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
} }
@Override @Override
public AuthorizationEntry get(final String token) throws ObjectNotFound, Exception{ public AuthorizationEntry get(final String token) throws ObjectNotFound, Exception{
final String methodPath = "/retrieve/"; 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()); URL url = new URL(callUrl.toString());
@ -138,4 +155,14 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
connection.setRequestMethod(method); connection.setRequestMethod(method);
return connection; return connection;
} }
@Override
public AuthorizationEndpoint getEndpoint() {
return this.endpoints.get(0);
}
@Override
public void setEndpoint(AuthorizationEndpoint authEndpoint) {
this.endpoints = Collections.singletonList(authEndpoint);
}
} }

View File

@ -2,7 +2,7 @@ package org.gcube.common.authorizationservice.cl;
import static org.gcube.common.authorization.client.Constants.authorizationService; 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.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
@ -23,7 +23,7 @@ public class CallTest {
public void requestToken() throws Exception { public void requestToken() throws Exception {
ScopeProvider.instance.set("/gcube"); ScopeProvider.instance.set("/gcube");
String token = authorizationService().generate("fabio.sinibaldi", Arrays.asList("User")); String token = authorizationService().generate("fabio.sinibaldi", new ArrayList<String>());
System.out.println("token is: "+token); System.out.println("token is: "+token);
} }