Search in sources :

Example 1 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapUtils method executeModifyOperation.

/**
     * Execute modify operation boolean.
     *
     * @param currentDn         the current dn
     * @param connectionFactory the connection factory
     * @param attributes        the attributes
     * @return true/false
     */
public static boolean executeModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final Map<String, Set<String>> attributes) {
    try (Connection modifyConnection = createConnection(connectionFactory)) {
        final ModifyOperation operation = new ModifyOperation(modifyConnection);
        final List<AttributeModification> mods = attributes.entrySet().stream().map(entry -> new AttributeModification(AttributeModificationType.REPLACE, new LdapAttribute(entry.getKey(), entry.getValue().toArray(new String[] {})))).collect(Collectors.toList());
        final ModifyRequest request = new ModifyRequest(currentDn, mods.toArray(new AttributeModification[] {}));
        request.setReferralHandler(new ModifyReferralHandler());
        operation.execute(request);
        return true;
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : ConnectionFactory(org.ldaptive.ConnectionFactory) SearchOperation(org.ldaptive.SearchOperation) ReturnAttributes(org.ldaptive.ReturnAttributes) UnicodePwdAttribute(org.ldaptive.ad.UnicodePwdAttribute) URL(java.net.URL) AttributeModificationType(org.ldaptive.AttributeModificationType) LoggerFactory(org.slf4j.LoggerFactory) AddRequest(org.ldaptive.AddRequest) Beans(org.apereo.cas.configuration.support.Beans) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) ModifyOperation(org.ldaptive.ModifyOperation) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler) SearchResult(org.ldaptive.SearchResult) Map(java.util.Map) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) ModifyReferralHandler(org.ldaptive.referral.ModifyReferralHandler) URI(java.net.URI) DeleteRequest(org.ldaptive.DeleteRequest) Logger(org.slf4j.Logger) ResultCode(org.ldaptive.ResultCode) ModifyRequest(org.ldaptive.ModifyRequest) AttributeModification(org.ldaptive.AttributeModification) Set(java.util.Set) AbstractLdapProperties(org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) SearchRequest(org.ldaptive.SearchRequest) Collectors(java.util.stream.Collectors) Response(org.ldaptive.Response) StandardCharsets(java.nio.charset.StandardCharsets) Connection(org.ldaptive.Connection) List(java.util.List) DeleteOperation(org.ldaptive.DeleteOperation) DeleteReferralHandler(org.ldaptive.referral.DeleteReferralHandler) AddOperation(org.ldaptive.AddOperation) Credential(org.ldaptive.Credential) LdapAttribute(org.ldaptive.LdapAttribute) NumberUtils(org.apache.commons.lang3.math.NumberUtils) LdapEntry(org.ldaptive.LdapEntry) LdapException(org.ldaptive.LdapException) SearchFilter(org.ldaptive.SearchFilter) ModifyReferralHandler(org.ldaptive.referral.ModifyReferralHandler) AttributeModification(org.ldaptive.AttributeModification) Connection(org.ldaptive.Connection) LdapAttribute(org.ldaptive.LdapAttribute) ModifyOperation(org.ldaptive.ModifyOperation) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) ModifyRequest(org.ldaptive.ModifyRequest) LdapException(org.ldaptive.LdapException)

Example 2 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapUtils method executePasswordModifyOperation.

/**
     * Execute a password modify operation.
     *
     * @param currentDn         the current dn
     * @param connectionFactory the connection factory
     * @param oldPassword       the old password
     * @param newPassword       the new password
     * @param type              the type
     * @return true /false
     */
public static boolean executePasswordModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final String oldPassword, final String newPassword, final AbstractLdapProperties.LdapType type) {
    try (Connection modifyConnection = createConnection(connectionFactory)) {
        if (!modifyConnection.getConnectionConfig().getUseSSL() && !modifyConnection.getConnectionConfig().getUseStartTLS()) {
            LOGGER.warn("Executing password modification op under a non-secure LDAP connection; " + "To modify password attributes, the connection to the LDAP server SHOULD be secured and/or encrypted.");
        }
        if (type == AbstractLdapProperties.LdapType.AD) {
            LOGGER.debug("Executing password modification op for active directory based on " + "[https://support.microsoft.com/en-us/kb/269190]");
            final ModifyOperation operation = new ModifyOperation(modifyConnection);
            final Response response = operation.execute(new ModifyRequest(currentDn, new AttributeModification(AttributeModificationType.REPLACE, new UnicodePwdAttribute(newPassword))));
            LOGGER.debug("Result code [{}], message: [{}]", response.getResult(), response.getMessage());
            return response.getResultCode() == ResultCode.SUCCESS;
        }
        LOGGER.debug("Executing password modification op for generic LDAP");
        final PasswordModifyOperation operation = new PasswordModifyOperation(modifyConnection);
        final Response response = operation.execute(new PasswordModifyRequest(currentDn, StringUtils.isNotBlank(oldPassword) ? new Credential(oldPassword) : null, new Credential(newPassword)));
        LOGGER.debug("Result code [{}], message: [{}]", response.getResult(), response.getMessage());
        return response.getResultCode() == ResultCode.SUCCESS;
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : Response(org.ldaptive.Response) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) Credential(org.ldaptive.Credential) UnicodePwdAttribute(org.ldaptive.ad.UnicodePwdAttribute) AttributeModification(org.ldaptive.AttributeModification) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) Connection(org.ldaptive.Connection) ModifyOperation(org.ldaptive.ModifyOperation) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) ModifyRequest(org.ldaptive.ModifyRequest) LdapException(org.ldaptive.LdapException)

Example 3 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapUtils method executeSearchOperation.

/**
     * Execute search operation.
     *
     * @param connectionFactory the connection factory
     * @param baseDn            the base dn
     * @param filter            the filter
     * @param binaryAttributes  the binary attributes
     * @param returnAttributes  the return attributes
     * @return the response
     * @throws LdapException the ldap exception
     */
public static Response<SearchResult> executeSearchOperation(final ConnectionFactory connectionFactory, final String baseDn, final SearchFilter filter, final String[] binaryAttributes, final String[] returnAttributes) throws LdapException {
    try (Connection connection = createConnection(connectionFactory)) {
        final SearchOperation searchOperation = new SearchOperation(connection);
        final SearchRequest request = Beans.newLdaptiveSearchRequest(baseDn, filter, binaryAttributes, returnAttributes);
        request.setReferralHandler(new SearchReferralHandler());
        return searchOperation.execute(request);
    }
}
Also used : SearchRequest(org.ldaptive.SearchRequest) SearchOperation(org.ldaptive.SearchOperation) Connection(org.ldaptive.Connection) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler)

Example 4 with Connection

use of org.ldaptive.Connection in project cas by apereo.

the class LdapUtils method executeDeleteOperation.

/**
     * Execute delete operation boolean.
     *
     * @param connectionFactory the connection factory
     * @param entry             the entry
     * @return true/false
     * @throws LdapException the ldap exception
     */
public static boolean executeDeleteOperation(final ConnectionFactory connectionFactory, final LdapEntry entry) throws LdapException {
    try (Connection connection = createConnection(connectionFactory)) {
        final DeleteOperation delete = new DeleteOperation(connection);
        final DeleteRequest request = new DeleteRequest(entry.getDn());
        request.setReferralHandler(new DeleteReferralHandler());
        final Response<Void> res = delete.execute(request);
        return res.getResultCode() == ResultCode.SUCCESS;
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : DeleteOperation(org.ldaptive.DeleteOperation) Connection(org.ldaptive.Connection) DeleteRequest(org.ldaptive.DeleteRequest) DeleteReferralHandler(org.ldaptive.referral.DeleteReferralHandler) LdapException(org.ldaptive.LdapException)

Example 5 with Connection

use of org.ldaptive.Connection 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)

Aggregations

Connection (org.ldaptive.Connection)8 LdapException (org.ldaptive.LdapException)5 AttributeModification (org.ldaptive.AttributeModification)3 ModifyOperation (org.ldaptive.ModifyOperation)3 ModifyRequest (org.ldaptive.ModifyRequest)3 SearchOperation (org.ldaptive.SearchOperation)3 AddOperation (org.ldaptive.AddOperation)2 AddRequest (org.ldaptive.AddRequest)2 Credential (org.ldaptive.Credential)2 DeleteOperation (org.ldaptive.DeleteOperation)2 DeleteRequest (org.ldaptive.DeleteRequest)2 Response (org.ldaptive.Response)2 SearchRequest (org.ldaptive.SearchRequest)2 SearchResult (org.ldaptive.SearchResult)2 UnicodePwdAttribute (org.ldaptive.ad.UnicodePwdAttribute)2 PasswordModifyOperation (org.ldaptive.extended.PasswordModifyOperation)2 PasswordModifyRequest (org.ldaptive.extended.PasswordModifyRequest)2 DeleteReferralHandler (org.ldaptive.referral.DeleteReferralHandler)2 SearchReferralHandler (org.ldaptive.referral.SearchReferralHandler)2 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)1