org.keycloak.models.map.userSession

## Class MapUserSessionProvider

- java.lang.Object
- - org.keycloak.models.map.userSession.MapUserSessionProvider

- All Implemented Interfaces:
    UserSessionProvider, Provider

      public class MapUserSessionProvider
      extends Object
      implements UserSessionProvider

  Author:
    Martin Kanis

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected MapKeycloakTransaction<MapAuthenticatedClientSessionEntity,AuthenticatedClientSessionModel>` | `clientSessionTx`
    `protected MapKeycloakTransaction<MapUserSessionEntity,UserSessionModel>` | `userSessionTx`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MapUserSessionProvider(KeycloakSession session, MapStorage<MapUserSessionEntity,UserSessionModel> userSessionStore, MapStorage<MapAuthenticatedClientSessionEntity,AuthenticatedClientSessionModel> clientSessionStore)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `AuthenticatedClientSessionModel` | `createClientSession(RealmModel realm, ClientModel client, UserSessionModel userSession)`
    `AuthenticatedClientSessionModel` | `createOfflineClientSession(AuthenticatedClientSessionModel clientSession, UserSessionModel offlineUserSession)` Will automatically attach newly created offline client session to the offlineUserSession
    `UserSessionModel` | `createOfflineUserSession(UserSessionModel userSession)` Newly created userSession won't contain attached AuthenticatedClientSessions
    `UserSessionModel` | `createUserSession(RealmModel realm, UserModel user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, String brokerSessionId, String brokerUserId)`
    `UserSessionModel` | `createUserSession(String id, RealmModel realm, UserModel user, String loginUsername, String ipAddress, String authMethod, boolean rememberMe, String brokerSessionId, String brokerUserId, UserSessionModel.SessionPersistenceState persistenceState)`
    `Map<String,Long>` | `getActiveClientSessionStats(RealmModel realm, boolean offline)` Returns a summary of client sessions key is client.getId()
    `long` | `getActiveUserSessions(RealmModel realm, ClientModel client)`
    `AuthenticatedClientSessionModel` | `getClientSession(UserSessionModel userSession, ClientModel client, String clientSessionId, boolean offline)`
    `KeycloakSession` | `getKeycloakSession()` Returns currently used Keycloak session.
    `long` | `getOfflineSessionsCount(RealmModel realm, ClientModel client)`
    `UserSessionModel` | `getOfflineUserSession(RealmModel realm, String userSessionId)`
    `UserSessionModel` | `getOfflineUserSessionByBrokerSessionId(RealmModel realm, String brokerSessionId)`
    `Stream<UserSessionModel>` | `getOfflineUserSessionByBrokerUserIdStream(RealmModel realm, String brokerUserId)` Obtains the offline user sessions associated with the user that matches the specified `brokerUserId`.
    `Stream<UserSessionModel>` | `getOfflineUserSessionsStream(RealmModel realm, ClientModel client, Integer firstResult, Integer maxResults)` Obtains the offline user sessions associated with the specified client, starting from the `firstResult` and containing at most `maxResults`.
    `Stream<UserSessionModel>` | `getOfflineUserSessionsStream(RealmModel realm, UserModel user)` Obtains the offline user sessions associated with the specified user.
    `UserSessionModel` | `getUserSession(RealmModel realm, String id)`
    `UserSessionModel` | `getUserSessionByBrokerSessionId(RealmModel realm, String brokerSessionId)`
    `Stream<UserSessionModel>` | `getUserSessionByBrokerUserIdStream(RealmModel realm, String brokerUserId)` Obtains the online user sessions associated with the user that matches the specified `brokerUserId`.
    `Stream<UserSessionModel>` | `getUserSessionsStream(RealmModel realm, ClientModel client)` Obtains the online user sessions associated with the specified client.
    `Stream<UserSessionModel>` | `getUserSessionsStream(RealmModel realm, ClientModel client, Integer firstResult, Integer maxResults)` Obtains the online user sessions associated with the specified client, starting from the `firstResult` and containing at most `maxResults`.
    `Stream<UserSessionModel>` | `getUserSessionsStream(RealmModel realm, UserModel user)` Obtains the online user sessions associated with the specified user.
    `UserSessionModel` | `getUserSessionWithPredicate(RealmModel realm, String id, boolean offline, Predicate<UserSessionModel> predicate)` Return userSession of specified ID as long as the predicate passes.
    `void` | `importUserSessions(Collection<UserSessionModel> persistentUserSessions, boolean offline)` Triggered by persister during pre-load.
    `void` | `onClientRemoved(RealmModel realm, ClientModel client)`
    `void` | `onRealmRemoved(RealmModel realm)`
    `void` | `removeAllExpired()` Remove expired user sessions and client sessions in all the realms
    `void` | `removeExpired(RealmModel realm)` Removes expired user sessions owned by this realm from this provider.
    `void` | `removeOfflineUserSession(RealmModel realm, UserSessionModel userSession)` Removes the attached clientSessions as well
    `void` | `removeUserSession(RealmModel realm, UserSessionModel session)` This will remove attached ClientLoginSessionModels too
    `void` | `removeUserSessions(RealmModel realm)`
    `void` | `removeUserSessions(RealmModel realm, UserModel user)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.keycloak.models.UserSessionProvider

      `addUserLoginFailure, getClientSession, getOfflineUserSessionByBrokerUserId, getOfflineUserSessions, getOfflineUserSessions, getUserLoginFailure, getUserSessionByBrokerUserId, getUserSessions, getUserSessions, getUserSessions, removeAllUserLoginFailures, removeUserLoginFailure`

- - ### Field Detail

    - #### userSessionTx

          protected final MapKeycloakTransaction<MapUserSessionEntity,UserSessionModel> userSessionTx

    - #### clientSessionTx

          protected final MapKeycloakTransaction<MapAuthenticatedClientSessionEntity,AuthenticatedClientSessionModel> clientSessionTx

  - ### Constructor Detail

    - #### MapUserSessionProvider

          public MapUserSessionProvider(KeycloakSession session,
                                        MapStorage<MapUserSessionEntity,UserSessionModel> userSessionStore,
                                        MapStorage<MapAuthenticatedClientSessionEntity,AuthenticatedClientSessionModel> clientSessionStore)

  - ### Method Detail

    - #### getKeycloakSession

          public KeycloakSession getKeycloakSession()

      Description copied from interface: `UserSessionProvider`
      Returns currently used Keycloak session.
      Specified by:
        `getKeycloakSession` in interface `UserSessionProvider`
      Returns:
        `KeycloakSession`

    - #### createClientSession

          public AuthenticatedClientSessionModel createClientSession(RealmModel realm,
                                                                     ClientModel client,
                                                                     UserSessionModel userSession)

      Specified by:
        `createClientSession` in interface `UserSessionProvider`

    - #### getClientSession

          public AuthenticatedClientSessionModel getClientSession(UserSessionModel userSession,
                                                                  ClientModel client,
                                                                  String clientSessionId,
                                                                  boolean offline)

      Specified by:
        `getClientSession` in interface `UserSessionProvider`

    - #### createUserSession

          public UserSessionModel createUserSession(RealmModel realm,
                                                    UserModel user,
                                                    String loginUsername,
                                                    String ipAddress,
                                                    String authMethod,
                                                    boolean rememberMe,
                                                    String brokerSessionId,
                                                    String brokerUserId)

      Specified by:
        `createUserSession` in interface `UserSessionProvider`

    - #### createUserSession

          public UserSessionModel createUserSession(String id,
                                                    RealmModel realm,
                                                    UserModel user,
                                                    String loginUsername,
                                                    String ipAddress,
                                                    String authMethod,
                                                    boolean rememberMe,
                                                    String brokerSessionId,
                                                    String brokerUserId,
                                                    UserSessionModel.SessionPersistenceState persistenceState)

      Specified by:
        `createUserSession` in interface `UserSessionProvider`

    - #### getUserSession

          public UserSessionModel getUserSession(RealmModel realm,
                                                 String id)

      Specified by:
        `getUserSession` in interface `UserSessionProvider`

    - #### getUserSessionsStream

          public Stream<UserSessionModel> getUserSessionsStream(RealmModel realm,
                                                                UserModel user)

      Description copied from interface: `UserSessionProvider`
      Obtains the online user sessions associated with the specified user.
      Specified by:
        `getUserSessionsStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference to the realm.
        `user` - the user whose sessions are being searched.
      Returns:
        a non-null `Stream` of online user sessions.

    - #### getUserSessionsStream

          public Stream<UserSessionModel> getUserSessionsStream(RealmModel realm,
                                                                ClientModel client)

      Description copied from interface: `UserSessionProvider`
      Obtains the online user sessions associated with the specified client.
      Specified by:
        `getUserSessionsStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference to the realm.
        `client` - the client whose user sessions are being searched.
      Returns:
        a non-null `Stream` of online user sessions.

    - #### getUserSessionsStream

          public Stream<UserSessionModel> getUserSessionsStream(RealmModel realm,
                                                                ClientModel client,
                                                                Integer firstResult,
                                                                Integer maxResults)

      Description copied from interface: `UserSessionProvider`
      Obtains the online user sessions associated with the specified client, starting from the `firstResult` and containing at most `maxResults`.
      Specified by:
        `getUserSessionsStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference tot he realm.
        `client` - the client whose user sessions are being searched.
        `firstResult` - first result to return. Ignored if negative or `null`.
        `maxResults` - maximum number of results to return. Ignored if negative or `null`.
      Returns:
        a non-null `Stream` of online user sessions.

    - #### getUserSessionByBrokerUserIdStream

          public Stream<UserSessionModel> getUserSessionByBrokerUserIdStream(RealmModel realm,
                                                                             String brokerUserId)

      Description copied from interface: `UserSessionProvider`
      Obtains the online user sessions associated with the user that matches the specified `brokerUserId`.
      Specified by:
        `getUserSessionByBrokerUserIdStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference to the realm.
        `brokerUserId` - the id of the broker user whose sessions are being searched.
      Returns:
        a non-null `Stream` of online user sessions.

    - #### getUserSessionByBrokerSessionId

          public UserSessionModel getUserSessionByBrokerSessionId(RealmModel realm,
                                                                  String brokerSessionId)

      Specified by:
        `getUserSessionByBrokerSessionId` in interface `UserSessionProvider`

    - #### getUserSessionWithPredicate

          public UserSessionModel getUserSessionWithPredicate(RealmModel realm,
                                                              String id,
                                                              boolean offline,
                                                              Predicate<UserSessionModel> predicate)

      Description copied from interface: `UserSessionProvider`
      Return userSession of specified ID as long as the predicate passes. Otherwise returns `null`. If predicate doesn't pass, implementation can do some best-effort actions to try have predicate passing (eg. download userSession from other DC)
      Specified by:
        `getUserSessionWithPredicate` in interface `UserSessionProvider`

    - #### getActiveUserSessions

          public long getActiveUserSessions(RealmModel realm,
                                            ClientModel client)

      Specified by:
        `getActiveUserSessions` in interface `UserSessionProvider`

    - #### getActiveClientSessionStats

          public Map<String,Long> getActiveClientSessionStats(RealmModel realm,
                                                              boolean offline)

      Description copied from interface: `UserSessionProvider`
      Returns a summary of client sessions key is client.getId()
      Specified by:
        `getActiveClientSessionStats` in interface `UserSessionProvider`
      Returns:

    - #### removeUserSession

          public void removeUserSession(RealmModel realm,
                                        UserSessionModel session)

      Description copied from interface: `UserSessionProvider`
      This will remove attached ClientLoginSessionModels too
      Specified by:
        `removeUserSession` in interface `UserSessionProvider`

    - #### removeUserSessions

          public void removeUserSessions(RealmModel realm,
                                         UserModel user)

      Specified by:
        `removeUserSessions` in interface `UserSessionProvider`

    - #### removeAllExpired

          public void removeAllExpired()

      Description copied from interface: `UserSessionProvider`
      Remove expired user sessions and client sessions in all the realms
      Specified by:
        `removeAllExpired` in interface `UserSessionProvider`

    - #### removeExpired

          public void removeExpired(RealmModel realm)

      Description copied from interface: `UserSessionProvider`
      Removes expired user sessions owned by this realm from this provider. If this `UserSessionProvider` uses `UserSessionPersister`, the removal of the expired `user sessions` is also propagated to relevant `UserSessionPersister`.
      Specified by:
        `removeExpired` in interface `UserSessionProvider`
      Parameters:
        `realm` - `RealmModel` Realm where all the expired user sessions to be removed from.

    - #### removeUserSessions

          public void removeUserSessions(RealmModel realm)

      Specified by:
        `removeUserSessions` in interface `UserSessionProvider`

    - #### onRealmRemoved

          public void onRealmRemoved(RealmModel realm)

      Specified by:
        `onRealmRemoved` in interface `UserSessionProvider`

    - #### onClientRemoved

          public void onClientRemoved(RealmModel realm,
                                      ClientModel client)

      Specified by:
        `onClientRemoved` in interface `UserSessionProvider`

    - #### createOfflineUserSession

          public UserSessionModel createOfflineUserSession(UserSessionModel userSession)

      Description copied from interface: `UserSessionProvider`
      Newly created userSession won't contain attached AuthenticatedClientSessions
      Specified by:
        `createOfflineUserSession` in interface `UserSessionProvider`

    - #### getOfflineUserSession

          public UserSessionModel getOfflineUserSession(RealmModel realm,
                                                        String userSessionId)

      Specified by:
        `getOfflineUserSession` in interface `UserSessionProvider`

    - #### removeOfflineUserSession

          public void removeOfflineUserSession(RealmModel realm,
                                               UserSessionModel userSession)

      Description copied from interface: `UserSessionProvider`
      Removes the attached clientSessions as well
      Specified by:
        `removeOfflineUserSession` in interface `UserSessionProvider`

    - #### createOfflineClientSession

          public AuthenticatedClientSessionModel createOfflineClientSession(AuthenticatedClientSessionModel clientSession,
                                                                            UserSessionModel offlineUserSession)

      Description copied from interface: `UserSessionProvider`
      Will automatically attach newly created offline client session to the offlineUserSession
      Specified by:
        `createOfflineClientSession` in interface `UserSessionProvider`

    - #### getOfflineUserSessionsStream

          public Stream<UserSessionModel> getOfflineUserSessionsStream(RealmModel realm,
                                                                       UserModel user)

      Description copied from interface: `UserSessionProvider`
      Obtains the offline user sessions associated with the specified user.
      Specified by:
        `getOfflineUserSessionsStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference to the realm.
        `user` - the user whose offline sessions are being searched.
      Returns:
        a non-null `Stream` of offline user sessions.

    - #### getOfflineUserSessionByBrokerSessionId

          public UserSessionModel getOfflineUserSessionByBrokerSessionId(RealmModel realm,
                                                                         String brokerSessionId)

      Specified by:
        `getOfflineUserSessionByBrokerSessionId` in interface `UserSessionProvider`

    - #### getOfflineUserSessionByBrokerUserIdStream

          public Stream<UserSessionModel> getOfflineUserSessionByBrokerUserIdStream(RealmModel realm,
                                                                                    String brokerUserId)

      Description copied from interface: `UserSessionProvider`
      Obtains the offline user sessions associated with the user that matches the specified `brokerUserId`.
      Specified by:
        `getOfflineUserSessionByBrokerUserIdStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference to the realm.
        `brokerUserId` - the id of the broker user whose sessions are being searched.
      Returns:
        a non-null `Stream` of offline user sessions.

    - #### getOfflineSessionsCount

          public long getOfflineSessionsCount(RealmModel realm,
                                              ClientModel client)

      Specified by:
        `getOfflineSessionsCount` in interface `UserSessionProvider`

    - #### getOfflineUserSessionsStream

          public Stream<UserSessionModel> getOfflineUserSessionsStream(RealmModel realm,
                                                                       ClientModel client,
                                                                       Integer firstResult,
                                                                       Integer maxResults)

      Description copied from interface: `UserSessionProvider`
      Obtains the offline user sessions associated with the specified client, starting from the `firstResult` and containing at most `maxResults`.
      Specified by:
        `getOfflineUserSessionsStream` in interface `UserSessionProvider`
      Parameters:
        `realm` - a reference tot he realm.
        `client` - the client whose user sessions are being searched.
        `firstResult` - first result to return. Ignored if negative or `null`.
        `maxResults` - maximum number of results to return. Ignored if negative or `null`.
      Returns:
        a non-null `Stream` of offline user sessions.

    - #### importUserSessions

          public void importUserSessions(Collection<UserSessionModel> persistentUserSessions,
                                         boolean offline)

      Description copied from interface: `UserSessionProvider`
      Triggered by persister during pre-load. It imports authenticatedClientSessions too
      Specified by:
        `importUserSessions` in interface `UserSessionProvider`

    - #### close

          public void close()

      Specified by:
        `close` in interface `UserSessionProvider`
      Specified by:
        `close` in interface `Provider`

Copyright © 2021 JBoss by Red Hat. All rights reserved.