gcat/src/main/java/org/gcube/gcat/utils/Constants.java

99 lines
3.2 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.SecretManager;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.SecretUtility;
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 PROPERTY_FILENAME = "config.properties";
// /*
// * Key : Context
// * Value : Application Token
// */
// protected static final Map<String,String> applicationTokens;
//
// static {
// try {
// applicationTokens = new HashMap<>();
// Properties properties = new Properties();
// InputStream input = Constants.class.getClassLoader().getResourceAsStream(PROPERTY_FILENAME);
// // load a properties file
// properties.load(input);
// Enumeration<?> enumeration = properties.propertyNames();
// while(enumeration.hasMoreElements()) {
// String context = (String) enumeration.nextElement();
// String applicationToken = properties.getProperty(context);
// applicationTokens.put(context, applicationToken);
// }
// } catch(Exception e) {
// throw new WebApplicationException(e);
// }
// }
//
// @Deprecated
// private static String getCatalogueApplicationToken() {
// String context = SecretManager.instance.get().getContext();
// try {
// return applicationTokens.get(context);
// } catch(Exception e) {
// throw new InternalServerErrorException(
// "Unable to retrieve Application Token for context " + context, e);
// }
// }
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 " + SecretManager.instance.get().getContext(), e);
}
}
private static String getJWTAccessToken() throws Exception {
String contextToAuthorise = SecretManager.instance.get().getContext();
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null);
return tr.getAccessToken();
}
public static String getCatalogueSecurityToken() throws Exception {
try {
return getJWTAccessToken();
}catch (Exception e) {
throw e;
// return getCatalogueApplicationToken();
}
}
public static Secret getCatalogueSecret() throws Exception {
String securityToken = getCatalogueSecurityToken();
return SecretUtility.getSecretByTokenString(securityToken);
}
}