Search in sources :

Example 6 with CRAMMD5BindRequest

use of com.unboundid.ldap.sdk.CRAMMD5BindRequest in project ldapsdk by pingidentity.

the class InMemoryDirectoryServerTestCase method testBind.

/**
 * Provides a number of tests for bind processing.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBind() throws Exception {
    final InMemoryDirectoryServer ds = getTestDS(true, true);
    final LDAPConnectionOptions options = new LDAPConnectionOptions();
    options.setBindWithDNRequiresPassword(false);
    final LDAPConnection conn = ds.getConnection(options);
    // Test the ability to bind as a user in the data set with the right
    // password.
    BindResult bindResult = conn.bind("uid=test.user,ou=People,dc=example,dc=com", "password");
    assertEquals(bindResult.getResultCode(), ResultCode.SUCCESS);
    assertMissingMatchedDN(bindResult);
    // Test the ability to with additional bind credentials.
    bindResult = conn.bind("cn=Directory Manager", "password");
    assertEquals(bindResult.getResultCode(), ResultCode.SUCCESS);
    assertMissingMatchedDN(bindResult);
    // Test the ability to bind with anonymous credentials.
    bindResult = conn.bind("", "");
    assertEquals(bindResult.getResultCode(), ResultCode.SUCCESS);
    assertMissingMatchedDN(bindResult);
    // Test the behavior when trying to bind as a user that doesn't exist.
    try {
        conn.bind("uid=missing,dc=example,dc=com", "password");
        fail("Expected an exception when trying to bind as a user that doesn't " + "exist.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.INVALID_CREDENTIALS);
        assertMissingMatchedDN(le);
    }
    // regular user.
    try {
        conn.bind("uid=test.user,ou=People,dc=example,dc=com", "wrong");
        fail("Expected an exception when trying to bind with the wrong " + "password for a normal user.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.INVALID_CREDENTIALS);
        assertMissingMatchedDN(le);
    }
    // additional bind user.
    try {
        conn.bind("cn=Directory Manager", "wrong");
        fail("Expected an exception when trying to bind with the wrong " + "password for an additional bind user.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.INVALID_CREDENTIALS);
        assertMissingMatchedDN(le);
    }
    // Test the behavior when trying to bind with a malformed DN.
    try {
        conn.bind("malformed-user-dn", "password");
        fail("Expected an exception when trying to bind with a malformed DN.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.INVALID_DN_SYNTAX);
        assertMissingMatchedDN(le);
    }
    // password.
    try {
        conn.bind("uid=test.user,ou=People,dc=example,dc=com", "");
        fail("Expected an exception when trying to bind with an empty password " + "and non-empty DN.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.UNWILLING_TO_PERFORM);
        assertMissingMatchedDN(le);
    }
    // password.
    try {
        conn.bind("", "password");
        fail("Expected an exception when trying to bind with an empty DN " + "and non-empty password.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.INVALID_CREDENTIALS);
        assertMissingMatchedDN(le);
    }
    // Test the behavior when trying to bind as a user without a password.
    final LDAPResult addResult = conn.add("dn: uid=test.2,ou=People,dc=example,dc=com", "objectClass: top", "objectClass: person", "objectClass: organizationalPerson", "objectClass: inetOrgPerson", "uid: test.2", "givenName: Test", "sn: 2", "cn: Test 2");
    assertEquals(addResult.getResultCode(), ResultCode.SUCCESS);
    try {
        conn.bind("uid=test.2,ou=People,dc=example,dc=com", "password");
        fail("Expected an exception when trying to bind as a user without a " + "password.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.INVALID_CREDENTIALS);
        assertMissingMatchedDN(le);
    }
    // Test the behavior when trying to bind using SASL authentication.
    try {
        conn.bind(new CRAMMD5BindRequest("dn:uid=test.user,ou=People,dc=example,dc=com", "password"));
        fail("Expected an exception when trying to perform an unsupported SASL " + "bind.");
    } catch (final LDAPException le) {
        assertEquals(le.getResultCode(), ResultCode.AUTH_METHOD_NOT_SUPPORTED);
        assertMissingMatchedDN(le);
    }
    final Control[] unbindControls = { new Control("1.2.3.4", false), new Control("1.2.3.5", false, new ASN1OctetString("foo")) };
    conn.close(unbindControls);
}
Also used : LDAPConnectionOptions(com.unboundid.ldap.sdk.LDAPConnectionOptions) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) IgnoreNoUserModificationRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.IgnoreNoUserModificationRequestControl) AuthorizationIdentityResponseControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPResult(com.unboundid.ldap.sdk.LDAPResult) BindResult(com.unboundid.ldap.sdk.BindResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) CRAMMD5BindRequest(com.unboundid.ldap.sdk.CRAMMD5BindRequest) Test(org.testng.annotations.Test)

Example 7 with CRAMMD5BindRequest

use of com.unboundid.ldap.sdk.CRAMMD5BindRequest in project ldapsdk by pingidentity.

the class AuthRateThread method run.

/**
 * Performs all search processing for this thread.
 */
@Override()
public void run() {
    try {
        authThread.set(currentThread());
        runningThreads.incrementAndGet();
        try {
            startBarrier.await();
        } catch (final Exception e) {
            Debug.debugException(e);
        }
        while (!stopRequested.get()) {
            if (searchConnection == null) {
                try {
                    searchConnection = authRate.getConnection();
                } catch (final LDAPException le) {
                    Debug.debugException(le);
                    errorCounter.incrementAndGet();
                    final ResultCode rc = le.getResultCode();
                    rcCounter.increment(rc);
                    resultCode.compareAndSet(null, rc);
                    if (fixedRateBarrier != null) {
                        fixedRateBarrier.await();
                    }
                    continue;
                }
            }
            if (bindConnection == null) {
                try {
                    bindConnection = authRate.getConnection();
                } catch (final LDAPException le) {
                    Debug.debugException(le);
                    errorCounter.incrementAndGet();
                    final ResultCode rc = le.getResultCode();
                    rcCounter.increment(rc);
                    resultCode.compareAndSet(null, rc);
                    if (fixedRateBarrier != null) {
                        fixedRateBarrier.await();
                    }
                    continue;
                }
            }
            if (!bindOnly) {
                try {
                    searchRequest.setBaseDN(baseDN.nextValue());
                    searchRequest.setFilter(filter.nextValue());
                } catch (final LDAPException le) {
                    Debug.debugException(le);
                    errorCounter.incrementAndGet();
                    final ResultCode rc = le.getResultCode();
                    rcCounter.increment(rc);
                    resultCode.compareAndSet(null, rc);
                    continue;
                }
            }
            // wait until starting the next authorization.
            if (fixedRateBarrier != null) {
                fixedRateBarrier.await();
            }
            final long startTime = System.nanoTime();
            try {
                final String bindDN;
                if (bindOnly) {
                    bindDN = baseDN.nextValue();
                } else {
                    final SearchResult r = searchConnection.search(searchRequest);
                    switch(r.getEntryCount()) {
                        case 0:
                            errorCounter.incrementAndGet();
                            rcCounter.increment(ResultCode.NO_RESULTS_RETURNED);
                            resultCode.compareAndSet(null, ResultCode.NO_RESULTS_RETURNED);
                            continue;
                        case 1:
                            // This is acceptable, and we can continue processing.
                            bindDN = r.getSearchEntries().get(0).getDN();
                            break;
                        default:
                            errorCounter.incrementAndGet();
                            rcCounter.increment(ResultCode.MORE_RESULTS_TO_RETURN);
                            resultCode.compareAndSet(null, ResultCode.MORE_RESULTS_TO_RETURN);
                            continue;
                    }
                }
                BindRequest bindRequest = null;
                switch(authType) {
                    case AUTH_TYPE_SIMPLE:
                        bindRequest = new SimpleBindRequest(bindDN, userPassword, bindControls);
                        break;
                    case AUTH_TYPE_CRAM_MD5:
                        bindRequest = new CRAMMD5BindRequest("dn:" + bindDN, userPassword, bindControls);
                        break;
                    case AUTH_TYPE_DIGEST_MD5:
                        bindRequest = new DIGESTMD5BindRequest("dn:" + bindDN, null, userPassword, null, bindControls);
                        break;
                    case AUTH_TYPE_PLAIN:
                        bindRequest = new PLAINBindRequest("dn:" + bindDN, userPassword, bindControls);
                        break;
                }
                bindConnection.bind(bindRequest);
            } catch (final LDAPException le) {
                Debug.debugException(le);
                errorCounter.incrementAndGet();
                final ResultCode rc = le.getResultCode();
                rcCounter.increment(rc);
                resultCode.compareAndSet(null, rc);
                if (!le.getResultCode().isConnectionUsable()) {
                    searchConnection.close();
                    searchConnection = null;
                    bindConnection.close();
                    bindConnection = null;
                }
            } finally {
                authCounter.incrementAndGet();
                authDurations.addAndGet(System.nanoTime() - startTime);
            }
        }
    } finally {
        if (searchConnection != null) {
            searchConnection.close();
        }
        if (bindConnection != null) {
            bindConnection.close();
        }
        authThread.set(null);
        runningThreads.decrementAndGet();
    }
}
Also used : SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) BindRequest(com.unboundid.ldap.sdk.BindRequest) PLAINBindRequest(com.unboundid.ldap.sdk.PLAINBindRequest) DIGESTMD5BindRequest(com.unboundid.ldap.sdk.DIGESTMD5BindRequest) CRAMMD5BindRequest(com.unboundid.ldap.sdk.CRAMMD5BindRequest) SimpleBindRequest(com.unboundid.ldap.sdk.SimpleBindRequest) DIGESTMD5BindRequest(com.unboundid.ldap.sdk.DIGESTMD5BindRequest) PLAINBindRequest(com.unboundid.ldap.sdk.PLAINBindRequest) SearchResult(com.unboundid.ldap.sdk.SearchResult) CRAMMD5BindRequest(com.unboundid.ldap.sdk.CRAMMD5BindRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) ResultCode(com.unboundid.ldap.sdk.ResultCode)

Aggregations

CRAMMD5BindRequest (com.unboundid.ldap.sdk.CRAMMD5BindRequest)7 Test (org.testng.annotations.Test)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)4 PLAINBindRequest (com.unboundid.ldap.sdk.PLAINBindRequest)4 BindRequest (com.unboundid.ldap.sdk.BindRequest)3 DIGESTMD5BindRequest (com.unboundid.ldap.sdk.DIGESTMD5BindRequest)3 InMemoryDirectoryServer (com.unboundid.ldap.listener.InMemoryDirectoryServer)2 BindResult (com.unboundid.ldap.sdk.BindResult)2 EXTERNALBindRequest (com.unboundid.ldap.sdk.EXTERNALBindRequest)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)2 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)2 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 ANONYMOUSBindRequest (com.unboundid.ldap.sdk.ANONYMOUSBindRequest)1 Control (com.unboundid.ldap.sdk.Control)1 DN (com.unboundid.ldap.sdk.DN)1 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)1 GSSAPIBindRequest (com.unboundid.ldap.sdk.GSSAPIBindRequest)1 LDAPConnectionOptions (com.unboundid.ldap.sdk.LDAPConnectionOptions)1 LDAPResult (com.unboundid.ldap.sdk.LDAPResult)1 OAUTHBEARERBindRequest (com.unboundid.ldap.sdk.OAUTHBEARERBindRequest)1