Search in sources :

Example 31 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class InitSaslBind method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<BindRequestDecorator> 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_04079);
        LOG.error(msg);
        BindResponseImpl response = new BindResponseImpl(bindRequestMessage.getMessageId());
        throw new ResponseCarryingException(msg, response, ResultCodeEnum.INVALID_CREDENTIALS, bindRequestMessage.getDn(), null);
    }
    bindRequestMessage.setSimple(false);
    if (IS_DEBUG) {
        LOG.debug("The SaslCredential has been created");
    }
}
Also used : ResponseCarryingException(org.apache.directory.api.ldap.codec.api.ResponseCarryingException) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) BindResponseImpl(org.apache.directory.api.ldap.model.message.BindResponseImpl) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 32 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class LdapNetworkConnection method bindSasl.

/**
 * Process the SASL Bind. It's a dialog with the server, we will send a first BindRequest, receive
 * a response and the, if this response is a challenge, continue by sending a new BindRequest with
 * the requested informations.
 *
 * @param saslRequest The SASL request object containing all the needed parameters
 * @return A {@link BindResponse} containing the result
 * @throws LdapException if some error occurred
 */
public BindFuture bindSasl(SaslRequest saslRequest) throws LdapException {
    // First switch to anonymous state
    authenticated.set(false);
    // try to connect, if we aren't already connected.
    connect();
    // If the session has not been establish, or is closed, we get out immediately
    checkSession();
    BindRequest bindRequest = createBindRequest((String) null, null, saslRequest.getSaslMechanism(), saslRequest.getControls());
    // Update the messageId
    int newId = messageId.incrementAndGet();
    bindRequest.setMessageId(newId);
    if (LOG.isDebugEnabled()) {
        LOG.debug(I18n.msg(I18n.MSG_03205_SENDING_REQUEST, bindRequest));
    }
    // Create a future for this Bind operation
    BindFuture bindFuture = new BindFuture(this, newId);
    // Store it in the future Map
    addToFutureMap(newId, bindFuture);
    try {
        BindResponse bindResponse;
        byte[] response;
        ResultCodeEnum result;
        // Creating a map for SASL properties
        Map<String, Object> properties = new HashMap<>();
        // Quality of Protection SASL property
        if (saslRequest.getQualityOfProtection() != null) {
            properties.put(Sasl.QOP, saslRequest.getQualityOfProtection().getValue());
        }
        // Security Strength SASL property
        if (saslRequest.getSecurityStrength() != null) {
            properties.put(Sasl.STRENGTH, saslRequest.getSecurityStrength().getValue());
        }
        // Mutual Authentication SASL property
        if (saslRequest.isMutualAuthentication()) {
            properties.put(Sasl.SERVER_AUTH, "true");
        }
        // Creating a SASL Client
        SaslClient sc = Sasl.createSaslClient(new String[] { bindRequest.getSaslMechanism() }, saslRequest.getAuthorizationId(), "ldap", config.getLdapHost(), properties, new SaslCallbackHandler(saslRequest));
        // for the requested mechanism. We then produce an Exception
        if (sc == null) {
            String message = "Cannot find a SASL factory for the " + bindRequest.getSaslMechanism() + " mechanism";
            LOG.error(message);
            throw new LdapException(message);
        }
        // deal with it immediately.
        if (sc.hasInitialResponse()) {
            byte[] challengeResponse = sc.evaluateChallenge(Strings.EMPTY_BYTES);
            // Stores the challenge's response, and send it to the server
            bindRequest.setCredentials(challengeResponse);
            writeRequest(bindRequest);
            // Get the server's response, blocking
            bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
            if (bindResponse == null) {
                // We didn't received anything : this is an error
                if (LOG.isErrorEnabled()) {
                    LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
                }
                throw new LdapException(TIME_OUT_ERROR);
            }
            result = bindResponse.getLdapResult().getResultCode();
        } else {
            // Copy the bindRequest without setting the credentials
            BindRequest bindRequestCopy = new BindRequestImpl();
            bindRequestCopy.setMessageId(newId);
            bindRequestCopy.setName(bindRequest.getName());
            bindRequestCopy.setSaslMechanism(bindRequest.getSaslMechanism());
            bindRequestCopy.setSimple(bindRequest.isSimple());
            bindRequestCopy.setVersion3(bindRequest.getVersion3());
            bindRequestCopy.addAllControls(bindRequest.getControls().values().toArray(new Control[0]));
            writeRequest(bindRequestCopy);
            bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
            if (bindResponse == null) {
                // We didn't received anything : this is an error
                if (LOG.isErrorEnabled()) {
                    LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
                }
                throw new LdapException(TIME_OUT_ERROR);
            }
            result = bindResponse.getLdapResult().getResultCode();
        }
        while (!sc.isComplete() && ((result == ResultCodeEnum.SASL_BIND_IN_PROGRESS) || (result == ResultCodeEnum.SUCCESS))) {
            response = sc.evaluateChallenge(bindResponse.getServerSaslCreds());
            if (result == ResultCodeEnum.SUCCESS) {
                if (response != null) {
                    throw new LdapException("protocol error");
                }
            } else {
                newId = messageId.incrementAndGet();
                bindRequest.setMessageId(newId);
                bindRequest.setCredentials(response);
                addToFutureMap(newId, bindFuture);
                writeRequest(bindRequest);
                bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
                if (bindResponse == null) {
                    // We didn't received anything : this is an error
                    if (LOG.isErrorEnabled()) {
                        LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
                    }
                    throw new LdapException(TIME_OUT_ERROR);
                }
                result = bindResponse.getLdapResult().getResultCode();
            }
        }
        bindFuture.set(bindResponse);
        return bindFuture;
    } catch (LdapException e) {
        throw e;
    } catch (Exception e) {
        LOG.error(e.getMessage());
        throw new LdapException(e);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ResultCodeEnum(org.apache.directory.api.ldap.model.message.ResultCodeEnum) SaslClient(javax.security.sasl.SaslClient) Control(org.apache.directory.api.ldap.model.message.Control) OpaqueControl(org.apache.directory.api.ldap.model.message.controls.OpaqueControl) SaslCallbackHandler(org.apache.directory.ldap.client.api.callback.SaslCallbackHandler) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) BindRequestImpl(org.apache.directory.api.ldap.model.message.BindRequestImpl)

Example 33 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class BatchRequestTest method testResponseWith1AuthRequestAnd1AddRequest.

/**
 * Test parsing of a Request with 1 AuthRequest and 1 AddRequest
 */
@Test
public void testResponseWith1AuthRequestAnd1AddRequest() {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(BatchRequestTest.class.getResource("request_with_1_AuthRequest_1_AddRequest.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    BatchRequestDsml batchRequest = parser.getBatchRequest();
    List<DsmlDecorator<? extends Request>> requests = batchRequest.getRequests();
    assertEquals(2, requests.size());
    if (requests.get(0) instanceof BindRequest) {
        assertTrue(true);
    } else {
        fail();
    }
    if (requests.get(1) instanceof AddRequest) {
        assertTrue(true);
    } else {
        fail();
    }
}
Also used : BatchRequestDsml(org.apache.directory.api.dsmlv2.request.BatchRequestDsml) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) CompareRequest(org.apache.directory.api.ldap.model.message.CompareRequest) Request(org.apache.directory.api.ldap.model.message.Request) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) ModifyDnRequest(org.apache.directory.api.ldap.model.message.ModifyDnRequest) AbandonRequest(org.apache.directory.api.ldap.model.message.AbandonRequest) DeleteRequest(org.apache.directory.api.ldap.model.message.DeleteRequest) ExtendedRequest(org.apache.directory.api.ldap.model.message.ExtendedRequest) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) DsmlDecorator(org.apache.directory.api.dsmlv2.DsmlDecorator) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 34 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class AuthRequestTest method testRequestWithPrincipal.

/**
 * 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());
}
Also used : BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Example 35 with BindRequest

use of org.apache.directory.api.ldap.model.message.BindRequest in project directory-ldap-api by apache.

the class Dsmlv2Engine method bind.

/**
 * Binds to the ldap server
 *
 * @param messageId the message Id
 * @throws LdapException If we had an issue while binding
 * @throws EncoderException If we had an issue while encoding the request
 * @throws DecoderException If we had an issue while decoding the request
 * @throws IOException If we had an issue while transmitting the request or re ceiving the response
 */
protected void bind(int messageId) throws LdapException, EncoderException, DecoderException, IOException {
    if ((connection != null) && connection.isAuthenticated()) {
        return;
    }
    if (connection == null) {
        throw new IOException(I18n.err(I18n.ERR_03101_MISSING_CONNECTION_TO));
    }
    BindRequest bindRequest = new BindRequestImpl();
    bindRequest.setSimple(true);
    bindRequest.setCredentials(Strings.getBytesUtf8(password));
    bindRequest.setName(user);
    bindRequest.setVersion3(true);
    bindRequest.setMessageId(messageId);
    BindResponse bindResponse = connection.bind(bindRequest);
    if (bindResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS) {
        LOG.warn("Error : {}", bindResponse.getLdapResult().getDiagnosticMessage());
    }
}
Also used : BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) IOException(java.io.IOException) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) BindRequestImpl(org.apache.directory.api.ldap.model.message.BindRequestImpl)

Aggregations

BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)45 Test (org.junit.Test)27 DecoderException (org.apache.directory.api.asn1.DecoderException)19 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)17 ByteBuffer (java.nio.ByteBuffer)16 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)16 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)15 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)15 EncoderException (org.apache.directory.api.asn1.EncoderException)12 Control (org.apache.directory.api.ldap.model.message.Control)11 AbstractTest (org.apache.directory.api.dsmlv2.AbstractTest)9 Dsmlv2Parser (org.apache.directory.api.dsmlv2.Dsmlv2Parser)9 BindRequestImpl (org.apache.directory.api.ldap.model.message.BindRequestImpl)9 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)7 BindResponse (org.apache.directory.api.ldap.model.message.BindResponse)7 DsmlControl (org.apache.directory.api.dsmlv2.DsmlControl)5 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)4 Dn (org.apache.directory.api.ldap.model.name.Dn)4 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)3 AbandonRequest (org.apache.directory.api.ldap.model.message.AbandonRequest)3