Search in sources :

Example 66 with ComponentRepresentation

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

the class LDAPSyncTest method test07SyncRestAPIWrongAction.

// KEYCLOAK-10770 user-storage/{id}/sync should return 400 instead of 404
@Test
public void test07SyncRestAPIWrongAction() {
    ComponentRepresentation ldapRep = testRealm().components().component(ldapModelId).toRepresentation();
    try {
        SynchronizationResultRepresentation syncResultRep = adminClient.realm("test").userStorage().syncUsers(ldapModelId, "wrong action");
        Assert.fail("Should throw 400");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof BadRequestException);
    }
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) SynchronizationResultRepresentation(org.keycloak.representations.idm.SynchronizationResultRepresentation) BadRequestException(javax.ws.rs.BadRequestException) BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.Test)

Example 67 with ComponentRepresentation

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

the class LDAPSyncTest method test06SyncRestAPIMissingAction.

// KEYCLOAK-10770 user-storage/{id}/sync should return 400 instead of 404
@Test
public void test06SyncRestAPIMissingAction() {
    ComponentRepresentation ldapRep = testRealm().components().component(ldapModelId).toRepresentation();
    try {
        SynchronizationResultRepresentation syncResultRep = adminClient.realm("test").userStorage().syncUsers(ldapModelId, null);
        Assert.fail("Should throw 400");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof BadRequestException);
    }
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) SynchronizationResultRepresentation(org.keycloak.representations.idm.SynchronizationResultRepresentation) BadRequestException(javax.ws.rs.BadRequestException) BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.Test)

Example 68 with ComponentRepresentation

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

the class UserStorageTest method testWeeklyEviction.

@Test
public void testWeeklyEviction() {
    ApiUtil.findUserByUsername(testRealmResource(), "thor");
    // set eviction to 4 days from now
    Calendar eviction = Calendar.getInstance();
    eviction.add(Calendar.HOUR, 4 * 24);
    ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
    propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.EVICT_WEEKLY.name());
    propProviderRW.getConfig().putSingle(EVICTION_DAY, Integer.toString(eviction.get(DAY_OF_WEEK)));
    propProviderRW.getConfig().putSingle(EVICTION_HOUR, Integer.toString(eviction.get(HOUR_OF_DAY)));
    propProviderRW.getConfig().putSingle(EVICTION_MINUTE, Integer.toString(eviction.get(MINUTE)));
    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);
    });
    // 2 days in future
    setTimeOffset(2 * 24 * 60 * 60);
    // 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);
    });
    // 5 days in future
    setTimeOffset(5 * 24 * 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) Calendar(java.util.Calendar) CachedUserModel(org.keycloak.models.cache.CachedUserModel) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test)

Example 69 with ComponentRepresentation

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

the class RealmManager method deactivateOtherRsaKeys.

private void deactivateOtherRsaKeys(String providerId) {
    List<String> otherRsaKeyProviderIds = realm.keys().getKeyMetadata().getKeys().stream().filter(key -> KeyType.RSA.equals(key.getType()) && !providerId.equals(key.getProviderId())).map(key -> key.getProviderId()).collect(Collectors.toList());
    for (String otherRsaKeyProviderId : otherRsaKeyProviderIds) {
        ComponentResource componentResource = realm.components().component(otherRsaKeyProviderId);
        ComponentRepresentation componentRepresentation = componentResource.toRepresentation();
        componentRepresentation.getConfig().putSingle(Attributes.ACTIVE_KEY, "false");
        componentResource.update(componentRepresentation);
    }
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator) X509Certificate(java.security.cert.X509Certificate) KeyPair(java.security.KeyPair) ComponentResource(org.keycloak.admin.client.resource.ComponentResource) Attributes(org.keycloak.keys.Attributes) ImportedRsaKeyProviderFactory(org.keycloak.keys.ImportedRsaKeyProviderFactory) RealmResource(org.keycloak.admin.client.resource.RealmResource) CertificateUtils(org.keycloak.common.util.CertificateUtils) PemUtils(org.keycloak.common.util.PemUtils) KeyType(org.keycloak.crypto.KeyType) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) Base64(org.keycloak.common.util.Base64) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) List(java.util.List) Certificate(java.security.cert.Certificate) Response(javax.ws.rs.core.Response) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyProvider(org.keycloak.keys.KeyProvider) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) ApiUtil(org.keycloak.testsuite.admin.ApiUtil) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ComponentResource(org.keycloak.admin.client.resource.ComponentResource)

Example 70 with ComponentRepresentation

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

the class AccountLinkTest method beforeBrokerTest.

@Before
public void beforeBrokerTest() {
    if (testContext.isInitialized()) {
        return;
    }
    // addIdpUser
    RealmResource realmParent = adminClient.realms().realm(PARENT_IDP);
    UserRepresentation user = new UserRepresentation();
    user.setUsername(PARENT_USERNAME);
    user.setEnabled(true);
    String userId = createUserAndResetPasswordWithAdminClient(realmParent, user, "password");
    // addChildUser
    RealmResource realmChild = adminClient.realms().realm(CHILD_IDP);
    user = new UserRepresentation();
    user.setUsername("child");
    user.setEnabled(true);
    userId = createUserAndResetPasswordWithAdminClient(realmChild, user, "password");
    // setupUserStorageProvider
    ComponentRepresentation provider = new ComponentRepresentation();
    provider.setName("passthrough");
    provider.setProviderId(PassThroughFederatedUserStorageProviderFactory.PROVIDER_ID);
    provider.setProviderType(UserStorageProvider.class.getName());
    provider.setConfig(new MultivaluedHashMap<>());
    provider.getConfig().putSingle("priority", Integer.toString(1));
    realmChild.components().add(provider);
    // createBroker
    createParentChild();
    testContext.setInitialized(true);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageProvider(org.keycloak.storage.UserStorageProvider) PassThroughFederatedUserStorageProvider(org.keycloak.testsuite.federation.PassThroughFederatedUserStorageProvider) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Before(org.junit.Before)

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