Search in sources :

Example 21 with Resource

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

the class AuthorizationBean method toPermissionRepresentation.

private Collection<RequesterBean> toPermissionRepresentation(List<PermissionTicket> permissionRequests) {
    Map<String, RequesterBean> requests = new HashMap<>();
    for (PermissionTicket ticket : permissionRequests) {
        Resource resource = ticket.getResource();
        if (!resource.isOwnerManagedAccess()) {
            continue;
        }
        requests.computeIfAbsent(ticket.getRequester(), resourceId -> new RequesterBean(ticket, authorization)).addScope(ticket);
    }
    return requests.values();
}
Also used : ClientModel(org.keycloak.models.ClientModel) Scope(org.keycloak.authorization.model.Scope) Date(java.util.Date) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) ResolveRelative(org.keycloak.services.util.ResolveRelative) UserModel(org.keycloak.models.UserModel) Map(java.util.Map) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) Time(org.keycloak.common.util.Time) RealmModel(org.keycloak.models.RealmModel) EnumMap(java.util.EnumMap) Collection(java.util.Collection) Set(java.util.Set) KeycloakSession(org.keycloak.models.KeycloakSession) Collectors(java.util.stream.Collectors) Policy(org.keycloak.authorization.model.Policy) List(java.util.List) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) PermissionTicketStore(org.keycloak.authorization.store.PermissionTicketStore) UriInfo(javax.ws.rs.core.UriInfo) Collections(java.util.Collections) Resource(org.keycloak.authorization.model.Resource) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) HashMap(java.util.HashMap) Resource(org.keycloak.authorization.model.Resource)

Example 22 with Resource

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

the class RepresentationToModel method updateResources.

private static void updateResources(Set<String> resourceIds, Policy policy, StoreFactory storeFactory) {
    if (resourceIds != null) {
        if (resourceIds.isEmpty()) {
            for (Resource resource : new HashSet<>(policy.getResources())) {
                policy.removeResource(resource);
            }
        }
        for (String resourceId : resourceIds) {
            boolean hasResource = false;
            for (Resource resourceModel : new HashSet<>(policy.getResources())) {
                if (resourceModel.getId().equals(resourceId) || resourceModel.getName().equals(resourceId)) {
                    hasResource = true;
                }
            }
            if (!hasResource && !"".equals(resourceId)) {
                Resource resource = storeFactory.getResourceStore().findById(resourceId, policy.getResourceServer().getId());
                if (resource == null) {
                    resource = storeFactory.getResourceStore().findByName(resourceId, policy.getResourceServer().getId());
                    if (resource == null) {
                        throw new RuntimeException("Resource with id or name [" + resourceId + "] does not exist or is not owned by the resource server");
                    }
                }
                policy.addResource(resource);
            }
        }
        for (Resource resourceModel : new HashSet<>(policy.getResources())) {
            boolean hasResource = false;
            for (String resourceId : resourceIds) {
                if (resourceModel.getId().equals(resourceId) || resourceModel.getName().equals(resourceId)) {
                    hasResource = true;
                }
            }
            if (!hasResource) {
                policy.removeResource(resourceModel);
            }
        }
    }
    policy.removeConfig("resources");
}
Also used : Resource(org.keycloak.authorization.model.Resource) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) HashSet(java.util.HashSet)

Example 23 with Resource

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

the class RepresentationToModel method updateClientProtocolMappers.

public static void updateClientProtocolMappers(ClientRepresentation rep, ClientModel resource) {
    if (rep.getProtocolMappers() != null) {
        Map<String, ProtocolMapperModel> existingProtocolMappers = resource.getProtocolMappersStream().collect(Collectors.toMap(mapper -> generateProtocolNameKey(mapper.getProtocol(), mapper.getName()), Function.identity()));
        for (ProtocolMapperRepresentation protocolMapperRepresentation : rep.getProtocolMappers()) {
            String protocolNameKey = generateProtocolNameKey(protocolMapperRepresentation.getProtocol(), protocolMapperRepresentation.getName());
            ProtocolMapperModel existingMapper = existingProtocolMappers.get(protocolNameKey);
            if (existingMapper != null) {
                ProtocolMapperModel updatedProtocolMapperModel = toModel(protocolMapperRepresentation);
                updatedProtocolMapperModel.setId(existingMapper.getId());
                resource.updateProtocolMapper(updatedProtocolMapperModel);
                existingProtocolMappers.remove(protocolNameKey);
            } else {
                resource.addProtocolMapper(toModel(protocolMapperRepresentation));
            }
        }
        for (Map.Entry<String, ProtocolMapperModel> entryToDelete : existingProtocolMappers.entrySet()) {
            resource.removeProtocolMapper(entryToDelete.getValue());
        }
    }
}
Also used : Arrays(java.util.Arrays) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) AuthenticationExecutionExportRepresentation(org.keycloak.representations.idm.AuthenticationExecutionExportRepresentation) LDAPConstants(org.keycloak.models.LDAPConstants) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) OAuth2DeviceConfig(org.keycloak.models.OAuth2DeviceConfig) Map(java.util.Map) SocialLinkRepresentation(org.keycloak.representations.idm.SocialLinkRepresentation) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) UserCredentialModel(org.keycloak.models.UserCredentialModel) PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation) Set(java.util.Set) IdentityProviderModel(org.keycloak.models.IdentityProviderModel) PolicyStore(org.keycloak.authorization.store.PolicyStore) ResourceStore(org.keycloak.authorization.store.ResourceStore) ClientTemplateRepresentation(org.keycloak.representations.idm.ClientTemplateRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) SocialIdentityProvider(org.keycloak.broker.social.SocialIdentityProvider) ResourceServerRepresentation(org.keycloak.representations.idm.authorization.ResourceServerRepresentation) SslRequired(org.keycloak.common.enums.SslRequired) PermissionTicketStore(org.keycloak.authorization.store.PermissionTicketStore) OAuth2Constants(org.keycloak.OAuth2Constants) IdentityProviderRepresentation(org.keycloak.representations.idm.IdentityProviderRepresentation) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel) ClaimRepresentation(org.keycloak.representations.idm.ClaimRepresentation) Constants(org.keycloak.models.Constants) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) MigrationProvider(org.keycloak.migration.MigrationProvider) ArrayList(java.util.ArrayList) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) OTPSecretData(org.keycloak.models.credential.dto.OTPSecretData) UserModel(org.keycloak.models.UserModel) UserFederationProviderRepresentation(org.keycloak.representations.idm.UserFederationProviderRepresentation) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) MigrationUtils(org.keycloak.migration.migrators.MigrationUtils) ValidationUtil(org.keycloak.validation.ValidationUtil) PolicyProviderFactory(org.keycloak.authorization.policy.provider.PolicyProviderFactory) CredentialModel(org.keycloak.credential.CredentialModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) WebAuthnPolicy(org.keycloak.models.WebAuthnPolicy) KeycloakSession(org.keycloak.models.KeycloakSession) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) IOException(java.io.IOException) ResourceServerStore(org.keycloak.authorization.store.ResourceServerStore) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) AbstractPolicyRepresentation(org.keycloak.representations.idm.authorization.AbstractPolicyRepresentation) ListIterator(java.util.ListIterator) MigrateTo8_0_0(org.keycloak.migration.migrators.MigrateTo8_0_0) PasswordPolicyNotMetException(org.keycloak.policy.PasswordPolicyNotMetException) Config(org.keycloak.Config) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) ClaimMask(org.keycloak.models.ClaimMask) ComponentModel(org.keycloak.component.ComponentModel) BrowserSecurityHeaders(org.keycloak.models.BrowserSecurityHeaders) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) ResourceOwnerRepresentation(org.keycloak.representations.idm.authorization.ResourceOwnerRepresentation) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) AuthorizationProvider(org.keycloak.authorization.AuthorizationProvider) PolicyEnforcementMode(org.keycloak.representations.idm.authorization.PolicyEnforcementMode) UriUtils(org.keycloak.common.util.UriUtils) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) AuthorizationProviderFactory(org.keycloak.authorization.AuthorizationProviderFactory) RoleModel(org.keycloak.models.RoleModel) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) PermissionTicketRepresentation(org.keycloak.representations.idm.authorization.PermissionTicketRepresentation) UserProvider(org.keycloak.models.UserProvider) List(java.util.List) ScopeStore(org.keycloak.authorization.store.ScopeStore) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Entry(java.util.Map.Entry) RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) ClientModel(org.keycloak.models.ClientModel) Scope(org.keycloak.authorization.model.Scope) IdentityProviderFactory(org.keycloak.broker.provider.IdentityProviderFactory) Profile(org.keycloak.common.Profile) IdentityProviderMapperModel(org.keycloak.models.IdentityProviderMapperModel) ScopeMappingRepresentation(org.keycloak.representations.idm.ScopeMappingRepresentation) Logger(org.jboss.logging.Logger) StoreFactory(org.keycloak.authorization.store.StoreFactory) HashMap(java.util.HashMap) OAuthClientRepresentation(org.keycloak.representations.idm.OAuthClientRepresentation) Function(java.util.function.Function) PolicyRepresentation(org.keycloak.representations.idm.authorization.PolicyRepresentation) HashSet(java.util.HashSet) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ComponentExportRepresentation(org.keycloak.representations.idm.ComponentExportRepresentation) UserFederationMapperRepresentation(org.keycloak.representations.idm.UserFederationMapperRepresentation) UserConsentModel(org.keycloak.models.UserConsentModel) KeyProvider(org.keycloak.keys.KeyProvider) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) IdentityProviderMapperRepresentation(org.keycloak.representations.idm.IdentityProviderMapperRepresentation) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) OTPPolicy(org.keycloak.models.OTPPolicy) GroupModel(org.keycloak.models.GroupModel) ScopeRepresentation(org.keycloak.representations.idm.authorization.ScopeRepresentation) LinkedList(java.util.LinkedList) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) IdentityProvider(org.keycloak.broker.provider.IdentityProvider) ScopeContainerModel(org.keycloak.models.ScopeContainerModel) ResourceServer(org.keycloak.authorization.model.ResourceServer) FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) RolesRepresentation(org.keycloak.representations.idm.RolesRepresentation) OTPCredentialData(org.keycloak.models.credential.dto.OTPCredentialData) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Iterator(java.util.Iterator) CibaConfig(org.keycloak.models.CibaConfig) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) ApplicationRepresentation(org.keycloak.representations.idm.ApplicationRepresentation) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) DecisionStrategy(org.keycloak.representations.idm.authorization.DecisionStrategy) JsonSerialization(org.keycloak.util.JsonSerialization) Policy(org.keycloak.authorization.model.Policy) AuthenticatorConfigRepresentation(org.keycloak.representations.idm.AuthenticatorConfigRepresentation) ModelException(org.keycloak.models.ModelException) ParConfig(org.keycloak.models.ParConfig) PasswordPolicy(org.keycloak.models.PasswordPolicy) Resource(org.keycloak.authorization.model.Resource) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) ProtocolMapperModel(org.keycloak.models.ProtocolMapperModel)

Example 24 with Resource

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

the class MapResourceStore method create.

@Override
public Resource create(String id, String name, ResourceServer resourceServer, String owner) {
    LOG.tracef("create(%s, %s, %s, %s)%s", id, name, resourceServer, owner, getShortStackTrace());
    // @UniqueConstraint(columnNames = {"NAME", "RESOURCE_SERVER_ID", "OWNER"})
    DefaultModelCriteria<Resource> mcb = forResourceServer(resourceServer.getId()).compare(SearchableFields.NAME, Operator.EQ, name).compare(SearchableFields.OWNER, Operator.EQ, owner);
    if (tx.getCount(withCriteria(mcb)) > 0) {
        throw new ModelDuplicateException("Resource with name '" + name + "' for " + resourceServer.getId() + " already exists for request owner " + owner);
    }
    MapResourceEntity entity = new MapResourceEntityImpl();
    entity.setId(id);
    entity.setName(name);
    entity.setResourceServerId(resourceServer.getId());
    entity.setOwner(owner);
    entity = tx.create(entity);
    return entityToAdapter(entity);
}
Also used : Resource(org.keycloak.authorization.model.Resource) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) MapResourceEntity(org.keycloak.models.map.authorization.entity.MapResourceEntity) MapResourceEntityImpl(org.keycloak.models.map.authorization.entity.MapResourceEntityImpl)

Example 25 with Resource

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

the class ResourceSetService method delete.

@Path("{id}")
@DELETE
public Response delete(@PathParam("id") String id) {
    requireManage();
    StoreFactory storeFactory = authorization.getStoreFactory();
    Resource resource = storeFactory.getResourceStore().findById(id, resourceServer.getId());
    if (resource == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    storeFactory.getResourceStore().delete(id);
    audit(toRepresentation(resource, resourceServer.getId(), authorization), OperationType.DELETE);
    return Response.noContent().build();
}
Also used : Resource(org.keycloak.authorization.model.Resource) StoreFactory(org.keycloak.authorization.store.StoreFactory) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

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