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;
}
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;
}
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());
}
Aggregations