Search in sources :

Example 36 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class UserStorageFailureTest method addProvidersBeforeTest.

@Before
public void addProvidersBeforeTest() {
    ComponentRepresentation memProvider = new ComponentRepresentation();
    memProvider.setName("failure");
    memProvider.setProviderId(FailableHardcodedStorageProviderFactory.PROVIDER_ID);
    memProvider.setProviderType(UserStorageProvider.class.getName());
    memProvider.setConfig(new MultivaluedHashMap<>());
    memProvider.getConfig().putSingle("priority", Integer.toString(0));
    failureProviderId = addComponent(memProvider);
    if (initialized)
        return;
    final String authServerRoot = OAuthClient.AUTH_SERVER_ROOT;
    testingClient.server().run(session -> {
        RealmManager manager = new RealmManager(session);
        RealmModel appRealm = manager.getRealmByName(AuthRealm.TEST);
        ClientModel offlineClient = appRealm.addClient("offline-client");
        offlineClient.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        offlineClient.setEnabled(true);
        offlineClient.setDirectAccessGrantsEnabled(true);
        offlineClient.setSecret("secret");
        HashSet<String> redirects = new HashSet<>();
        redirects.add(authServerRoot + "/offline-client");
        offlineClient.setRedirectUris(redirects);
        offlineClient.setServiceAccountsEnabled(true);
        offlineClient.setFullScopeAllowed(true);
        UserModel serviceAccount = manager.getSession().users().addUser(appRealm, ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + offlineClient.getClientId());
        serviceAccount.setEnabled(true);
        RoleModel role = appRealm.getRole("offline_access");
        Assert.assertNotNull(role);
        serviceAccount.grantRole(role);
        serviceAccount.setServiceAccountClientLink(offlineClient.getClientId());
        UserModel localUser = manager.getSession().userLocalStorage().addUser(appRealm, LOCAL_USER);
        localUser.setEnabled(true);
    });
    initialized = true;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) ClientModel(org.keycloak.models.ClientModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) RoleModel(org.keycloak.models.RoleModel) RealmManager(org.keycloak.services.managers.RealmManager) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 37 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class UserStorageTest method testMaxLifespan.

@Test
public void testMaxLifespan() {
    ApiUtil.findUserByUsername(testRealmResource(), "thor");
    // set eviction to 1 hour from now
    ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
    propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.MAX_LIFESPAN.name());
    // 1 hour in milliseconds
    propProviderRW.getConfig().putSingle(MAX_LIFESPAN, Long.toString(1 * 60 * 60 * 1000));
    testRealmResource().components().component(propProviderRWId).update(propProviderRW);
    // now
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        UserModel user = session.users().getUserByUsername(realm, "thor");
        System.out.println("User class: " + user.getClass());
        // should still be cached
        Assert.assertTrue(user instanceof CachedUserModel);
    });
    // 1/2 hour in future
    setTimeOffset(1 / 2 * 60 * 60);
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        UserModel user = session.users().getUserByUsername(realm, "thor");
        System.out.println("User class: " + user.getClass());
        // should still be cached
        Assert.assertTrue(user instanceof CachedUserModel);
    });
    // 2 hours in future
    setTimeOffset(2 * 60 * 60);
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        UserModel user = session.users().getUserByUsername(realm, "thor");
        System.out.println("User class: " + user.getClass());
        // should be evicted
        Assert.assertFalse(user instanceof CachedUserModel);
    });
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) RealmModel(org.keycloak.models.RealmModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 38 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class UserStorageTest method testNoCache.

@Test
public void testNoCache() {
    ApiUtil.findUserByUsername(testRealmResource(), "thor");
    // set NO_CACHE policy
    ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
    propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.NO_CACHE.name());
    testRealmResource().components().component(propProviderRWId).update(propProviderRW);
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        UserModel user = session.users().getUserByUsername(realm, "thor");
        System.out.println("User class: " + user.getClass());
        // should be evicted
        Assert.assertFalse(user instanceof CachedUserModel);
    });
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        UserModel thor2 = session.users().getUserByUsername(realm, "thor");
        Assert.assertFalse(thor2 instanceof CachedUserModel);
    });
    propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
    propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.DEFAULT.name());
    propProviderRW.getConfig().remove("evictionHour");
    propProviderRW.getConfig().remove("evictionMinute");
    propProviderRW.getConfig().remove("evictionDay");
    testRealmResource().components().component(propProviderRWId).update(propProviderRW);
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        UserModel thor = session.users().getUserByUsername(realm, "thor");
        System.out.println("Foo");
    });
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) RealmModel(org.keycloak.models.RealmModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 39 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class UserStorageTest method setDailyEvictionTime.

private void setDailyEvictionTime(int hour, int minutes) {
    if (hour < 0 || hour > 23) {
        throw new IllegalArgumentException("hour == " + hour);
    }
    if (minutes < 0 || minutes > 59) {
        throw new IllegalArgumentException("minutes == " + minutes);
    }
    ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
    propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.EVICT_DAILY.name());
    propProviderRW.getConfig().putSingle(EVICTION_HOUR, String.valueOf(hour));
    propProviderRW.getConfig().putSingle(EVICTION_MINUTE, String.valueOf(minutes));
    testRealmResource().components().component(propProviderRWId).update(propProviderRW);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation)

Example 40 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class UserStorageTest method newPropProviderRW.

protected ComponentRepresentation newPropProviderRW() {
    ComponentRepresentation propProviderRW = new ComponentRepresentation();
    propProviderRW.setName("user-props");
    propProviderRW.setProviderId(UserPropertyFileStorageFactory.PROVIDER_ID);
    propProviderRW.setProviderType(UserStorageProvider.class.getName());
    propProviderRW.setConfig(new MultivaluedHashMap<>());
    propProviderRW.getConfig().putSingle("priority", Integer.toString(2));
    propProviderRW.getConfig().putSingle("propertyFile", CONFIG_DIR.getAbsolutePath() + File.separator + "user-password.properties");
    propProviderRW.getConfig().putSingle("federatedStorage", "true");
    return propProviderRW;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageProvider(org.keycloak.storage.UserStorageProvider)

Aggregations

ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)120 Test (org.junit.Test)56 Response (javax.ws.rs.core.Response)44 UserStorageProvider (org.keycloak.storage.UserStorageProvider)22 KeyProvider (org.keycloak.keys.KeyProvider)17 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)13 Before (org.junit.Before)12 KeysMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation)12 RealmModel (org.keycloak.models.RealmModel)10 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)10 KeyPair (java.security.KeyPair)9 UserModel (org.keycloak.models.UserModel)9 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)8 RealmResource (org.keycloak.admin.client.resource.RealmResource)7 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)7 ComponentModel (org.keycloak.component.ComponentModel)7 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)7 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)6 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)6 List (java.util.List)5