Search in sources :

Example 1 with StorageId

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

the class UserCacheSession method cacheUser.

protected UserModel cacheUser(RealmModel realm, UserModel delegate, Long revision) {
    int notBefore = getDelegate().getNotBeforeOfUser(realm, delegate);
    StorageId storageId = delegate.getFederationLink() != null ? new StorageId(delegate.getFederationLink(), delegate.getId()) : new StorageId(delegate.getId());
    CachedUser cached = null;
    UserAdapter adapter = null;
    if (!storageId.isLocal()) {
        ComponentModel component = realm.getComponent(storageId.getProviderId());
        UserStorageProviderModel model = new UserStorageProviderModel(component);
        if (!model.isEnabled()) {
            return new ReadOnlyUserModelDelegate(delegate) {

                @Override
                public boolean isEnabled() {
                    return false;
                }
            };
        }
        UserStorageProviderModel.CachePolicy policy = model.getCachePolicy();
        if (policy != null && policy == UserStorageProviderModel.CachePolicy.NO_CACHE) {
            return delegate;
        }
        cached = new CachedUser(revision, realm, delegate, notBefore);
        adapter = new UserAdapter(cached, this, session, realm);
        onCache(realm, adapter, delegate);
        long lifespan = model.getLifespan();
        if (lifespan > 0) {
            cache.addRevisioned(cached, startupRevision, lifespan);
        } else {
            cache.addRevisioned(cached, startupRevision);
        }
    } else {
        cached = new CachedUser(revision, realm, delegate, notBefore);
        adapter = new UserAdapter(cached, this, session, realm);
        onCache(realm, adapter, delegate);
        cache.addRevisioned(cached, startupRevision);
    }
    return adapter;
}
Also used : ReadOnlyUserModelDelegate(org.keycloak.models.utils.ReadOnlyUserModelDelegate) ComponentModel(org.keycloak.component.ComponentModel) CachedUser(org.keycloak.models.cache.infinispan.entities.CachedUser) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) StorageId(org.keycloak.storage.StorageId)

Example 2 with StorageId

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

the class UserCacheSession method validateCache.

protected UserModel validateCache(RealmModel realm, CachedUser cached) {
    if (!realm.getId().equals(cached.getRealm())) {
        return null;
    }
    StorageId storageId = cached.getFederationLink() != null ? new StorageId(cached.getFederationLink(), cached.getId()) : new StorageId(cached.getId());
    if (!storageId.isLocal()) {
        ComponentModel component = realm.getComponent(storageId.getProviderId());
        CacheableStorageProviderModel model = new CacheableStorageProviderModel(component);
        // its also hard to test stuff
        if (model.shouldInvalidate(cached)) {
            registerUserInvalidation(realm, cached);
            return getDelegate().getUserById(realm, cached.getId());
        }
    }
    return new UserAdapter(cached, this, session, realm);
}
Also used : CacheableStorageProviderModel(org.keycloak.storage.CacheableStorageProviderModel) ComponentModel(org.keycloak.component.ComponentModel) StorageId(org.keycloak.storage.StorageId)

Example 3 with StorageId

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

the class MapUserProvider method preRemove.

@Override
public void preRemove(RealmModel realm, ComponentModel component) {
    String componentId = component.getId();
    LOG.tracef("preRemove[ComponentModel](%s, %s)%s", realm, componentId, getShortStackTrace());
    if (component.getProviderType().equals(UserStorageProvider.class.getName())) {
        removeImportedUsers(realm, componentId);
    }
    if (component.getProviderType().equals(ClientStorageProvider.class.getName())) {
        DefaultModelCriteria<UserModel> mcb = criteria();
        mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()).compare(SearchableFields.CONSENT_CLIENT_FEDERATION_LINK, Operator.EQ, componentId);
        try (Stream<MapUserEntity> s = tx.read(withCriteria(mcb))) {
            String providerIdS = new StorageId(componentId, "").getId();
            s.forEach(removeConsentsForExternalClient(providerIdS));
        }
    }
}
Also used : UserModel(org.keycloak.models.UserModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) ClientStorageProvider(org.keycloak.storage.client.ClientStorageProvider) StorageId(org.keycloak.storage.StorageId)

Example 4 with StorageId

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

the class ApplicationsBean method getApplications.

private Stream<ClientModel> getApplications(KeycloakSession session, RealmModel realm, UserModel user) {
    Predicate<ClientModel> bearerOnly = ClientModel::isBearerOnly;
    Stream<ClientModel> clients = realm.getClientsStream().filter(bearerOnly.negate());
    Predicate<ClientModel> isLocal = client -> new StorageId(client.getId()).isLocal();
    return Stream.concat(clients, session.users().getConsentsStream(realm, user.getId()).map(UserConsentModel::getClient).filter(isLocal.negate())).distinct();
}
Also used : ClientModel(org.keycloak.models.ClientModel) AdminPermissions(org.keycloak.services.resources.admin.permissions.AdminPermissions) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmModel(org.keycloak.models.RealmModel) Predicate(java.util.function.Predicate) Constants(org.keycloak.models.Constants) KeycloakSession(org.keycloak.models.KeycloakSession) Set(java.util.Set) RoleModel(org.keycloak.models.RoleModel) TokenManager(org.keycloak.protocol.oidc.TokenManager) Collectors(java.util.stream.Collectors) StorageId(org.keycloak.storage.StorageId) ResolveRelative(org.keycloak.services.util.ResolveRelative) ArrayList(java.util.ArrayList) OrderedModel(org.keycloak.models.OrderedModel) Objects(java.util.Objects) List(java.util.List) UserModel(org.keycloak.models.UserModel) Stream(java.util.stream.Stream) UserSessionManager(org.keycloak.services.managers.UserSessionManager) UserConsentModel(org.keycloak.models.UserConsentModel) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) LinkedList(java.util.LinkedList) ClientModel(org.keycloak.models.ClientModel) StorageId(org.keycloak.storage.StorageId)

Example 5 with StorageId

use of org.keycloak.storage.StorageId 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)

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