Search in sources :

Example 16 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class RegistrationEmailAsUsernameEmailValueValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    RealmModel realm = context.getSession().getContext().getRealm();
    if (!realm.isRegistrationEmailAsUsername()) {
        return context;
    }
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    if (!(Validation.isBlank(value) || Validation.isEmailValid(value))) {
        context.addError(new ValidationError(ID, inputHint, Messages.INVALID_EMAIL));
    }
    return context;
}
Also used : RealmModel(org.keycloak.models.RealmModel) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 17 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class RegistrationUsernameExistsValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    KeycloakSession session = context.getSession();
    RealmModel realm = session.getContext().getRealm();
    if (realm.isRegistrationEmailAsUsername()) {
        return context;
    }
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    UserModel existing = session.users().getUserByUsername(realm, value);
    if (existing != null) {
        context.addError(new ValidationError(ID, inputHint, Messages.USERNAME_EXISTS).setStatusCode(Response.Status.CONFLICT));
    }
    return context;
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) KeycloakSession(org.keycloak.models.KeycloakSession) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 18 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class AttributeRequiredByMetadataValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    AttributeContext attContext = UserProfileAttributeValidationContext.from(context).getAttributeContext();
    AttributeMetadata metadata = attContext.getMetadata();
    if (!metadata.isRequired(attContext)) {
        return context;
    }
    if (metadata.isReadOnly(attContext)) {
        return context;
    }
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        context.addError(new ValidationError(ID, inputHint, ERROR_USER_ATTRIBUTE_REQUIRED));
    } else {
        for (String value : values) {
            if (Validation.isBlank(value)) {
                context.addError(new ValidationError(ID, inputHint, ERROR_USER_ATTRIBUTE_REQUIRED));
                return context;
            }
        }
    }
    return context;
}
Also used : AttributeContext(org.keycloak.userprofile.AttributeContext) AttributeMetadata(org.keycloak.userprofile.AttributeMetadata) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 19 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class BlankAttributeValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    boolean failOnNull = config.getBooleanOrDefault(CFG_FAIL_ON_NULL, false);
    if (values.isEmpty() && !failOnNull) {
        return context;
    }
    String value = values.isEmpty() ? null : values.get(0);
    if ((failOnNull || value != null) && Validation.isBlank(value)) {
        context.addError(new ValidationError(ID, inputHint, config.getStringOrDefault(CFG_ERROR_MESSAGE, AttributeRequiredByMetadataValidator.ERROR_USER_ATTRIBUTE_REQUIRED)));
    }
    return context;
}
Also used : List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 20 with ValidationError

use of org.keycloak.validate.ValidationError in project keycloak by keycloak.

the class DuplicateUsernameValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    if (Validation.isBlank(value))
        return context;
    KeycloakSession session = context.getSession();
    UserModel existing = session.users().getUserByUsername(session.getContext().getRealm(), value);
    UserModel user = UserProfileAttributeValidationContext.from(context).getAttributeContext().getUser();
    if (user != null && !value.equals(user.getFirstAttribute(UserModel.USERNAME)) && (existing != null && !existing.getId().equals(user.getId()))) {
        context.addError(new ValidationError(ID, inputHint, Messages.USERNAME_EXISTS).setStatusCode(Response.Status.CONFLICT));
    }
    return context;
}
Also used : UserModel(org.keycloak.models.UserModel) KeycloakSession(org.keycloak.models.KeycloakSession) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Aggregations

ValidationError (org.keycloak.validate.ValidationError)23 List (java.util.List)14 UserModel (org.keycloak.models.UserModel)8 RealmModel (org.keycloak.models.RealmModel)7 KeycloakSession (org.keycloak.models.KeycloakSession)5 AttributeContext (org.keycloak.userprofile.AttributeContext)4 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 ValidationResult (org.keycloak.validate.ValidationResult)2 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1