Search in sources :

Example 36 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserSessionPersisterProvider method removeClientSession.

@Override
public void removeClientSession(String userSessionId, String clientUUID, boolean offline) {
    String offlineStr = offlineToString(offline);
    StorageId clientStorageId = new StorageId(clientUUID);
    String clientId = PersistentClientSessionEntity.EXTERNAL;
    String clientStorageProvider = PersistentClientSessionEntity.LOCAL;
    String externalId = PersistentClientSessionEntity.LOCAL;
    if (clientStorageId.isLocal()) {
        clientId = clientUUID;
    } else {
        clientStorageProvider = clientStorageId.getProviderId();
        externalId = clientStorageId.getExternalId();
    }
    PersistentClientSessionEntity sessionEntity = em.find(PersistentClientSessionEntity.class, new PersistentClientSessionEntity.Key(userSessionId, clientId, clientStorageProvider, externalId, offlineStr), LockModeType.PESSIMISTIC_WRITE);
    if (sessionEntity != null) {
        em.remove(sessionEntity);
        // Remove userSession if it was last clientSession
        List<PersistentClientSessionEntity> clientSessions = getClientSessionsByUserSession(sessionEntity.getUserSessionId(), offline);
        if (clientSessions.size() == 0) {
            offlineStr = offlineToString(offline);
            PersistentUserSessionEntity userSessionEntity = em.find(PersistentUserSessionEntity.class, new PersistentUserSessionEntity.Key(sessionEntity.getUserSessionId(), offlineStr), LockModeType.PESSIMISTIC_WRITE);
            if (userSessionEntity != null) {
                em.remove(userSessionEntity);
            }
        }
        em.flush();
    }
}
Also used : StorageId(org.keycloak.storage.StorageId)

Example 37 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserSessionPersisterProvider method onClientRemoved.

private void onClientRemoved(String clientUUID) {
    int num = 0;
    StorageId clientStorageId = new StorageId(clientUUID);
    if (clientStorageId.isLocal()) {
        num = em.createNamedQuery("deleteClientSessionsByClient").setParameter("clientId", clientUUID).executeUpdate();
    } else {
        num = em.createNamedQuery("deleteClientSessionsByExternalClient").setParameter("clientStorageProvider", clientStorageId.getProviderId()).setParameter("externalClientId", clientStorageId.getExternalId()).executeUpdate();
    }
}
Also used : StorageId(org.keycloak.storage.StorageId)

Example 38 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserProvider method getGrantedConsentEntity.

private UserConsentEntity getGrantedConsentEntity(String userId, String clientId, LockModeType lockMode) {
    StorageId clientStorageId = new StorageId(clientId);
    String queryName = clientStorageId.isLocal() ? "userConsentByUserAndClient" : "userConsentByUserAndExternalClient";
    TypedQuery<UserConsentEntity> query = em.createNamedQuery(queryName, UserConsentEntity.class);
    query.setParameter("userId", userId);
    if (clientStorageId.isLocal()) {
        query.setParameter("clientId", clientId);
    } else {
        query.setParameter("clientStorageProvider", clientStorageId.getProviderId());
        query.setParameter("externalClientId", clientStorageId.getExternalId());
    }
    query.setLockMode(lockMode);
    List<UserConsentEntity> results = query.getResultList();
    if (results.size() > 1) {
        throw new ModelException("More results found for user [" + userId + "] and client [" + clientId + "]");
    } else if (results.size() == 1) {
        return results.get(0);
    } else {
        return null;
    }
}
Also used : ModelException(org.keycloak.models.ModelException) StorageId(org.keycloak.storage.StorageId) UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity)

Example 39 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class JpaUserProvider method addConsent.

@Override
public void addConsent(RealmModel realm, String userId, UserConsentModel consent) {
    String clientId = consent.getClient().getId();
    UserConsentEntity consentEntity = getGrantedConsentEntity(userId, clientId, LockModeType.NONE);
    if (consentEntity != null) {
        throw new ModelDuplicateException("Consent already exists for client [" + clientId + "] and user [" + userId + "]");
    }
    long currentTime = Time.currentTimeMillis();
    consentEntity = new UserConsentEntity();
    consentEntity.setId(KeycloakModelUtils.generateId());
    consentEntity.setUser(em.getReference(UserEntity.class, userId));
    StorageId clientStorageId = new StorageId(clientId);
    if (clientStorageId.isLocal()) {
        consentEntity.setClientId(clientId);
    } else {
        consentEntity.setClientStorageProvider(clientStorageId.getProviderId());
        consentEntity.setExternalClientId(clientStorageId.getExternalId());
    }
    consentEntity.setCreatedDate(currentTime);
    consentEntity.setLastUpdatedDate(currentTime);
    em.persist(consentEntity);
    em.flush();
    updateGrantedConsentEntity(consentEntity, consent);
}
Also used : ModelDuplicateException(org.keycloak.models.ModelDuplicateException) StorageId(org.keycloak.storage.StorageId) UserConsentEntity(org.keycloak.models.jpa.entities.UserConsentEntity) UserEntity(org.keycloak.models.jpa.entities.UserEntity)

Example 40 with StorageId

use of org.keycloak.storage.StorageId in project keycloak by keycloak.

the class MapFieldPredicates method getUserConsentClientFederationLink.

private static MapModelCriteriaBuilder<Object, MapUserEntity, UserModel> getUserConsentClientFederationLink(MapModelCriteriaBuilder<Object, MapUserEntity, UserModel> mcb, Operator op, Object[] values) {
    String providerId = ensureEqSingleValue(UserModel.SearchableFields.CONSENT_CLIENT_FEDERATION_LINK, "provider_id", op, values);
    String providerIdS = new StorageId((String) providerId, "").getId();
    Function<MapUserEntity, ?> getter;
    getter = ue -> Optional.ofNullable(ue.getUserConsents()).orElseGet(Collections::emptySet).stream().map(MapUserConsentEntity::getClientId).anyMatch(v -> v != null && v.startsWith(providerIdS));
    return mcb.fieldCompare(Boolean.TRUE::equals, getter);
}
Also used : MapScopeEntity(org.keycloak.models.map.authorization.entity.MapScopeEntity) Arrays(java.util.Arrays) MapClientScopeEntity(org.keycloak.models.map.clientscope.MapClientScopeEntity) MapRoleEntity(org.keycloak.models.map.role.MapRoleEntity) MapResourceServerEntity(org.keycloak.models.map.authorization.entity.MapResourceServerEntity) SearchableModelField(org.keycloak.storage.SearchableModelField) MapRealmEntity(org.keycloak.models.map.realm.MapRealmEntity) AuthenticatedClientSessionModel(org.keycloak.models.AuthenticatedClientSessionModel) Map(java.util.Map) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) IdentityHashMap(java.util.IdentityHashMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) RoleModel(org.keycloak.models.RoleModel) CriterionNotSupportedException(org.keycloak.models.map.storage.CriterionNotSupportedException) Objects(java.util.Objects) Operator(org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator) List(java.util.List) AbstractEntity(org.keycloak.models.map.common.AbstractEntity) Stream(java.util.stream.Stream) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) MapResourceEntity(org.keycloak.models.map.authorization.entity.MapResourceEntity) MapUserLoginFailureEntity(org.keycloak.models.map.loginFailure.MapUserLoginFailureEntity) Optional(java.util.Optional) MapRootAuthenticationSessionEntity(org.keycloak.models.map.authSession.MapRootAuthenticationSessionEntity) ClientModel(org.keycloak.models.ClientModel) Scope(org.keycloak.authorization.model.Scope) MapPermissionTicketEntity(org.keycloak.models.map.authorization.entity.MapPermissionTicketEntity) HashMap(java.util.HashMap) MapUserSessionEntity(org.keycloak.models.map.userSession.MapUserSessionEntity) Function(java.util.function.Function) StorageId(org.keycloak.storage.StorageId) PermissionTicket(org.keycloak.authorization.model.PermissionTicket) UserModel(org.keycloak.models.UserModel) QueryParameters(org.keycloak.models.map.storage.QueryParameters) GroupModel(org.keycloak.models.GroupModel) MapPolicyEntity(org.keycloak.models.map.authorization.entity.MapPolicyEntity) ResourceServer(org.keycloak.authorization.model.ResourceServer) UserLoginFailureModel(org.keycloak.models.UserLoginFailureModel) CORRESPONDING_SESSION_ID(org.keycloak.models.UserSessionModel.CORRESPONDING_SESSION_ID) UserSessionModel(org.keycloak.models.UserSessionModel) MapClientEntity(org.keycloak.models.map.client.MapClientEntity) MapUserEntity(org.keycloak.models.map.user.MapUserEntity) Policy(org.keycloak.authorization.model.Policy) UpdatePredicatesFunc(org.keycloak.models.map.storage.chm.MapModelCriteriaBuilder.UpdatePredicatesFunc) MapUserConsentEntity(org.keycloak.models.map.user.MapUserConsentEntity) MapAuthenticatedClientSessionEntity(org.keycloak.models.map.userSession.MapAuthenticatedClientSessionEntity) Comparator(java.util.Comparator) Collections(java.util.Collections) Resource(org.keycloak.authorization.model.Resource) MapGroupEntity(org.keycloak.models.map.group.MapGroupEntity) MapUserConsentEntity(org.keycloak.models.map.user.MapUserConsentEntity) Collections(java.util.Collections) StorageId(org.keycloak.storage.StorageId) MapUserEntity(org.keycloak.models.map.user.MapUserEntity)

Aggregations

StorageId (org.keycloak.storage.StorageId)44 UserModel (org.keycloak.models.UserModel)7 RealmModel (org.keycloak.models.RealmModel)6 ComponentModel (org.keycloak.component.ComponentModel)5 ClientModel (org.keycloak.models.ClientModel)5 ClientScopeModel (org.keycloak.models.ClientScopeModel)5 List (java.util.List)3 Objects (java.util.Objects)3 Stream (java.util.stream.Stream)3 Test (org.junit.Test)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 ModelException (org.keycloak.models.ModelException)3 UserConsentModel (org.keycloak.models.UserConsentModel)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2