/**
   * Contains dictionary words.
   *
   * @param passWord the pass word
   * @return true, if successful
   * @throws FileNotFoundException the file not found exception
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static boolean containsDictionaryWords(String passWord)
      throws FileNotFoundException, IOException {

    String fileName = "en_US.dic";
    URL dicUrl = new ResourceLoader().getResourceAsURL(fileName);
    if (null == dicUrl) {
      throw new FileNotFoundException(fileName + " doesnot exists in /WEB-INF/classes location");
    }
    AbstractWordList awl =
        WordLists.createFromReader(
            new FileReader[] {new FileReader(dicUrl.getFile())}, false, new ArraysSort());
    // 	create a dictionary for searching
    WordListDictionary dict = new WordListDictionary(awl);
    DictionarySubstringRule dictRule = new DictionarySubstringRule(dict);
    dictRule.setWordLength(8); // size of words to check in the password
    List<Rule> ruleList = new ArrayList<Rule>();
    ruleList.add(dictRule);

    PasswordValidator validator = new PasswordValidator(ruleList);
    PasswordData passwordData = new PasswordData(new Password(passWord));

    RuleResult result = validator.validate(passwordData);
    if (result.isValid()) {
      logger.info("Password Supplied Is Valid password");
    } else {
      logger.info("Password Supplied Is Invalid password:");
    }
    return result.isValid();
  }
Exemplo n.º 2
0
  public void validate(String username, String newPassword) throws AdempiereException {

    ArrayList<Rule> ruleList = new ArrayList<Rule>();

    if (getMinLength() > 0 || getMaxLength() > 0) {
      LengthRule lengthRule = new LengthRule();
      if (getMinLength() > 0) lengthRule.setMinimumLength(getMinLength());
      if (getMaxLength() > 0) lengthRule.setMaximumLength(getMaxLength());
      ruleList.add(lengthRule);
    }

    if (isWhitespace()) {
      ruleList.add(new WhitespaceRule());
    }

    // control allowed characters
    CharacterCharacteristicsRule charRule = new CharacterCharacteristicsRule();
    int numValidations = 0;
    if (getDigitCharacter() > 0) {
      // require at least n digit in passwords
      numValidations++;
      charRule.getRules().add(new DigitCharacterRule(getDigitCharacter()));
    }
    if (getNonAlphaNumericCharacter() > 0) {
      // require at least n non-alphanumeric char
      numValidations++;
      charRule.getRules().add(new NonAlphanumericCharacterRule(getNonAlphaNumericCharacter()));
    }
    if (getUppercaseCharacter() > 0) {
      numValidations++;
      charRule.getRules().add(new UppercaseCharacterRule(getUppercaseCharacter()));
    }
    if (getLowercaseCharacter() > 0) {
      numValidations++;
      charRule.getRules().add(new LowercaseCharacterRule(getLowercaseCharacter()));
    }
    if (getAlphabeticalCharacter() > 0) {
      numValidations++;
      charRule.getRules().add(new AlphabeticalCharacterRule(getAlphabeticalCharacter()));
    }
    if (!charRule.getRules().isEmpty()) {
      charRule.setNumberOfCharacteristics(numValidations);
      ruleList.add(charRule);
    }

    if (getAlphabeticalSequence() > 0) {
      ruleList.add(new AlphabeticalSequenceRule(getAlphabeticalSequence(), true));
    }

    if (getNumericalSequence() > 0) {
      ruleList.add(new NumericalSequenceRule(getNumericalSequence(), true));
    }
    if (getQWERTYSequence() > 0) {
      ruleList.add(new QwertySequenceRule(getQWERTYSequence(), true));
    }

    if (getRepeatCharacterRegex() > 0) {
      ruleList.add(new RepeatCharacterRegexRule(getRepeatCharacterRegex()));
    }

    if (isUserNameRule()) {
      ruleList.add(new UsernameRule(true, true));
    }

    if (isUsingDictionary()) {
      if (getPathDictionary().length() > 0) {
        try {
          ArrayWordList awl =
              WordLists.createFromReader(
                  new FileReader[] {new FileReader(getPathDictionary())}, true, new ArraysSort());

          WordListDictionary dict = new WordListDictionary(awl);
          DictionarySubstringRule dictRule = new DictionarySubstringRule(dict);

          if (getDictWordLength() > 0) {
            dictRule.setWordLength(
                getDictWordLength()); // size of words to check in the password
          } else {
            dictRule.setWordLength(DictionarySubstringRule.DEFAULT_WORD_LENGTH);
          }

          if (isDictMatchBackwards()) {
            dictRule.setMatchBackwards(true); // match dictionary words backwards
          }
          ruleList.add(dictRule);

        } catch (FileNotFoundException e) {
          throw new AdempiereException("Could not find dictionary file: " + e.getMessage());
        } catch (IOException e) {
          throw new AdempiereException("Could not find dictionary file: " + e.getMessage());
        }
      }
    }

    if (!ruleList.isEmpty()) {
      PasswordValidator validator = new PasswordValidator(getCustomResolver(), ruleList);
      PasswordData passwordData = new PasswordData(new Password(newPassword));
      passwordData.setUsername(username);
      RuleResult result = validator.validate(passwordData);
      if (!result.isValid()) {
        StringBuilder error = new StringBuilder(Msg.getMsg(getCtx(), "PasswordErrors"));
        error.append(": [");
        for (String msg : validator.getMessages(result)) {
          error.append(" ").append(msg);
        }
        error.append(" ]");
        throw new AdempiereException(error.toString());
      }
    }
  }