/**
   * 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();
  }
  public JSONObject validatePwd(String pwd) {

    //        Properties props = new Properties();
    //        try{
    ////            InputStream msgProp = new FileInputStream("/messages.properties");
    //            InputStream msgProp =
    // PwdValidator.class.getClassLoader().getResourceAsStream("/messages.properties");
    //            props.load(msgProp);
    //        } catch (Exception e) {
    //                e.printStackTrace();
    //        }
    //        MessageResolver resolver = new MessageResolver(props);

    // password must be between 8 and 16 chars long
    LengthRule lengthRule = new LengthRule(6, 16);

    // control allowed characters
    CharacterCharacteristicsRule charRule = new CharacterCharacteristicsRule();
    // require at least 1 digit in passwords
    charRule.getRules().add(new DigitCharacterRule(1));
    // require at least 1 non-alphanumeric char
    charRule.getRules().add(new NonAlphanumericCharacterRule(1));
    // require at least 1 upper case char
    charRule.getRules().add(new UppercaseCharacterRule(1));
    // require at least 1 lower case char
    charRule.getRules().add(new LowercaseCharacterRule(1));
    // require at least 3 of the previous rules be met
    charRule.setNumberOfCharacteristics(4);

    // group all rules together in a List
    List<Rule> ruleList = new ArrayList<Rule>();
    ruleList.add(lengthRule);
    ruleList.add(charRule);

    PasswordValidator validator = new PasswordValidator(ruleList);
    //        PasswordValidator validator = new PasswordValidator(resolver, ruleList);
    PasswordData passwordData = new PasswordData(new Password(pwd));

    JSONObject msg = new JSONObject();
    try {
      msg.put("tooShort", false);
      msg.put("noCaps", false);
      msg.put("noSpecials", false);
      msg.put("noDigits", false);
    } catch (JSONException e) {
      e.printStackTrace();
    }

    RuleResult result = validator.validate(passwordData);
    if (result.isValid()) {
      return msg;
    } else {
      ListIterator<RuleResultDetail> resultDetailIterator = result.getDetails().listIterator();
      while (resultDetailIterator.hasNext()) {
        RuleResultDetail ruleResultDetail = resultDetailIterator.next();
        String errorCodeString = ruleResultDetail.getErrorCode();
        if (errorCodeString.equals("TOO_SHORT")) {
          try {
            msg.put("tooShort", true);
          } catch (JSONException e) {
            e.printStackTrace();
          }
        } else if (errorCodeString.equals("INSUFFICIENT_CHARACTERS")) {
          if (ruleResultDetail.getParameters().containsValue("digit")) {
            try {
              msg.put("noDigits", true);
            } catch (JSONException e) {
              e.printStackTrace();
            }
          } else if (ruleResultDetail.getParameters().containsValue("non-alphanumeric")) {
            try {
              msg.put("noSpecials", true);
            } catch (JSONException e) {
              e.printStackTrace();
            }
          } else if (ruleResultDetail.getParameters().containsValue("uppercase")) {
            try {
              msg.put("noCaps", true);
            } catch (JSONException e) {
              e.printStackTrace();
            }
          }
        }
      }
      return msg;
    }
  }
示例#3
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());
      }
    }
  }