Search in sources :

Example 86 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class RepresentationToModel method toModel.

public static Resource toModel(ResourceRepresentation resource, ResourceServer resourceServer, AuthorizationProvider authorization) {
    ResourceStore resourceStore = authorization.getStoreFactory().getResourceStore();
    ResourceOwnerRepresentation owner = resource.getOwner();
    if (owner == null) {
        owner = new ResourceOwnerRepresentation();
        owner.setId(resourceServer.getId());
    }
    String ownerId = owner.getId();
    if (ownerId == null) {
        ownerId = resourceServer.getId();
    }
    if (!resourceServer.getId().equals(ownerId)) {
        RealmModel realm = authorization.getRealm();
        KeycloakSession keycloakSession = authorization.getKeycloakSession();
        UserProvider users = keycloakSession.users();
        UserModel ownerModel = users.getUserById(realm, ownerId);
        if (ownerModel == null) {
            ownerModel = users.getUserByUsername(realm, ownerId);
        }
        if (ownerModel == null) {
            throw new RuntimeException("Owner must be a valid username or user identifier. If the resource server, the client id or null.");
        }
        ownerId = ownerModel.getId();
    }
    Resource existing;
    if (resource.getId() != null) {
        existing = resourceStore.findById(resource.getId(), resourceServer.getId());
    } else {
        existing = resourceStore.findByName(resource.getName(), ownerId, resourceServer.getId());
    }
    if (existing != null) {
        existing.setName(resource.getName());
        existing.setDisplayName(resource.getDisplayName());
        existing.setType(resource.getType());
        existing.updateUris(resource.getUris());
        existing.setIconUri(resource.getIconUri());
        existing.setOwnerManagedAccess(Boolean.TRUE.equals(resource.getOwnerManagedAccess()));
        existing.updateScopes(resource.getScopes().stream().map((ScopeRepresentation scope) -> toModel(scope, resourceServer, authorization, false)).collect(Collectors.toSet()));
        Map<String, List<String>> attributes = resource.getAttributes();
        if (attributes != null) {
            Set<String> existingAttrNames = existing.getAttributes().keySet();
            for (String name : existingAttrNames) {
                if (attributes.containsKey(name)) {
                    existing.setAttribute(name, attributes.get(name));
                    attributes.remove(name);
                } else {
                    existing.removeAttribute(name);
                }
            }
            for (String name : attributes.keySet()) {
                existing.setAttribute(name, attributes.get(name));
            }
        }
        return existing;
    }
    Resource model = resourceStore.create(resource.getId(), resource.getName(), resourceServer, ownerId);
    model.setDisplayName(resource.getDisplayName());
    model.setType(resource.getType());
    model.updateUris(resource.getUris());
    model.setIconUri(resource.getIconUri());
    model.setOwnerManagedAccess(Boolean.TRUE.equals(resource.getOwnerManagedAccess()));
    Set<ScopeRepresentation> scopes = resource.getScopes();
    if (scopes != null) {
        model.updateScopes(scopes.stream().map(scope -> toModel(scope, resourceServer, authorization, false)).collect(Collectors.toSet()));
    }
    Map<String, List<String>> attributes = resource.getAttributes();
    if (attributes != null) {
        for (Entry<String, List<String>> entry : attributes.entrySet()) {
            model.setAttribute(entry.getKey(), entry.getValue());
        }
    }
    resource.setId(model.getId());
    return model;
}
Also used : Resource(org.keycloak.authorization.model.Resource) ResourceStore(org.keycloak.authorization.store.ResourceStore) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserProvider(org.keycloak.models.UserProvider) KeycloakSession(org.keycloak.models.KeycloakSession) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 87 with Resource

use of org.keycloak.authorization.model.Resource in project keycloak by keycloak.

the class ModelToRepresentation method toRepresentation.

public static PermissionTicketRepresentation toRepresentation(PermissionTicket ticket, AuthorizationProvider authorization, boolean returnNames) {
    PermissionTicketRepresentation representation = new PermissionTicketRepresentation();
    representation.setId(ticket.getId());
    representation.setGranted(ticket.isGranted());
    representation.setOwner(ticket.getOwner());
    representation.setRequester(ticket.getRequester());
    Resource resource = ticket.getResource();
    representation.setResource(resource.getId());
    if (returnNames) {
        representation.setResourceName(resource.getName());
        KeycloakSession keycloakSession = authorization.getKeycloakSession();
        RealmModel realm = authorization.getRealm();
        UserModel userOwner = keycloakSession.users().getUserById(realm, ticket.getOwner());
        UserModel requester = keycloakSession.users().getUserById(realm, ticket.getRequester());
        representation.setRequesterName(requester.getUsername());
        if (userOwner != null) {
            representation.setOwnerName(userOwner.getUsername());
        } else {
            ClientModel clientOwner = realm.getClientById(ticket.getOwner());
            representation.setOwnerName(clientOwner.getClientId());
        }
    }
    Scope scope = ticket.getScope();
    if (scope != null) {
        representation.setScope(scope.getId());
        if (returnNames) {
            representation.setScopeName(scope.getName());
        }
    }
    return representation;
}
Also used : Scope(org.keycloak.authorization.model.Scope) Resource(org.keycloak.authorization.model.Resource)

Aggregations

Resource (org.keycloak.authorization.model.Resource)87 ResourceServer (org.keycloak.authorization.model.ResourceServer)51 Policy (org.keycloak.authorization.model.Policy)45 Scope (org.keycloak.authorization.model.Scope)44 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)27 ResourceStore (org.keycloak.authorization.store.ResourceStore)27 StoreFactory (org.keycloak.authorization.store.StoreFactory)26 ArrayList (java.util.ArrayList)22 ClientModel (org.keycloak.models.ClientModel)22 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)19 UserModel (org.keycloak.models.UserModel)18 RealmModel (org.keycloak.models.RealmModel)16 HashMap (java.util.HashMap)15 Set (java.util.Set)15 EnumMap (java.util.EnumMap)14 Collectors (java.util.stream.Collectors)14 Path (javax.ws.rs.Path)13 PolicyStore (org.keycloak.authorization.store.PolicyStore)13