use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class RegistrationEmailAsUsernameEmailValueValidator method validate.
@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
RealmModel realm = context.getSession().getContext().getRealm();
if (!realm.isRegistrationEmailAsUsername()) {
return context;
}
@SuppressWarnings("unchecked") List<String> values = (List<String>) input;
if (values == null || values.isEmpty()) {
return context;
}
String value = values.get(0);
if (!(Validation.isBlank(value) || Validation.isEmailValid(value))) {
context.addError(new ValidationError(ID, inputHint, Messages.INVALID_EMAIL));
}
return context;
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class RegistrationUsernameExistsValidator method validate.
@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
KeycloakSession session = context.getSession();
RealmModel realm = session.getContext().getRealm();
if (realm.isRegistrationEmailAsUsername()) {
return context;
}
@SuppressWarnings("unchecked") List<String> values = (List<String>) input;
if (values == null || values.isEmpty()) {
return context;
}
String value = values.get(0);
UserModel existing = session.users().getUserByUsername(realm, value);
if (existing != null) {
context.addError(new ValidationError(ID, inputHint, Messages.USERNAME_EXISTS).setStatusCode(Response.Status.CONFLICT));
}
return context;
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class AttributeRequiredByMetadataValidator method validate.
@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
AttributeContext attContext = UserProfileAttributeValidationContext.from(context).getAttributeContext();
AttributeMetadata metadata = attContext.getMetadata();
if (!metadata.isRequired(attContext)) {
return context;
}
if (metadata.isReadOnly(attContext)) {
return context;
}
@SuppressWarnings("unchecked") List<String> values = (List<String>) input;
if (values == null || values.isEmpty()) {
context.addError(new ValidationError(ID, inputHint, ERROR_USER_ATTRIBUTE_REQUIRED));
} else {
for (String value : values) {
if (Validation.isBlank(value)) {
context.addError(new ValidationError(ID, inputHint, ERROR_USER_ATTRIBUTE_REQUIRED));
return context;
}
}
}
return context;
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class BlankAttributeValidator method validate.
@Override
public ValidationContext validate(Object input, String inputHint, ValidationContext context, ValidatorConfig config) {
@SuppressWarnings("unchecked") List<String> values = (List<String>) input;
boolean failOnNull = config.getBooleanOrDefault(CFG_FAIL_ON_NULL, false);
if (values.isEmpty() && !failOnNull) {
return context;
}
String value = values.isEmpty() ? null : values.get(0);
if ((failOnNull || value != null) && Validation.isBlank(value)) {
context.addError(new ValidationError(ID, inputHint, config.getStringOrDefault(CFG_ERROR_MESSAGE, AttributeRequiredByMetadataValidator.ERROR_USER_ATTRIBUTE_REQUIRED)));
}
return context;
}
use of org.keycloak.validate.ValidationError in project keycloak by keycloak.
the class DuplicateUsernameValidator 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;
KeycloakSession session = context.getSession();
UserModel existing = session.users().getUserByUsername(session.getContext().getRealm(), value);
UserModel user = UserProfileAttributeValidationContext.from(context).getAttributeContext().getUser();
if (user != null && !value.equals(user.getFirstAttribute(UserModel.USERNAME)) && (existing != null && !existing.getId().equals(user.getId()))) {
context.addError(new ValidationError(ID, inputHint, Messages.USERNAME_EXISTS).setStatusCode(Response.Status.CONFLICT));
}
return context;
}
Aggregations