/* See LICENSE for licensing and NOTICE for copyright. */ package org.ldaptive.auth; import java.util.Arrays; import org.ldaptive.BindRequest; import org.ldaptive.Connection; import org.ldaptive.ConnectionFactory; import org.ldaptive.ConnectionFactoryManager; import org.ldaptive.LdapException; import org.ldaptive.Response; import org.ldaptive.ResultCode; /** * Provides an LDAP authentication implementation that leverages the LDAP bind operation. * * @author Middleware Services */ public class BindAuthenticationHandler extends AbstractBindAuthenticationHandler implements ConnectionFactoryManager { /** Connection factory. */ private ConnectionFactory factory; /** Default constructor. */ public BindAuthenticationHandler() {} /** * Creates a new bind authentication handler. * * @param cf connection factory */ public BindAuthenticationHandler(final ConnectionFactory cf) { setConnectionFactory(cf); } @Override public ConnectionFactory getConnectionFactory() { return factory; } @Override public void setConnectionFactory(final ConnectionFactory cf) { factory = cf; } @Override protected Connection getConnection() throws LdapException { return factory.getConnection(); } @Override protected AuthenticationHandlerResponse authenticateInternal( final Connection c, final AuthenticationCriteria criteria) throws LdapException { AuthenticationHandlerResponse response; final BindRequest request = new BindRequest(criteria.getDn(), criteria.getCredential()); request.setSaslConfig(getAuthenticationSaslConfig()); request.setControls(getAuthenticationControls()); try { final Response<Void> connResponse = c.open(request); response = new AuthenticationHandlerResponse( ResultCode.SUCCESS == connResponse.getResultCode(), connResponse.getResultCode(), c, connResponse.getMessage(), connResponse.getControls(), connResponse.getMessageId()); } catch (LdapException e) { if (e.getResultCode() != null) { response = new AuthenticationHandlerResponse( false, e.getResultCode(), c, e.getMessage(), e.getControls(), e.getMessageId()); } else { throw e; } } return response; } @Override public String toString() { return String.format( "[%s@%d::factory=%s, saslConfig=%s, controls=%s]", getClass().getName(), hashCode(), factory, getAuthenticationSaslConfig(), Arrays.toString(getAuthenticationControls())); } }