Search in sources :

Example 16 with DeclarativeUserProfileProvider

use of org.keycloak.userprofile.DeclarativeUserProfileProvider in project keycloak by keycloak.

the class UserProfileTest method testRequiredIfAdmin.

private static void testRequiredIfAdmin(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();
    requirements.setRoles(Collections.singleton(ROLE_ADMIN));
    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");
    // NO fail on common contexts
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    profile = provider.create(UserProfileContext.ACCOUNT, attributes);
    profile.validate();
    profile = provider.create(UserProfileContext.REGISTRATION_PROFILE, attributes);
    profile.validate();
    // fail on User 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 17 with DeclarativeUserProfileProvider

use of org.keycloak.userprofile.DeclarativeUserProfileProvider in project keycloak by keycloak.

the class UserProfileTest method testOptionalAttributes.

private static void testOptionalAttributes(KeycloakSession session) throws IOException {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    ComponentModel component = provider.getComponentModel();
    assertNotNull(component);
    UPConfig config = new UPConfig();
    UPAttribute attribute = new UPAttribute();
    attribute.setName(UserModel.FIRST_NAME);
    Map<String, Object> validatorConfig = new HashMap<>();
    validatorConfig.put(LengthValidator.KEY_MAX, 4);
    attribute.addValidation(LengthValidator.ID, validatorConfig);
    config.addAttribute(attribute);
    attribute = new UPAttribute();
    attribute.setName(UserModel.LAST_NAME);
    attribute.addValidation(LengthValidator.ID, validatorConfig);
    config.addAttribute(attribute);
    provider.setConfiguration(JsonSerialization.writeValueAsString(config));
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, "user");
    // not present attributes are OK
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    // empty attributes are OK
    attributes.put(UserModel.FIRST_NAME, "");
    attributes.put(UserModel.LAST_NAME, "");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    // filled attributes are OK
    attributes.put(UserModel.FIRST_NAME, "John");
    attributes.put(UserModel.LAST_NAME, "Doe");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    // fails due to additional length validation so it is executed correctly
    attributes.put(UserModel.FIRST_NAME, "JohnTooLong");
    attributes.put(UserModel.LAST_NAME, "DoeTooLong");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(UserModel.FIRST_NAME));
        assertTrue(ve.isAttributeOnError(UserModel.LAST_NAME));
    }
}
Also used : 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) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 18 with DeclarativeUserProfileProvider

use of org.keycloak.userprofile.DeclarativeUserProfileProvider in project keycloak by keycloak.

the class CustomUserProfileTest method testCustomUserProfileProviderIsActive.

private static void testCustomUserProfileProviderIsActive(KeycloakSession session) {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    assertEquals(CustomUserProfileProvider.class.getName(), provider.getClass().getName());
    assertTrue(provider instanceof CustomUserProfileProvider);
    assertEquals("custom-user-profile", provider.getComponentModel().getProviderId());
}
Also used : DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider)

Example 19 with DeclarativeUserProfileProvider

use of org.keycloak.userprofile.DeclarativeUserProfileProvider in project keycloak by keycloak.

the class CustomUserProfileTest method testInvalidConfiguration.

private static void testInvalidConfiguration(KeycloakSession session) {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    try {
        provider.setConfiguration("{\"validateConfigAttribute\": true}");
        fail("Should fail validation");
    } catch (ComponentValidationException ve) {
    // OK
    }
}
Also used : ComponentValidationException(org.keycloak.component.ComponentValidationException) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider)

Example 20 with DeclarativeUserProfileProvider

use of org.keycloak.userprofile.DeclarativeUserProfileProvider in project keycloak by keycloak.

the class CustomUserProfileTest method testConfigurationChunks.

private static void testConfigurationChunks(KeycloakSession session) throws IOException {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    ComponentModel component = provider.getComponentModel();
    assertNotNull(component);
    String newConfig = generateLargeProfileConfig();
    provider.setConfiguration(newConfig);
    component = provider.getComponentModel();
    // assert config is persisted in 2 pieces
    Assert.assertEquals("2", component.get(DeclarativeUserProfileProvider.UP_PIECES_COUNT_COMPONENT_CONFIG_KEY));
    // assert config is returned correctly
    Assert.assertEquals(newConfig, provider.getConfiguration());
}
Also used : DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) ComponentModel(org.keycloak.component.ComponentModel)

Aggregations

DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)20 ComponentModel (org.keycloak.component.ComponentModel)13 ComponentValidationException (org.keycloak.component.ComponentValidationException)13 HashMap (java.util.HashMap)11 UserProfile (org.keycloak.userprofile.UserProfile)11 ValidationException (org.keycloak.userprofile.ValidationException)10 UPAttribute (org.keycloak.userprofile.config.UPAttribute)10 UPConfig (org.keycloak.userprofile.config.UPConfig)10 UPAttributeRequired (org.keycloak.userprofile.config.UPAttributeRequired)7 UPAttributePermissions (org.keycloak.userprofile.config.UPAttributePermissions)6 RealmModel (org.keycloak.models.RealmModel)1 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)1 UPAttributeSelector (org.keycloak.userprofile.config.UPAttributeSelector)1