54 lines
1.8 KiB
Java
54 lines
1.8 KiB
Java
package org.gcube.gcat.utils;
|
|
|
|
import java.io.InputStream;
|
|
import java.util.Properties;
|
|
|
|
import javax.ws.rs.InternalServerErrorException;
|
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
|
import org.gcube.common.authorization.utils.secret.JWTSecret;
|
|
import org.gcube.common.authorization.utils.secret.Secret;
|
|
import org.gcube.common.keycloak.KeycloakClientFactory;
|
|
import org.gcube.common.keycloak.model.TokenResponse;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class Constants {
|
|
|
|
public static final String CATALOGUE_NAME = "gCat";
|
|
|
|
private static final String CLIENT_SECRET_FILENAME = "config.properties";
|
|
private static final String CLIENT_ID = "gcat";
|
|
protected static String clientSecret;
|
|
|
|
private static String getClientSecret(String context) {
|
|
try {
|
|
if(clientSecret==null) {
|
|
Properties properties = new Properties();
|
|
InputStream input = Constants.class.getClassLoader().getResourceAsStream(CLIENT_SECRET_FILENAME);
|
|
properties.load(input);
|
|
String root = context.substring(0, context.indexOf('/', 1));
|
|
clientSecret = properties.getProperty(root);
|
|
}
|
|
return clientSecret;
|
|
} catch(Exception e) {
|
|
throw new InternalServerErrorException(
|
|
"Unable to retrieve Application Token for context " + SecretManagerProvider.instance.get().getContext(), e);
|
|
}
|
|
}
|
|
|
|
private static TokenResponse getJWTAccessToken() throws Exception {
|
|
String contextToAuthorise = SecretManagerProvider.instance.get().getContext();
|
|
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null);
|
|
return tr;
|
|
}
|
|
|
|
public static Secret getCatalogueSecret() throws Exception {
|
|
TokenResponse tr = getJWTAccessToken();
|
|
Secret secret = new JWTSecret(tr.getAccessToken());
|
|
return secret;
|
|
}
|
|
|
|
}
|