Search in sources :

Example 1 with BindRequestProtocolOp

use of com.unboundid.ldap.protocol.BindRequestProtocolOp in project ldapsdk by pingidentity.

the class InterceptedSimpleBindOperationTestCase method testBasics.

/**
 * Provides basic test coverage for an intercepted simple bind operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    // Create an intercepted simple bind operation.  We'll use a null
    // connection, which shouldn't happen naturally but will be sufficient for
    // this test.
    final BindRequestProtocolOp requestOp = new BindRequestProtocolOp(new SimpleBindRequest("uid=test.user,ou=People,dc=example,dc=com", "password"));
    final InterceptedSimpleBindOperation o = new InterceptedSimpleBindOperation(null, 1, requestOp);
    assertNotNull(o.toString());
    // Test methods for a generic intercepted operation.
    assertNull(o.getClientConnection());
    assertEquals(o.getConnectionID(), -1L);
    assertNull(o.getConnectedAddress());
    assertEquals(o.getConnectedPort(), -1);
    assertEquals(o.getMessageID(), 1);
    assertNull(o.getProperty("propX"));
    o.setProperty("propX", "valX");
    assertNotNull(o.getProperty("propX"));
    assertEquals(o.getProperty("propX"), "valX");
    assertNotNull(o.toString());
    o.setProperty("propX", null);
    assertNull(o.getProperty("propX"));
    // Test methods specific to an intercepted simple bind operation.
    assertNotNull(o.getRequest());
    assertEquals(o.getRequest().getPassword().stringValue(), "password");
    assertNotNull(o.toString());
    final SimpleBindRequest r = new SimpleBindRequest("uid=test.user,ou=People,dc=example,dc=com", "newPassword");
    o.setRequest(r);
    assertNotNull(o.getRequest());
    assertEquals(o.getRequest().getPassword().stringValue(), "newPassword");
    assertNotNull(o.toString());
    assertNull(o.getResult());
    o.setResult(new BindResult(o.getMessageID(), ResultCode.SUCCESS, null, null, null, null));
    assertNotNull(o.getResult());
    assertNotNull(o.toString());
}
Also used : SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) BindResult(com.unboundid.ldap.sdk.BindResult) Test(org.testng.annotations.Test)

Example 2 with BindRequestProtocolOp

use of com.unboundid.ldap.protocol.BindRequestProtocolOp in project ldapsdk by pingidentity.

the class UNBOUNDIDTESTServer method run.

/**
 * Performs the processing for this server.
 */
@Override()
public void run() {
    try {
        serverSocket = new ServerSocket(0);
        listenPort = serverSocket.getLocalPort();
        while (!stopRequested.get()) {
            // Accept a connection from a client.
            clientSocket = serverSocket.accept();
            final InputStream inputStream = clientSocket.getInputStream();
            final OutputStream outputStream = clientSocket.getOutputStream();
            final ASN1StreamReader asn1Reader = new ASN1StreamReader(inputStream, 0);
            // The client must first send an UNBOUNDID-TEST bind request with no
            // credentials.
            LDAPMessage requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            BindRequestProtocolOp bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNull(bindRequestOp.getSASLCredentials());
            // Return a "SASL bind in progress" response.
            LDAPMessage responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SASL_BIND_IN_PROGRESS_INT_VALUE, null, null, null, null));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // The next request must be an UNBOUNDID-TEST bind request with
            // credentials.  We won't do anything to validate the credentials, but
            // we will look at the third element to see what QoP the client
            // requested.
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNotNull(bindRequestOp.getSASLCredentials());
            final ASN1Sequence credSequence = ASN1Sequence.decodeAsSequence(bindRequestOp.getSASLCredentials().getValue());
            final ASN1Element[] credElements = credSequence.elements();
            final SASLQualityOfProtection qop = SASLQualityOfProtection.forName(ASN1OctetString.decodeAsOctetString(credElements[2]).stringValue());
            assertNotNull(qop);
            final boolean qopEncode = ((qop == SASLQualityOfProtection.AUTH_INT) || (qop == SASLQualityOfProtection.AUTH_CONF));
            // Return a "success" response.  Include server SASL credentials with
            // the requested QoP.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null, new ASN1OctetString(qop.toString())));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final SearchRequestProtocolOp searchRequestOp = requestMessage.getSearchRequestProtocolOp();
            assertEquals(searchRequestOp.getBaseDN(), "");
            assertEquals(searchRequestOp.getScope(), SearchScope.BASE);
            assertEquals(searchRequestOp.getFilter(), Filter.createPresenceFilter("objectClass"));
            assertEquals(searchRequestOp.getAttributes(), Arrays.asList("1.1"));
            // Return a search result entry message with a DN but no attributes.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultEntryProtocolOp("", Collections.<Attribute>emptyList()));
            byte[] messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // Return a "success" search result done message.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultDoneProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null));
            messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // The next request should be an unbind request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final UnbindRequestProtocolOp unbindRequestOp = requestMessage.getUnbindRequestProtocolOp();
            // Close the connection.
            try {
                asn1Reader.close();
            } catch (final Exception e) {
            }
            try {
                outputStream.close();
            } catch (final Exception e) {
            }
            try {
                clientSocket.close();
            } catch (final Exception e) {
            }
            clientSocket = null;
        }
    } catch (final Exception e) {
        stopServer();
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ServerSocket(java.net.ServerSocket) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp)

Example 3 with BindRequestProtocolOp

use of com.unboundid.ldap.protocol.BindRequestProtocolOp in project ldapsdk by pingidentity.

the class InMemoryDirectoryServer method bind.

/**
 * Processes the provided bind request.  Only simple and SASL PLAIN bind
 * requests are supported.  Note that the bind processing will verify that the
 * provided credentials are valid, but it will not alter the server in any
 * way.
 *
 * @param  bindRequest  The bind request to be processed.  It must not be
 *                      {@code null}.
 *
 * @return  The result of processing the bind operation.
 *
 * @throws  LDAPException  If the server rejects the bind request, or if a
 *                         problem occurs while sending the request or reading
 *                         the response.
 */
@NotNull()
public BindResult bind(@NotNull final BindRequest bindRequest) throws LDAPException {
    final ArrayList<Control> requestControlList = new ArrayList<>(bindRequest.getControlList());
    requestControlList.add(new Control(InMemoryRequestHandler.OID_INTERNAL_OPERATION_REQUEST_CONTROL, false));
    final BindRequestProtocolOp bindOp;
    if (bindRequest instanceof SimpleBindRequest) {
        final SimpleBindRequest r = (SimpleBindRequest) bindRequest;
        bindOp = new BindRequestProtocolOp(r.getBindDN(), r.getPassword().getValue());
    } else if (bindRequest instanceof PLAINBindRequest) {
        final PLAINBindRequest r = (PLAINBindRequest) bindRequest;
        // Create the byte array that should comprise the credentials.
        final byte[] authZIDBytes = StaticUtils.getBytes(r.getAuthorizationID());
        final byte[] authNIDBytes = StaticUtils.getBytes(r.getAuthenticationID());
        final byte[] passwordBytes = r.getPasswordBytes();
        final byte[] credBytes = new byte[2 + authZIDBytes.length + authNIDBytes.length + passwordBytes.length];
        System.arraycopy(authZIDBytes, 0, credBytes, 0, authZIDBytes.length);
        int pos = authZIDBytes.length + 1;
        System.arraycopy(authNIDBytes, 0, credBytes, pos, authNIDBytes.length);
        pos += authNIDBytes.length + 1;
        System.arraycopy(passwordBytes, 0, credBytes, pos, passwordBytes.length);
        bindOp = new BindRequestProtocolOp(null, "PLAIN", new ASN1OctetString(credBytes));
    } else {
        throw new LDAPException(ResultCode.AUTH_METHOD_NOT_SUPPORTED, ERR_MEM_DS_UNSUPPORTED_BIND_TYPE.get());
    }
    final LDAPMessage responseMessage = inMemoryHandler.processBindRequest(1, bindOp, requestControlList);
    final BindResponseProtocolOp bindResponse = responseMessage.getBindResponseProtocolOp();
    final BindResult bindResult = new BindResult(new LDAPResult(responseMessage.getMessageID(), ResultCode.valueOf(bindResponse.getResultCode()), bindResponse.getDiagnosticMessage(), bindResponse.getMatchedDN(), bindResponse.getReferralURLs(), responseMessage.getControls()));
    switch(bindResponse.getResultCode()) {
        case ResultCode.SUCCESS_INT_VALUE:
            return bindResult;
        default:
            throw new LDAPException(bindResult);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Control(com.unboundid.ldap.sdk.Control) SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) LDAPException(com.unboundid.ldap.sdk.LDAPException) ArrayList(java.util.ArrayList) PLAINBindRequest(com.unboundid.ldap.sdk.PLAINBindRequest) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) BindResult(com.unboundid.ldap.sdk.BindResult) NotNull(com.unboundid.util.NotNull)

Example 4 with BindRequestProtocolOp

use of com.unboundid.ldap.protocol.BindRequestProtocolOp in project ldapsdk by pingidentity.

the class InterceptedSASLBindOperationTestCase method testBasics.

/**
 * Provides basic test coverage for an intercepted SASL bind operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    // Create an intercepted SASL bind operation.  We'll use a null connection,
    // which shouldn't happen naturally but will be sufficient for this test.
    final BindRequestProtocolOp requestOp = new BindRequestProtocolOp(new GenericSASLBindRequest(null, "MECH-A", null));
    final InterceptedSASLBindOperation o = new InterceptedSASLBindOperation(null, 1, requestOp);
    assertNotNull(o.toString());
    // Test methods for a generic intercepted operation.
    assertNull(o.getClientConnection());
    assertEquals(o.getConnectionID(), -1L);
    assertNull(o.getConnectedAddress());
    assertEquals(o.getConnectedPort(), -1);
    assertEquals(o.getMessageID(), 1);
    assertNull(o.getProperty("propX"));
    o.setProperty("propX", "valX");
    assertNotNull(o.getProperty("propX"));
    assertEquals(o.getProperty("propX"), "valX");
    assertNotNull(o.toString());
    o.setProperty("propX", null);
    assertNull(o.getProperty("propX"));
    // Test methods specific to an intercepted SASL bind operation.
    assertNotNull(o.getRequest());
    assertEquals(o.getRequest().getSASLMechanismName(), "MECH-A");
    assertNotNull(o.toString());
    final GenericSASLBindRequest r = new GenericSASLBindRequest(null, "MECH-B", null);
    o.setRequest(r);
    assertNotNull(o.getRequest());
    assertEquals(o.getRequest().getSASLMechanismName(), "MECH-B");
    assertNotNull(o.toString());
    assertNull(o.getResult());
    o.setResult(new BindResult(o.getMessageID(), ResultCode.SUCCESS, null, null, null, null));
    assertNotNull(o.getResult());
    assertNotNull(o.toString());
}
Also used : GenericSASLBindRequest(com.unboundid.ldap.sdk.GenericSASLBindRequest) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) BindResult(com.unboundid.ldap.sdk.BindResult) Test(org.testng.annotations.Test)

Example 5 with BindRequestProtocolOp

use of com.unboundid.ldap.protocol.BindRequestProtocolOp in project ldapsdk by pingidentity.

the class CannedResponseRequestHandlerTestCase method testDefaultConstructor.

/**
 * Tests the behavior of the request handler with the default configuration.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testDefaultConstructor() throws Exception {
    final CannedResponseRequestHandler handler = new CannedResponseRequestHandler().newInstance(null);
    LDAPMessage m = handler.processAddRequest(1, new AddRequestProtocolOp("dc=example,dc=com", Arrays.asList(new Attribute("objectClass", "top", "domain"), new Attribute("dc", "example"))), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 1);
    assertTrue(m.getProtocolOp() instanceof AddResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processBindRequest(2, new BindRequestProtocolOp("uid=admin,dc=example,dc=com", "password"), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 2);
    assertTrue(m.getProtocolOp() instanceof BindResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processCompareRequest(3, new CompareRequestProtocolOp("dc=example,dc=com", "objectClass", new ASN1OctetString("top")), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 3);
    assertTrue(m.getProtocolOp() instanceof CompareResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processDeleteRequest(4, new DeleteRequestProtocolOp("dc=example,dc=com"), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 4);
    assertTrue(m.getProtocolOp() instanceof DeleteResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processExtendedRequest(5, new ExtendedRequestProtocolOp("1.2.3.4", null), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 5);
    assertTrue(m.getProtocolOp() instanceof ExtendedResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processModifyRequest(6, new ModifyRequestProtocolOp("dc=example,dc=com", Arrays.asList(new Modification(ModificationType.REPLACE, "description", "foo"))), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 6);
    assertTrue(m.getProtocolOp() instanceof ModifyResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processModifyDNRequest(6, new ModifyDNRequestProtocolOp("ou=People,dc=example,dc=com", "ou=Users", true, null), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 6);
    assertTrue(m.getProtocolOp() instanceof ModifyDNResponseProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
    m = handler.processSearchRequest(7, new SearchRequestProtocolOp("dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, Filter.createEqualityFilter("uid", "test"), Arrays.<String>asList()), Collections.<Control>emptyList());
    assertNotNull(m);
    assertEquals(m.getMessageID(), 7);
    assertTrue(m.getProtocolOp() instanceof SearchResultDoneProtocolOp);
    assertNotNull(m.getControls());
    assertTrue(m.getControls().isEmpty());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Modification(com.unboundid.ldap.sdk.Modification) ExtendedResponseProtocolOp(com.unboundid.ldap.protocol.ExtendedResponseProtocolOp) Attribute(com.unboundid.ldap.sdk.Attribute) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) AddRequestProtocolOp(com.unboundid.ldap.protocol.AddRequestProtocolOp) CompareRequestProtocolOp(com.unboundid.ldap.protocol.CompareRequestProtocolOp) DeleteResponseProtocolOp(com.unboundid.ldap.protocol.DeleteResponseProtocolOp) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) AddResponseProtocolOp(com.unboundid.ldap.protocol.AddResponseProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ModifyDNResponseProtocolOp(com.unboundid.ldap.protocol.ModifyDNResponseProtocolOp) ExtendedRequestProtocolOp(com.unboundid.ldap.protocol.ExtendedRequestProtocolOp) DeleteRequestProtocolOp(com.unboundid.ldap.protocol.DeleteRequestProtocolOp) ModifyRequestProtocolOp(com.unboundid.ldap.protocol.ModifyRequestProtocolOp) Control(com.unboundid.ldap.sdk.Control) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ModifyDNRequestProtocolOp(com.unboundid.ldap.protocol.ModifyDNRequestProtocolOp) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) CompareResponseProtocolOp(com.unboundid.ldap.protocol.CompareResponseProtocolOp) ModifyResponseProtocolOp(com.unboundid.ldap.protocol.ModifyResponseProtocolOp) Test(org.testng.annotations.Test)

Aggregations

BindRequestProtocolOp (com.unboundid.ldap.protocol.BindRequestProtocolOp)7 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)5 BindResponseProtocolOp (com.unboundid.ldap.protocol.BindResponseProtocolOp)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)3 BindResult (com.unboundid.ldap.sdk.BindResult)3 NotNull (com.unboundid.util.NotNull)3 Test (org.testng.annotations.Test)3 SearchRequestProtocolOp (com.unboundid.ldap.protocol.SearchRequestProtocolOp)2 SearchResultDoneProtocolOp (com.unboundid.ldap.protocol.SearchResultDoneProtocolOp)2 Control (com.unboundid.ldap.sdk.Control)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)2 ASN1Element (com.unboundid.asn1.ASN1Element)1 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)1 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)1 AddRequestProtocolOp (com.unboundid.ldap.protocol.AddRequestProtocolOp)1 AddResponseProtocolOp (com.unboundid.ldap.protocol.AddResponseProtocolOp)1 CompareRequestProtocolOp (com.unboundid.ldap.protocol.CompareRequestProtocolOp)1 CompareResponseProtocolOp (com.unboundid.ldap.protocol.CompareResponseProtocolOp)1 DeleteRequestProtocolOp (com.unboundid.ldap.protocol.DeleteRequestProtocolOp)1