Search in sources :

Example 66 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class LDAPProvidersFullNameMapperTest method testUpdatingFirstNameAndLastNamePropagatesToFullnameMapper.

@Test
public void testUpdatingFirstNameAndLastNamePropagatesToFullnameMapper() {
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        ComponentModel ldapModel = LDAPTestUtils.getLdapProviderModel(appRealm);
        LDAPStorageProvider ldapFedProvider = LDAPTestUtils.getLdapProvider(session, ldapModel);
        LDAPTestUtils.addLDAPUser(ldapFedProvider, appRealm, "fullname", "James", "Dee", "fullname@email.org", null, "4578");
        // Assert user is successfully imported in Keycloak DB now with correct firstName and lastName
        LDAPTestAsserts.assertUserImported(session.users(), appRealm, "fullname", "James", "Dee", "fullname@email.org", "4578");
    });
    // Assert user will be changed in LDAP too
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        UserModel fullnameUser = session.users().getUserByUsername(appRealm, "fullname");
        fullnameUser.setFirstName("James2");
        fullnameUser.setLastName("Dee2");
    });
    // Assert changed user available in Keycloak
    testingClient.server().run(session -> {
        LDAPTestContext ctx = LDAPTestContext.init(session);
        RealmModel appRealm = ctx.getRealm();
        // Assert user is successfully imported in Keycloak DB now with correct firstName and lastName
        LDAPTestAsserts.assertUserImported(session.users(), appRealm, "fullname", "James2", "Dee2", "fullname@email.org", "4578");
        // Remove "fullnameUser" to assert he is removed from LDAP.
        UserModel fullnameUser = session.users().getUserByUsername(appRealm, "fullname");
        session.users().removeUser(appRealm, fullnameUser);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ComponentModel(org.keycloak.component.ComponentModel) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) Test(org.junit.Test)

Example 67 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class UserProfileTest method testNoValidationsIfUserReadOnly.

private static void testNoValidationsIfUserReadOnly(KeycloakSession session) throws IOException {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    ComponentModel component = provider.getComponentModel();
    assertNotNull(component);
    UPConfig config = new UPConfig();
    UPAttribute attribute = new UPAttribute();
    attribute.setName(ATT_ADDRESS);
    UPAttributeRequired requirements = new UPAttributeRequired();
    attribute.setRequired(requirements);
    UPAttributePermissions permissions = new UPAttributePermissions();
    permissions.setEdit(Collections.singleton(UPConfigUtils.ROLE_ADMIN));
    attribute.setPermissions(permissions);
    config.addAttribute(attribute);
    provider.setConfiguration(JsonSerialization.writeValueAsString(config));
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, "user");
    attributes.put(UserModel.FIRST_NAME, "user");
    attributes.put(UserModel.LAST_NAME, "user");
    // NO fail on USER contexts
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    // Fails on ADMIN context - User REST API
    try {
        profile = provider.create(UserProfileContext.USER_API, attributes);
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(ATT_ADDRESS));
    }
}
Also used : UPAttributePermissions(org.keycloak.userprofile.config.UPAttributePermissions) ComponentValidationException(org.keycloak.component.ComponentValidationException) ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) HashMap(java.util.HashMap) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UPConfig(org.keycloak.userprofile.config.UPConfig) ComponentModel(org.keycloak.component.ComponentModel) UPAttributeRequired(org.keycloak.userprofile.config.UPAttributeRequired) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 68 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class UserProfileTest method testConfigurationInvalidScope.

private static void testConfigurationInvalidScope(KeycloakSession session) throws IOException {
    RealmModel realm = session.getContext().getRealm();
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    ComponentModel component = provider.getComponentModel();
    assertNotNull(component);
    UPConfig config = new UPConfig();
    UPAttribute attribute = new UPAttribute();
    attribute.setName(ATT_ADDRESS);
    UPAttributeRequired requirements = new UPAttributeRequired();
    requirements.setScopes(Collections.singleton("invalid"));
    attribute.setRequired(requirements);
    attribute.setSelector(new UPAttributeSelector());
    attribute.getSelector().setScopes(Collections.singleton("invalid"));
    config.addAttribute(attribute);
    try {
        provider.setConfiguration(JsonSerialization.writeValueAsString(config));
        Assert.fail("Expected to fail due to invalid client scope");
    } catch (ComponentValidationException cve) {
    // ignore
    }
}
Also used : RealmModel(org.keycloak.models.RealmModel) ComponentValidationException(org.keycloak.component.ComponentValidationException) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UPConfig(org.keycloak.userprofile.config.UPConfig) ComponentModel(org.keycloak.component.ComponentModel) UPAttributeRequired(org.keycloak.userprofile.config.UPAttributeRequired) UPAttribute(org.keycloak.userprofile.config.UPAttribute) UPAttributeSelector(org.keycloak.userprofile.config.UPAttributeSelector)

Example 69 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class UserModelTest method getUserFederationInstance.

private UserStorageProvider getUserFederationInstance(KeycloakSession session, final RealmModel realm) throws RuntimeException {
    UserStorageProvider instance = (UserStorageProvider) session.getAttribute(userFederationId);
    if (instance == null) {
        ComponentModel model = realm.getComponent(userFederationId);
        UserStorageProviderFactory factory = (UserStorageProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(UserStorageProvider.class, model.getProviderId());
        instance = factory.create(session, model);
        if (instance == null) {
            throw new RuntimeException("UserStorageProvideFactory (of type " + factory.getClass().getName() + ") produced a null instance");
        }
        session.enlistForClose(instance);
        session.setAttribute(userFederationId, instance);
    }
    return instance;
}
Also used : UserStorageProvider(org.keycloak.storage.UserStorageProvider) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ComponentModel(org.keycloak.component.ComponentModel)

Example 70 with ComponentModel

use of org.keycloak.component.ComponentModel in project keycloak by keycloak.

the class UserSyncTest method createEnvironment.

@Override
public void createEnvironment(KeycloakSession s) {
    inComittedTransaction(session -> {
        RealmModel realm = session.realms().createRealm("realm");
        realm.setDefaultRole(session.roles().addRealmRole(realm, Constants.DEFAULT_ROLES_ROLE_PREFIX + "-" + realm.getName()));
        this.realmId = realm.getId();
    });
    getParameters(UserStorageProviderModel.class).forEach(fs -> inComittedTransaction(session -> {
        if (userFederationId != null || !fs.isImportEnabled())
            return;
        RealmModel realm = session.realms().getRealm(realmId);
        fs.setParentId(realmId);
        ComponentModel res = realm.addComponentModel(fs);
        // Check if the provider implements ImportSynchronization interface
        UserStorageProviderFactory userStorageProviderFactory = (UserStorageProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(UserStorageProvider.class, res.getProviderId());
        if (!ImportSynchronization.class.isAssignableFrom(userStorageProviderFactory.getClass())) {
            return;
        }
        userFederationId = res.getId();
        log.infof("Added %s user federation provider: %s", fs.getName(), res.getId());
    }));
    assumeThat("Cannot run UserSyncTest because there is no user federation provider that supports sync", userFederationId, notNullValue());
}
Also used : RealmModel(org.keycloak.models.RealmModel) IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) RealmModel(org.keycloak.models.RealmModel) Assume.assumeThat(org.junit.Assume.assumeThat) LDAPStorageProvider(org.keycloak.storage.ldap.LDAPStorageProvider) UserStorageProvider(org.keycloak.storage.UserStorageProvider) LDAPTestUtils(org.keycloak.testsuite.util.LDAPTestUtils) Constants(org.keycloak.models.Constants) Matchers(org.hamcrest.Matchers) KeycloakSession(org.keycloak.models.KeycloakSession) Test(org.junit.Test) LDAPStorageProviderFactory(org.keycloak.storage.ldap.LDAPStorageProviderFactory) UserStorageSyncManager(org.keycloak.services.managers.UserStorageSyncManager) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ClusterProvider(org.keycloak.cluster.ClusterProvider) UserProvider(org.keycloak.models.UserProvider) ImportSynchronization(org.keycloak.storage.user.ImportSynchronization) RealmProvider(org.keycloak.models.RealmProvider) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) SynchronizationResult(org.keycloak.storage.user.SynchronizationResult) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) UserStorageProviderFactory(org.keycloak.storage.UserStorageProviderFactory) ComponentModel(org.keycloak.component.ComponentModel) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel)

Aggregations

ComponentModel (org.keycloak.component.ComponentModel)155 RealmModel (org.keycloak.models.RealmModel)74 Test (org.junit.Test)52 LDAPStorageProvider (org.keycloak.storage.ldap.LDAPStorageProvider)46 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)34 UserModel (org.keycloak.models.UserModel)29 HashMap (java.util.HashMap)22 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)22 GroupModel (org.keycloak.models.GroupModel)21 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)18 SynchronizationResult (org.keycloak.storage.user.SynchronizationResult)18 ComponentValidationException (org.keycloak.component.ComponentValidationException)16 UserStorageProvider (org.keycloak.storage.UserStorageProvider)16 LDAPStorageMapper (org.keycloak.storage.ldap.mappers.LDAPStorageMapper)15 GroupLDAPStorageMapper (org.keycloak.storage.ldap.mappers.membership.group.GroupLDAPStorageMapper)15 Path (javax.ws.rs.Path)14 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)13 ModelException (org.keycloak.models.ModelException)11 UPAttribute (org.keycloak.userprofile.config.UPAttribute)11 UPConfig (org.keycloak.userprofile.config.UPConfig)11