clientId is now got from properties

This commit is contained in:
luca.frosini 2023-07-10 16:14:54 +02:00
parent e5cf080bcd
commit ca07b2f9f1
2 changed files with 27 additions and 19 deletions

View File

@ -2,6 +2,8 @@ package org.gcube.gcat.utils;
import java.io.InputStream;
import java.net.URL;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.Properties;
import javax.ws.rs.InternalServerErrorException;
@ -23,24 +25,29 @@ public class Constants {
public static final String CATALOGUE_NAME = "gCat";
protected static final String CLIENT_SECRET_FILENAME = "config.properties";
protected static final String CLIENT_ID = "gcat";
protected static String clientSecret;
protected static final String CLIENT_ID_SECRET_FILENAME = "config.properties";
protected static final String CLIENT_ID_PROPERTY_NAME = "clientId";
private static String getClientSecret(String context) {
private static Entry<String, String> getClientIdAndClientSecret(String context) {
try {
if(clientSecret==null) {
Properties properties = new Properties();
ClassLoader classLoader = Constants.class.getClassLoader();
URL url = classLoader.getResource(CLIENT_SECRET_FILENAME);
logger.trace("Going to read {} at {}", CLIENT_SECRET_FILENAME, url.toString());
InputStream input = classLoader.getResourceAsStream(CLIENT_SECRET_FILENAME);
properties.load(input);
int index = context.indexOf('/', 1);
String root = context.substring(0, index == -1 ? context.length() : index);
clientSecret = properties.getProperty(root);
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);
}
return clientSecret;
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;
} catch(Exception e) {
throw new InternalServerErrorException(
"Unable to retrieve Application Token for context " + SecretManagerProvider.instance.get().getContext(), e);
@ -49,7 +56,8 @@ public class Constants {
private static TokenResponse getJWTAccessToken() throws Exception {
String contextToAuthorise = SecretManagerProvider.instance.get().getContext();
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(CLIENT_ID, getClientSecret(contextToAuthorise), contextToAuthorise, null);
Entry<String,String> entry = getClientIdAndClientSecret(contextToAuthorise);
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(entry.getKey(), entry.getValue(), contextToAuthorise, null);
return tr;
}

View File

@ -38,11 +38,11 @@ public class ConstantsTest extends ContextTest {
@Test
public void getResourceTest() {
URL url1 = ConstantsTest.class.getResource(Constants.CLIENT_SECRET_FILENAME);
URL url1 = ConstantsTest.class.getResource(Constants.CLIENT_ID_SECRET_FILENAME);
logger.debug("{}", url1);
URL url2 = ConstantsTest.class.getClassLoader().getResource(Constants.CLIENT_SECRET_FILENAME);
URL url2 = ConstantsTest.class.getClassLoader().getResource(Constants.CLIENT_ID_SECRET_FILENAME);
logger.debug("{}", url2);
URL url3 = ClassLoader.getSystemClassLoader().getResource(Constants.CLIENT_SECRET_FILENAME);
URL url3 = ClassLoader.getSystemClassLoader().getResource(Constants.CLIENT_ID_SECRET_FILENAME);
logger.debug("{}", url3);
}