Search in sources :

Example 1 with PasswordValidator

use of org.passay.PasswordValidator in project dataverse by IQSS.

the class PasswordValidatorServiceBean method addStandardValidator.

/**
 * standardValidator
 * <p>
 * Apply Rules 1, 2 and 3.
 */
private void addStandardValidator() {
    int maxLength = getMaxLength();
    int minLength = getMinLength();
    int numberOfCharacteristics = getNumberOfCharacteristics();
    int numberOfConsecutiveDigitsAllowed = getNumberOfConsecutiveDigitsAllowed();
    PasswordValidator passwordValidator = validators.get(ValidatorTypes.StandardValidator);
    if (passwordValidator == null) {
        final List<Rule> rules = new ArrayList<>(4);
        rules.add(dictionarySubstringRule());
        final LengthRule lengthRule = new LengthRule();
        if (maxLength != 0) {
            lengthRule.setMaximumLength(maxLength);
        }
        if (minLength != 0) {
            lengthRule.setMinimumLength(minLength);
        }
        rules.add(lengthRule);
        if (numberOfCharacteristics != 0) {
            rules.add(characterRule(getCharacterRules()));
        }
        rules.add(repeatingDigitsRule(numberOfConsecutiveDigitsAllowed));
        passwordValidator = new PasswordValidator(messageResolver, rules);
        validators.put(ValidatorTypes.StandardValidator, passwordValidator);
    }
}
Also used : LengthRule(org.passay.LengthRule) PasswordValidator(org.passay.PasswordValidator) ArrayList(java.util.ArrayList) DictionaryRule(org.passay.DictionaryRule) CharacterRule(org.passay.CharacterRule) IllegalRegexRule(org.passay.IllegalRegexRule) CharacterCharacteristicsRule(org.passay.CharacterCharacteristicsRule) Rule(org.passay.Rule) DictionarySubstringRule(org.passay.DictionarySubstringRule) LengthRule(org.passay.LengthRule)

Example 2 with PasswordValidator

use of org.passay.PasswordValidator in project dataverse by IQSS.

the class PasswordValidatorServiceBean method addGoodStrengthValidator.

/**
 * goodStrengthValidator
 * <p>
 * Apply Rule 4: It will forgo all the above three requirements for passwords that have a minimum length of
 * MIN_LENGTH_BIG_LENGTH.
 */
private void addGoodStrengthValidator() {
    int goodStrength = getGoodStrength();
    if (goodStrength != 0) {
        PasswordValidator passwordValidator = validators.get(ValidatorTypes.GoodStrengthValidator);
        if (passwordValidator == null) {
            final GoodStrengthRule lengthRule = new GoodStrengthRule();
            lengthRule.setMinimumLength(goodStrength);
            final List<Rule> rules = Collections.singletonList(lengthRule);
            passwordValidator = new PasswordValidator(messageResolver, rules);
            validators.put(ValidatorTypes.GoodStrengthValidator, passwordValidator);
        }
    }
}
Also used : PasswordValidator(org.passay.PasswordValidator) DictionaryRule(org.passay.DictionaryRule) CharacterRule(org.passay.CharacterRule) IllegalRegexRule(org.passay.IllegalRegexRule) CharacterCharacteristicsRule(org.passay.CharacterCharacteristicsRule) Rule(org.passay.Rule) DictionarySubstringRule(org.passay.DictionarySubstringRule) LengthRule(org.passay.LengthRule)

Example 3 with PasswordValidator

use of org.passay.PasswordValidator in project dataverse by IQSS.

the class PasswordValidatorServiceBean method validate.

/**
 * validate
 * <p>
 * Validates the password properties and its modification date and determine if their valid.
 *
 * @param passwordModificationTime The time the password was set or changed.
 * @param password                 The password to check
 * @param isHumanReadable          The expression of the error messages. True if the audience is human.
 * @return A List with error messages. Empty when the password is valid.
 */
public List<String> validate(String password, Date passwordModificationTime, boolean isHumanReadable) {
    // public List<String> validate(String password, boolean isHumanReadable) {
    init();
    final PasswordData passwordData = PasswordData.newInstance(password, String.valueOf(passwordModificationTime.getTime()), null);
    // final PasswordData passwordData = PasswordData.newInstance(password, "username", null);
    final RuleResult result = new RuleResult();
    for (PasswordValidator currentUser : validators.values()) {
        logger.fine("characterRules.size(): " + characterRules.size());
        logger.fine("numberOfCharacteristics: " + numberOfCharacteristics);
        RuleResult r = currentUser.validate(passwordData);
        if (r.isValid())
            return Collections.emptyList();
        result.getDetails().addAll(r.getDetails());
    }
    if (isHumanReadable) {
        return validators.get(ValidatorTypes.StandardValidator).getMessages(result);
    } else {
        return result.getDetails().stream().map(RuleResultDetail::getErrorCode).collect(Collectors.toList());
    }
}
Also used : PasswordData(org.passay.PasswordData) PasswordValidator(org.passay.PasswordValidator) RuleResult(org.passay.RuleResult)

Aggregations

PasswordValidator (org.passay.PasswordValidator)3 CharacterCharacteristicsRule (org.passay.CharacterCharacteristicsRule)2 CharacterRule (org.passay.CharacterRule)2 DictionaryRule (org.passay.DictionaryRule)2 DictionarySubstringRule (org.passay.DictionarySubstringRule)2 IllegalRegexRule (org.passay.IllegalRegexRule)2 LengthRule (org.passay.LengthRule)2 Rule (org.passay.Rule)2 ArrayList (java.util.ArrayList)1 PasswordData (org.passay.PasswordData)1 RuleResult (org.passay.RuleResult)1