Search in sources :

Example 1 with ValidationError

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

the class DefaultAttributes method validate.

@Override
public boolean validate(String name, Consumer<ValidationError>... listeners) {
    Entry<String, List<String>> attribute = createAttribute(name);
    List<AttributeMetadata> metadatas = new ArrayList<>();
    metadatas.addAll(Optional.ofNullable(this.metadataByAttribute.get(attribute.getKey())).map(Collections::singletonList).orElse(Collections.emptyList()));
    metadatas.addAll(Optional.ofNullable(this.metadataByAttribute.get(READ_ONLY_ATTRIBUTE_KEY)).map(Collections::singletonList).orElse(Collections.emptyList()));
    Boolean result = null;
    for (AttributeMetadata metadata : metadatas) {
        AttributeContext attributeContext = createAttributeContext(attribute, metadata);
        for (AttributeValidatorMetadata validator : metadata.getValidators()) {
            ValidationContext vc = validator.validate(attributeContext);
            if (vc.isValid()) {
                continue;
            }
            if (result == null) {
                result = false;
            }
            if (listeners != null) {
                for (ValidationError error : vc.getErrors()) {
                    for (Consumer<ValidationError> consumer : listeners) {
                        consumer.accept(error);
                    }
                }
            }
        }
    }
    return result == null;
}
Also used : ArrayList(java.util.ArrayList) ValidationContext(org.keycloak.validate.ValidationContext) ArrayList(java.util.ArrayList) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError) Collections(java.util.Collections)

Example 2 with ValidationError

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

the class BrokeringFederatedUsernameHasValueValidator method validate.

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

Example 3 with ValidationError

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

the class ImmutableAttributeValidator method validate.

@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
    UserProfileAttributeValidationContext ac = (UserProfileAttributeValidationContext) context;
    AttributeContext attributeContext = ac.getAttributeContext();
    if (!isReadOnly(attributeContext)) {
        return context;
    }
    UserModel user = attributeContext.getUser();
    if (user == null) {
        return context;
    }
    List<String> currentValue = user.getAttributeStream(inputHint).collect(Collectors.toList());
    List<String> values = (List<String>) input;
    if (!CollectionUtil.collectionEquals(currentValue, values)) {
        if (currentValue.isEmpty() && !notBlankValidator().validate(values).isValid()) {
            return context;
        }
        context.addError(new ValidationError(ID, inputHint, DEFAULT_ERROR_MESSAGE));
    }
    return context;
}
Also used : UserModel(org.keycloak.models.UserModel) AttributeContext(org.keycloak.userprofile.AttributeContext) UserProfileAttributeValidationContext(org.keycloak.userprofile.UserProfileAttributeValidationContext) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 4 with ValidationError

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

the class UsernameMutationValidator 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;
    }
    AttributeContext attributeContext = UserProfileAttributeValidationContext.from(context).getAttributeContext();
    UserModel user = attributeContext.getUser();
    RealmModel realm = context.getSession().getContext().getRealm();
    if (!realm.isEditUsernameAllowed() && user != null && !value.equals(user.getFirstAttribute(UserModel.USERNAME))) {
        if (realm.isRegistrationEmailAsUsername() && UserProfileContext.UPDATE_PROFILE.equals(attributeContext.getContext())) {
            // it is expected that username changes when attributes are normalized by the provider
            return context;
        }
        context.addError(new ValidationError(ID, inputHint, Messages.READ_ONLY_USERNAME));
    }
    return context;
}
Also used : UserModel(org.keycloak.models.UserModel) RealmModel(org.keycloak.models.RealmModel) AttributeContext(org.keycloak.userprofile.AttributeContext) List(java.util.List) ValidationError(org.keycloak.validate.ValidationError)

Example 5 with ValidationError

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

the class LocalDateValidator method doValidate.

@Override
protected void doValidate(String value, String inputHint, ValidationContext context, ValidatorConfig config) {
    UserModel user = (UserModel) context.getAttributes().get(UserModel.class.getName());
    KeycloakSession session = context.getSession();
    KeycloakContext keycloakContext = session.getContext();
    Locale locale = keycloakContext.resolveLocale(user);
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    formatter.setLenient(false);
    try {
        formatter.parse(value);
    } catch (ParseException e) {
        context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_DATE));
    }
}
Also used : UserModel(org.keycloak.models.UserModel) Locale(java.util.Locale) KeycloakContext(org.keycloak.models.KeycloakContext) KeycloakSession(org.keycloak.models.KeycloakSession) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) 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