Search in sources :

Example 1 with LdapException

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

the class LdapServiceRegistryDao method save.

@Override
public RegisteredService save(final RegisteredService rs) {
    if (rs.getId() != RegisteredService.INITIAL_IDENTIFIER_VALUE) {
        return update(rs);
    }
    try {
        final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.baseDn, rs);
        LdapUtils.executeAddOperation(this.connectionFactory, entry);
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return rs;
}
Also used : LdapEntry(org.ldaptive.LdapEntry) LdapException(org.ldaptive.LdapException)

Example 2 with LdapException

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

the class LdapConsentRepository method readConsentEntry.

/**
 * Fetches a user entry along with its consent attributes.
 *
 * @param principal user name
 * @return the user's LDAP entry
 */
private LdapEntry readConsentEntry(final String principal) {
    try {
        final SearchFilter filter = LdapUtils.newLdaptiveSearchFilter(this.searchFilter, CollectionUtils.wrap(Arrays.asList(principal)));
        LOGGER.debug("Locating consent LDAP entry via filter [{}] based on attribute [{}]", filter, this.ldap.getConsentAttributeName());
        final Response<SearchResult> response = LdapUtils.executeSearchOperation(this.connectionFactory, this.ldap.getBaseDn(), filter, this.ldap.getConsentAttributeName());
        if (LdapUtils.containsResultEntry(response)) {
            final LdapEntry entry = response.getResult().getEntry();
            LOGGER.debug("Locating consent LDAP entry [{}]", entry);
            return entry;
        }
    } catch (final LdapException e) {
        LOGGER.debug(e.getMessage(), e);
    }
    return null;
}
Also used : SearchFilter(org.ldaptive.SearchFilter) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) LdapException(org.ldaptive.LdapException)

Example 3 with LdapException

use of org.ldaptive.LdapException 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
 */
public static boolean executeDeleteOperation(final ConnectionFactory connectionFactory, final LdapEntry entry) {
    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 4 with LdapException

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

the class LdaptiveResourceCRLFetcher method fetchCRLFromLdap.

/**
 * Downloads a CRL from given LDAP url.
 *
 * @param r the resource that is the ldap url.
 * @return the x 509 cRL
 * @throws IOException          the exception thrown if resources cant be fetched
 * @throws CRLException         the exception thrown if resources cant be fetched
 * @throws CertificateException if connection to ldap fails, or attribute to get the revocation list is unavailable
 */
protected X509CRL fetchCRLFromLdap(final Object r) throws CertificateException, IOException, CRLException {
    try {
        final String ldapURL = r.toString();
        LOGGER.debug("Fetching CRL from ldap [{}]", ldapURL);
        final Response<SearchResult> result = performLdapSearch(ldapURL);
        if (result.getResultCode() == ResultCode.SUCCESS) {
            final LdapEntry entry = result.getResult().getEntry();
            final LdapAttribute attribute = entry.getAttribute(this.certificateAttribute);
            if (attribute.isBinary()) {
                LOGGER.debug("Located entry [{}]. Retrieving first attribute [{}]", entry, attribute);
                return fetchX509CRLFromAttribute(attribute);
            }
            LOGGER.warn("Found certificate attribute [{}] but it is not marked as a binary attribute", this.certificateAttribute);
        }
        LOGGER.debug("Failed to execute the search [{}]", result);
        throw new CertificateException("Failed to establish a connection ldap and search.");
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
        throw new CertificateException(e.getMessage());
    }
}
Also used : LdapAttribute(org.ldaptive.LdapAttribute) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) CertificateException(java.security.cert.CertificateException) LdapException(org.ldaptive.LdapException)

Example 5 with LdapException

use of org.ldaptive.LdapException 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) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) ModifyOperation(org.ldaptive.ModifyOperation) ModifyRequest(org.ldaptive.ModifyRequest) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) LdapException(org.ldaptive.LdapException)

Aggregations

LdapException (org.ldaptive.LdapException)11 LdapEntry (org.ldaptive.LdapEntry)6 SearchResult (org.ldaptive.SearchResult)6 Connection (org.ldaptive.Connection)4 SearchFilter (org.ldaptive.SearchFilter)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 AttributeModification (org.ldaptive.AttributeModification)2 Credential (org.ldaptive.Credential)2 DeleteOperation (org.ldaptive.DeleteOperation)2 DeleteRequest (org.ldaptive.DeleteRequest)2 LdapAttribute (org.ldaptive.LdapAttribute)2 ModifyOperation (org.ldaptive.ModifyOperation)2 ModifyRequest (org.ldaptive.ModifyRequest)2 Response (org.ldaptive.Response)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 URI (java.net.URI)1