edu.vt.middleware.password.AllowedCharacterRule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vt-password Show documentation
Show all versions of vt-password Show documentation
Library for checking that a password complies with a custom set of rules
The newest version!
/*
$Id: AllowedCharacterRule.java 2704 2013-04-24 21:30:32Z dfisher $
Copyright (C) 2003-2013 Virginia Tech.
All rights reserved.
SEE LICENSE FOR MORE INFORMATION
Author: Middleware Services
Email: [email protected]
Version: $Revision: 2704 $
Updated: $Date: 2013-04-24 17:30:32 -0400 (Wed, 24 Apr 2013) $
*/
package edu.vt.middleware.password;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Rule for determining if a password contains allowed characters.
*
* @author Middleware Services
* @version $Revision: 2704 $ $Date: 2013-04-24 17:30:32 -0400 (Wed, 24 Apr 2013) $
*/
public class AllowedCharacterRule implements Rule
{
/** Error code for allowed character failures. */
public static final String ERROR_CODE = "ALLOWED_CHAR";
/** Stores the characters that are allowed. */
private final char[] allowedChar;
/**
* Create a new allowed character rule.
*
* @param c allowed characters
*/
public AllowedCharacterRule(final char[] c)
{
allowedChar = c;
Arrays.sort(allowedChar);
}
/** {@inheritDoc} */
@Override
public RuleResult validate(final PasswordData passwordData)
{
final RuleResult result = new RuleResult(true);
for (char c : passwordData.getPassword().getText().toCharArray()) {
if (Arrays.binarySearch(allowedChar, c) < 0) {
result.setValid(false);
result.getDetails().add(
new RuleResultDetail(
ERROR_CODE,
createRuleResultDetailParameters(c)));
break;
}
}
return result;
}
/**
* Creates the parameter data for the rule result detail.
*
* @param c illegal character
*
* @return map of parameter name to value
*/
protected Map createRuleResultDetailParameters(final char c)
{
final Map m = new LinkedHashMap();
m.put("illegalCharacter", c);
return m;
}
/** {@inheritDoc} */
@Override
public String toString()
{
return
String.format(
"%s@%h::allowedChar=%s",
getClass().getName(),
hashCode(),
allowedChar != null ? Arrays.toString(allowedChar) : null);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy