Search in sources :

Example 26 with KeycloakSession

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

the class PersisterLastSessionRefreshStore method sendMessage.

protected void sendMessage(KeycloakSession kcSession, Map<String, SessionData> refreshesToSend) {
    Map<String, Set<String>> sessionIdsByRealm = refreshesToSend.entrySet().stream().collect(Collectors.groupingBy(entry -> entry.getValue().getRealmId(), Collectors.mapping(Map.Entry::getKey, Collectors.toSet())));
    // Update DB with a bit lower value than current time to ensure 'revokeRefreshToken' will work correctly taking server
    int lastSessionRefresh = Time.currentTime() - SessionTimeoutHelper.PERIODIC_TASK_INTERVAL_SECONDS;
    if (logger.isDebugEnabled()) {
        logger.debugf("Updating %d userSessions with lastSessionRefresh: %d", refreshesToSend.size(), lastSessionRefresh);
    }
    UserSessionPersisterProvider persister = kcSession.getProvider(UserSessionPersisterProvider.class);
    for (Map.Entry<String, Set<String>> entry : sessionIdsByRealm.entrySet()) {
        RealmModel realm = kcSession.realms().getRealm(entry.getKey());
        // Case when realm was deleted in the meantime. UserSessions were already deleted as well (callback for realm deletion)
        if (realm == null) {
            continue;
        }
        Set<String> userSessionIds = entry.getValue();
        persister.updateLastSessionRefreshes(realm, lastSessionRefresh, userSessionIds, offline);
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) Map(java.util.Map) Logger(org.jboss.logging.Logger) SessionTimeoutHelper(org.keycloak.models.utils.SessionTimeoutHelper) Set(java.util.Set) KeycloakSession(org.keycloak.models.KeycloakSession) Collectors(java.util.stream.Collectors) Time(org.keycloak.common.util.Time) RealmModel(org.keycloak.models.RealmModel) Set(java.util.Set) UserSessionPersisterProvider(org.keycloak.models.session.UserSessionPersisterProvider) Map(java.util.Map)

Example 27 with KeycloakSession

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

the class InfinispanCacheInitializer method startLoading.

// Just coordinator will run this
@Override
protected void startLoading() {
    InitializerState state = getStateFromCache();
    SessionLoader.LoaderContext[] ctx = new SessionLoader.LoaderContext[1];
    if (state == null) {
        // Rather use separate transactions for update and counting
        KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

            @Override
            public void run(KeycloakSession session) {
                sessionLoader.init(session);
            }
        });
        KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

            @Override
            public void run(KeycloakSession session) {
                ctx[0] = sessionLoader.computeLoaderContext(session);
            }
        });
        state = new InitializerState(ctx[0].getSegmentsCount());
    } else {
        KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

            @Override
            public void run(KeycloakSession session) {
                ctx[0] = sessionLoader.computeLoaderContext(session);
            }
        });
    }
    log.debugf("Start loading with loader: '%s', ctx: '%s' , state: %s", sessionLoader.toString(), ctx[0].toString(), state.toString());
    startLoadingImpl(state, ctx[0]);
}
Also used : KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 28 with KeycloakSession

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

the class AbstractAuthSessionClusterListener method eventReceived.

@Override
public void eventReceived(ClusterEvent event) {
    KeycloakModelUtils.runJobInTransaction(sessionFactory, (KeycloakSession session) -> {
        InfinispanAuthenticationSessionProvider provider = (InfinispanAuthenticationSessionProvider) session.getProvider(AuthenticationSessionProvider.class, InfinispanAuthenticationSessionProviderFactory.PROVIDER_ID);
        SE sessionEvent = (SE) event;
        if (!provider.getCache().getStatus().allowInvocations()) {
            log.debugf("Cache in state '%s' doesn't allow invocations", provider.getCache().getStatus());
            return;
        }
        log.debugf("Received authentication session event '%s'", sessionEvent.toString());
        eventReceived(session, provider, sessionEvent);
    });
}
Also used : KeycloakSession(org.keycloak.models.KeycloakSession) InfinispanAuthenticationSessionProvider(org.keycloak.models.sessions.infinispan.InfinispanAuthenticationSessionProvider) AuthenticationSessionProvider(org.keycloak.sessions.AuthenticationSessionProvider) InfinispanAuthenticationSessionProvider(org.keycloak.models.sessions.infinispan.InfinispanAuthenticationSessionProvider)

Example 29 with KeycloakSession

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

the class SessionInitializerWorker method apply.

@Override
public SessionLoader.WorkerResult apply(EmbeddedCacheManager embeddedCacheManager) {
    Cache<Object, Object> workCache = embeddedCacheManager.getCache(cacheName);
    if (log.isTraceEnabled()) {
        log.tracef("Running computation for segment %s with worker %s", workerCtx.getSegment(), workerCtx.getWorkerId());
    }
    KeycloakSessionFactory sessionFactory = workCache.getAdvancedCache().getComponentRegistry().getComponent(KeycloakSessionFactory.class);
    if (sessionFactory == null) {
        log.debugf("KeycloakSessionFactory not yet set in cache. Worker skipped");
        return sessionLoader.createFailedWorkerResult(loaderCtx, workerCtx);
    }
    SessionLoader.WorkerResult[] ref = new SessionLoader.WorkerResult[1];
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            ref[0] = sessionLoader.loadSessions(session, loaderCtx, workerCtx);
        }
    });
    return ref[0];
}
Also used : KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory)

Example 30 with KeycloakSession

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

the class DefaultHostnameProvider method getRealmFrontEndUrl.

protected URI getRealmFrontEndUrl() {
    KeycloakSession session = Resteasy.getContextData(KeycloakSession.class);
    URI realmUrl = (URI) session.getAttribute(REALM_URI_SESSION_ATTRIBUTE);
    if (realmUrl == null) {
        RealmModel realm = session.getContext().getRealm();
        if (realm != null) {
            String frontendUrl = realm.getAttribute("frontendUrl");
            if (isNotBlank(frontendUrl)) {
                realmUrl = URI.create(frontendUrl);
                session.setAttribute(DefaultHostnameProvider.REALM_URI_SESSION_ATTRIBUTE, realmUrl);
                return realmUrl;
            }
        }
    }
    return realmUrl;
}
Also used : RealmModel(org.keycloak.models.RealmModel) KeycloakSession(org.keycloak.models.KeycloakSession) URI(java.net.URI)

Aggregations

KeycloakSession (org.keycloak.models.KeycloakSession)189 RealmModel (org.keycloak.models.RealmModel)136 UserModel (org.keycloak.models.UserModel)78 Test (org.junit.Test)76 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)61 ClientModel (org.keycloak.models.ClientModel)58 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)53 List (java.util.List)34 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 Collectors (java.util.stream.Collectors)21 IOException (java.io.IOException)20 Map (java.util.Map)19 UserSessionModel (org.keycloak.models.UserSessionModel)19 ArrayList (java.util.ArrayList)18 ClientScopeModel (org.keycloak.models.ClientScopeModel)18 RoleModel (org.keycloak.models.RoleModel)18 Set (java.util.Set)16 RealmManager (org.keycloak.services.managers.RealmManager)16 HashMap (java.util.HashMap)14 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)14