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

71 lines
2.5 KiB
Java
Raw Normal View History

package org.gcube.gcat.utils;
import java.io.InputStream;
2023-06-26 10:43:41 +02:00
import java.net.URL;
2023-07-10 16:14:54 +02:00
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.Properties;
import javax.ws.rs.InternalServerErrorException;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
2022-01-31 11:59:58 +01:00
import org.gcube.common.authorization.utils.secret.JWTSecret;
import org.gcube.common.authorization.utils.secret.Secret;
2021-11-30 11:48:35 +01:00
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.TokenResponse;
2023-06-26 10:43:41 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-11-30 11:48:35 +01:00
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Constants {
2023-06-26 10:43:41 +02:00
private static final Logger logger = LoggerFactory.getLogger(Constants.class);
public static final String CATALOGUE_NAME = "gCat";
2023-07-10 16:14:54 +02:00
protected static final String CLIENT_ID_SECRET_FILENAME = "config.properties";
protected static final String CLIENT_ID_PROPERTY_NAME = "clientId";
2021-11-30 11:48:35 +01:00
2023-07-10 16:14:54 +02:00
private static Entry<String, String> getClientIdAndClientSecret(String context) {
2021-11-30 11:48:35 +01:00
try {
2023-07-10 16:14:54 +02:00
Properties properties = new Properties();
ClassLoader classLoader = Constants.class.getClassLoader();
URL url = classLoader.getResource(CLIENT_ID_SECRET_FILENAME);
logger.trace("Going to read {} at {}", CLIENT_ID_SECRET_FILENAME, url.toString());
InputStream input = classLoader.getResourceAsStream(CLIENT_ID_SECRET_FILENAME);
properties.load(input);
String clientId = "gcat";
if(properties.containsKey(CLIENT_ID_PROPERTY_NAME)) {
clientId = properties.getProperty(CLIENT_ID_PROPERTY_NAME);
2021-11-30 11:48:35 +01:00
}
2023-07-10 16:14:54 +02:00
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
String clientSecret = properties.getProperty(root);
SimpleEntry<String, String> entry = new SimpleEntry<String, String>(clientId, clientSecret);
return entry;
2021-11-30 11:48:35 +01:00
} catch(Exception e) {
throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " + SecretManagerProvider.instance.get().getContext(), e);
2021-11-30 11:48:35 +01:00
}
}
2022-01-31 11:59:58 +01:00
private static TokenResponse getJWTAccessToken() throws Exception {
2023-07-11 10:25:13 +02:00
String context = SecretManagerProvider.instance.get().getContext();
Entry<String,String> entry = getClientIdAndClientSecret(context);
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(context, entry.getKey(), entry.getValue(), context, null);
2022-01-31 11:59:58 +01:00
return tr;
2021-11-30 11:48:35 +01:00
}
public static Secret getCatalogueSecret() throws Exception {
2022-01-31 11:59:58 +01:00
TokenResponse tr = getJWTAccessToken();
Secret secret = new JWTSecret(tr.getAccessToken());
return secret;
}
}