Search in sources :

Example 6 with ResourceOwnerRepresentation

use of org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation in project keycloak by keycloak.

the class ResourceManagementTest method createResource.

private ResourceRepresentation createResource(String name, String owner, String uri, String type, String iconUri) {
    ResourceRepresentation newResource = new ResourceRepresentation();
    newResource.setName(name);
    newResource.setUri(uri);
    newResource.setType(type);
    newResource.setIconUri(iconUri);
    newResource.setOwner(owner != null ? new ResourceOwnerRepresentation(owner) : null);
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("a", Arrays.asList("a1", "a2", "a3"));
    attributes.put("b", Arrays.asList("b1"));
    newResource.setAttributes(attributes);
    return doCreateResource(newResource);
}
Also used : HashMap(java.util.HashMap) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) List(java.util.List) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Example 7 with ResourceOwnerRepresentation

use of org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation in project keycloak by keycloak.

the class ResourcesTable method toRepresentation.

public ResourceRepresentation toRepresentation(WebElement row) {
    ResourceRepresentation representation = null;
    List<WebElement> tds = row.findElements(tagName("td"));
    try {
        if (!(tds.isEmpty() || getTextFromElement(tds.get(1)).isEmpty())) {
            representation = new ResourceRepresentation();
            representation.setName(getTextFromElement(tds.get(1)));
            representation.setType(getTextFromElement(tds.get(2)));
            representation.setUri(getTextFromElement(tds.get(3)));
            ResourceOwnerRepresentation owner = new ResourceOwnerRepresentation();
            owner.setName(getTextFromElement(tds.get(4)));
            representation.setOwner(owner);
        }
    } catch (IndexOutOfBoundsException cause) {
    // is empty
    }
    return representation;
}
Also used : ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) WebElement(org.openqa.selenium.WebElement) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation)

Example 8 with ResourceOwnerRepresentation

use of org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation in project keycloak by keycloak.

the class ResourceSetService method create.

public ResourceRepresentation create(ResourceRepresentation resource) {
    requireManage();
    StoreFactory storeFactory = this.authorization.getStoreFactory();
    ResourceOwnerRepresentation owner = resource.getOwner();
    if (owner == null) {
        owner = new ResourceOwnerRepresentation();
        owner.setId(resourceServer.getId());
        resource.setOwner(owner);
    }
    String ownerId = owner.getId();
    if (ownerId == null) {
        throw new ErrorResponseException(OAuthErrorException.INVALID_REQUEST, "You must specify the resource owner.", Status.BAD_REQUEST);
    }
    Resource existingResource = storeFactory.getResourceStore().findByName(resource.getName(), ownerId, this.resourceServer.getId());
    if (existingResource != null) {
        throw new ErrorResponseException(OAuthErrorException.INVALID_REQUEST, "Resource with name [" + resource.getName() + "] already exists.", Status.CONFLICT);
    }
    return toRepresentation(toModel(resource, this.resourceServer, authorization), resourceServer.getId(), authorization);
}
Also used : Resource(org.keycloak.authorization.model.Resource) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) ErrorResponseException(org.keycloak.services.ErrorResponseException) StoreFactory(org.keycloak.authorization.store.StoreFactory)

Example 9 with ResourceOwnerRepresentation

use of org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation 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)

Aggregations

ResourceOwnerRepresentation (org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation)9 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)7 List (java.util.List)3 Resource (org.keycloak.authorization.model.Resource)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 RealmModel (org.keycloak.models.RealmModel)3 UserModel (org.keycloak.models.UserModel)3 ScopeRepresentation (org.keycloak.representations.idm.authorization.ScopeRepresentation)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Response (javax.ws.rs.core.Response)2 ResourceServer (org.keycloak.authorization.model.ResourceServer)2 StoreFactory (org.keycloak.authorization.store.StoreFactory)2 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)1 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 IOException (java.io.IOException)1