{
LOG.debug( "Authenticating {}", bindContext.getDn() );
}
LdapConnectionConfig connectionConfig;
LdapNetworkConnection ldapConnection;
// Create a connection on the remote host
if ( delegateTls )
{
connectionConfig = new LdapConnectionConfig();
connectionConfig.setLdapHost( delegateHost );
connectionConfig.setLdapPort( delegatePort );
connectionConfig.setTrustManagers( new NoVerificationTrustManager() );
ldapConnection = new LdapNetworkConnection( connectionConfig );
ldapConnection.connect();
ldapConnection.startTls();
}
else if ( delegateSsl )
{
connectionConfig = new LdapConnectionConfig();
connectionConfig.setLdapHost( delegateHost );
connectionConfig.setUseSsl( true );
connectionConfig.setLdapPort( delegatePort );
connectionConfig.setTrustManagers( new NoVerificationTrustManager() );
ldapConnection = new LdapNetworkConnection( connectionConfig );
ldapConnection.connect();
}
else
{
connectionConfig = new LdapConnectionConfig();
connectionConfig.setLdapHost( delegateHost );
connectionConfig.setLdapPort( delegatePort );
ldapConnection = new LdapNetworkConnection( delegateHost, delegatePort );
ldapConnection.connect();
}
ldapConnection.setTimeOut( 0L );
try
{
// Try to bind
try
{
ldapConnection.bind( bindContext.getDn(),
Strings.utf8ToString( bindContext.getCredentials() ) );
// no need to remain bound to delegate host
ldapConnection.unBind();
}
catch ( LdapException le )
{
String message = I18n.err( I18n.ERR_230, bindContext.getDn().getName() );
LOG.info( message );
throw new LdapAuthenticationException( message );
}
// Create the new principal
principal = new LdapPrincipal( getDirectoryService().getSchemaManager(), bindContext.getDn(),
AuthenticationLevel.SIMPLE,
bindContext.getCredentials() );
IoSession session = bindContext.getIoSession();
if ( session != null )
{
SocketAddress clientAddress = session.getRemoteAddress();
principal.setClientAddress( clientAddress );
SocketAddress serverAddress = session.getServiceAddress();
principal.setServerAddress( serverAddress );
}
return principal;
}
catch ( LdapException e )
{
// Bad password ...
String message = I18n.err( I18n.ERR_230, bindContext.getDn().getName() );
LOG.info( message );
throw new LdapAuthenticationException( message );
}
finally
{
ldapConnection.close();
}
}