protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authtoken) throws AuthenticationException {
// safe, we only handle this type
UsernamePasswordToken token = (UsernamePasswordToken) authtoken;
final LdapConnectionConfig config = new LdapConnectionConfig();
final LdapSettings ldapSettings = settings.get();
if (ldapSettings == null || !ldapSettings.isEnabled()) {
LOG.trace("LDAP is disabled, skipping");
return null;
}
config.setLdapHost(ldapSettings.getUri().getHost());
config.setLdapPort(ldapSettings.getUri().getPort());
config.setUseSsl(ldapSettings.getUri().getScheme().startsWith("ldaps"));
config.setUseTls(ldapSettings.isUseStartTls());
if (ldapSettings.isTrustAllCertificates()) {
config.setTrustManagers(new TrustAllX509TrustManager());
}
config.setName(ldapSettings.getSystemUserName());
config.setCredentials(ldapSettings.getSystemPassword());
final String principal = String.valueOf(token.getPrincipal());
LdapNetworkConnection connection = null;
try {
connection = ldapConnector.connect(config);
if(null == connection) {
LOG.error("Couldn't connect to LDAP directory");
return null;
}
final String password = String.valueOf(token.getPassword());
final LdapEntry userEntry = ldapConnector.search(connection,
ldapSettings.getSearchBase(),
ldapSettings.getSearchPattern(),
principal,
ldapSettings.isActiveDirectory());
if (userEntry == null) {
LOG.debug("User {} not found in LDAP", principal);
return null;
}