Search in sources :

Example 6 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);
            } else {
                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 7 with LdapException

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

the class LdapSpnegoKnownClientSystemsFilterAction method executeSearchForSpnegoAttribute.

/**
     * Searches the ldap instance for the attribute value.
     *
     * @param remoteIp the remote ip
     * @return true/false
     */
protected boolean executeSearchForSpnegoAttribute(final String remoteIp) {
    Connection connection = null;
    final String remoteHostName = getRemoteHostName(remoteIp);
    LOGGER.debug("Resolved remote hostname [{}] based on ip [{}]", remoteHostName, remoteIp);
    try {
        connection = createConnection();
        final Operation searchOperation = new SearchOperation(connection);
        this.searchRequest.getSearchFilter().setParameter(0, remoteHostName);
        LOGGER.debug("Using search filter [{}] on baseDn [{}]", this.searchRequest.getSearchFilter().format(), this.searchRequest.getBaseDn());
        final Response<SearchResult> searchResult = searchOperation.execute(this.searchRequest);
        if (searchResult.getResultCode() == ResultCode.SUCCESS) {
            return processSpnegoAttribute(searchResult);
        }
        throw new RuntimeException("Failed to establish a connection ldap. " + searchResult.getMessage());
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : SearchOperation(org.ldaptive.SearchOperation) Connection(org.ldaptive.Connection) SearchResult(org.ldaptive.SearchResult) SearchOperation(org.ldaptive.SearchOperation) Operation(org.ldaptive.Operation) LdapException(org.ldaptive.LdapException)

Example 8 with LdapException

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

the class LdapUtils method executeAddOperation.

/**
     * Execute add operation boolean.
     *
     * @param connectionFactory the connection factory
     * @param entry             the entry
     * @return true/false
     * @throws LdapException the ldap exception
     */
public static boolean executeAddOperation(final ConnectionFactory connectionFactory, final LdapEntry entry) throws LdapException {
    try (Connection connection = createConnection(connectionFactory)) {
        final AddOperation operation = new AddOperation(connection);
        operation.execute(new AddRequest(entry.getDn(), entry.getAttributes()));
        return true;
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : AddRequest(org.ldaptive.AddRequest) Connection(org.ldaptive.Connection) AddOperation(org.ldaptive.AddOperation) LdapException(org.ldaptive.LdapException)

Example 9 with LdapException

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

the class BaseUseAttributesAuthorizationGenerator method generate.

@Override
public CommonProfile generate(final WebContext context, final CommonProfile profile) {
    Assert.notNull(this.connectionFactory, "connectionFactory must not be null");
    Assert.notNull(this.userSearchExecutor, "userSearchExecutor must not be null");
    final String username = profile.getId();
    final SearchResult userResult;
    try {
        LOGGER.debug("Attempting to get details for user [{}].", username);
        final Response<SearchResult> response = this.userSearchExecutor.search(this.connectionFactory, Beans.newLdaptiveSearchFilter(this.userSearchExecutor.getSearchFilter().getFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(username)));
        LOGGER.debug("LDAP user search response: [{}]", response);
        userResult = response.getResult();
        if (userResult.size() == 0) {
            throw new RuntimeException(new AccountNotFoundException(username + " not found."));
        }
        if (userResult.size() > 1 && !this.allowMultipleResults) {
            throw new IllegalStateException("Found multiple results for user which is not allowed (allowMultipleResults=false).");
        }
        final LdapEntry userEntry = userResult.getEntry();
        return generateAuthorizationForLdapEntry(profile, userEntry);
    } catch (final LdapException e) {
        throw new RuntimeException("LDAP error fetching details for user.", e);
    }
}
Also used : SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) AccountNotFoundException(org.pac4j.core.exception.AccountNotFoundException) LdapException(org.ldaptive.LdapException)

Example 10 with LdapException

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

the class LdapUserGroupsToRolesAuthorizationGenerator method generateAuthorizationForLdapEntry.

@Override
protected CommonProfile generateAuthorizationForLdapEntry(final CommonProfile profile, final LdapEntry userEntry) {
    try {
        LOGGER.debug("Attempting to get roles for user [{}].", userEntry.getDn());
        final Response<SearchResult> response = this.groupSearchExecutor.search(this.connectionFactory, Beans.newLdaptiveSearchFilter(this.groupSearchExecutor.getSearchFilter().getFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(userEntry.getDn())));
        LOGGER.debug("LDAP role search response: [{}]", response);
        final SearchResult groupResult = response.getResult();
        for (final LdapEntry entry : groupResult.getEntries()) {
            final LdapAttribute groupAttribute = entry.getAttribute(this.groupAttributeName);
            if (groupAttribute == null) {
                LOGGER.warn("Role attribute not found on entry [{}]", entry);
                continue;
            }
            addProfileRolesFromAttributes(profile, groupAttribute, this.groupPrefix);
        }
    } catch (final LdapException e) {
        throw new RuntimeException("LDAP error fetching roles for user.", e);
    }
    return profile;
}
Also used : LdapAttribute(org.ldaptive.LdapAttribute) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) LdapException(org.ldaptive.LdapException)

Aggregations

LdapException (org.ldaptive.LdapException)10 Connection (org.ldaptive.Connection)5 LdapEntry (org.ldaptive.LdapEntry)5 SearchResult (org.ldaptive.SearchResult)5 LdapAttribute (org.ldaptive.LdapAttribute)3 AddOperation (org.ldaptive.AddOperation)2 AddRequest (org.ldaptive.AddRequest)2 AttributeModification (org.ldaptive.AttributeModification)2 Credential (org.ldaptive.Credential)2 DeleteOperation (org.ldaptive.DeleteOperation)2 DeleteRequest (org.ldaptive.DeleteRequest)2 ModifyOperation (org.ldaptive.ModifyOperation)2 ModifyRequest (org.ldaptive.ModifyRequest)2 Response (org.ldaptive.Response)2 SearchOperation (org.ldaptive.SearchOperation)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