This commit is contained in:
Lucio Lelii 2016-09-13 16:14:01 +00:00
parent 8564f62cf2
commit 7a21492162
4 changed files with 63 additions and 42 deletions

View File

@ -1,9 +0,0 @@
package org.gcube.common.authorization.client;
public class Utils {
static void notNull(String message,Object o) {
if (o==null)
throw new IllegalArgumentException(o+" cannot be null");
}
}

View File

@ -38,8 +38,6 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
private static Map<String, AuthorizationEntryCache> cache = Collections.synchronizedMap(new WeakHashMap<String, AuthorizationEntryCache>());
private Map<Integer, AuthorizationEndpoint> endpoints;
private static final String TOKEN_SEPARATOR ="\\|";
public DefaultAuthorizationProxy() {
endpoints = AuthorizationEndpointScanner.endpoints();
@ -56,7 +54,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
String methodPath = "/token/service";
int infrastructureHash = getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
@ -90,7 +88,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
String methodPath = "/token/user";
int infrastructureHash = getInfrastructureHashfromContext(context);
int infrastructureHash = Utils.getInfrastructureHashfromContext(context);
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath).append("?")
.append(CONTEXT_PARAM).append("=").append(context);
@ -127,7 +125,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
String methodPath = String.format("/apikey?qualifier=%s",apiQualifier);
int infrastructureHash = getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
@ -159,7 +157,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
public Map<String, String> retrieveApiKeys() throws Exception{
String methodPath = "/apikey/";
int infrastructureHash = getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
int infrastructureHash = Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get());
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(infrastructureHash)).append(methodPath);
@ -179,25 +177,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
}
}
private int getInfrastructureHashfromContext(String context) {
try{
String infrastructure = context.split("/")[1];
return infrastructure.hashCode();
}catch(Exception e){
throw new RuntimeException("invalid context");
}
}
private int getInfrastructureHashFromToken(String token) {
if (token==null)
throw new RuntimeException("valid token required for this method");
try{
String hashCodeAsString = token.split(TOKEN_SEPARATOR)[1];
return Integer.parseInt(hashCodeAsString);
}catch(Exception e){
throw new RuntimeException("invalid token",e);
}
}
@Override
public String requestActivation(ContainerInfo container, String context) throws Exception {
@ -205,7 +185,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
String methodPath = "/token/node";
StringBuilder callUrl =
new StringBuilder(getInternalEnpoint(getInfrastructureHashfromContext(context))).append(methodPath).append("?")
new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashfromContext(context))).append(methodPath).append("?")
.append(CONTEXT_PARAM).append("=").append(context);
URL url = new URL(callUrl.toString());
@ -240,7 +220,8 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
final String methodPath = "/token/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashFromToken(token))).append(methodPath).append(token);
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(token)))
.append(methodPath).append(Utils.getRealToken(token));
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", false);
@ -261,7 +242,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
public void addPolicies(List<Policy> policies) throws Exception {
final String methodPath = "/policyManager";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()))).append(methodPath);
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()))).append(methodPath);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "POST", true);
@ -279,7 +260,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
@Override
public void removePolicies(long... ids) throws Exception {
final String methodPath = "/policyManager/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()))).append(methodPath);
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashFromToken(SecurityTokenProvider.instance.get()))).append(methodPath);
List<Long> errorIds = new ArrayList<Long>();
for (long id: ids){
URL url = new URL(callUrl.toString()+id);
@ -294,7 +275,7 @@ public class DefaultAuthorizationProxy implements AuthorizationProxy {
public List<Policy> getPolicies(String context) throws Exception{
final String methodPath = "/policyManager/";
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(getInfrastructureHashfromContext(context))).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context);
StringBuilder callUrl = new StringBuilder(getInternalEnpoint(Utils.getInfrastructureHashfromContext(context))).append(methodPath).append("?").append(CONTEXT_PARAM).append("=").append(context);
URL url = new URL(callUrl.toString());
HttpURLConnection connection = makeRequest(url, "GET", true);

View File

@ -0,0 +1,49 @@
package org.gcube.common.authorization.client.proxy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utils {
private static final String NEW_TOKEN_REGEXPR ="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-[0-9]+";
private static final String OLD_TOKEN_REGEXPR ="[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
private static final String REAL_TOKEN_REGEXPR ="([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(-[0-9]+)?";
private static final String DEFAULT_INFRASTRUCTURE_FOR_OLD_TOKEN = "gcube";
protected static int getInfrastructureHashfromContext(String context) {
try{
String infrastructure = context.split("/")[1];
return infrastructure.hashCode();
}catch(Exception e){
throw new RuntimeException("invalid context");
}
}
public static int getInfrastructureHashFromToken(String token) {
if (token==null) throw new RuntimeException("token required for this method");
else if (token.matches(NEW_TOKEN_REGEXPR)){
String hashCodeAsString = token.substring(token.lastIndexOf("-")+1, token.length());
return Integer.parseInt(hashCodeAsString);
} else if (token.matches(OLD_TOKEN_REGEXPR))
return DEFAULT_INFRASTRUCTURE_FOR_OLD_TOKEN.hashCode();
throw new RuntimeException("valid token required for this method");
}
protected static String AddInfrastructureHashToToken(String token, int infrastructureHash) {
return String.format("%s-%d", token, infrastructureHash);
}
protected static String getRealToken(String token) {
Pattern pattern = Pattern.compile(REAL_TOKEN_REGEXPR);
Matcher matcher = pattern.matcher(token);
matcher.find();
String realToken = matcher.group(1);
return realToken;
}
}

View File

@ -20,12 +20,12 @@ public class CallTest {
@Test
public void resolveNodeToken() throws Exception{
System.out.println(resolveToken("81caac0f-8a0d-4923-9312-7ff0eb3f2d5e|98187548"));
System.out.println(resolveToken("c3c52f5b-ea65-4364-8357-be930763fdad-98187548")); //81caac0f-8a0d-4923-9312-7ff0eb3f2d5e|98187548"));
}
@Test
public void requestNodeToken() throws Exception {
String token = authorizationService().requestActivation(new ContainerInfo("dlib29.isti.cnr.it",8080), "/gcube/devNext");
String token = authorizationService().requestActivation(new ContainerInfo("dataminer1-devnext.d4science.org",80), "/gcube/devNext/NextNext");
System.out.println(token);
}
@ -75,7 +75,7 @@ public class CallTest {
@Test
public void createTestToken() throws Exception {
System.out.println(requestTestToken("/gcube/devsec"));
System.out.println(requestTestToken("/gcube/devNext"));
}
private String requestTestToken(String context) throws Exception{