common-smartgears/src/main/java/org/gcube/smartgears/security/defaults/DefaultAuthorizationProvide...

77 lines
2.5 KiB
Java
Raw Normal View History

package org.gcube.smartgears.security.defaults;
import java.net.URL;
2022-03-31 11:58:49 +02:00
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
2022-03-31 11:58:49 +02:00
import org.gcube.common.keycloak.KeycloakClient;
import org.gcube.common.keycloak.KeycloakClientFactory;
import org.gcube.common.keycloak.model.AccessToken.Access;
import org.gcube.common.keycloak.model.ModelUtils;
import org.gcube.common.keycloak.model.TokenResponse;
import org.gcube.common.security.ContextBean;
2022-05-30 18:29:46 +02:00
import org.gcube.common.security.secrets.Secret;
2023-02-06 17:34:18 +01:00
import org.gcube.common.security.secrets.UmaTokenSecret;
import org.gcube.smartgears.security.AuthorizationProvider;
import org.gcube.smartgears.security.SimpleCredentials;
2022-03-31 11:58:49 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultAuthorizationProvider implements AuthorizationProvider {
2022-03-31 11:58:49 +02:00
private static Logger LOG = LoggerFactory.getLogger(DefaultAuthorizationProvider.class);
private KeycloakClient client = KeycloakClientFactory.newInstance();
private SimpleCredentials credentials;
2022-05-26 14:39:31 +02:00
private String endpoint;
public DefaultAuthorizationProvider(SimpleCredentials credentials, String endpoint) {
this.credentials = credentials;
2022-05-26 14:39:31 +02:00
this.endpoint = endpoint;
}
@Override
2022-05-26 14:39:31 +02:00
public Set<String> getContexts() {
2022-03-31 11:58:49 +02:00
Set<String> contexts = new HashSet<String>();
try {
TokenResponse response = client.queryOIDCToken(new URL(this.endpoint), credentials.getClientID(), credentials.getSecret());
2022-03-31 11:58:49 +02:00
Map<String, Access> resourceAccess = ModelUtils.getAccessTokenFrom(response).getResourceAccess();
for (String context : resourceAccess.keySet()) {
try {
ContextBean scope = new ContextBean(context.replaceAll("%2F", "/"));
2022-03-31 11:58:49 +02:00
contexts.add(scope.toString());
2022-08-03 12:29:53 +02:00
LOG.debug("found context {}",context);
2022-03-31 11:58:49 +02:00
}catch (IllegalArgumentException e) {
2024-02-20 12:20:16 +01:00
LOG.debug("invalid context found in token: {}", context);
2022-03-31 11:58:49 +02:00
}
}
} catch (Exception e) {
LOG.error("error getting OIDToken from keycloak",e);
return Collections.emptySet();
}
return contexts;
}
2022-05-30 18:29:46 +02:00
@Override
public Secret getSecretForContext(String context) {
try {
TokenResponse response = client.queryUMAToken(new URL(this.endpoint), credentials.getClientID(), credentials.getSecret(), context, null);
2023-02-06 17:34:18 +01:00
return new UmaTokenSecret(response.getAccessToken());
2022-05-30 18:29:46 +02:00
} catch (Exception e) {
LOG.error("error getting OIDToken from keycloak",e);
throw new RuntimeException("error getting access token for context "+context, e);
}
}
2022-06-22 18:50:54 +02:00
public SimpleCredentials getCredentials() {
return credentials;
}
}