DirContext dirContext;
try {
dirContext = this.connectionSource.getContext();
} catch (UserStoreException e) {
throw new DirectoryServerManagerException("Unable to retrieve directory connection.", e);
}
//first search the existing user entry.
String searchBase = this.realmConfiguration.getUserStoreProperty(LDAPConstants.USER_SEARCH_BASE);
String searchFilter = getServicePrincipleFilter(serverName);
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchControls.setReturningAttributes(new String[]{LDAPServerManagerConstants.LDAP_PASSWORD});
try {
NamingEnumeration<SearchResult> namingEnumeration = dirContext
.search(searchBase, searchFilter, searchControls);
// here we assume only one user
while (namingEnumeration.hasMore()) {
SearchResult searchResult = namingEnumeration.next();
Attributes attributes = searchResult.getAttributes();
Attribute userPassword = attributes.get(LDAPServerManagerConstants.LDAP_PASSWORD);
NamingEnumeration passwords = userPassword.getAll();
String passwordHashMethod = null;
if (passwords.hasMore()) {
byte[] byteArray = (byte[]) passwords.next();
String password = new String(byteArray);
if (password.startsWith("{")) {
passwordHashMethod = password.substring(password.indexOf("{") + 1, password.indexOf("}"));
}
return password.equals(getPasswordToStore((String) existingCredentials, passwordHashMethod));
}
}
} catch (NamingException e) {
log.error("Failed, validating password. " +
"Can not access the directory service", e);
throw new DirectoryServerManagerException("Failed, validating password. " +
"Can not access the directory service", e);
} finally {
try {
JNDIUtil.closeContext(dirContext);
} catch (UserStoreException e) {