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);
}
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();
}
}
Aggregations