103 lines
3.5 KiB
Java
103 lines
3.5 KiB
Java
package org.gcube.keycloack;
|
|
|
|
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;
|
|
|
|
import org.gcube.common.security.providers.SecretManagerProvider;
|
|
import org.gcube.common.keycloak.DefaultKeycloakClient;
|
|
import org.gcube.common.keycloak.KeycloakClientException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class KeycloackClientParams_UNUSED {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(KeycloackClientParams_UNUSED.class);
|
|
|
|
public static final String CATALOGUE_NAME = "IDM";
|
|
|
|
protected static final String CLIENT_ID_SECRET_FILENAME = "config.properties";
|
|
protected static final String CLIENT_ID_PROPERTY_NAME = "clientId";
|
|
|
|
public String context;
|
|
public String clientId;
|
|
public String clientSecret;
|
|
public DefaultKeycloakClient gcubeKeycloakClient;
|
|
|
|
// Reads the property file and extracts the keycloack configuration params
|
|
protected static Entry<String, String> getClientIdAndClientSecret(String context) {
|
|
try {
|
|
Properties properties = new Properties();
|
|
ClassLoader classLoader = KeycloackClientParams_UNUSED.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 = "IDM";
|
|
if (properties.containsKey(CLIENT_ID_PROPERTY_NAME)) {
|
|
clientId = properties.getProperty(CLIENT_ID_PROPERTY_NAME);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
// TODO: VERIFICARE
|
|
public URL getRealmBaseURL() throws KeycloakClientException {
|
|
return this.gcubeKeycloakClient.getRealmBaseURL(this.context);
|
|
}
|
|
|
|
public URL getRealmBaseURL(String realm) throws KeycloakClientException {
|
|
return this.gcubeKeycloakClient.getRealmBaseURL(this.context, realm);
|
|
}
|
|
|
|
public URL getServerURL() {
|
|
try {
|
|
return this.getRealmBaseURL();
|
|
} catch (KeycloakClientException e) {
|
|
// That should be almost impossible
|
|
logger.warn("Cannot create base URL", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public String getClientid() {
|
|
return clientId;
|
|
}
|
|
|
|
// TODO: serve? implementare
|
|
public String getPassword() {
|
|
return null;
|
|
}
|
|
|
|
// TODO: VERIFICARE
|
|
public String getRealm() {
|
|
return this.context;
|
|
}
|
|
|
|
public KeycloackClientParams_UNUSED(String context) {
|
|
this.context = context;
|
|
Entry<String, String> params = getClientIdAndClientSecret(context);
|
|
this.clientId = params.getKey();
|
|
this.clientSecret = params.getKey();
|
|
|
|
this.gcubeKeycloakClient = new DefaultKeycloakClient();
|
|
}
|
|
|
|
}
|