Search in sources :

Example 1 with UPConfig

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

the class AbstractUserProfileTest method generateLargeProfileConfig.

/**
 * Generate big configuration to test slicing in the persistence/component config
 * @return a configuration that is expected to be split into 2 slices
 * @throws IOException
 */
protected static String generateLargeProfileConfig() throws IOException {
    UPConfig config = new UPConfig();
    for (int i = 0; i < 80; i++) {
        UPAttribute attribute = new UPAttribute();
        attribute.setName(UserModel.USERNAME + i);
        Map<String, Object> validatorConfig = new HashMap<>();
        validatorConfig.put("min", 3);
        attribute.addValidation("length", validatorConfig);
        config.addAttribute(attribute);
    }
    String newConfig = JsonSerialization.writeValueAsString(config);
    return newConfig;
}
Also used : HashMap(java.util.HashMap) UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 2 with UPConfig

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

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

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

the class UserProfileTest method testValidateComplianceWithUserProfile.

private static void testValidateComplianceWithUserProfile(KeycloakSession session) throws IOException {
    RealmModel realm = session.getContext().getRealm();
    UserModel user = session.users().addUser(realm, "profiled-user");
    UserProfileProvider provider = getDynamicUserProfileProvider(session);
    UPConfig config = new UPConfig();
    UPAttribute attribute = new UPAttribute();
    attribute.setName("address");
    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));
    UserProfile profile = provider.create(UserProfileContext.ACCOUNT, user);
    try {
        profile.validate();
        Assert.fail("Should fail validation");
    } catch (ValidationException ve) {
        // username is mandatory
        assertTrue(ve.isAttributeOnError("address"));
    }
    user.setAttribute("address", Arrays.asList("fixed-address"));
    profile = provider.create(UserProfileContext.ACCOUNT, user);
    profile.validate();
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) UPAttributePermissions(org.keycloak.userprofile.config.UPAttributePermissions) ComponentValidationException(org.keycloak.component.ComponentValidationException) ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttributeRequired(org.keycloak.userprofile.config.UPAttributeRequired) UPAttribute(org.keycloak.userprofile.config.UPAttribute)

Example 5 with UPConfig

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

the class UPConfigParserTest method validateConfiguration_attributeValidationsErrors.

private static void validateConfiguration_attributeValidationsErrors(KeycloakSession session) throws IOException {
    UPConfig config = loadValidConfig();
    // reset all validations not to affect our test as they may be invalid
    for (UPAttribute att : config.getAttributes()) {
        att.setValidations(null);
    }
    // add validation config for one attribute for testing purposes
    Map<String, Map<String, Object>> validationConfig = new HashMap<>();
    config.getAttributes().get(1).setValidations(validationConfig);
    // empty validator name
    validationConfig.put(" ", null);
    List<String> errors = validate(session, config);
    Assert.assertEquals(1, errors.size());
    // wrong configuration for "length" validator
    validationConfig.clear();
    Map<String, Object> vc = new HashMap<>();
    vc.put("min", "aaa");
    validationConfig.put("length", vc);
    errors = validate(session, config);
    Assert.assertEquals(1, errors.size());
}
Also used : HashMap(java.util.HashMap) UPConfig(org.keycloak.userprofile.config.UPConfig) UPAttribute(org.keycloak.userprofile.config.UPAttribute) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UPConfig (org.keycloak.userprofile.config.UPConfig)26 UPAttribute (org.keycloak.userprofile.config.UPAttribute)22 HashMap (java.util.HashMap)13 ComponentValidationException (org.keycloak.component.ComponentValidationException)13 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 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 RealmModel (org.keycloak.models.RealmModel)3 UserModel (org.keycloak.models.UserModel)3 UPGroup (org.keycloak.userprofile.config.UPGroup)3 Map (java.util.Map)2 Test (org.junit.Test)2 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)2 UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)2 UPAttributeSelector (org.keycloak.userprofile.config.UPAttributeSelector)2