String sTargetedGroupDN = "";
LdapUserProperties userProps = getConfiguration().getUserProperties();
// determine the authentication method
Credentials credentials = user.getCredentials();
UsernamePasswordCredentials upCredentials = null;
boolean bUseDirectConnect = false;
boolean bUseLoginPattern = false;
if (credentials != null) {
if (credentials instanceof UsernamePasswordCredentials) {
upCredentials = (UsernamePasswordCredentials)credentials;
upCredentials.setTargetedGroupDN("");
sUsername = upCredentials.getUsername();
String sPattern = userProps.getUsernameSearchPattern();
if (sUsername.length() > 0) {
if (userProps.hasSpecialDNCharacter(sUsername)) {
bUseDirectConnect = true;
} else {
bUseLoginPattern = (sPattern.length() > 0);
}
}
} else if (credentials instanceof DistinguishedNameCredential) {
DistinguishedNameCredential dnCredential;
dnCredential = (DistinguishedNameCredential)credentials;
sAuthenticatedDN = dnCredential.getDistinguishedName();
} else if (credentials instanceof UsernameCredential) {
UsernameCredential unCredential = (UsernameCredential)credentials;
String sBaseDN = userProps.getUserSearchDIT();
String sFilter = userProps.returnUserLoginSearchFilter(unCredential.getUsername());
StringSet ssDNs = getQueryFunctions().searchDNs(
getConnectedContext(),sBaseDN,sFilter);
if (ssDNs.size() > 1) {
throw new IdentityException("Multiple LDAP usernames matched for:"+ unCredential.getUsername());
} else if (ssDNs.size() == 1) {
sAuthenticatedDN = ssDNs.iterator().next();
}
}
}
// Attempt to connect with the supplied credentials.
// An AuthenticationException will be thrown if the credentials are invalid
if (bUseDirectConnect) {
connectionClient = new LdapClient(getConfiguration(),upCredentials);
sAuthenticatedDN = connectionClient.connect();
bUseLoginPattern = false;
connectionClient.close();
connectionClient = null;
}
// Attempt to authenticate by first executing a search for all users
// matching the input username, then checking the supplied password against
// each matching DN.
// An AuthenticationException will be thrown if the credentials are invalid.
if (bUseLoginPattern) {
sAuthenticatedDN = searchForUser(upCredentials);
sTargetedGroupDN = upCredentials.getTargetedGroupDN();
}
// ensure an authenticated DN
if (sAuthenticatedDN.length() == 0) {
throw new AuthenticationException("Invalid credentials.");