try {
List<HashMap<String, Object>> users = db.select(query, sqlIdentity);
if (users.size() == 0) {
LOGGER.warning("User not found in the database ["
+ username + "] domain [" + domain + "]");
return new AuthenticationResponse(false, "", null);
} else if (users.size() > 1) {
StringBuffer sb = new StringBuffer("Multiple users found in the "
+ "database matching [" + domain + "]\\[" + username + "]: ");
for (HashMap<String, Object> u : users) {
sb.append("[").append(u.get("dn")).append("] ");
}
LOGGER.warning(sb.toString());
return new AuthenticationResponse(false, "", null);
}
HashMap<String, Object>user = users.get(0);
List<Principal> groups =
getAllGroupsForTheUser((Number) user.get(AdConstants.DB_ENTITYID));
if (password != null && !authenticateUser(
(String) user.get(AdConstants.DB_DNSROOT),
(String) user.get(AdConstants.DB_NETBIOSNAME)
+ AdConstants.BACKSLASH
+ (String) user.get(AdConstants.DB_SAMACCOUNTNAME),
password)) {
return new AuthenticationResponse(false, "", null);
}
if (LOGGER.isLoggable(Level.INFO)) {
StringBuffer sb = new StringBuffer("Resolved ").append(groups.size())
.append(" AD group(s) for user [").append(username).append("]")
.append(" domain [").append(domain).append("]: ");
for (Principal group : groups) {
sb.append("[").append(group.getName()).append("] ");
}
LOGGER.info(sb.toString());
}
if (identity instanceof MutableIdentity) {
MutableIdentity mutable = (MutableIdentity) identity;
mutable.setDomain((String) user.get(AdConstants.DB_NETBIOSNAME));
mutable.setUsername((String) user.get(AdConstants.DB_SAMACCOUNTNAME));
LOGGER.fine("New identity: [" + domain + "\\" + username
+ "] Active Directory: [" + identity.getDomain()
+ "\\" + identity.getUsername() + "]");
}
LOGGER.log(Level.INFO, "Elapsed time for Active Directory authentication "
+ "of user [{0}\\{1}] = [{2}ms]", new Object[] {domain, username,
System.currentTimeMillis() - startAuthN});
return new AuthenticationResponse(true, "", groups);
} catch (SQLException e) {
LOGGER.log(Level.WARNING,
"Failed to retrieve information about user from database ["
+ username + "] domain [" + domain + "].", e);
return new AuthenticationResponse(false, "", null);
}
}