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