Search in sources :

Example 11 with ValidationError

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

the class LengthValidator method doValidate.

@Override
protected void doValidate(String value, String inputHint, ValidationContext context, ValidatorConfig config) {
    Integer min = config.getInt(KEY_MIN);
    Integer max = config.getInt(KEY_MAX);
    if (!config.getBooleanOrDefault(KEY_TRIM_DISABLED, Boolean.FALSE)) {
        value = value.trim();
    }
    int length = value.length();
    if (config.containsKey(KEY_MIN) && length < min.intValue()) {
        context.addError(new ValidationError(ID, inputHint, selectErrorMessage(config), min, max));
        return;
    }
    if (config.containsKey(KEY_MAX) && length > max.intValue()) {
        context.addError(new ValidationError(ID, inputHint, selectErrorMessage(config), min, max));
        return;
    }
}
Also used : ValidationError(org.keycloak.validate.ValidationError)

Example 12 with ValidationError

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

the class ValidationException method addError.

void addError(ValidationError error) {
    List<Error> errors = this.errors.computeIfAbsent(error.getMessage(), (k) -> new ArrayList<>());
    errors.add(new Error(error));
}
Also used : ValidationError(org.keycloak.validate.ValidationError)

Example 13 with ValidationError

use of org.keycloak.validate.ValidationError 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)

Example 14 with ValidationError

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

the class DuplicateEmailValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    if (Validation.isBlank(value))
        return context;
    KeycloakSession session = context.getSession();
    RealmModel realm = session.getContext().getRealm();
    if (!realm.isDuplicateEmailsAllowed()) {
        UserModel userByEmail = session.users().getUserByEmail(realm, value);
        UserModel user = UserProfileAttributeValidationContext.from(context).getAttributeContext().getUser();
        // check for duplicated email
        if (userByEmail != null && (user == null || !userByEmail.getId().equals(user.getId()))) {
            context.addError(new ValidationError(ID, inputHint, Messages.EMAIL_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 15 with ValidationError

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

the class EmailExistsAsUsernameValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    @SuppressWarnings("unchecked") List<String> values = (List<String>) input;
    if (values == null || values.isEmpty()) {
        return context;
    }
    String value = values.get(0);
    if (Validation.isBlank(value))
        return context;
    KeycloakSession session = context.getSession();
    RealmModel realm = session.getContext().getRealm();
    if (!realm.isDuplicateEmailsAllowed() && realm.isRegistrationEmailAsUsername()) {
        UserModel user = UserProfileAttributeValidationContext.from(context).getAttributeContext().getUser();
        UserModel userByEmail = session.users().getUserByEmail(realm, value);
        if (userByEmail != null && user != null && !userByEmail.getId().equals(user.getId())) {
            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)

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