Search in sources :

Example 1 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method onUserRemoved.

protected void onUserRemoved(RealmModel realm, UserModel user) {
    removeUserSessions(realm, user, true);
    removeUserSessions(realm, user, false);
    UserSessionPersisterProvider persisterProvider = session.getProvider(UserSessionPersisterProvider.class);
    if (persisterProvider != null) {
        persisterProvider.onUserRemoved(realm, user);
    }
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider)

Example 2 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method getUserSessionsStream.

protected Stream<UserSessionModel> getUserSessionsStream(RealmModel realm, UserSessionPredicate predicate, boolean offline) {
    if (offline && loadOfflineSessionsFromDatabase) {
        // fetch the offline user-sessions from the persistence provider
        UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
        if (predicate.getUserId() != null) {
            UserModel user = session.users().getUserById(realm, predicate.getUserId());
            if (user != null) {
                return persister.loadUserSessionsStream(realm, user, true, 0, null);
            }
        }
        if (predicate.getBrokerUserId() != null) {
            String[] idpAliasSessionId = predicate.getBrokerUserId().split("\\.");
            Map<String, String> attributes = new HashMap<>();
            attributes.put(UserModel.IDP_ALIAS, idpAliasSessionId[0]);
            attributes.put(UserModel.IDP_USER_ID, idpAliasSessionId[1]);
            UserProvider userProvider = session.getProvider(UserProvider.class);
            UserModel userModel = userProvider.searchForUserStream(realm, attributes, 0, null).findFirst().orElse(null);
            return userModel != null ? persister.loadUserSessionsStream(realm, userModel, true, 0, null) : Stream.empty();
        }
        if (predicate.getBrokerSessionId() != null) {
            // currently it is not possible to access the brokerSessionId in offline user-session in a database agnostic way
            throw new ModelException("Dynamic database lookup for offline user-sessions by broker session ID is currently only supported for preloaded sessions. " + "Set preloadOfflineSessionsFromDatabase option to \"true\" in " + UserSessionSpi.NAME + " SPI in " + InfinispanUserSessionProviderFactory.PROVIDER_ID + " provider to enable the lookup.");
        }
    }
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
    cache = CacheDecorators.skipCacheLoaders(cache);
    // and then mapped locally to avoid serialization issues when trying to manipulate the cache stream directly.
    return StreamSupport.stream(cache.entrySet().stream().filter(predicate).spliterator(), false).map(Mappers.userSessionEntity()).map(entity -> this.wrap(realm, entity, offline));
}
Also used : UserModel(org.keycloak.models.UserModel) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) ModelException(org.keycloak.models.ModelException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) UserProvider(org.keycloak.models.UserProvider) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 3 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method getUserSessionEntityFromPersistenceProvider.

private UserSessionEntity getUserSessionEntityFromPersistenceProvider(RealmModel realm, String sessionId, boolean offline) {
    log.debugf("Offline user-session not found in infinispan, attempting UserSessionPersisterProvider lookup for sessionId=%s", sessionId);
    UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
    UserSessionModel persistentUserSession = persister.loadUserSession(realm, sessionId, offline);
    if (persistentUserSession == null) {
        log.debugf("Offline user-session not found in UserSessionPersisterProvider for sessionId=%s", sessionId);
        return null;
    }
    return importUserSession(realm, offline, persistentUserSession);
}
Also used : OfflineUserSessionModel(org.keycloak.models.OfflineUserSessionModel) UserSessionModel(org.keycloak.models.UserSessionModel) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider)

Example 4 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method getActiveClientSessionStats.

@Override
public Map<String, Long> getActiveClientSessionStats(RealmModel realm, boolean offline) {
    if (offline && loadOfflineSessionsFromDatabase) {
        UserSessionPersisterProvider persister = session.getProvider(UserSessionPersisterProvider.class);
        return persister.getUserSessionsCountsByClients(realm, true);
    }
    Cache<String, SessionEntityWrapper<UserSessionEntity>> cache = getCache(offline);
    cache = CacheDecorators.skipCacheLoaders(cache);
    return cache.entrySet().stream().filter(UserSessionPredicate.create(realm.getId())).map(Mappers.authClientSessionSetMapper()).flatMap((Serializable & Function<Set<String>, Stream<? extends String>>) Mappers::toStream).collect(CacheCollectors.serializableCollector(() -> Collectors.groupingBy(Function.identity(), Collectors.counting())));
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) Set(java.util.Set) Mappers(org.keycloak.models.sessions.infinispan.stream.Mappers) Stream(java.util.stream.Stream) StreamsUtil.paginatedStream(org.keycloak.utils.StreamsUtil.paginatedStream) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Example 5 with UserSessionPersisterProvider

use of org.keycloak.models.session.UserSessionPersisterProvider in project keycloak by keycloak.

the class InfinispanUserSessionProvider method onRealmRemoved.

@Override
public void onRealmRemoved(RealmModel realm) {
    // Don't send message to all DCs, just to all cluster nodes in current DC. The remoteCache will notify client listeners for removed userSessions.
    clusterEventsSenderTx.addEvent(RealmRemovedSessionEvent.createEvent(RealmRemovedSessionEvent.class, InfinispanUserSessionProviderFactory.REALM_REMOVED_SESSION_EVENT, session, realm.getId(), true), ClusterProvider.DCNotify.LOCAL_DC_ONLY);
    UserSessionPersisterProvider sessionsPersister = session.getProvider(UserSessionPersisterProvider.class);
    if (sessionsPersister != null) {
        sessionsPersister.onRealmRemoved(realm);
    }
}
Also used : UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) RealmRemovedSessionEvent(org.keycloak.models.sessions.infinispan.events.RealmRemovedSessionEvent)

Aggregations

UserSessionPersisterProvider (org.keycloak.models.session.UserSessionPersisterProvider)20 UserSessionModel (org.keycloak.models.UserSessionModel)10 RealmModel (org.keycloak.models.RealmModel)7 Test (org.junit.Test)6 KeycloakModelTest (org.keycloak.testsuite.model.KeycloakModelTest)6 AuthenticatedClientSessionModel (org.keycloak.models.AuthenticatedClientSessionModel)4 KeycloakSession (org.keycloak.models.KeycloakSession)4 UserModel (org.keycloak.models.UserModel)4 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 SessionEntityWrapper (org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)3 ResetTimeOffsetEvent (org.keycloak.models.utils.ResetTimeOffsetEvent)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Time (org.keycloak.common.util.Time)2 ClientModel (org.keycloak.models.ClientModel)2