Search in sources :

Example 31 with ClientModel

use of org.keycloak.models.ClientModel in project keycloak by keycloak.

the class JpaRealmProvider method addClientScopes.

@Override
public void addClientScopes(RealmModel realm, ClientModel client, Set<ClientScopeModel> clientScopes, boolean defaultScope) {
    // Defaults to openid-connect
    String clientProtocol = client.getProtocol() == null ? OIDCLoginProtocol.LOGIN_PROTOCOL : client.getProtocol();
    Map<String, ClientScopeModel> existingClientScopes = getClientScopes(realm, client, true);
    existingClientScopes.putAll(getClientScopes(realm, client, false));
    clientScopes.stream().filter(clientScope -> !existingClientScopes.containsKey(clientScope.getName())).filter(clientScope -> Objects.equals(clientScope.getProtocol(), clientProtocol)).forEach(clientScope -> {
        ClientScopeClientMappingEntity entity = new ClientScopeClientMappingEntity();
        entity.setClientScopeId(clientScope.getId());
        entity.setClientId(client.getId());
        entity.setDefaultScope(defaultScope);
        em.persist(entity);
        em.flush();
        em.detach(entity);
    });
}
Also used : GroupEntity(org.keycloak.models.jpa.entities.GroupEntity) Join(javax.persistence.criteria.Join) ClientProvider(org.keycloak.models.ClientProvider) RoleContainerModel(org.keycloak.models.RoleContainerModel) StackUtil.getShortStackTrace(org.keycloak.common.util.StackUtil.getShortStackTrace) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) DeploymentStateProvider(org.keycloak.models.DeploymentStateProvider) ClientEntity(org.keycloak.models.jpa.entities.ClientEntity) Time(org.keycloak.common.util.Time) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) RoleProvider(org.keycloak.models.RoleProvider) StreamsUtil.closing(org.keycloak.utils.StreamsUtil.closing) Set(java.util.Set) RoleModel(org.keycloak.models.RoleModel) Collectors(java.util.stream.Collectors) PaginationUtils.paginateQuery(org.keycloak.models.jpa.PaginationUtils.paginateQuery) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) RoleEntity(org.keycloak.models.jpa.entities.RoleEntity) ClientModel(org.keycloak.models.ClientModel) ClientScopeEntity(org.keycloak.models.jpa.entities.ClientScopeEntity) RealmLocalizationTextsEntity(org.keycloak.models.jpa.entities.RealmLocalizationTextsEntity) KeycloakModelUtils(org.keycloak.models.utils.KeycloakModelUtils) Logger(org.jboss.logging.Logger) HashMap(java.util.HashMap) MigrationModel(org.keycloak.migration.MigrationModel) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) ArrayList(java.util.ArrayList) RealmProvider(org.keycloak.models.RealmProvider) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) GroupModel(org.keycloak.models.GroupModel) GroupProvider(org.keycloak.models.GroupProvider) ClientModelLazyDelegate(org.keycloak.models.delegate.ClientModelLazyDelegate) Root(javax.persistence.criteria.Root) RoleRemovedEvent(org.keycloak.models.RoleContainerModel.RoleRemovedEvent) KeycloakSession(org.keycloak.models.KeycloakSession) ClientScopeClientMappingEntity(org.keycloak.models.jpa.entities.ClientScopeClientMappingEntity) EntityManager(javax.persistence.EntityManager) JpaUtils(org.keycloak.connections.jpa.util.JpaUtils) ModelException(org.keycloak.models.ModelException) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) LockModeType(javax.persistence.LockModeType) RealmEntity(org.keycloak.models.jpa.entities.RealmEntity) ClientScopeProvider(org.keycloak.models.ClientScopeProvider) ClientAttributeEntity(org.keycloak.models.jpa.entities.ClientAttributeEntity) ClientScopeClientMappingEntity(org.keycloak.models.jpa.entities.ClientScopeClientMappingEntity) ClientScopeModel(org.keycloak.models.ClientScopeModel)

Example 32 with ClientModel

use of org.keycloak.models.ClientModel in project keycloak by keycloak.

the class JpaRealmProvider method removeRole.

@Override
public boolean removeRole(RoleModel role) {
    RealmModel realm;
    if (role.getContainer() instanceof RealmModel) {
        realm = (RealmModel) role.getContainer();
    } else if (role.getContainer() instanceof ClientModel) {
        realm = ((ClientModel) role.getContainer()).getRealm();
    } else {
        throw new IllegalStateException("RoleModel's container isn not instance of either RealmModel or ClientModel");
    }
    session.users().preRemove(realm, role);
    RoleEntity roleEntity = em.getReference(RoleEntity.class, role.getId());
    if (roleEntity == null || !roleEntity.getRealmId().equals(realm.getId())) {
        // Throw model exception to ensure transaction rollback and revert previous operations (removing default roles) as well
        throw new ModelException("Role not found or trying to remove role from incorrect realm");
    }
    String compositeRoleTable = JpaUtils.getTableNameForNativeQuery("COMPOSITE_ROLE", em);
    em.createNativeQuery("delete from " + compositeRoleTable + " where CHILD_ROLE = :role").setParameter("role", roleEntity).executeUpdate();
    em.createNamedQuery("deleteClientScopeRoleMappingByRole").setParameter("role", roleEntity).executeUpdate();
    em.flush();
    em.remove(roleEntity);
    session.getKeycloakSessionFactory().publish(roleRemovedEvent(role));
    em.flush();
    return true;
}
Also used : RealmModel(org.keycloak.models.RealmModel) RoleEntity(org.keycloak.models.jpa.entities.RoleEntity) ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException)

Example 33 with ClientModel

use of org.keycloak.models.ClientModel in project keycloak by keycloak.

the class JpaRealmProvider method removeClient.

@Override
public boolean removeClient(RealmModel realm, String id) {
    logger.tracef("removeClient(%s, %s)%s", realm, id, getShortStackTrace());
    final ClientModel client = getClientById(realm, id);
    if (client == null)
        return false;
    session.users().preRemove(realm, client);
    session.roles().removeRoles(client);
    ClientEntity clientEntity = em.find(ClientEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
    session.getKeycloakSessionFactory().publish(new ClientModel.ClientRemovedEvent() {

        @Override
        public ClientModel getClient() {
            return client;
        }

        @Override
        public KeycloakSession getKeycloakSession() {
            return session;
        }
    });
    int countRemoved = em.createNamedQuery("deleteClientScopeClientMappingByClient").setParameter("clientId", clientEntity.getId()).executeUpdate();
    // i have no idea why, but this needs to come before deleteScopeMapping
    em.remove(clientEntity);
    try {
        em.flush();
    } catch (RuntimeException e) {
        logger.errorv("Unable to delete client entity: {0} from realm {1}", client.getClientId(), realm.getName());
        throw e;
    }
    return true;
}
Also used : ClientModel(org.keycloak.models.ClientModel) ClientEntity(org.keycloak.models.jpa.entities.ClientEntity) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 34 with ClientModel

use of org.keycloak.models.ClientModel in project keycloak by keycloak.

the class JpaUserProvider method toConsentModel.

private UserConsentModel toConsentModel(RealmModel realm, UserConsentEntity entity) {
    if (entity == null) {
        return null;
    }
    StorageId clientStorageId = null;
    if (entity.getClientId() == null) {
        clientStorageId = new StorageId(entity.getClientStorageProvider(), entity.getExternalClientId());
    } else {
        clientStorageId = new StorageId(entity.getClientId());
    }
    ClientModel client = realm.getClientById(clientStorageId.getId());
    if (client == null) {
        throw new ModelException("Client with id " + clientStorageId.getId() + " is not available");
    }
    UserConsentModel model = new UserConsentModel(client);
    model.setCreatedDate(entity.getCreatedDate());
    model.setLastUpdatedDate(entity.getLastUpdatedDate());
    Collection<UserConsentClientScopeEntity> grantedClientScopeEntities = entity.getGrantedClientScopes();
    if (grantedClientScopeEntities != null) {
        for (UserConsentClientScopeEntity grantedClientScope : grantedClientScopeEntities) {
            ClientScopeModel grantedClientScopeModel = KeycloakModelUtils.findClientScopeById(realm, client, grantedClientScope.getScopeId());
            if (grantedClientScopeModel != null) {
                model.addGrantedClientScope(grantedClientScopeModel);
            }
        }
    }
    return model;
}
Also used : ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException) ClientScopeModel(org.keycloak.models.ClientScopeModel) StorageId(org.keycloak.storage.StorageId) UserConsentClientScopeEntity(org.keycloak.models.jpa.entities.UserConsentClientScopeEntity) UserConsentModel(org.keycloak.models.UserConsentModel)

Example 35 with ClientModel

use of org.keycloak.models.ClientModel in project keycloak by keycloak.

the class JpaRealmProviderFactory method onEvent.

@Override
public void onEvent(ProviderEvent event) {
    if (event instanceof RoleContainerModel.RoleRemovedEvent) {
        RoleRemovedEvent e = (RoleContainerModel.RoleRemovedEvent) event;
        RoleModel role = e.getRole();
        RoleContainerModel container = role.getContainer();
        RealmModel realm;
        if (container instanceof RealmModel) {
            realm = (RealmModel) container;
        } else if (container instanceof ClientModel) {
            realm = ((ClientModel) container).getRealm();
        } else {
            return;
        }
        ((JpaRealmProvider) e.getKeycloakSession().getProvider(RealmProvider.class)).preRemove(realm, role);
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) RealmProvider(org.keycloak.models.RealmProvider) RoleRemovedEvent(org.keycloak.models.RoleContainerModel.RoleRemovedEvent) RoleModel(org.keycloak.models.RoleModel) RoleContainerModel(org.keycloak.models.RoleContainerModel)

Aggregations

ClientModel (org.keycloak.models.ClientModel)344 RealmModel (org.keycloak.models.RealmModel)148 UserModel (org.keycloak.models.UserModel)88 RoleModel (org.keycloak.models.RoleModel)74 KeycloakSession (org.keycloak.models.KeycloakSession)67 Test (org.junit.Test)64 UserSessionModel (org.keycloak.models.UserSessionModel)41 ResourceServer (org.keycloak.authorization.model.ResourceServer)39 Policy (org.keycloak.authorization.model.Policy)38 HashMap (java.util.HashMap)37 AuthorizationProvider (org.keycloak.authorization.AuthorizationProvider)36 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)34 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)34 List (java.util.List)32 Map (java.util.Map)32 Path (javax.ws.rs.Path)29 LinkedList (java.util.LinkedList)28 ClientScopeModel (org.keycloak.models.ClientScopeModel)28 ArrayList (java.util.ArrayList)27 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)27