Search in sources :

Example 1 with DeclarativeUserProfileProvider

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

the class AbstractUserProfileTest method getDynamicUserProfileProvider.

protected static DeclarativeUserProfileProvider getDynamicUserProfileProvider(KeycloakSession session) {
    UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
    provider.setConfiguration(null);
    return (DeclarativeUserProfileProvider) provider;
}
Also used : DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider)

Example 2 with DeclarativeUserProfileProvider

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

the class UserProfileTest method testDefaultConfig.

private static void testDefaultConfig(KeycloakSession session) {
    DeclarativeUserProfileProvider provider = getDynamicUserProfileProvider(session);
    // reset configuration to default
    provider.setConfiguration(null);
    // failed required validations
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, Collections.emptyMap());
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(UserModel.USERNAME));
    }
    // failed for blank values also
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(UserModel.FIRST_NAME, "");
    attributes.put(UserModel.LAST_NAME, " ");
    attributes.put(UserModel.EMAIL, "");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    try {
        profile.validate();
        fail("Should fail validation");
    } catch (ValidationException ve) {
        assertTrue(ve.isAttributeOnError(UserModel.USERNAME));
        assertTrue(ve.isAttributeOnError(UserModel.FIRST_NAME));
        assertTrue(ve.isAttributeOnError(UserModel.LAST_NAME));
        assertTrue(ve.isAttributeOnError(UserModel.EMAIL));
    }
    // all OK
    attributes.put(UserModel.USERNAME, "jdoeusername");
    attributes.put(UserModel.FIRST_NAME, "John");
    attributes.put(UserModel.LAST_NAME, "Doe");
    attributes.put(UserModel.EMAIL, "jdoe@acme.org");
    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)

Example 3 with DeclarativeUserProfileProvider

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

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

the class UserProfileTest 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 5 with DeclarativeUserProfileProvider

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

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