Search in sources :

Example 11 with ValidationException

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

the class UserProfileTest method testCustomAttributeOptional.

private static void testCustomAttributeOptional(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);
    Map<String, Object> validatorConfig = new HashMap<>();
    validatorConfig.put(LengthValidator.KEY_MIN, 4);
    attribute.addValidation(LengthValidator.ID, validatorConfig);
    config.addAttribute(attribute);
    provider.setConfiguration(JsonSerialization.writeValueAsString(config));
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, "user");
    // null is OK as attribute is optional
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    // blank String have to be OK as it is what UI forms send for not filled in optional attributes
    attributes.put(ATT_ADDRESS, "");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    // fails on length validation
    attributes.put(ATT_ADDRESS, "adr");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(ATT_ADDRESS));
    }
    // all OK
    attributes.put(ATT_ADDRESS, "adress ok");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
}
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 12 with ValidationException

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

the class UserProfileTest method testResolveProfile.

private static void testResolveProfile(KeycloakSession session) {
    configureAuthenticationSession(session);
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, "profiled-user");
    UserProfileProvider provider = getDynamicUserProfileProvider(session);
    provider.setConfiguration("{\"attributes\": [{\"name\": \"business.address\", \"required\": {\"scopes\": [\"customer\"]}, \"permissions\": {\"edit\": [\"user\"]}}]}");
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.getAttributes();
    try {
        profile.validate();
        Assert.fail("Should fail validation");
    } catch (ValidationException ve) {
        // address is mandatory
        assertTrue(ve.isAttributeOnError("business.address"));
    }
    attributes.put("business.address", "valid-address");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    profile = provider.create(UserProfileContext.ACCOUNT, attributes);
    profile.validate();
}
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) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider)

Example 13 with ValidationException

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

the class UserProfileTest method testReadonlyUpdates.

private static void testReadonlyUpdates(KeycloakSession session) {
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, org.keycloak.models.utils.KeycloakModelUtils.generateId());
    attributes.put("address", Arrays.asList("fixed-address"));
    attributes.put("department", Arrays.asList("sales"));
    UserProfileProvider provider = getDynamicUserProfileProvider(session);
    provider.setConfiguration("{\"attributes\": [{\"name\": \"department\", \"permissions\": {\"edit\": [\"admin\"]}}]}");
    UserProfile profile = provider.create(UserProfileContext.ACCOUNT, attributes);
    UserModel user = profile.create();
    assertThat(profile.getAttributes().nameSet(), containsInAnyOrder(UserModel.USERNAME, UserModel.EMAIL, "address", "department"));
    assertNull(user.getFirstAttribute("department"));
    profile = provider.create(UserProfileContext.USER_API, attributes, user);
    Set<String> attributesUpdated = new HashSet<>();
    profile.update((attributeName, userModel, oldValue) -> assertTrue(attributesUpdated.add(attributeName)));
    assertThat(attributesUpdated, containsInAnyOrder("department"));
    assertEquals("sales", user.getFirstAttribute("department"));
    attributes.put("department", "cannot-change");
    profile = provider.create(UserProfileContext.ACCOUNT, attributes, user);
    try {
        profile.update();
        fail("Should fail due to read only attribute");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError("department"));
    }
    assertEquals("sales", user.getFirstAttribute("department"));
    assertTrue(profile.getAttributes().isReadOnly("department"));
}
Also used : UserModel(org.keycloak.models.UserModel) ComponentValidationException(org.keycloak.component.ComponentValidationException) ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) HashMap(java.util.HashMap) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) HashSet(java.util.HashSet)

Example 14 with ValidationException

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

the class UserProfileTest method testCustomValidationForUsername.

private static void testCustomValidationForUsername(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.USERNAME);
    Map<String, Object> validatorConfig = new HashMap<>();
    validatorConfig.put("min", 4);
    attribute.addValidation(LengthValidator.ID, validatorConfig);
    config.addAttribute(attribute);
    provider.setConfiguration(JsonSerialization.writeValueAsString(config));
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, "us");
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(UserModel.USERNAME));
        assertTrue(ve.hasError(LengthValidator.MESSAGE_INVALID_LENGTH_TOO_SHORT));
    }
    attributes.put(UserModel.USERNAME, "user");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
    provider.setConfiguration(null);
    attributes.put(UserModel.USERNAME, "user");
    attributes.put(UserModel.FIRST_NAME, "Joe");
    attributes.put(UserModel.LAST_NAME, "Doe");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    profile.validate();
}
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 15 with ValidationException

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

the class UserProfileTest method testRequiredIfUser.

private static void testRequiredIfUser(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_USER));
    attribute.setRequired(requirements);
    UPAttributePermissions permissions = new UPAttributePermissions();
    permissions.setEdit(Collections.singleton(ROLE_USER));
    attribute.setPermissions(permissions);
    config.addAttribute(attribute);
    provider.setConfiguration(JsonSerialization.writeValueAsString(config));
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.USERNAME, "user");
    // fail on common contexts
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(ATT_ADDRESS));
    }
    profile = provider.create(UserProfileContext.ACCOUNT, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(ATT_ADDRESS));
    }
    profile = provider.create(UserProfileContext.REGISTRATION_PROFILE, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(ATT_ADDRESS));
    }
    attributes.put(UserModel.FIRST_NAME, "Joe");
    attributes.put(UserModel.LAST_NAME, "Doe");
    // no fail on User API
    profile = provider.create(UserProfileContext.USER_API, attributes);
    profile.validate();
}
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)

Aggregations

ValidationException (org.keycloak.userprofile.ValidationException)25 UserProfile (org.keycloak.userprofile.UserProfile)24 ComponentValidationException (org.keycloak.component.ComponentValidationException)16 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)16 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)15 HashMap (java.util.HashMap)14 UPAttribute (org.keycloak.userprofile.config.UPAttribute)10 UPConfig (org.keycloak.userprofile.config.UPConfig)10 ComponentModel (org.keycloak.component.ComponentModel)9 List (java.util.List)7 UPAttributePermissions (org.keycloak.userprofile.config.UPAttributePermissions)7 UPAttributeRequired (org.keycloak.userprofile.config.UPAttributeRequired)7 UserModel (org.keycloak.models.UserModel)6 EventBuilder (org.keycloak.events.EventBuilder)3 RealmModel (org.keycloak.models.RealmModel)3 EventAuditingAttributeChangeListener (org.keycloak.userprofile.EventAuditingAttributeChangeListener)3 ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2