Search in sources :

Example 1 with LDAPConnection

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));
}
Also used : LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Test(org.junit.Test)

Example 2 with LDAPConnection

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();
    }
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchResult(com.unboundid.ldap.sdk.SearchResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection)

Example 3 with LDAPConnection

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();
    }
}
Also used : AttributeModification(org.ldaptive.AttributeModification) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) Connection(org.ldaptive.Connection) ModifyOperation(org.ldaptive.ModifyOperation) ModifyRequest(org.ldaptive.ModifyRequest) IOException(java.io.IOException)

Example 4 with LDAPConnection

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();
        }
    }
}
Also used : LDAPException(com.unboundid.ldap.sdk.LDAPException) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) BindResult(com.unboundid.ldap.sdk.BindResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) SSLContext(javax.net.ssl.SSLContext) StartTLSExtendedRequest(com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest)

Example 5 with LDAPConnection

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);
        }
    }
}
Also used : LdapMappingException(org.gluu.site.ldap.persistence.exception.LdapMappingException) LDIFWriter(com.unboundid.ldif.LDIFWriter) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) IOException(java.io.IOException) LdifDataUtility(org.gluu.site.ldap.persistence.LdifDataUtility) IOException(java.io.IOException) LdapMappingException(org.gluu.site.ldap.persistence.exception.LdapMappingException) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)11 LDAPException (com.unboundid.ldap.sdk.LDAPException)7 IOException (java.io.IOException)3 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)2 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)2 SearchResult (com.unboundid.ldap.sdk.SearchResult)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 StartTLSExtendedRequest (com.unboundid.ldap.sdk.extensions.StartTLSExtendedRequest)2 SSLUtil (com.unboundid.util.ssl.SSLUtil)2 GeneralSecurityException (java.security.GeneralSecurityException)2 LdifDataUtility (org.gluu.site.ldap.persistence.LdifDataUtility)2 LdapMappingException (org.gluu.site.ldap.persistence.exception.LdapMappingException)2 BindResult (com.unboundid.ldap.sdk.BindResult)1 LDAPConnectionOptions (com.unboundid.ldap.sdk.LDAPConnectionOptions)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 LDIFReader (com.unboundid.ldif.LDIFReader)1 LDIFWriter (com.unboundid.ldif.LDIFWriter)1 HostNameSSLSocketVerifier (com.unboundid.util.ssl.HostNameSSLSocketVerifier)1 TrustAllTrustManager (com.unboundid.util.ssl.TrustAllTrustManager)1 TrustStoreTrustManager (com.unboundid.util.ssl.TrustStoreTrustManager)1