gcube-cms-suite/cms-test-commons/src/main/java/org/gcube/application/cms/tests/TokenSetter.java

75 lines
2.6 KiB
Java
Raw Normal View History

2021-09-21 17:45:18 +02:00
package org.gcube.application.cms.tests;
2021-09-20 18:11:51 +02:00
import lombok.extern.slf4j.Slf4j;
2022-05-24 14:26:47 +02:00
import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
2021-09-20 18:11:51 +02:00
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
2022-09-27 12:23:08 +02:00
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.secret.GCubeSecret;
2022-10-26 18:00:15 +02:00
import org.gcube.common.authorization.utils.secret.JWTSecret;
2022-09-27 12:23:08 +02:00
import org.gcube.common.authorization.utils.secret.Secret;
2021-09-20 18:11:51 +02:00
import org.gcube.common.scope.api.ScopeProvider;
2022-05-24 14:26:47 +02:00
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
2021-09-20 18:11:51 +02:00
import java.util.Properties;
2023-01-13 17:36:22 +01:00
/**
* This class is used in order to set gCube Credentials for tests that needs a gCube Infrastructure.
*
* The method TokenSetter.set(String context) is used in order to specify the context to test.
*
* The library expects a Properties file /tokens.properties in the classpath like the following example :
*
* /root/vo/vre1 = GCUBE-TOKEN
* ...
* /devRoot/vo = JWT-TOKEN
* ...
*
*/
2021-09-20 18:11:51 +02:00
@Slf4j
public class TokenSetter {
private static Properties props=new Properties();
static{
try {
props.load(TokenSetter.class.getResourceAsStream("/tokens.properties"));
} catch (Exception e) {
2022-06-22 17:15:32 +02:00
throw new RuntimeException("YOU NEED TO SET TOKEN FILE IN CONFIGURATION",e);
2021-09-20 18:11:51 +02:00
}
}
public static void set(String scope){
2022-09-27 12:23:08 +02:00
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
String toSet= props.getProperty(scope);
log.debug("Setting secret "+toSet);
2022-10-26 18:00:15 +02:00
Secret secret = null;
if(toSet.length()>50)
secret = new JWTSecret(toSet); // se nuovo token
else
secret = new GCubeSecret(toSet); // se vecchio token
2022-09-27 12:23:08 +02:00
SecretManagerProvider.instance.get().addSecret(secret);
2021-09-20 18:11:51 +02:00
try{
2022-09-27 12:23:08 +02:00
SecretManagerProvider.instance.get().set();
}catch(Exception e ){throw new RuntimeException("Unable to set secret for context "+scope,e);}
2022-05-24 14:26:47 +02:00
}
2022-09-27 12:23:08 +02:00
//
// public static void setUma() throws IOException {
// File umaFile = new File("uma.json");
// String uma= Files.readFileAsString(umaFile.getAbsolutePath(), Charset.defaultCharset());
// AccessTokenProvider.instance.set(uma);
//
// }
}