Search in sources :

Example 61 with ComponentRepresentation

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

the class LDAPComponentBuilder method build.

public ComponentRepresentation build() {
    ComponentRepresentation componentRepresentation = new ComponentRepresentation();
    componentRepresentation.setName(name);
    componentRepresentation.setParentId(parentId);
    componentRepresentation.setProviderType(providerType.toString());
    componentRepresentation.setProviderId(providerId.toString());
    componentRepresentation.setConfig(ldapConfig);
    return componentRepresentation;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation)

Example 62 with ComponentRepresentation

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

the class UserStorageOTPTest method addProvidersBeforeTest.

@Before
public void addProvidersBeforeTest() throws URISyntaxException, IOException {
    ComponentRepresentation dummyProvider = new ComponentRepresentation();
    dummyProvider.setName("dummy");
    dummyProvider.setId(DummyUserFederationProviderFactory.PROVIDER_NAME);
    dummyProvider.setProviderId(DummyUserFederationProviderFactory.PROVIDER_NAME);
    dummyProvider.setProviderType(UserStorageProvider.class.getName());
    dummyProvider.setConfig(new MultivaluedHashMap<>());
    dummyProvider.getConfig().putSingle("priority", Integer.toString(0));
    dummyProvider.getConfig().putSingle(IMPORT_ENABLED, Boolean.toString(false));
    addComponent(testRealm(), getCleanup(), dummyProvider);
    UserRepresentation user = UserBuilder.create().username("test-user").email("test-user@something.org").build();
    String testUserId = ApiUtil.createUserWithAdminClient(testRealm(), user);
    getCleanup().addUserId(testUserId);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Before(org.junit.Before)

Example 63 with ComponentRepresentation

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

the class UserStorageTest method addProvidersBeforeTest.

@Before
public void addProvidersBeforeTest() throws URISyntaxException, IOException {
    ComponentRepresentation memProvider = new ComponentRepresentation();
    memProvider.setName("memory");
    memProvider.setProviderId(UserMapStorageFactory.PROVIDER_ID);
    memProvider.setProviderType(UserStorageProvider.class.getName());
    memProvider.setConfig(new MultivaluedHashMap<>());
    memProvider.getConfig().putSingle("priority", Integer.toString(0));
    memProvider.getConfig().putSingle(IMPORT_ENABLED, Boolean.toString(false));
    memProviderId = addComponent(memProvider);
    // copy files used by the following RO/RW user providers
    File stResDir = new File(getClass().getResource("/storage-test").toURI());
    if (stResDir.exists() && stResDir.isDirectory() && CONFIG_DIR.exists() && CONFIG_DIR.isDirectory()) {
        for (File f : stResDir.listFiles()) {
            log.infof("Copying %s to %s", f.getName(), CONFIG_DIR.getAbsolutePath());
            FileUtils.copyFileToDirectory(f, CONFIG_DIR);
        }
    } else {
        throw new RuntimeException("Property `auth.server.config.dir` must be set to run UserStorageTests.");
    }
    ComponentRepresentation propProviderRO = new ComponentRepresentation();
    propProviderRO.setName("read-only-user-props");
    propProviderRO.setProviderId(UserPropertyFileStorageFactory.PROVIDER_ID);
    propProviderRO.setProviderType(UserStorageProvider.class.getName());
    propProviderRO.setConfig(new MultivaluedHashMap<>());
    propProviderRO.getConfig().putSingle("priority", Integer.toString(1));
    propProviderRO.getConfig().putSingle("propertyFile", CONFIG_DIR.getAbsolutePath() + File.separator + "read-only-user-password.properties");
    propProviderROId = addComponent(propProviderRO);
    propProviderRWId = addComponent(newPropProviderRW());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageProvider(org.keycloak.storage.UserStorageProvider) File(java.io.File) Before(org.junit.Before)

Example 64 with ComponentRepresentation

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

the class LDAPProvidersIntegrationNoImportTest method testUnsynced.

@Test
@Override
public // Unsynced mode doesn't have much sense in no-import. So it is not allowed at the configuration level
void testUnsynced() throws Exception {
    ComponentResource ldapProviderResource = testRealm().components().component(ldapModelId);
    ComponentRepresentation ldapProviderRep = ldapProviderResource.toRepresentation();
    String currentEditMode = ldapProviderRep.getConfig().getFirst(LDAPConstants.EDIT_MODE);
    Assert.assertEquals(UserStorageProvider.EditMode.WRITABLE.toString(), currentEditMode);
    // Try update editMode to UNSYNCED. It should not work as UNSYNCED with no-import is not allowed
    ldapProviderRep.getConfig().putSingle(LDAPConstants.EDIT_MODE, UserStorageProvider.EditMode.UNSYNCED.toString());
    try {
        ldapProviderResource.update(ldapProviderRep);
        Assert.fail("Not expected to successfully update provider");
    } catch (BadRequestException bre) {
    // Expected
    }
    // Try to set editMode to WRITABLE should work
    ldapProviderRep.getConfig().putSingle(LDAPConstants.EDIT_MODE, currentEditMode);
    ldapProviderResource.update(ldapProviderRep);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) ComponentResource(org.keycloak.admin.client.resource.ComponentResource) BadRequestException(javax.ws.rs.BadRequestException) LDAPProvidersIntegrationTest(org.keycloak.testsuite.federation.ldap.LDAPProvidersIntegrationTest) Test(org.junit.Test)

Example 65 with ComponentRepresentation

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

the class LDAPSyncTest method test05MissingLDAPUsernameSync.

// KEYCLOAK-1728
@Test
public void test05MissingLDAPUsernameSync() {
    String origUsernameAttrName = testingClient.server().fetch(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        // Remove all users from model
        session.userLocalStorage().getUsersStream(ctx.getRealm(), true).peek(user -> System.out.println("trying to delete user: " + user.getUsername())).collect(Collectors.toList()).forEach(user -> {
            UserCache userCache = session.userCache();
            if (userCache != null) {
                userCache.evict(ctx.getRealm(), user);
            }
            session.userLocalStorage().removeUser(ctx.getRealm(), user);
        });
        // Add street mapper and add some user including street
        ComponentModel streetMapper = LDAPTestUtils.addUserAttributeMapper(ctx.getRealm(), ctx.getLdapModel(), "streetMapper", "street", LDAPConstants.STREET);
        LDAPObject streetUser = LDAPTestUtils.addLDAPUser(ctx.getLdapProvider(), ctx.getRealm(), "user8", "User8FN", "User8LN", "user8@email.org", "user8street", "126");
        // Change name of username attribute name to street
        String origUsernameAttrNamee = ctx.getLdapModel().get(LDAPConstants.USERNAME_LDAP_ATTRIBUTE);
        ctx.getLdapModel().getConfig().putSingle(LDAPConstants.USERNAME_LDAP_ATTRIBUTE, "street");
        // Need to change this due to ApacheDS pagination bug (For other LDAP servers, pagination works fine) TODO: Remove once ApacheDS upgraded and pagination is fixed
        ctx.getLdapModel().put(LDAPConstants.BATCH_SIZE_FOR_SYNC, "10");
        ctx.getRealm().updateComponent(ctx.getLdapModel());
        return origUsernameAttrNamee;
    }, String.class);
    // Just user8 synced. All others failed to sync
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        KeycloakSessionFactory sessionFactory = session.getKeycloakSessionFactory();
        SynchronizationResult syncResult = new UserStorageSyncManager().syncAllUsers(sessionFactory, "test", ctx.getLdapModel());
        Assert.assertEquals(1, syncResult.getAdded());
        Assert.assertTrue(syncResult.getFailed() > 0);
    });
    // Revert config changes
    ComponentRepresentation ldapRep = testRealm().components().component(ldapModelId).toRepresentation();
    if (origUsernameAttrName == null) {
        ldapRep.getConfig().remove(LDAPConstants.USERNAME_LDAP_ATTRIBUTE);
    } else {
        ldapRep.getConfig().putSingle(LDAPConstants.USERNAME_LDAP_ATTRIBUTE, origUsernameAttrName);
    }
    testRealm().components().component(ldapModelId).update(ldapRep);
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        // Revert config changes
        ComponentModel streetMapper = LDAPTestUtils.getSubcomponentByName(ctx.getRealm(), ctx.getLdapModel(), "streetMapper");
        ctx.getRealm().removeComponent(streetMapper);
    });
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) ComponentModel(org.keycloak.component.ComponentModel) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) UserCache(org.keycloak.models.cache.UserCache) KeycloakSessionFactory(org.keycloak.models.KeycloakSessionFactory) 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