Search in sources :

Example 1 with ClientScopeDefinition

use of org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition in project keycloak by keycloak.

the class ClientScopePolicyProviderFactory method onExport.

@Override
public void onExport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorizationProvider) {
    Map<String, String> config = new HashMap<>();
    Set<ClientScopePolicyRepresentation.ClientScopeDefinition> clientScopes = toRepresentation(policy, authorizationProvider).getClientScopes();
    for (ClientScopePolicyRepresentation.ClientScopeDefinition clientScopeDefinition : clientScopes) {
        ClientScopeModel clientScope = authorizationProvider.getRealm().getClientScopeById(clientScopeDefinition.getId());
        clientScopeDefinition.setId(clientScope.getName());
    }
    try {
        config.put("clientScopes", JsonSerialization.writeValueAsString(clientScopes));
    } catch (IOException e) {
        throw new RuntimeException("Failed to export client scope policy [" + policy.getName() + "]", e);
    }
    representation.setConfig(config);
}
Also used : ClientScopeDefinition(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition) HashMap(java.util.HashMap) ClientScopeDefinition(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition) ClientScopePolicyRepresentation(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation) ClientScopeModel(org.keycloak.models.ClientScopeModel) IOException(java.io.IOException)

Example 2 with ClientScopeDefinition

use of org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition in project keycloak by keycloak.

the class ClientScopePolicyProviderFactory method updateClientScopes.

private void updateClientScopes(Policy policy, AuthorizationProvider authorization, Set<ClientScopeDefinition> clientScopes) {
    RealmModel realm = authorization.getRealm();
    Set<ClientScopePolicyRepresentation.ClientScopeDefinition> updatedClientScopes = new HashSet<>();
    if (clientScopes != null) {
        for (ClientScopePolicyRepresentation.ClientScopeDefinition definition : clientScopes) {
            String clientScopeName = definition.getId();
            ClientScopeModel clientScope = realm.getClientScopesStream().filter(scope -> scope.getName().equals(clientScopeName)).findAny().orElse(null);
            if (clientScope == null) {
                clientScope = realm.getClientScopeById(clientScopeName);
            }
            if (clientScope == null) {
                throw new RuntimeException("Error while updating policy [" + policy.getName() + "]. Client Scope [" + "] could not be found.");
            }
            definition.setId(clientScope.getId());
            updatedClientScopes.add(definition);
        }
    }
    try {
        policy.putConfig("clientScopes", JsonSerialization.writeValueAsString(updatedClientScopes));
    } catch (IOException e) {
        throw new RuntimeException("Failed to serialize client scopes", e);
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientScopeDefinition(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition) ClientScopeDefinition(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition) ClientScopePolicyRepresentation(org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation) ClientScopeModel(org.keycloak.models.ClientScopeModel) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)2 ClientScopeModel (org.keycloak.models.ClientScopeModel)2 ClientScopePolicyRepresentation (org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation)2 ClientScopeDefinition (org.keycloak.representations.idm.authorization.ClientScopePolicyRepresentation.ClientScopeDefinition)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RealmModel (org.keycloak.models.RealmModel)1