Search in sources :

Example 6 with ValidationError

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

the class AbstractNumberValidator method validateConfig.

@Override
public ValidationResult validateConfig(KeycloakSession session, ValidatorConfig config) {
    Set<ValidationError> errors = new LinkedHashSet<>();
    if (config != null) {
        boolean containsMin = config.containsKey(KEY_MIN);
        boolean containsMax = config.containsKey(KEY_MAX);
        Number min = getMinMaxConfig(config, KEY_MIN);
        Number max = getMinMaxConfig(config, KEY_MAX);
        if (containsMin && min == null) {
            errors.add(new ValidationError(getId(), KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MIN)));
        }
        if (containsMax && max == null) {
            errors.add(new ValidationError(getId(), KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MAX)));
        }
        if (errors.isEmpty() && containsMin && containsMax && (!isFirstGreaterThanToSecond(max, min))) {
            errors.add(new ValidationError(getId(), KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_VALUE));
        }
    }
    ValidationResult s = super.validateConfig(session, config);
    if (!s.isValid()) {
        errors.addAll(s.getErrors());
    }
    return new ValidationResult(errors);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ValidationError(org.keycloak.validate.ValidationError) ValidationResult(org.keycloak.validate.ValidationResult)

Example 7 with ValidationError

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

the class UriValidator method validateUri.

public boolean validateUri(URI uri, String inputHint, ValidationContext context, Set<String> allowedSchemes, boolean allowFragment, boolean requireValidUrl) throws MalformedURLException {
    boolean valid = true;
    if (uri.getScheme() != null && !allowedSchemes.contains(uri.getScheme())) {
        context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_SCHEME, uri, uri.getScheme()));
        valid = false;
    }
    if (!allowFragment && uri.getFragment() != null) {
        context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_FRAGMENT, uri, uri.getFragment()));
        valid = false;
    }
    // scheme, sometimes it doesn't).
    if (requireValidUrl && valid) {
        // throws an exception
        URL ignored = uri.toURL();
    }
    return valid;
}
Also used : ValidationError(org.keycloak.validate.ValidationError) URL(java.net.URL)

Example 8 with ValidationError

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

the class UriValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    if (input == null || (input instanceof String && ((String) input).isEmpty())) {
        return context;
    }
    try {
        URI uri = toUri(input);
        if (uri == null) {
            context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_URI, input));
        } else {
            Set<String> allowedSchemes = config.getStringSetOrDefault(KEY_ALLOWED_SCHEMES, DEFAULT_ALLOWED_SCHEMES);
            boolean allowFragment = config.getBooleanOrDefault(KEY_ALLOW_FRAGMENT, DEFAULT_ALLOW_FRAGMENT);
            boolean requireValidUrl = config.getBooleanOrDefault(KEY_REQUIRE_VALID_URL, DEFAULT_REQUIRE_VALID_URL);
            validateUri(uri, inputHint, context, allowedSchemes, allowFragment, requireValidUrl);
        }
    } catch (MalformedURLException | IllegalArgumentException | URISyntaxException e) {
        context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_URI, input, e.getMessage()));
    }
    return context;
}
Also used : MalformedURLException(java.net.MalformedURLException) ValidationError(org.keycloak.validate.ValidationError) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 9 with ValidationError

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

the class AbstractNumberValidator method doValidate.

@Override
protected void doValidate(Object value, String inputHint, ValidationContext context, ValidatorConfig config) {
    if (config == null || config.isEmpty()) {
        config = defaultConfig;
    }
    Number number = null;
    if (value != null) {
        try {
            number = convert(value, config);
        } catch (NumberFormatException ignore) {
        // N/A
        }
    }
    if (number == null) {
        context.addError(new ValidationError(getId(), inputHint, MESSAGE_INVALID_NUMBER));
        return;
    }
    Number min = getMinMaxConfig(config, KEY_MIN);
    Number max = getMinMaxConfig(config, KEY_MAX);
    if (min != null && isFirstGreaterThanToSecond(min, number)) {
        context.addError(new ValidationError(getId(), inputHint, selectRangeErrorMessage(config), min, max));
        return;
    }
    if (max != null && isFirstGreaterThanToSecond(number, max)) {
        context.addError(new ValidationError(getId(), inputHint, selectRangeErrorMessage(config), min, max));
        return;
    }
    return;
}
Also used : ValidationError(org.keycloak.validate.ValidationError)

Example 10 with ValidationError

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

the class LengthValidator method validateConfig.

@Override
public ValidationResult validateConfig(KeycloakSession session, ValidatorConfig config) {
    Set<ValidationError> errors = new LinkedHashSet<>();
    if (config == null || config == ValidatorConfig.EMPTY) {
        errors.add(new ValidationError(ID, KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
        errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
    } else {
        if (config.containsKey(KEY_TRIM_DISABLED) && (config.getBoolean(KEY_TRIM_DISABLED) == null)) {
            errors.add(new ValidationError(ID, KEY_TRIM_DISABLED, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_BOOLEAN_VALUE, config.get(KEY_TRIM_DISABLED)));
        }
        boolean containsMin = config.containsKey(KEY_MIN);
        boolean containsMax = config.containsKey(KEY_MAX);
        if (!(containsMin || containsMax)) {
            errors.add(new ValidationError(ID, KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
            errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_MISSING_VALUE));
        } else {
            if (containsMin && config.getInt(KEY_MIN) == null) {
                errors.add(new ValidationError(ID, KEY_MIN, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MIN)));
            }
            if (containsMax && config.getInt(KEY_MAX) == null) {
                errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_NUMBER_VALUE, config.get(KEY_MAX)));
            }
            if (errors.isEmpty() && containsMin && containsMax && (config.getInt(KEY_MIN) > config.getInt(KEY_MAX))) {
                errors.add(new ValidationError(ID, KEY_MAX, ValidatorConfigValidator.MESSAGE_CONFIG_INVALID_VALUE));
            }
        }
    }
    return new ValidationResult(errors);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ValidationError(org.keycloak.validate.ValidationError) ValidationResult(org.keycloak.validate.ValidationResult)

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