Search in sources :

Example 76 with RealmModel

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

the class MigrationTest method migration2_xTest.

@Test
@Migration(versionFrom = "2.")
public void migration2_xTest() throws Exception {
    // the realm with special characters in its id was succesfully migrated (no error during migration)
    // removing it now as testMigratedData() expects specific clients and roles
    // we need to perform the removal via run on server to workaround escaping parameters when using rest call
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealm("test ' and ; and -- and \"");
        new RealmManager(session).removeRealm(realm);
    });
    testMigratedData();
    testMigrationTo3_x();
    testMigrationTo4_x();
    testMigrationTo5_x();
    testMigrationTo6_x();
    testMigrationTo7_x(true);
    testMigrationTo8_x();
    testMigrationTo9_x();
    testMigrationTo12_x(false);
    // Always test offline-token login during migration test
    testOfflineTokenLogin();
}
Also used : RealmModel(org.keycloak.models.RealmModel) RealmManager(org.keycloak.services.managers.RealmManager) Test(org.junit.Test) Migration(org.keycloak.testsuite.arquillian.migration.Migration)

Example 77 with RealmModel

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

the class LoginTimeoutValidationTest method before.

@Before
public void before() {
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        realm = session.realms().getRealm("test");
        session.users().addUser(realm, "user1");
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) Before(org.junit.Before)

Example 78 with RealmModel

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

the class LoginTimeoutValidationTest method testIsLoginTimeoutValid.

@Test
@ModelTest
public void testIsLoginTimeoutValid(KeycloakSession keycloakSession) {
    RealmModel realm = keycloakSession.realms().getRealmByName("test");
    UserSessionModel userSession = keycloakSession.sessions().createUserSession(realm, keycloakSession.users().getUserByUsername(realm, "user1"), "user1", "127.0.0.1", "form", true, null, null);
    ClientModel client = realm.getClientByClientId("account");
    AuthenticationSessionModel authSession = keycloakSession.authenticationSessions().createRootAuthenticationSession(realm).createAuthenticationSession(client);
    ClientSessionCode clientSessionCode = new ClientSessionCode(keycloakSession, realm, authSession);
    /*
         * KEYCLOAK-10636 Large Login timeout causes login failure
         * realm > Realm setting > Tokens > Login timeout
         */
    // Login timeout
    int accessCodeLifespanLoginOrig = realm.getAccessCodeLifespanLogin();
    realm.setAccessCodeLifespanLogin(Integer.MAX_VALUE);
    Assert.assertTrue("Login validataion with large Login Timeout failed", clientSessionCode.isActionActive(ClientSessionCode.ActionType.LOGIN));
    realm.setAccessCodeLifespanLogin(accessCodeLifespanLoginOrig);
    /*
         * KEYCLOAK-10637 Large Login Action timeout causes login failure
         * realm > Realm setting > Tokens > Login Action timeout
         */
    // Login Action timeout
    int accessCodeLifespanUserActionOrig = realm.getAccessCodeLifespanUserAction();
    realm.setAccessCodeLifespanUserAction(Integer.MAX_VALUE);
    Assert.assertTrue("Login validataion with large Login Action Timeout failed", clientSessionCode.isActionActive(ClientSessionCode.ActionType.USER));
    realm.setAccessCodeLifespanUserAction(accessCodeLifespanUserActionOrig);
}
Also used : RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) UserSessionModel(org.keycloak.models.UserSessionModel) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) ClientSessionCode(org.keycloak.services.managers.ClientSessionCode) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 79 with RealmModel

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

the class LoginTimeoutValidationTest method after.

@After
public void after() {
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        session.sessions().removeUserSessions(realm);
        UserModel user1 = session.users().getUserByUsername(realm, "user1");
        UserManager um = new UserManager(session);
        if (user1 != null) {
            um.removeUser(realm, user1);
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UserManager(org.keycloak.models.UserManager) After(org.junit.After)

Example 80 with RealmModel

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

the class RemoteCacheSessionListener method replaceRemoteEntityInCache.

protected void replaceRemoteEntityInCache(K key, long eventVersion) {
    // TODO can be optimized and remoteSession sent in the event itself?
    AtomicBoolean replaced = new AtomicBoolean(false);
    int replaceRetries = 0;
    int sleepInterval = 25;
    do {
        replaceRetries++;
        SessionEntityWrapper<V> localEntityWrapper = cache.get(key);
        VersionedValue<SessionEntityWrapper<V>> remoteSessionVersioned = remoteCache.getWithMetadata(key);
        // Probably already removed
        if (remoteSessionVersioned == null || remoteSessionVersioned.getValue() == null) {
            logger.debugf("Entity '%s' not present in remoteCache. Ignoring replace", key);
            return;
        }
        if (remoteSessionVersioned.getVersion() < eventVersion) {
            try {
                logger.debugf("Got replace remote entity event prematurely for entity '%s', will try again. Event version: %d, got: %d", key, eventVersion, remoteSessionVersioned == null ? -1 : remoteSessionVersioned.getVersion());
                // using exponential backoff
                Thread.sleep(new Random().nextInt(sleepInterval));
                continue;
            } catch (InterruptedException ex) {
                continue;
            } finally {
                sleepInterval = sleepInterval << 1;
            }
        }
        SessionEntity remoteSession = remoteSessionVersioned.getValue().getEntity();
        logger.debugf("Read session entity from the remote cache: %s . replaceRetries=%d", remoteSession, replaceRetries);
        SessionEntityWrapper<V> sessionWrapper = remoteSession.mergeRemoteEntityWithLocalEntity(localEntityWrapper);
        KeycloakModelUtils.runJobInTransaction(sessionFactory, (session -> {
            RealmModel realm = session.realms().getRealm(sessionWrapper.getEntity().getRealmId());
            long lifespanMs = lifespanMsLoader.apply(realm, sessionWrapper.getEntity());
            long maxIdleTimeMs = maxIdleTimeMsLoader.apply(realm, sessionWrapper.getEntity());
            // We received event from remoteCache, so we won't update it back
            replaced.set(cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES).replace(key, localEntityWrapper, sessionWrapper, lifespanMs, TimeUnit.MILLISECONDS, maxIdleTimeMs, TimeUnit.MILLISECONDS));
        }));
        if (!replaced.get()) {
            logger.debugf("Did not succeed in merging sessions, will try again: %s", remoteSession);
        }
    } while (replaceRetries < MAXIMUM_REPLACE_RETRIES && !replaced.get());
}
Also used : KeycloakModelUtils(org.keycloak.models.utils.KeycloakModelUtils) Logger(org.jboss.logging.Logger) BiFunction(java.util.function.BiFunction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientEvent(org.infinispan.client.hotrod.event.ClientEvent) Random(java.util.Random) Cache(org.infinispan.Cache) RemoteCache(org.infinispan.client.hotrod.RemoteCache) ClientCacheEntryCreated(org.infinispan.client.hotrod.annotation.ClientCacheEntryCreated) ExecutorsProvider(org.keycloak.executors.ExecutorsProvider) TopologyInfo(org.keycloak.connections.infinispan.TopologyInfo) SessionEntity(org.keycloak.models.sessions.infinispan.entities.SessionEntity) ClientCacheEntryCreatedEvent(org.infinispan.client.hotrod.event.ClientCacheEntryCreatedEvent) InfinispanUtil(org.keycloak.connections.infinispan.InfinispanUtil) ExecutorService(java.util.concurrent.ExecutorService) RealmModel(org.keycloak.models.RealmModel) ClientListener(org.infinispan.client.hotrod.annotation.ClientListener) KeycloakSession(org.keycloak.models.KeycloakSession) ClientCacheEntryRemoved(org.infinispan.client.hotrod.annotation.ClientCacheEntryRemoved) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper) TimeUnit(java.util.concurrent.TimeUnit) VersionedValue(org.infinispan.client.hotrod.VersionedValue) ClientCacheEntryModified(org.infinispan.client.hotrod.annotation.ClientCacheEntryModified) Flag(org.infinispan.context.Flag) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) ClientCacheEntryModifiedEvent(org.infinispan.client.hotrod.event.ClientCacheEntryModifiedEvent) ClientCacheEntryRemovedEvent(org.infinispan.client.hotrod.event.ClientCacheEntryRemovedEvent) RealmModel(org.keycloak.models.RealmModel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) SessionEntity(org.keycloak.models.sessions.infinispan.entities.SessionEntity) SessionEntityWrapper(org.keycloak.models.sessions.infinispan.changes.SessionEntityWrapper)

Aggregations

RealmModel (org.keycloak.models.RealmModel)591 Test (org.junit.Test)249 UserModel (org.keycloak.models.UserModel)225 KeycloakSession (org.keycloak.models.KeycloakSession)152 ClientModel (org.keycloak.models.ClientModel)149 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)90 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)84 ComponentModel (org.keycloak.component.ComponentModel)83 RoleModel (org.keycloak.models.RoleModel)73 UserSessionModel (org.keycloak.models.UserSessionModel)64 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)62 List (java.util.List)55 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)51 GroupModel (org.keycloak.models.GroupModel)47 HashMap (java.util.HashMap)38 Collectors (java.util.stream.Collectors)34 CachedUserModel (org.keycloak.models.cache.CachedUserModel)34 Path (javax.ws.rs.Path)30 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)30 Map (java.util.Map)29