|
|
|
@ -2,54 +2,57 @@ package org.gcube.data.access.accounting.summary.access.test;
|
|
|
|
|
|
|
|
|
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
|
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManager;
|
|
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
|
|
|
|
import org.gcube.common.authorization.utils.secret.GCubeSecret;
|
|
|
|
|
import org.gcube.common.authorization.utils.secret.Secret;
|
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class TokenSetter{
|
|
|
|
|
public class TokenSetter {
|
|
|
|
|
|
|
|
|
|
private static Properties props=null;
|
|
|
|
|
|
|
|
|
|
static{
|
|
|
|
|
private static Properties props=new Properties();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized void set(String scope){
|
|
|
|
|
try{
|
|
|
|
|
if(props==null) {
|
|
|
|
|
props=new Properties();
|
|
|
|
|
try {
|
|
|
|
|
props.load(TokenSetter.class.getResourceAsStream("/tokens.properties"));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("YOU NEED TO SET TOKEN FILE IN CONFIGURATION");
|
|
|
|
|
}
|
|
|
|
|
static{
|
|
|
|
|
try {
|
|
|
|
|
props.load(TokenSetter.class.getResourceAsStream("/tokens.properties"));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException("YOU NEED TO SET TOKEN FILE IN CONFIGURATION",e);
|
|
|
|
|
}
|
|
|
|
|
if(!props.containsKey(scope)) throw new Exception("No token found for scope : "+scope);
|
|
|
|
|
SecurityTokenProvider.instance.set(props.getProperty(scope));
|
|
|
|
|
}catch(Throwable e){
|
|
|
|
|
log.trace("Unable to set token for scope "+scope,e);
|
|
|
|
|
}
|
|
|
|
|
ScopeProvider.instance.set(scope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void setToken(String token){
|
|
|
|
|
try{
|
|
|
|
|
AuthorizationEntry entry = authorizationService().get(token);
|
|
|
|
|
ScopeProvider.instance.set(entry.getContext());
|
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
|
}catch(Throwable t) {
|
|
|
|
|
throw new RuntimeException("Unable to set token "+token,t);
|
|
|
|
|
public static void set(String scope){
|
|
|
|
|
SecretManagerProvider.instance.set(new SecretManager());
|
|
|
|
|
if(!props.containsKey(scope)) throw new RuntimeException("No token found for scope : "+scope);
|
|
|
|
|
// Secret secret = SecretUtility. getSecretByTokenString(token); // se non sai con che token hai a che fare;
|
|
|
|
|
// oppure
|
|
|
|
|
Secret secret = new GCubeSecret(props.getProperty(scope)); // se vecchio token
|
|
|
|
|
// oppure
|
|
|
|
|
// Secret secret = new JWTSecret(token); // se nuovo token
|
|
|
|
|
|
|
|
|
|
SecretManagerProvider.instance.get().addSecret(secret);
|
|
|
|
|
try{
|
|
|
|
|
SecretManagerProvider.instance.get().set();
|
|
|
|
|
}catch(Exception e ){throw new RuntimeException("Unable to set secret for context "+scope,e);}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// public static void setUma() throws IOException {
|
|
|
|
|
// File umaFile = new File("uma.json");
|
|
|
|
|
// String uma= Files.readFileAsString(umaFile.getAbsolutePath(), Charset.defaultCharset());
|
|
|
|
|
// AccessTokenProvider.instance.set(uma);
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public static String getCurrentToken() {
|
|
|
|
|
return SecurityTokenProvider.instance.get();
|
|
|
|
|
}
|
|
|
|
|
}
|