Search in sources :

Example 96 with ComponentRepresentation

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

the class LDAPBinaryAttributesTest method after.

@After
public void after() {
    ComponentRepresentation jpegMapper = adminClient.realm("test").components().query(ldapModelId, LDAPStorageMapper.class.getName(), "jpeg-mapper").get(0);
    adminClient.realm("test").components().component(jpegMapper.getId()).remove();
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) After(org.junit.After)

Example 97 with ComponentRepresentation

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

the class LDAPGroupMapperSyncTest method test05SyncRestAPI.

@Test
public void test05SyncRestAPI() {
    ComponentRepresentation groupMapperRep = findMapperRepByName("groupsMapper");
    try {
        // testing KEYCLOAK-3980 which threw an NPE because I was looking up the factory wrong.
        SynchronizationResultRepresentation syncResultRep = adminClient.realm("test").userStorage().syncMapperData(ldapModelId, groupMapperRep.getId(), "error");
        Assert.fail("Should throw 400");
    } catch (BadRequestException e) {
    }
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) SynchronizationResultRepresentation(org.keycloak.representations.idm.SynchronizationResultRepresentation) BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.Test)

Example 98 with ComponentRepresentation

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

the class ExportImportTest method testRealmExportImport.

private void testRealmExportImport() throws LifecycleException {
    testingClient.testing().exportImport().setAction(ExportImportConfig.ACTION_EXPORT);
    testingClient.testing().exportImport().setRealmName("test");
    testingClient.testing().exportImport().runExport();
    List<ComponentRepresentation> components = adminClient.realm("test").components().query();
    KeysMetadataRepresentation keyMetadata = adminClient.realm("test").keys().getKeyMetadata();
    String sampleRealmRoleId = adminClient.realm("test").roles().get("sample-realm-role").toRepresentation().getId();
    Map<String, List<String>> roleAttributes = adminClient.realm("test").roles().get("attribute-role").toRepresentation().getAttributes();
    String testAppId = adminClient.realm("test").clients().findByClientId("test-app").get(0).getId();
    String sampleClientRoleId = adminClient.realm("test").clients().get(testAppId).roles().get("sample-client-role").toRepresentation().getId();
    String sampleClientRoleAttribute = adminClient.realm("test").clients().get(testAppId).roles().get("sample-client-role").toRepresentation().getAttributes().get("sample-client-role-attribute").get(0);
    // Delete some realm (and some data in admin realm)
    adminClient.realm("test").remove();
    Assert.assertNames(adminClient.realms().findAll(), "test-realm", "master");
    assertNotAuthenticated("test", "test-user@localhost", "password");
    assertNotAuthenticated("test", "user1", "password");
    assertNotAuthenticated("test", "user2", "password");
    assertNotAuthenticated("test", "user3", "password");
    assertNotAuthenticated("test", "user-requiredOTP", "password");
    assertNotAuthenticated("test", "user-requiredWebAuthn", "password");
    // Configure import
    testingClient.testing().exportImport().setAction(ExportImportConfig.ACTION_IMPORT);
    testingClient.testing().exportImport().runImport();
    // Ensure data are imported back, but just for "test" realm
    Assert.assertNames(adminClient.realms().findAll(), "master", "test", "test-realm");
    assertAuthenticated("test", "test-user@localhost", "password");
    assertAuthenticated("test", "user1", "password");
    assertAuthenticated("test", "user2", "password");
    assertAuthenticated("test", "user3", "password");
    assertAuthenticated("test", "user-requiredOTP", "password");
    assertAuthenticated("test", "user-requiredWebAuthn", "password");
    RealmResource testRealmRealm = adminClient.realm("test");
    assertTrue(testRealmRealm.users().search("user-requiredOTP").get(0).getRequiredActions().get(0).equals(UserModel.RequiredAction.CONFIGURE_TOTP.name()));
    assertTrue(testRealmRealm.users().search("user-requiredWebAuthn").get(0).getRequiredActions().get(0).equals(WebAuthnRegisterFactory.PROVIDER_ID));
    List<ComponentRepresentation> componentsImported = adminClient.realm("test").components().query();
    assertComponents(components, componentsImported);
    KeysMetadataRepresentation keyMetadataImported = adminClient.realm("test").keys().getKeyMetadata();
    assertEquals(keyMetadata.getActive(), keyMetadataImported.getActive());
    String importedSampleRealmRoleId = adminClient.realm("test").roles().get("sample-realm-role").toRepresentation().getId();
    assertEquals(sampleRealmRoleId, importedSampleRealmRoleId);
    Map<String, List<String>> importedRoleAttributes = adminClient.realm("test").roles().get("attribute-role").toRepresentation().getAttributes();
    Assert.assertRoleAttributes(roleAttributes, importedRoleAttributes);
    String importedSampleClientRoleId = adminClient.realm("test").clients().get(testAppId).roles().get("sample-client-role").toRepresentation().getId();
    assertEquals(sampleClientRoleId, importedSampleClientRoleId);
    String importedSampleClientRoleAttribute = adminClient.realm("test").clients().get(testAppId).roles().get("sample-client-role").toRepresentation().getAttributes().get("sample-client-role-attribute").get(0);
    assertEquals(sampleClientRoleAttribute, importedSampleClientRoleAttribute);
    checkEventsConfig(adminClient.realm("test").getRealmEventsConfig());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) List(java.util.List)

Example 99 with ComponentRepresentation

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

the class ExportImportTest method assertComponents.

private void assertComponents(List<ComponentRepresentation> expected, List<ComponentRepresentation> actual) {
    expected.sort((o1, o2) -> o1.getId().compareTo(o2.getId()));
    actual.sort((o1, o2) -> o1.getId().compareTo(o2.getId()));
    assertEquals(expected.size(), actual.size());
    for (int i = 0; i < expected.size(); i++) {
        ComponentRepresentation e = expected.get(i);
        ComponentRepresentation a = actual.get(i);
        assertEquals(e.getId(), a.getId());
        assertEquals(e.getName(), a.getName());
        assertEquals(e.getProviderId(), a.getProviderId());
        assertEquals(e.getProviderType(), a.getProviderType());
        assertEquals(e.getParentId(), a.getParentId());
        assertEquals(e.getSubType(), a.getSubType());
        Assert.assertNames(e.getConfig().keySet(), a.getConfig().keySet().toArray(new String[] {}));
        // Compare config values without take order into account
        for (Map.Entry<String, List<String>> entry : e.getConfig().entrySet()) {
            List<String> eList = entry.getValue();
            List<String> aList = a.getConfig().getList(entry.getKey());
            Assert.assertNames(eList, aList.toArray(new String[] {}));
        }
    }
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 100 with ComponentRepresentation

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

the class LDAPNoMSADTest method testUpdateWithUnmappedRdnAttribute.

// KEYCLOAK-4364
@Test
public void testUpdateWithUnmappedRdnAttribute() {
    ComponentRepresentation snMapperRep = findMapperRepByName("last name");
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        ComponentModel snMapper = null;
        // Create LDAP user with "sn" attribute in RDN like "sn=Doe2,ou=People,dc=domain,dc=com"
        LDAPStorageProvider ldapProvider = LDAPTestUtils.getLdapProvider(session, ctx.getLdapModel());
        LDAPObject john2 = LDAPTestUtils.addLDAPUser(ldapProvider, appRealm, "johnkeycloak2", "John2", "Doe2", "john2@email.org", null, "4321");
        john2.setRdnAttributeName("sn");
        ldapProvider.getLdapIdentityStore().update(john2);
        // Assert DN was changed
        Assert.assertEquals("sn=Doe2", john2.getDn().getFirstRdn().toString());
        // Remove "sn" mapper
        snMapper = appRealm.getComponentsStream(ctx.getLdapModel().getId(), LDAPStorageMapper.class.getName()).filter(mapper -> Objects.equals(mapper.getName(), "last name")).findFirst().orElse(null);
        Assert.assertNotNull(snMapper);
        appRealm.removeComponent(snMapper);
    });
    // Try to update johnkeycloak2 user. It shouldn't try to update DN
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserModel johnkeycloak2 = session.users().getUserByUsername(appRealm, "johnkeycloak2");
        Assert.assertNotNull(johnkeycloak2);
        johnkeycloak2.setFirstName("foo2");
        johnkeycloak2.setLastName("foo");
    });
    // Re-create "sn" mapper back
    snMapperRep.setId(null);
    testRealm().components().add(snMapperRep);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) RealmModel(org.keycloak.models.RealmModel) MethodSorters(org.junit.runners.MethodSorters) RealmModel(org.keycloak.models.RealmModel) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) LDAPTestUtils(org.keycloak.testsuite.util.LDAPTestUtils) LDAPStorageMapper(org.keycloak.storage.ldap.mappers.LDAPStorageMapper) Test(org.junit.Test) Matchers.equalToIgnoringCase(org.hamcrest.Matchers.equalToIgnoringCase) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) LDAPConstants(org.keycloak.models.LDAPConstants) Objects(java.util.Objects) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) List(java.util.List) UserModel(org.keycloak.models.UserModel) LDAPRule(org.keycloak.testsuite.util.LDAPRule) ComponentModel(org.keycloak.component.ComponentModel) Assert(org.junit.Assert) ClassRule(org.junit.ClassRule) FixMethodOrder(org.junit.FixMethodOrder) UserResource(org.keycloak.admin.client.resource.UserResource) LDAPTestConfiguration(org.keycloak.testsuite.util.LDAPTestConfiguration) LDAPDn(org.keycloak.storage.ldap.idm.model.LDAPDn) UserModel(org.keycloak.models.UserModel) LDAPStorageMapper(org.keycloak.storage.ldap.mappers.LDAPStorageMapper) ComponentModel(org.keycloak.component.ComponentModel) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) Test(org.junit.Test)

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