2016-09-13 18:14:01 +02:00
|
|
|
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]+)?";
|
|
|
|
|
2019-02-20 16:39:09 +01:00
|
|
|
|
2018-02-21 16:22:14 +01:00
|
|
|
|
2016-09-13 18:14:01 +02:00
|
|
|
protected static int getInfrastructureHashfromContext(String context) {
|
|
|
|
try{
|
2019-02-27 15:47:49 +01:00
|
|
|
String infrastructure = context.replaceAll("^/?([^/]*)/?.*", "$1");
|
2019-02-20 16:39:09 +01:00
|
|
|
|
|
|
|
int hashcode= infrastructure.hashCode();
|
|
|
|
return Math.abs(hashcode);
|
2016-09-13 18:14:01 +02:00
|
|
|
}catch(Exception e){
|
|
|
|
throw new RuntimeException("invalid context");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 10:44:15 +02:00
|
|
|
public static int getInfrastructureHashFromToken(String token, String defaultInfrastructureToUse) {
|
2016-09-13 18:14:01 +02:00
|
|
|
if (token==null) throw new RuntimeException("token required for this method");
|
2016-10-04 15:01:08 +02:00
|
|
|
|
|
|
|
if (token.matches(NEW_TOKEN_REGEXPR)){
|
2016-09-13 18:14:01 +02:00
|
|
|
String hashCodeAsString = token.substring(token.lastIndexOf("-")+1, token.length());
|
|
|
|
return Integer.parseInt(hashCodeAsString);
|
|
|
|
} else if (token.matches(OLD_TOKEN_REGEXPR))
|
2016-09-22 10:44:15 +02:00
|
|
|
return defaultInfrastructureToUse.hashCode();
|
2016-09-13 18:14:01 +02:00
|
|
|
|
|
|
|
throw new RuntimeException("valid token required for this method");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-16 10:00:01 +02:00
|
|
|
protected static String addInfrastructureHashToToken(String token, int infrastructureHash) {
|
2016-09-13 18:14:01 +02:00
|
|
|
return String.format("%s-%d", token, infrastructureHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static String getRealToken(String token) {
|
2016-10-04 15:00:12 +02:00
|
|
|
try{
|
|
|
|
Pattern pattern = Pattern.compile(REAL_TOKEN_REGEXPR);
|
|
|
|
Matcher matcher = pattern.matcher(token);
|
|
|
|
matcher.find();
|
|
|
|
String realToken = matcher.group(1);
|
|
|
|
return realToken;
|
|
|
|
}catch(Exception e){
|
2018-02-21 16:22:14 +01:00
|
|
|
throw new RuntimeException("valid token required for this method", e);
|
2016-10-04 15:00:12 +02:00
|
|
|
}
|
2016-09-13 18:14:01 +02:00
|
|
|
}
|
|
|
|
}
|