Search in sources :

Example 6 with UPAttribute

use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.

the class UPConfigParserTest method validateConfiguration_attributeAnnotationsErrors.

private static void validateConfiguration_attributeAnnotationsErrors(KeycloakSession session) throws IOException {
    UPConfig config = loadValidConfig();
    // attribute references group that is not configured
    UPAttribute att = config.getAttributes().get(1);
    att.getAnnotations().put("inputOptions", "");
    att.getAnnotations().put("inputOptionLabels", "");
    List<String> errors = validate(session, config);
    Assert.assertEquals(2, errors.size());
}
Also used : UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 7 with UPAttribute

use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.

the class UPConfigParserTest method validateConfiguration_attributeGroupReferenceErrors.

private static void validateConfiguration_attributeGroupReferenceErrors(KeycloakSession session) throws IOException {
    UPConfig config = loadValidConfig();
    // attribute references group that is not configured
    UPAttribute firstAttribute = config.getAttributes().get(0);
    firstAttribute.setGroup("non-existing-group");
    List<String> errors = validate(session, config);
    Assert.assertEquals(1, errors.size());
    Assert.assertEquals("Attribute 'username' references unknown group 'non-existing-group'", errors.get(0));
}
Also used : UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 8 with UPAttribute

use of org.keycloak.userprofile.config.UPAttribute in project keycloak by keycloak.

the class UserProfileTest method testCustomAttributeRequired.

private static void testCustomAttributeRequired(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);
    // make it ALWAYS required
    UPAttributeRequired requirements = new UPAttributeRequired();
    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");
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    // fails on required validation
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(ATT_ADDRESS));
    }
    // 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");
    attributes.put(UserModel.FIRST_NAME, "Joe");
    attributes.put(UserModel.LAST_NAME, "Doe");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, 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)

Example 9 with UPAttribute

use of org.keycloak.userprofile.config.UPAttribute 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 10 with UPAttribute

use of org.keycloak.userprofile.config.UPAttribute 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)

Aggregations

UPAttribute (org.keycloak.userprofile.config.UPAttribute)22 UPConfig (org.keycloak.userprofile.config.UPConfig)22 HashMap (java.util.HashMap)13 ComponentValidationException (org.keycloak.component.ComponentValidationException)12 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)12 ComponentModel (org.keycloak.component.ComponentModel)11 UserProfile (org.keycloak.userprofile.UserProfile)11 UPAttributePermissions (org.keycloak.userprofile.config.UPAttributePermissions)11 ValidationException (org.keycloak.userprofile.ValidationException)10 UPAttributeRequired (org.keycloak.userprofile.config.UPAttributeRequired)10 RealmModel (org.keycloak.models.RealmModel)3 UserModel (org.keycloak.models.UserModel)3 Map (java.util.Map)2 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)2 UPAttributeSelector (org.keycloak.userprofile.config.UPAttributeSelector)2 UPGroup (org.keycloak.userprofile.config.UPGroup)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1