Search in sources :

Example 21 with UserProfileProvider

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

the class RegistrationProfile method validate.

@Override
public void validate(org.keycloak.authentication.ValidationContext context) {
    MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
    context.getEvent().detail(Details.REGISTER_METHOD, "form");
    UserProfileProvider profileProvider = context.getSession().getProvider(UserProfileProvider.class);
    UserProfile profile = profileProvider.create(UserProfileContext.REGISTRATION_PROFILE, formData);
    try {
        profile.validate();
    } catch (ValidationException pve) {
        List<FormMessage> errors = Validation.getFormErrorsFromValidation(pve.getErrors());
        if (pve.hasError(Messages.EMAIL_EXISTS, Messages.INVALID_EMAIL)) {
            context.getEvent().detail(Details.EMAIL, profile.getAttributes().getFirstValue(UserModel.EMAIL));
        }
        if (pve.hasError(Messages.EMAIL_EXISTS)) {
            context.error(Errors.EMAIL_IN_USE);
        } else
            context.error(Errors.INVALID_REGISTRATION);
        context.validationError(formData, errors);
        return;
    }
    context.success();
}
Also used : ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) List(java.util.List)

Example 22 with UserProfileProvider

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

the class UpdateProfile method processAction.

@Override
public void processAction(RequiredActionContext context) {
    EventBuilder event = context.getEvent();
    event.event(EventType.UPDATE_PROFILE).detail(Details.CONTEXT, UserProfileContext.UPDATE_PROFILE.name());
    MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
    UserModel user = context.getUser();
    UserProfileProvider provider = context.getSession().getProvider(UserProfileProvider.class);
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, formData, user);
    try {
        // backward compatibility with old account console where attributes are not removed if missing
        profile.update(false, new EventAuditingAttributeChangeListener(profile, event));
        context.success();
    } catch (ValidationException pve) {
        List<FormMessage> errors = Validation.getFormErrorsFromValidation(pve.getErrors());
        context.challenge(createResponse(context, formData, errors));
    }
}
Also used : UserModel(org.keycloak.models.UserModel) EventBuilder(org.keycloak.events.EventBuilder) ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) EventAuditingAttributeChangeListener(org.keycloak.userprofile.EventAuditingAttributeChangeListener) List(java.util.List)

Example 23 with UserProfileProvider

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

the class VerifyUserProfile method evaluateTriggers.

@Override
public void evaluateTriggers(RequiredActionContext context) {
    UserModel user = context.getUser();
    UserProfileProvider provider = context.getSession().getProvider(UserProfileProvider.class);
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, user);
    try {
        profile.validate();
        context.getAuthenticationSession().removeRequiredAction(getId());
        user.removeRequiredAction(getId());
    } catch (ValidationException e) {
        context.getAuthenticationSession().addRequiredAction(getId());
    }
}
Also used : UserModel(org.keycloak.models.UserModel) ValidationException(org.keycloak.userprofile.ValidationException) UserProfile(org.keycloak.userprofile.UserProfile) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider)

Example 24 with UserProfileProvider

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

the class UserProfileTest method testIdempotentProfile.

private static void testIdempotentProfile(KeycloakSession session) {
    Map<String, Object> attributes = new HashMap<>();
    UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    attributes.put(UserModel.USERNAME, "profiled-user");
    // once created, profile attributes can not be changed
    assertTrue(profile.getAttributes().contains(UserModel.USERNAME));
    assertNull(profile.getAttributes().getFirstValue(UserModel.USERNAME));
}
Also used : UserProfile(org.keycloak.userprofile.UserProfile) HashMap(java.util.HashMap) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider)

Example 25 with UserProfileProvider

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

the class UserProfileTest method testAttributeValidation.

private static void testAttributeValidation(KeycloakSession session) {
    Map<String, Object> attributes = new HashMap<>();
    UserProfileProvider provider = session.getProvider(UserProfileProvider.class);
    UserProfile profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    List<ValidationError> errors = new ArrayList<>();
    assertFalse(profile.getAttributes().validate(UserModel.USERNAME, (Consumer<ValidationError>) errors::add));
    assertTrue(containsErrorMessage(errors, Messages.MISSING_USERNAME));
    errors.clear();
    attributes.clear();
    attributes.put(UserModel.EMAIL, "invalid");
    profile = provider.create(UserProfileContext.UPDATE_PROFILE, attributes);
    assertFalse(profile.getAttributes().validate(UserModel.EMAIL, (Consumer<ValidationError>) errors::add));
    assertTrue(containsErrorMessage(errors, EmailValidator.MESSAGE_INVALID_EMAIL));
}
Also used : UserProfile(org.keycloak.userprofile.UserProfile) Consumer(java.util.function.Consumer) HashMap(java.util.HashMap) DeclarativeUserProfileProvider(org.keycloak.userprofile.DeclarativeUserProfileProvider) UserProfileProvider(org.keycloak.userprofile.UserProfileProvider) ArrayList(java.util.ArrayList) ValidationError(org.keycloak.validate.ValidationError)

Aggregations

UserProfileProvider (org.keycloak.userprofile.UserProfileProvider)30 UserProfile (org.keycloak.userprofile.UserProfile)24 ValidationException (org.keycloak.userprofile.ValidationException)15 UserModel (org.keycloak.models.UserModel)13 DeclarativeUserProfileProvider (org.keycloak.userprofile.DeclarativeUserProfileProvider)13 HashMap (java.util.HashMap)9 List (java.util.List)9 ComponentValidationException (org.keycloak.component.ComponentValidationException)7 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Consumes (javax.ws.rs.Consumes)4 RealmModel (org.keycloak.models.RealmModel)4 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 NoCache (org.jboss.resteasy.annotations.cache.NoCache)3 EventBuilder (org.keycloak.events.EventBuilder)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 EventAuditingAttributeChangeListener (org.keycloak.userprofile.EventAuditingAttributeChangeListener)3 LinkedList (java.util.LinkedList)2