Difference between revisions of "Create password validator"

From OpenKM Documentation
Jump to: navigation, search
Line 1: Line 1:
 
You can create your own password validation in OpenKM. By default OpenKM has password validation disabled and there's used the '''NoPasswordValidator''' class. Also there's some default OpenKM password validation class called '''CompletePasswordValidator''' class.  
 
You can create your own password validation in OpenKM. By default OpenKM has password validation disabled and there's used the '''NoPasswordValidator''' class. Also there's some default OpenKM password validation class called '''CompletePasswordValidator''' class.  
 
  
 
Enabling '''CompletePasswordValidator''' class can configure minimum and maximum password length, number of lowercase and uppercase characters, number of numeric characters and number of special characters that appearing in password. You can see more information at [[Application_configuration]]
 
Enabling '''CompletePasswordValidator''' class can configure minimum and maximum password length, number of lowercase and uppercase characters, number of numeric characters and number of special characters that appearing in password. You can see more information at [[Application_configuration]]
 
  
 
You can also create your own password class that must implements the PasswordValidator interface:
 
You can also create your own password class that must implements the PasswordValidator interface:
Line 10: Line 8:
 
public interface PasswordValidator {
 
public interface PasswordValidator {
 
 
/**
+
  /**
* Validate
+
  * Validate
*  
+
  *  
* @param password
+
  * @param password
* @throws ValidatorException
+
  * @throws ValidatorException
*/
+
  */
public void Validate(String password) throws ValidatorException;
+
  public void Validate(String password) throws ValidatorException;
 
 
 
}
 
}
 
</source>
 
</source>
 
  
 
'''CompletePasswordValidator''' class:
 
'''CompletePasswordValidator''' class:
Line 28: Line 25:
 
  */
 
  */
 
public class CompletePasswordValidator implements PasswordValidator {
 
public class CompletePasswordValidator implements PasswordValidator {
@SuppressWarnings("unused")
+
  @SuppressWarnings("unused")
private static Logger log = LoggerFactory.getLogger(CompletePasswordValidator.class);
+
  private static Logger log = LoggerFactory.getLogger(CompletePasswordValidator.class);
+
 
@Override
+
  @Override
public void Validate(String password) throws ValidatorException {
+
  public void Validate(String password) throws ValidatorException {
validateLength(password);
+
    validateLength(password);
checkLowerCase(password);
+
    checkLowerCase(password);
checkUpperCase(password);
+
    checkUpperCase(password);
checkDigits(password);
+
    checkDigits(password);
checkSpecial(password);
+
    checkSpecial(password);
}
+
  }
 +
 
 +
  /**
 +
  * Validate length
 +
  */
 +
  private void validateLength(String password) throws ValidatorException {
 +
    if (Config.VALIDATOR_PASSWORD_MIN_LENGTH > 0 &&
 +
        password.length() < Config.VALIDATOR_PASSWORD_MIN_LENGTH) {
 +
      throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_LENGTH);
 +
    }
 +
 
 +
    if (Config.VALIDATOR_PASSWORD_MAX_LENGTH > 0 &&
 +
        password.length() > Config.VALIDATOR_PASSWORD_MAX_LENGTH) {
 +
      throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MAX_LENGTH);
 +
    }
 +
  }
 +
 
 +
  /**
 +
  * Validate lowercase characters
 +
  */
 +
  private void checkLowerCase(String password) throws ValidatorException {
 +
    int count = 0;
 +
 
 +
    if (Config.VALIDATOR_PASSWORD_MIN_LOWERCASE > 0) {
 +
      for (int i=0; i<password.length(); i++) {
 +
        if (Character.isLowerCase(password.charAt(i))) {
 +
          count ++;
 +
        }
 +
      }
 +
 
 +
      if (Config.VALIDATOR_PASSWORD_MIN_LOWERCASE > count) {
 +
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_LOWERCASE);
 +
      }
 +
    }
 +
  }
 +
 
 +
  /**
 +
  * Validate uppercase characters
 +
  */
 +
  private void checkUpperCase(String password) throws ValidatorException {
 +
    int count = 0;
 +
 
 +
    if (Config.VALIDATOR_PASSWORD_MIN_UPPERCASE > 0) {
 +
      for (int i=0; i<password.length(); i++) {
 +
        if (Character.isUpperCase(password.charAt(i))) {
 +
          count ++;
 +
        }
 +
      }
 +
 
 +
      if (Config.VALIDATOR_PASSWORD_MIN_UPPERCASE > count) {
 +
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_UPPERCASE);
 +
      }
 +
    }
 +
  }
 
 
/**
+
  /**
* validateLength
+
  * Validate digits
*
+
  */
* @throws AuthException
+
  private void checkDigits(String password) throws ValidatorException {
*/
+
    int count = 0;
private void validateLength(String password) throws ValidatorException {
+
 
if (Config.VALIDATOR_PASSWORD_MIN_LENGTH > 0 &&
+
    if (Config.VALIDATOR_PASSWORD_MIN_DIGITS > 0) {
password.length() < Config.VALIDATOR_PASSWORD_MIN_LENGTH) {
+
      for (int i=0; i<password.length(); i++) {
throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_LENGTH);
+
        if (Character.isDigit(password.charAt(i))) {
}
+
          count ++;
+
        }
if (Config.VALIDATOR_PASSWORD_MAX_LENGTH > 0 &&
+
      }
password.length() > Config.VALIDATOR_PASSWORD_MAX_LENGTH) {
+
 
throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MAX_LENGTH);
+
      if (Config.VALIDATOR_PASSWORD_MIN_DIGITS > count) {
}
+
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_DIGITS);  
}
+
      }
+
    }
/**
+
  }
* Validate lowercase characters
+
 
*/
+
  /**
private void checkLowerCase(String password) throws ValidatorException {
+
  * Validate special characters
int count = 0;
+
  */
+
  private void checkSpecial(String password) throws ValidatorException {
if (Config.VALIDATOR_PASSWORD_MIN_LOWERCASE > 0) {
+
    int count = 0;
for (int i=0; i<password.length(); i++) {
+
 
if (Character.isLowerCase(password.charAt(i))) {
+
    if (Config.VALIDATOR_PASSWORD_MIN_SPECIAL > 0) {
count ++;
+
      for (int i=0; i<password.length(); i++) {
}
+
        if (!Character.isLetterOrDigit(password.charAt(i)) &&
}
+
    !Character.isWhitespace(password.charAt(i))) {
+
          count ++;
if (Config.VALIDATOR_PASSWORD_MIN_LOWERCASE > count) {
+
        }
throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_LOWERCASE);
+
      }
}
+
 
}
+
      if (Config.VALIDATOR_PASSWORD_MIN_SPECIAL > count) {
}
+
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_SPECIAL);  
+
      }
/**
+
    }
* Validate uppercase characters
+
  }
*/
 
private void checkUpperCase(String password) throws ValidatorException {
 
int count = 0;
 
 
if (Config.VALIDATOR_PASSWORD_MIN_UPPERCASE > 0) {
 
for (int i=0; i<password.length(); i++) {
 
if (Character.isUpperCase(password.charAt(i))) {
 
count ++;
 
}
 
}
 
 
if (Config.VALIDATOR_PASSWORD_MIN_UPPERCASE > count) {
 
throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_UPPERCASE);
 
}
 
}
 
}
 
 
/**
 
* Validate digits
 
*/
 
private void checkDigits(String password) throws ValidatorException {
 
int count = 0;
 
 
if (Config.VALIDATOR_PASSWORD_MIN_DIGITS > 0) {
 
for (int i=0; i<password.length(); i++) {
 
if (Character.isDigit(password.charAt(i))) {
 
count ++;
 
}
 
}
 
 
if (Config.VALIDATOR_PASSWORD_MIN_DIGITS > count) {
 
throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_DIGITS);  
 
}
 
}
 
}
 
 
/**
 
* Validate special characters
 
*/
 
private void checkSpecial(String password) throws ValidatorException {
 
int count = 0;
 
 
if (Config.VALIDATOR_PASSWORD_MIN_SPECIAL > 0) {
 
for (int i=0; i<password.length(); i++) {
 
if (!Character.isLetterOrDigit(password.charAt(i)) &&
 
!Character.isWhitespace(password.charAt(i))) {
 
count ++;
 
}
 
}
 
 
if (Config.VALIDATOR_PASSWORD_MIN_SPECIAL > count) {
 
throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_SPECIAL);  
 
}
 
}
 
}
 
 
}
 
}
 
</source>
 
</source>

Revision as of 16:33, 17 May 2010

You can create your own password validation in OpenKM. By default OpenKM has password validation disabled and there's used the NoPasswordValidator class. Also there's some default OpenKM password validation class called CompletePasswordValidator class.

Enabling CompletePasswordValidator class can configure minimum and maximum password length, number of lowercase and uppercase characters, number of numeric characters and number of special characters that appearing in password. You can see more information at Application_configuration

You can also create your own password class that must implements the PasswordValidator interface:

public interface PasswordValidator {
	
  /**
   * Validate
   * 
   * @param password
   * @throws ValidatorException
   */
  public void Validate(String password) throws ValidatorException;
	
}

CompletePasswordValidator class:

/**
 * Complex password validator
 */
public class CompletePasswordValidator implements PasswordValidator {
  @SuppressWarnings("unused")
  private static Logger log = LoggerFactory.getLogger(CompletePasswordValidator.class);

  @Override
  public void Validate(String password) throws ValidatorException {
    validateLength(password);
    checkLowerCase(password);
    checkUpperCase(password);
    checkDigits(password);
    checkSpecial(password);
  }

  /**
   * Validate length
   */
  private void validateLength(String password) throws ValidatorException {
    if (Config.VALIDATOR_PASSWORD_MIN_LENGTH > 0 && 
        password.length() < Config.VALIDATOR_PASSWORD_MIN_LENGTH) {
      throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_LENGTH); 
    }

    if (Config.VALIDATOR_PASSWORD_MAX_LENGTH > 0 && 
        password.length() > Config.VALIDATOR_PASSWORD_MAX_LENGTH) {
      throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MAX_LENGTH); 
    }
  }

  /**
   * Validate lowercase characters
   */
  private void checkLowerCase(String password) throws ValidatorException {
    int count = 0;

    if (Config.VALIDATOR_PASSWORD_MIN_LOWERCASE > 0) {
      for (int i=0; i<password.length(); i++) {
        if (Character.isLowerCase(password.charAt(i))) {
          count ++;
        }
      }

      if (Config.VALIDATOR_PASSWORD_MIN_LOWERCASE > count) {
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_LOWERCASE); 
      }
    }
  }

  /**
   * Validate uppercase characters
   */
  private void checkUpperCase(String password) throws ValidatorException {
    int count = 0;

    if (Config.VALIDATOR_PASSWORD_MIN_UPPERCASE > 0) {
      for (int i=0; i<password.length(); i++) {
        if (Character.isUpperCase(password.charAt(i))) {
          count ++;
        }
      }

      if (Config.VALIDATOR_PASSWORD_MIN_UPPERCASE > count) {
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_UPPERCASE); 
      }
    }
  }
	
  /**
   * Validate digits
   */
  private void checkDigits(String password) throws ValidatorException {
    int count = 0;

    if (Config.VALIDATOR_PASSWORD_MIN_DIGITS > 0) {
      for (int i=0; i<password.length(); i++) {
        if (Character.isDigit(password.charAt(i))) {
          count ++;
        }
      }

      if (Config.VALIDATOR_PASSWORD_MIN_DIGITS > count) {
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_DIGITS); 
      }
    }
  }

  /**
   * Validate special characters
   */
  private void checkSpecial(String password) throws ValidatorException {
    int count = 0;

    if (Config.VALIDATOR_PASSWORD_MIN_SPECIAL > 0) {
      for (int i=0; i<password.length(); i++) {
        if (!Character.isLetterOrDigit(password.charAt(i)) &&
	    !Character.isWhitespace(password.charAt(i))) {
          count ++;
        }
      }

      if (Config.VALIDATOR_PASSWORD_MIN_SPECIAL > count) {
        throw new ValidatorException(Config.VALIDATOR_PASSWORD_ERROR_MIN_SPECIAL); 
      }
    }
  }
}