edu.vt.middleware.password.UsernameRule 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: UsernameRule.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.LinkedHashMap;
import java.util.Map;
/**
* Rule for determining if a password contains the username associated with that
* password.
*
* @author Middleware Services
* @version $Revision: 2704 $ $Date: 2013-04-24 17:30:32 -0400 (Wed, 24 Apr 2013) $
*/
public class UsernameRule implements Rule
{
/** Error code for matching username. */
public static final String ERROR_CODE = "ILLEGAL_USERNAME";
/** Error code for matching reversed dictionary word. */
public static final String ERROR_CODE_REVERSED = "ILLEGAL_USERNAME_REVERSED";
/** Whether to search for username backwards. */
private boolean matchBackwards;
/** Whether to ignore case when checking for usernames. */
private boolean ignoreCase;
/** Default constructor. */
public UsernameRule() {}
/**
* Create a new username rule.
*
* @param mb whether to match backwards
* @param ic whether to ignore case
*/
public UsernameRule(final boolean mb, final boolean ic)
{
setMatchBackwards(mb);
setIgnoreCase(ic);
}
/**
* Sets whether the verify method will search the password for the username
* spelled backwards as well as forwards.
*
* @param b whether to match username backwards
*/
public void setMatchBackwards(final boolean b)
{
matchBackwards = b;
}
/**
* Returns whether to match the username backwards.
*
* @return whether to match username backwards
*/
public boolean isMatchBackwards()
{
return matchBackwards;
}
/**
* Sets whether the verify method will ignore case when searching the for a
* username.
*
* @param b whether to ignore case
*/
public void setIgnoreCase(final boolean b)
{
ignoreCase = b;
}
/**
* Returns whether to ignore the case of the username.
*
* @return whether to ignore case
*/
public boolean isIgnoreCase()
{
return ignoreCase;
}
/** {@inheritDoc} */
@Override
public RuleResult validate(final PasswordData passwordData)
{
final RuleResult result = new RuleResult(true);
String text = passwordData.getPassword().getText();
String user = passwordData.getUsername();
String reverseUser = new StringBuilder(user).reverse().toString();
if (ignoreCase) {
text = text.toLowerCase();
user = user.toLowerCase();
reverseUser = reverseUser.toLowerCase();
}
if (text.contains(user)) {
result.setValid(false);
result.getDetails().add(
new RuleResultDetail(
ERROR_CODE,
createRuleResultDetailParameters(user)));
}
if (matchBackwards && text.contains(reverseUser)) {
result.setValid(false);
result.getDetails().add(
new RuleResultDetail(
ERROR_CODE_REVERSED,
createRuleResultDetailParameters(user)));
}
return result;
}
/**
* Creates the parameter data for the rule result detail.
*
* @param username matching username
*
* @return map of parameter name to value
*/
protected Map createRuleResultDetailParameters(
final String username)
{
final Map m = new LinkedHashMap();
m.put("username", username);
return m;
}
/** {@inheritDoc} */
@Override
public String toString()
{
return
String.format(
"%s@%h::ignoreCase=%s,matchBackwards=%s",
getClass().getName(),
hashCode(),
ignoreCase,
matchBackwards);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy