org.apache.directory.api.ldap.model.message.BindRequest Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.message.BindRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with a bad version (128)
 */
@Test
public void testDecodeBindRequestBadVersion128() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x0C );
    stream.put( new byte[]
        {
            0x30, 0x0A,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x04,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x02, 0x00, ( byte ) 0x80 // version INTEGER (1..127),
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #2
Source File: StoreName.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<BindRequest> container )
{
    BindRequest bindRequestMessage = container.getMessage();

    // Get the Value and store it in the BindRequest
    TLV tlv = container.getCurrentTLV();

    // We have to handle the special case of a 0 length name
    if ( tlv.getLength() == 0 )
    {
        bindRequestMessage.setName( "" );
    }
    else
    {
        byte[] nameBytes = tlv.getValue().getData();
        String nameStr = Strings.utf8ToString( nameBytes );
        bindRequestMessage.setName( nameStr );
    }

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05116_BIND_NAME_IS, bindRequestMessage.getName() ) );
    }
}
 
Example #3
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Deal with a received BindRequest
 * 
 * @param ldapSession The current session
 * @param bindRequest The received BindRequest
 * @throws Exception If the authentication cannot be handled
 */
public void handle( LdapSession ldapSession, BindRequest bindRequest ) throws Exception
{
    LOG.debug( "Received: {}", bindRequest );

    // Guard clause:  LDAP version 3
    if ( !bindRequest.getVersion3() )
    {
        LOG.error( I18n.err( I18n.ERR_162 ) );
        LdapResult bindResult = bindRequest.getResultResponse().getLdapResult();
        bindResult.setResultCode( ResultCodeEnum.PROTOCOL_ERROR );
        bindResult.setDiagnosticMessage( I18n.err( I18n.ERR_163 ) );
        ldapSession.getIoSession().write( bindRequest.getResultResponse() );
        return;
    }

    // Deal with the two kinds of authentication : Simple and SASL
    if ( bindRequest.isSimple() )
    {
        handleSimpleAuth( ldapSession, bindRequest );
    }
    else
    {
        handleSaslAuth( ldapSession, bindRequest );
    }
}
 
Example #4
Source File: InitSaslBind.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<BindRequest> container ) throws DecoderException
{
    BindRequest bindRequestMessage = container.getMessage();
    TLV tlv = container.getCurrentTLV();

    // We will check that the sasl is not null
    if ( tlv.getLength() == 0 )
    {
        String msg = I18n.err( I18n.ERR_05116_SASL_CREDS_CANT_BE_NULL );
        LOG.error( msg );

        BindResponseImpl response = new BindResponseImpl( bindRequestMessage.getMessageId() );

        throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_CREDENTIALS,
            bindRequestMessage.getDn(), null );
    }

    bindRequestMessage.setSimple( false );

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05115_SASL_CREDS_CREATED ) );
    }
}
 
Example #5
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Send back an AUTH-METH-NOT-SUPPORTED error message to the client
 */
private void sendAuthMethNotSupported(LdapSession ldapSession, BindRequest bindRequest) {
    // First, r-einit the state to Anonymous, and clear the
    // saslProperty map
    ldapSession.clearSaslProperties();
    ldapSession.setAnonymous();

    // And send the response to the client
    LdapResult bindResult = bindRequest.getResultResponse().getLdapResult();
    bindResult.setResultCode(ResultCodeEnum.AUTH_METHOD_NOT_SUPPORTED);
    bindResult.setDiagnosticMessage(ResultCodeEnum.AUTH_METHOD_NOT_SUPPORTED.toString() + ": "
                                    + bindRequest.getSaslMechanism() + " is not a supported mechanism.");

    // Write back the error
    ldapSession.getIoSession().write(bindRequest.getResultResponse());
}
 
Example #6
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with an empty body
 */
@Test
public void testDecodeBindRequestEmptyBody() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x07 );
    stream.put( new byte[]
        {
            0x30, 0x05,         // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01, // messageID MessageID
              0x60, 0x00        // CHOICE { ..., bindRequest BindRequest, ...
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #7
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with an empty version
 */
@Test
public void testDecodeBindRequestEmptyVersion() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x09 );
    stream.put( new byte[]
        {
            0x30, 0x07,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x02,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x00          // version INTEGER (1..127),
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #8
Source File: AuthRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test parsing of a request with the principal attribute
 */
@Test
public void testRequestWithPrincipal()
{
    Dsmlv2Parser parser = null;
    try
    {
        parser = newParser();

        parser.setInput( AuthRequestTest.class.getResource( "request_with_principal_attribute.xml" ).openStream(),
            "UTF-8" );

        parser.parse();
    }
    catch ( Exception e )
    {
        fail( e.getMessage() );
    }

    BindRequest bindRequest = ( BindRequest ) parser.getBatchRequest().getCurrentRequest();

    assertEquals( "CN=Bob Rush,OU=Dev,DC=Example,DC=COM", bindRequest.getName() );
}
 
Example #9
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with a bad version (0)
 */
@Test
public void testDecodeBindRequestBadVersion0() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x0A );
    stream.put( new byte[]
        {
            0x30, 0x08,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x03,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x00    // version INTEGER (1..127),
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #10
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with a bad version (4)
 */
@Test
public void testDecodeBindRequestBadVersion4() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x0A );
    stream.put( new byte[]
        {
            0x30, 0x08,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x03,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x04    // version INTEGER (1..127),
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #11
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with no name
 */
@Test
public void testDecodeBindRequestNoName() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x0A );
    stream.put( new byte[]
        {
            0x30, 0x08,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x03,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x03    // version INTEGER (1..127),
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #12
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the decoding of a BindRequest with an empty name
 */
@Test
public void testDecodeBindRequestEmptyName() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x0C );
    stream.put( new byte[]
        {
            0x30, 0x0A,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x05,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x03,   // version INTEGER (1..127),
                0x04, 0x00
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #13
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Send a SUCCESS message back to the client.
 */
private void sendBindSuccess(LdapSession ldapSession, BindRequest bindRequest, byte[] tokenBytes) {
    // Return the successful response
    BindResponse response = (BindResponse) bindRequest.getResultResponse();
    response.getLdapResult().setResultCode(ResultCodeEnum.SUCCESS);
    response.setServerSaslCreds(tokenBytes);

    if (!ldapSession.getCoreSession().isAnonymous()) {
        // If we have not been asked to authenticate as Anonymous, authenticate the user
        ldapSession.setAuthenticated();
    } else {
        // Otherwise, switch back to Anonymous
        ldapSession.setAnonymous();
    }

    // Clean the SaslProperties, we don't need them anymore
    MechanismHandler handler = (MechanismHandler) ldapSession.getSaslProperty(SaslConstants.SASL_MECH_HANDLER);

    if (handler != null) {
        handler.cleanup(ldapSession);
    }

    ldapSession.getIoSession().write(response);

    LOG.debug("Returned SUCCESS message: {}.", response);
}
 
Example #14
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Deal with a received BindRequest
 *
 * @param ldapSession The current session
 * @param bindRequest The received BindRequest
 * @throws Exception If the authentication cannot be handled
 */
public void handle(LdapSession ldapSession, BindRequest bindRequest) throws Exception {
    LOG.debug("Received: {}", bindRequest);

    // Guard clause:  LDAP version 3
    if (!bindRequest.getVersion3()) {
        LOG.error(I18n.err(I18n.ERR_162));
        LdapResult bindResult = bindRequest.getResultResponse().getLdapResult();
        bindResult.setResultCode(ResultCodeEnum.PROTOCOL_ERROR);
        bindResult.setDiagnosticMessage(I18n.err(I18n.ERR_163));
        ldapSession.getIoSession().write(bindRequest.getResultResponse());
        return;
    }

    // Deal with the two kinds of authentication : Simple and SASL
    if (bindRequest.isSimple()) {
        handleSimpleAuth(ldapSession, bindRequest);
    } else {
        handleSaslAuth(ldapSession, bindRequest);
    }
}
 
Example #15
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public BindFuture bindAsync( String name, String credentials ) throws LdapException
{
    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_04102_BIND_REQUEST, name ) );
    }

    // The password must not be empty or null
    if ( Strings.isEmpty( credentials ) && Strings.isNotEmpty( name ) )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_04105_MISSING_PASSWORD ) );
        }
        
        throw new LdapAuthenticationException( I18n.msg( I18n.MSG_04105_MISSING_PASSWORD ) );
    }

    // Create the BindRequest
    BindRequest bindRequest = createBindRequest( name, Strings.getBytesUtf8( credentials ) );

    return bindAsync( bindRequest );
}
 
Example #16
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void bind() throws LdapException
{
    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg(  I18n.MSG_04112_BIND ) );
    }

    // Create the BindRequest
    BindRequest bindRequest = createBindRequest( config.getName(), Strings.getBytesUtf8( config.getCredentials() ) );

    BindResponse bindResponse = bind( bindRequest );

    processResponse( bindResponse );
}
 
Example #17
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void anonymousBind() throws LdapException
{
    if ( LOG.isDebugEnabled() )
    { 
        LOG.debug( I18n.msg( I18n.MSG_04109_ANONYMOUS_BIND ) );
    }

    // Create the BindRequest
    BindRequest bindRequest = createBindRequest( StringConstants.EMPTY, Strings.EMPTY_BYTES );

    BindResponse bindResponse = bind( bindRequest );

    processResponse( bindResponse );
}
 
Example #18
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Send back an AUTH-METH-NOT-SUPPORTED error message to the client
 */
private void sendAuthMethNotSupported( LdapSession ldapSession, BindRequest bindRequest )
{
    // First, r-einit the state to Anonymous, and clear the
    // saslProperty map
    ldapSession.clearSaslProperties();
    ldapSession.setAnonymous();

    // And send the response to the client
    LdapResult bindResult = bindRequest.getResultResponse().getLdapResult();
    bindResult.setResultCode( ResultCodeEnum.AUTH_METHOD_NOT_SUPPORTED );
    bindResult.setDiagnosticMessage( ResultCodeEnum.AUTH_METHOD_NOT_SUPPORTED.toString() + ": "
        + bindRequest.getSaslMechanism() + " is not a supported mechanism." );

    // Write back the error
    ldapSession.getIoSession().write( bindRequest.getResultResponse() );
}
 
Example #19
Source File: LdapServer.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Inject the MessageReceived and MessageSent handler into the IoHandler
 * 
 * @param bindRequestHandler The BindRequest message received handler
 * @param bindResponseHandler The BindResponse message sent handler
 */
public void setBindHandlers( LdapRequestHandler<BindRequest> bindRequestHandler,
    LdapResponseHandler<BindResponse> bindResponseHandler )
{
    handler.removeReceivedMessageHandler( BindRequest.class );
    this.bindRequestHandler = bindRequestHandler;
    this.bindRequestHandler.setLdapServer( this );
    handler.addReceivedMessageHandler( BindRequest.class, this.bindRequestHandler );

    handler.removeSentMessageHandler( BindResponse.class );
    this.bindResponseHandler = bindResponseHandler;
    this.bindResponseHandler.setLdapServer( this );
    handler.addSentMessageHandler( BindResponse.class, this.bindResponseHandler );
}
 
Example #20
Source File: StoreSimpleAuth.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void action( LdapMessageContainer<BindRequest> container )
{
    BindRequest bindRequestMessage = container.getMessage();
    TLV tlv = container.getCurrentTLV();

    // Allocate the Authentication Object
    bindRequestMessage.setSimple( true );

    // We have to handle the special case of a 0 length simple
    if ( tlv.getLength() == 0 )
    {
        bindRequestMessage.setCredentials( Strings.EMPTY_BYTES );
    }
    else
    {
        bindRequestMessage.setCredentials( tlv.getValue().getData() );
    }

    // We can have an END transition
    container.setGrammarEndAllowed( true );

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_05119_SIMPLE_CREDENTIAL_DECODED ) );
    }
}
 
Example #21
Source File: LdapServer.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Inject the MessageReceived and MessageSent handler into the IoHandler
 * 
 * @param bindRequestHandler The BindRequest message received handler
 * @param bindResponseHandler The BindResponse message sent handler
 */
public void setBindHandlers( LdapRequestHandler<BindRequest> bindRequestHandler,
    LdapResponseHandler<BindResponse> bindResponseHandler )
{
    handler.removeReceivedMessageHandler( BindRequest.class );
    this.bindRequestHandler = bindRequestHandler;
    this.bindRequestHandler.setLdapServer( this );
    handler.addReceivedMessageHandler( BindRequest.class, this.bindRequestHandler );

    handler.removeSentMessageHandler( BindResponse.class );
    this.bindResponseHandler = bindResponseHandler;
    this.bindResponseHandler.setLdapServer( this );
    handler.addSentMessageHandler( BindResponse.class, this.bindResponseHandler );
}
 
Example #22
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the decoding of a BindRequest with an empty mechanism
 */
@Test
public void testDecodeBindRequestEmptyMechanism() throws DecoderException, EncoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x10 );
    stream.put( new byte[]
        {
            0x30, 0x0E,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x09,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x03,   // version INTEGER (1..127),
                0x04, 0x00,
                ( byte ) 0xA3, 0x02,
                  0x04, 0x00
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode the BindRequest PDU
    Asn1Decoder.decode( stream, container );

    // Check the decoded BindRequest
    BindRequest bindRequest = container.getMessage();

    assertEquals( 1, bindRequest.getMessageId() );
    assertTrue( bindRequest.isVersion3() );
    assertEquals( "", bindRequest.getName() );
    assertFalse( bindRequest.isSimple() );
    assertEquals( "", bindRequest.getSaslMechanism() );

    // Check encode reverse
    Asn1Buffer buffer = new Asn1Buffer();

    LdapEncoder.encodeMessage( buffer, codec, bindRequest );
    assertArrayEquals( stream.array(), buffer.getBytes().array() );
}
 
Example #23
Source File: BindRequestDsml.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Element toDsml( Element root )
{
    Element element = super.toDsml( root );

    BindRequest request = getDecorated();

    // Principal
    Dn dn = request.getDn();

    if ( !Dn.isNullOrEmpty( dn ) )
    {
        // A DN has been provided

        element.addAttribute( "principal", dn.getName() );
    }
    else
    {
        // No DN has been provided, let's use the name as a string instead

        String name = request.getName();

        element.addAttribute( "principal", name );
    }

    return element;
}
 
Example #24
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the decoding of a BindRequest with Simple authentication, no name
 * and no controls
 */
@Test
public void testDecodeBindRequestSimpleNoName() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x15 );
    stream.put( new byte[]
        {
            0x30, 0x13,                 // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,         // messageID MessageID
              0x60, 0x0D,               // CHOICE { ..., bindRequest BindRequest, ...
                                        // BindRequest ::= APPLICATION[0] SEQUENCE {
                0x02, 0x01, 0x03,       // version INTEGER (1..127),
                ( byte ) 0x80, 0x08,    // authentication AuthenticationChoice
                                        // AuthenticationChoice ::= CHOICE { simple [0] OCTET STRING,
                                        // ...
                  'p', 'a', 's', 's', 'w', 'o', 'r', 'd'
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    Asn1Container container = new LdapMessageContainer<BindRequest>( codec );

    // Decode the BindRequest PDU
    assertThrows( DecoderException.class, ( ) ->
    {
        Asn1Decoder.decode( stream, container );
    } );
}
 
Example #25
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
private void handleSaslAuthPending( LdapSession ldapSession, BindRequest bindRequest ) throws Exception
{
    // First, check that we have the same mechanism
    String saslMechanism = bindRequest.getSaslMechanism();

    // The empty mechanism is also a request for a new Bind session
    if ( Strings.isEmpty( saslMechanism )
        || !ldapSession.getSaslProperty( SaslConstants.SASL_MECH ).equals( saslMechanism ) )
    {
        sendAuthMethNotSupported( ldapSession, bindRequest );
        return;
    }

    // We have already received a first BindRequest, and sent back some challenge.
    // First, check if the mechanism is the same
    MechanismHandler mechanismHandler = handlers.get( saslMechanism );

    if ( mechanismHandler == null )
    {
        String message = I18n.err( I18n.ERR_161, saslMechanism );

        // Clear the saslProperties, and move to the anonymous state
        ldapSession.clearSaslProperties();
        ldapSession.setAnonymous();

        LOG.error( message );
        throw new IllegalArgumentException( message );
    }

    // Get the previously created SaslServer instance
    SaslServer ss = mechanismHandler.handleMechanism( ldapSession, bindRequest );

    generateSaslChallengeOrComplete( ldapSession, ss, bindRequest );
}
 
Example #26
Source File: BindRequestTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the decoding of a BindRequest with an empty sasl
 */
@Test
public void testDecodeBindRequestEmptySasl() throws DecoderException
{
    ByteBuffer stream = ByteBuffer.allocate( 0x0E );
    stream.put( new byte[]
        {
            0x30, 0x0C,             // LDAPMessage ::=SEQUENCE {
              0x02, 0x01, 0x01,     // messageID MessageID
              0x60, 0x07,           // CHOICE { ..., bindRequest BindRequest, ...
                0x02, 0x01, 0x03,   // version INTEGER (1..127),
                0x04, 0x00,
                ( byte ) 0xA3, 0x00
        } );

    stream.flip();

    // Allocate a LdapMessage Container
    LdapMessageContainer<BindRequest> container = new LdapMessageContainer<>( codec );

    // Decode a BindRequest message
    assertThrows( DecoderException.class, ( ) ->
    {
        try
        {
            Asn1Decoder.decode( stream, container );
        }
        catch ( DecoderException de )
        {
            assertTrue( de instanceof ResponseCarryingException );
            Message response = ( ( ResponseCarryingException ) de ).getResponse();
            assertTrue( response instanceof BindResponseImpl );
            assertEquals( ResultCodeEnum.INVALID_CREDENTIALS, ( ( BindResponseImpl ) response ).getLdapResult()
                .getResultCode() );

            throw de;
        }
    } );
}
 
Example #27
Source File: ValidatingPoolableLdapConnectionFactoryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
@Override
public BindResponse bind( BindRequest bindRequest ) throws LdapException
{
    BindResponse response = connection.bind( bindRequest );
    bindCalled = true;
    return response;
}
 
Example #28
Source File: BindRequestHandler.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Send a SUCCESS message back to the client.
 */
private void sendBindSuccess( LdapSession ldapSession, BindRequest bindRequest, byte[] tokenBytes )
{
    // Return the successful response
    BindResponse response = bindRequest.getResultResponse();
    response.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
    response.setServerSaslCreds( tokenBytes );

    if ( !ldapSession.getCoreSession().isAnonymous() )
    {
        // If we have not been asked to authenticate as Anonymous, authenticate the user
        ldapSession.setAuthenticated();
    }
    else
    {
        // Otherwise, switch back to Anonymous
        ldapSession.setAnonymous();
    }

    // Clean the SaslProperties, we don't need them anymore
    MechanismHandler handler = ( MechanismHandler ) ldapSession.getSaslProperty( SaslConstants.SASL_MECH_HANDLER );

    if ( handler != null )
    {
        handler.cleanup( ldapSession );
    }

    ldapSession.getIoSession().write( response );

    LOG.debug( "Returned SUCCESS message: {}.", response );
}
 
Example #29
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Asynchronous unauthenticated authentication Bind on a server.
 *
 * @param name The name we use to authenticate the user. It must be a
 * valid Dn
 * @return The BindResponse LdapResponse
 * @throws LdapException if some error occurred
 */
public BindFuture bindAsync( Dn name ) throws LdapException
{
    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_04102_BIND_REQUEST, name ) );
    }

    // Create the BindRequest
    BindRequest bindRequest = createBindRequest( name, Strings.EMPTY_BYTES );

    return bindAsync( bindRequest );
}
 
Example #30
Source File: LdapNetworkConnection.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Asynchronous unauthenticated authentication bind
 *
 * @param name The name we use to authenticate the user. It must be a
 * valid Dn
 * @return The BindResponse LdapResponse
 * @throws LdapException if some error occurred
 */
public BindFuture bindAsync( String name ) throws LdapException
{
    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_04102_BIND_REQUEST, name ) );
    }

    // Create the BindRequest
    BindRequest bindRequest = createBindRequest( name, Strings.EMPTY_BYTES );

    return bindAsync( bindRequest );
}