use of com.unboundid.ldap.sdk.LDAPConnection in project spring-boot by spring-projects.
the class EmbeddedLdapAutoConfigurationTests method testRandomPortWithValueAnnotation.
@Test
public void testRandomPortWithValueAnnotation() throws LDAPException {
EnvironmentTestUtils.addEnvironment(this.context, "spring.ldap.embedded.base-dn:dc=spring,dc=org");
this.context.register(EmbeddedLdapAutoConfiguration.class, LdapClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
LDAPConnection connection = this.context.getBean(LDAPConnection.class);
assertThat(connection.getConnectedPort()).isEqualTo(this.context.getEnvironment().getProperty("local.ldap.port", Integer.class));
}
use of com.unboundid.ldap.sdk.LDAPConnection in project keywhiz by square.
the class LdapAuthenticator method dnFromUsername.
private String dnFromUsername(String username) throws LDAPException, GeneralSecurityException {
String baseDN = config.getUserBaseDN();
String lookup = String.format("(%s=%s)", config.getUserAttribute(), username);
SearchRequest searchRequest = new SearchRequest(baseDN, SearchScope.SUB, lookup);
LDAPConnection connection = connectionFactory.getLDAPConnection();
try {
SearchResult sr = connection.search(searchRequest);
if (sr.getEntryCount() == 0) {
throw new LDAPException(ResultCode.INVALID_CREDENTIALS);
}
return sr.getSearchEntries().get(0).getDN();
} finally {
connection.close();
}
}
use of com.unboundid.ldap.sdk.LDAPConnection in project cas by apereo.
the class LdapTestUtils method modifyLdapEntry.
/**
* Modify ldap entry.
*
* @param serverCon the server con
* @param dn the dn
* @param attr the attr
* @param add the add
*/
public static void modifyLdapEntry(final LDAPConnection serverCon, final String dn, final LdapAttribute attr, final AttributeModificationType add) {
try {
final String address = "ldap://" + serverCon.getConnectedAddress() + ':' + serverCon.getConnectedPort();
try (Connection conn = DefaultConnectionFactory.getConnection(address)) {
try {
conn.open();
final ModifyOperation modify = new ModifyOperation(conn);
modify.execute(new ModifyRequest(dn, new AttributeModification(add, attr)));
} catch (final Exception e) {
LOGGER.debug(e.getMessage(), e);
}
}
} finally {
serverCon.close();
}
}
use of com.unboundid.ldap.sdk.LDAPConnection in project zm-mailbox by Zimbra.
the class UBIDLdapContext method ldapAuthenticate.
/**
* authenticate to LDAP server.
*
* This is method is called for:
* - external LDAP auth
* - auth to Zimbra LDAP server when the stored password is not SSHA.
*
* @param urls
* @param wantStartTLS
* @param bindDN
* @param password
* @param note
* @throws ServiceException
*/
private static void ldapAuthenticate(LdapServerConfig config, String bindDN, String password, LdapUsage usage) throws ServiceException {
/*
* About dereferencing alias.
*
* The legacy JNDI implementation supports specifying deref
* alias policy during bind, via the "java.naming.ldap.derefAliases"
* DirContext env property.
*
* Doesn't look like unboundid has an obvious way to specify
* deref alias policy during bind.
*
* The LDAP protocol http://tools.ietf.org/html/rfc4511 disallows
* LDAP server to deref alias during bind anyway.
*
* section 4.2
* ..., it SHALL NOT perform alias dereferencing.
*
* Therefore, we do *not* support dereferencing alias during bind anymore.
*
*/
boolean succeeded = false;
LdapServerPool serverPool = new LdapServerPool(config);
LDAPConnection connection = null;
BindResult bindResult = null;
long startTime = UBIDLdapOperation.GENERIC_OP.begin();
try {
if (InMemoryLdapServer.isOn()) {
connection = InMemoryLdapServer.getConnection();
password = InMemoryLdapServer.Password.treatPassword(password);
} else {
connection = serverPool.getServerSet().getConnection();
}
if (serverPool.getConnectionType() == LdapConnType.STARTTLS) {
SSLContext startTLSContext = LdapSSLUtil.createSSLContext(config.sslAllowUntrustedCerts());
ExtendedResult extendedResult = connection.processExtendedOperation(new StartTLSExtendedRequest(startTLSContext));
// response.
if (extendedResult.getResultCode() != ResultCode.SUCCESS) {
throw ServiceException.FAILURE("unable to send or receive startTLS extended operation", null);
}
}
bindResult = connection.bind(bindDN, password);
if (bindResult.getResultCode() != ResultCode.SUCCESS) {
throw ServiceException.FAILURE("unable to bind", null);
}
succeeded = true;
} catch (LDAPException e) {
throw UBIDLdapException.mapToExternalLdapException("unable to ldap authenticate", e);
} finally {
UBIDLdapOperation.GENERIC_OP.end(LdapOp.OPEN_CONN, usage, startTime, succeeded, bindResult, String.format("conn=[%s], url=[%s], connType=[%s], bindDN=[%s]", connection == null ? "null" : connection.getConnectionID(), serverPool.getRawUrls(), serverPool.getConnectionType().name(), bindDN));
if (connection != null) {
UBIDLogger.beforeOp(LdapOp.CLOSE_CONN, connection);
connection.close();
}
}
}
use of com.unboundid.ldap.sdk.LDAPConnection in project oxTrust by GluuFederation.
the class LdifService method exportLDIFFile.
public void exportLDIFFile(List<String> checkedItems, OutputStream output) throws LDAPException {
List<SearchResultEntry> result = null;
LDAPConnection connection = ldapEntryManager.getLdapOperationService().getConnection();
try {
LdifDataUtility ldifDataUtility = LdifDataUtility.instance();
result = ldifDataUtility.getAttributeResultEntryLDIF(connection, checkedItems, attributeService.getDnForAttribute(null));
} catch (Exception ex) {
log.error("Failed to export ldif file: ", ex);
} finally {
ldapEntryManager.getLdapOperationService().releaseConnection(connection);
}
if (result != null && result.size() > 0) {
// Write all of the matching entries to LDIF.
LDIFWriter ldifWriter;
try {
ldifWriter = new LDIFWriter(output);
for (SearchResultEntry entry : result) {
ldifWriter.writeEntry(entry);
}
ldifWriter.close();
} catch (IOException e) {
throw new LdapMappingException("Error writing to file, try again", e);
}
}
}
Aggregations