Search in sources :

Example 1 with ValidationContext

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

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

the class DefaultAttributes method isReadOnlyInternalAttribute.

/**
 * <p>Returns whether an attribute is read only based on the provider configuration (using provider config),
 * usually related to internal attributes managed by the server.
 *
 * <p>For user-defined attributes, it should be preferable to use the user profile configuration.
 *
 * @param attributeName the attribute name
 * @return {@code true} if the attribute is readonly. Otherwise, returns {@code false}
 */
protected boolean isReadOnlyInternalAttribute(String attributeName) {
    // read-only can be configured through the provider so we try to validate global validations
    AttributeMetadata readonlyMetadata = metadataByAttribute.get(READ_ONLY_ATTRIBUTE_KEY);
    if (readonlyMetadata == null) {
        return false;
    }
    AttributeContext attributeContext = createAttributeContext(attributeName, readonlyMetadata);
    for (AttributeValidatorMetadata validator : readonlyMetadata.getValidators()) {
        ValidationContext vc = validator.validate(attributeContext);
        if (!vc.isValid()) {
            return true;
        }
    }
    return false;
}
Also used : ValidationContext(org.keycloak.validate.ValidationContext)

Example 3 with ValidationContext

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

the class ValidatorTest method testDateValidator.

private static void testDateValidator(KeycloakSession session) {
    assertTrue(Validators.dateValidator().validate(null, new ValidationContext(session)).isValid());
    assertTrue(Validators.dateValidator().validate("", new ValidationContext(session)).isValid());
    // defaults to Locale.ENGLISH as per default locale selector
    assertFalse(Validators.dateValidator().validate("13/12/2021", new ValidationContext(session)).isValid());
    assertFalse(Validators.dateValidator().validate("13/12/21", new ValidationContext(session)).isValid());
    assertTrue(Validators.dateValidator().validate("12/13/2021", new ValidationContext(session)).isValid());
    RealmModel realm = session.getContext().getRealm();
    realm.setInternationalizationEnabled(true);
    realm.setDefaultLocale(Locale.FRANCE.getLanguage());
    assertTrue(Validators.dateValidator().validate("13/12/21", new ValidationContext(session)).isValid());
    assertTrue(Validators.dateValidator().validate("13/12/2021", new ValidationContext(session)).isValid());
    assertFalse(Validators.dateValidator().validate("12/13/2021", new ValidationContext(session)).isValid());
    UserModel alice = session.users().getUserByUsername(realm, "alice");
    alice.setAttribute(UserModel.LOCALE, Collections.singletonList(Locale.ENGLISH.getLanguage()));
    ValidationContext context = new ValidationContext(session);
    context.getAttributes().put(UserModel.class.getName(), alice);
    assertFalse(Validators.dateValidator().validate("13/12/2021", context).isValid());
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ValidationContext(org.keycloak.validate.ValidationContext)

Aggregations

ValidationContext (org.keycloak.validate.ValidationContext)3 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 RealmModel (org.keycloak.models.RealmModel)1 UserModel (org.keycloak.models.UserModel)1 ValidationError (org.keycloak.validate.ValidationError)1