com.bernardomg.validation.constraint.PasswordConstraintValidator Maven / Gradle / Ivy
/**
* The MIT License (MIT)
*
* Copyright (c) 2023-2024-2024 the original author or authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.bernardomg.validation.constraint;
import java.util.ArrayList;
import java.util.List;
import org.passay.CharacterRule;
import org.passay.EnglishCharacterData;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
import org.passay.WhitespaceRule;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
/**
* Validator for the {@link StrongPassword} annotation. Makes use of the Passay library to validate the password.
*/
public final class PasswordConstraintValidator implements ConstraintValidator {
/**
* Password validator to be used.
*/
private final PasswordValidator validator;
public PasswordConstraintValidator() {
super();
final List rules = new ArrayList<>();
// TODO: Make this configurable on annotation
// Rule 1: Password length should be in between
// 8 and 16 characters
rules.add(new LengthRule(8, 16));
// Rule 2: No whitespace allowed
rules.add(new WhitespaceRule());
// Rule 3.a: At least one Upper-case character
rules.add(new CharacterRule(EnglishCharacterData.UpperCase, 1));
// Rule 3.b: At least one Lower-case character
rules.add(new CharacterRule(EnglishCharacterData.LowerCase, 1));
// Rule 3.c: At least one digit
rules.add(new CharacterRule(EnglishCharacterData.Digit, 1));
// Rule 3.d: At least one special character
rules.add(new CharacterRule(EnglishCharacterData.Special, 1));
validator = new PasswordValidator(rules);
}
@Override
public final boolean isValid(final String password, final ConstraintValidatorContext context) {
final RuleResult result;
final boolean valid;
result = validator.validate(new PasswordData(password));
if (result.isValid()) {
valid = true;
} else {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(String.join(",", validator.getMessages(result)))
.addConstraintViolation();
valid = false;
}
return valid;
}
}