return;
}
ConnectionManager connectionManager = this.connectionManager.getValue();
String username = null;
VerifyPasswordCallback verifyPasswordCallback = null;
for (Callback current : callbacks) {
if (current instanceof NameCallback) {
username = ((NameCallback) current).getDefaultName();
} else if (current instanceof RealmCallback) {
// TODO - Nothing at the moment
} else if (current instanceof VerifyPasswordCallback) {
verifyPasswordCallback = (VerifyPasswordCallback) current;
} else {
throw new UnsupportedCallbackException(current);
}
}
if (username == null || username.length() == 0) {
throw MESSAGES.noUsername();
}
if (verifyPasswordCallback == null) {
throw MESSAGES.noPassword();
}
InitialDirContext searchContext = null;
InitialDirContext userContext = null;
NamingEnumeration<SearchResult> searchEnumeration = null;
try {
// 1 - Obtain Connection to LDAP
searchContext = (InitialDirContext) connectionManager.getConnection();
// 2 - Search to identify the DN of the user connecting
SearchControls searchControls = new SearchControls();
if (recursive) {
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else {
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
}
searchControls.setReturningAttributes(new String[]{userDn});
searchControls.setTimeLimit(searchTimeLimit);
Object[] filterArguments = new Object[]{username};
String filter = usernameAttribute != null ? "(" + usernameAttribute + "={0})" : advancedFilter;
searchEnumeration = searchContext.search(baseDn, filter, filterArguments, searchControls);
if (searchEnumeration.hasMore() == false) {
throw MESSAGES.userNotFoundInDirectory(username);
}
String distinguishedUserDN = null;
SearchResult result = searchEnumeration.next();
Attributes attributes = result.getAttributes();
if (attributes != null) {
Attribute dn = attributes.get(userDn);
if (dn != null) {
distinguishedUserDN = (String) dn.get();
}
}
if (distinguishedUserDN == null) {
if (result.isRelative() == true)
distinguishedUserDN = result.getName() + ("".equals(baseDn) ? "" : "," + baseDn);
else
throw MESSAGES.nameNotFound(result.getName());
}
// 3 - Connect as user once their DN is identified
userContext = (InitialDirContext) connectionManager.getConnection(distinguishedUserDN, verifyPasswordCallback.getPassword());
if (userContext != null) {
verifyPasswordCallback.setVerified(true);
}
} catch (Exception e) {
throw MESSAGES.cannotPerformVerification(e);
} finally {