//~ Methods ================================================================
public LdapUserInfo authenticate(String username, String password) {
// locate the user and check the password
LdapUserInfo user = null;
DirContext ctx = getInitialDirContextFactory().newInitialDirContext();
Iterator dns = getUserDns(username).iterator();
try {
while(dns.hasNext() && user == null) {
String userDn = (String)dns.next();
String relativeName = LdapUtils.getRelativeName(userDn, ctx);
user = new LdapUserInfo(userDn,
ctx.getAttributes(relativeName, getUserAttributes()));
}
if (user == null && getUserSearch() != null) {
user = getUserSearch().searchForUser(username);
}
if (user == null) {
throw new UsernameNotFoundException(username);
}
Attribute passwordAttribute = user.getAttributes().get(passwordAttributeName);
if(passwordAttribute != null) {
Object retrievedPassword = passwordAttribute.get();
if (!(retrievedPassword instanceof String)) {
// Assume it's binary
retrievedPassword = new String((byte[])retrievedPassword);
}
if (!verifyPassword(password, (String)retrievedPassword)) {
throw new BadCredentialsException(messages.getMessage(
"PasswordComparisonAuthenticator.badCredentials",
"Bad credentials"));
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Password attribute " + passwordAttributeName
+ " wasn't retrieved for user " + username);
}
doPasswordCompare(ctx, user.getRelativeName(ctx), password);
}
return user;
} catch(NamingException ne) {
throw new BadCredentialsException("Authentication failed due to exception ", ne);