Search in sources :

Example 1 with SearchFilter

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

the class Beans method newLdaptiveBlockingConnectionPool.

/**
     * New blocking connection pool connection pool.
     *
     * @param l the l
     * @return the connection pool
     */
public static ConnectionPool newLdaptiveBlockingConnectionPool(final AbstractLdapProperties l) {
    final DefaultConnectionFactory bindCf = newLdaptiveConnectionFactory(l);
    final PoolConfig pc = newLdaptivePoolConfig(l);
    final BlockingConnectionPool cp = new BlockingConnectionPool(pc, bindCf);
    cp.setBlockWaitTime(newDuration(l.getBlockWaitTime()));
    cp.setPoolConfig(pc);
    final IdlePruneStrategy strategy = new IdlePruneStrategy();
    strategy.setIdleTime(newDuration(l.getIdleTime()));
    strategy.setPrunePeriod(newDuration(l.getPrunePeriod()));
    cp.setPruneStrategy(strategy);
    switch(l.getValidator().getType().trim().toLowerCase()) {
        case "compare":
            final CompareRequest compareRequest = new CompareRequest();
            compareRequest.setDn(l.getValidator().getDn());
            compareRequest.setAttribute(new LdapAttribute(l.getValidator().getAttributeName(), l.getValidator().getAttributeValues().toArray(new String[] {})));
            compareRequest.setReferralHandler(new SearchReferralHandler());
            cp.setValidator(new CompareValidator(compareRequest));
            break;
        case "none":
            LOGGER.debug("No validator is configured for the LDAP connection pool of [{}]", l.getLdapUrl());
            break;
        case "search":
        default:
            final SearchRequest searchRequest = new SearchRequest();
            searchRequest.setBaseDn(l.getValidator().getBaseDn());
            searchRequest.setSearchFilter(new SearchFilter(l.getValidator().getSearchFilter()));
            searchRequest.setReturnAttributes(ReturnAttributes.NONE.value());
            searchRequest.setSearchScope(l.getValidator().getScope());
            searchRequest.setSizeLimit(1L);
            searchRequest.setReferralHandler(new SearchReferralHandler());
            cp.setValidator(new SearchValidator(searchRequest));
            break;
    }
    cp.setFailFastInitialize(l.isFailFast());
    if (StringUtils.isNotBlank(l.getPoolPassivator())) {
        final AbstractLdapProperties.LdapConnectionPoolPassivator pass = AbstractLdapProperties.LdapConnectionPoolPassivator.valueOf(l.getPoolPassivator().toUpperCase());
        switch(pass) {
            case CLOSE:
                cp.setPassivator(new ClosePassivator());
                break;
            case BIND:
                final BindRequest bindRequest = new BindRequest();
                bindRequest.setDn(l.getBindDn());
                bindRequest.setCredential(new Credential(l.getBindCredential()));
                cp.setPassivator(new BindPassivator(bindRequest));
                break;
            default:
                break;
        }
    }
    LOGGER.debug("Initializing ldap connection pool for [{}] and bindDn [{}]", l.getLdapUrl(), l.getBindDn());
    cp.initialize();
    return cp;
}
Also used : DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) IdlePruneStrategy(org.ldaptive.pool.IdlePruneStrategy) SearchRequest(org.ldaptive.SearchRequest) MongoCredential(com.mongodb.MongoCredential) Credential(org.ldaptive.Credential) ClosePassivator(org.ldaptive.pool.ClosePassivator) BindRequest(org.ldaptive.BindRequest) BlockingConnectionPool(org.ldaptive.pool.BlockingConnectionPool) SearchFilter(org.ldaptive.SearchFilter) AbstractLdapProperties(org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties) CompareRequest(org.ldaptive.CompareRequest) SearchValidator(org.ldaptive.pool.SearchValidator) CompareValidator(org.ldaptive.pool.CompareValidator) BindPassivator(org.ldaptive.pool.BindPassivator) LdapAttribute(org.ldaptive.LdapAttribute) PoolConfig(org.ldaptive.pool.PoolConfig) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler)

Example 2 with SearchFilter

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

the class Beans method newLdaptiveSearchFilter.

/**
     * Constructs a new search filter using {@link SearchExecutor#searchFilter} as a template and
     * the username as a parameter.
     *
     * @param filterQuery the query filter
     * @param paramName   the param name
     * @param params      the username
     * @return Search filter with parameters applied.
     */
public static SearchFilter newLdaptiveSearchFilter(final String filterQuery, final String paramName, final List<String> params) {
    final SearchFilter filter = new SearchFilter();
    filter.setFilter(filterQuery);
    if (params != null) {
        IntStream.range(0, params.size()).forEach(i -> {
            if (filter.getFilter().contains("{" + i + '}')) {
                filter.setParameter(i, params.get(i));
            } else {
                filter.setParameter(paramName, params.get(i));
            }
        });
    }
    LOGGER.debug("Constructed LDAP search filter [{}]", filter.format());
    return filter;
}
Also used : SearchFilter(org.ldaptive.SearchFilter)

Example 3 with SearchFilter

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

the class LdapUserGraphicalAuthenticationRepository method searchForId.

private Response<SearchResult> searchForId(final String id) throws LdapException {
    final GraphicalUserAuthenticationProperties gua = casProperties.getAuthn().getGua();
    final SearchFilter filter = Beans.newLdaptiveSearchFilter(gua.getLdap().getUserFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(id));
    return LdapUtils.executeSearchOperation(Beans.newLdaptiveConnectionFactory(gua.getLdap()), gua.getLdap().getBaseDn(), filter, new String[] { gua.getLdap().getImageAttribute() }, ReturnAttributes.NONE.value());
}
Also used : SearchFilter(org.ldaptive.SearchFilter) GraphicalUserAuthenticationProperties(org.apereo.cas.configuration.model.support.gua.GraphicalUserAuthenticationProperties)

Example 4 with SearchFilter

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

the class LdapPasswordManagementService method getSecurityQuestions.

@Override
public Map<String, String> getSecurityQuestions(final String username) {
    final Map<String, String> set = new LinkedHashMap<>();
    try {
        final PasswordManagementProperties.Ldap ldap = passwordManagementProperties.getLdap();
        final SearchFilter filter = Beans.newLdaptiveSearchFilter(ldap.getUserFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(username));
        LOGGER.debug("Constructed LDAP filter [{}] to locate security questions", filter);
        final ConnectionFactory factory = Beans.newLdaptivePooledConnectionFactory(ldap);
        final Response<SearchResult> response = LdapUtils.executeSearchOperation(factory, ldap.getBaseDn(), filter);
        LOGGER.debug("LDAP response for security questions [{}]", response);
        if (LdapUtils.containsResultEntry(response)) {
            final LdapEntry entry = response.getResult().getEntry();
            LOGGER.debug("Located LDAP entry [{}] in the response", entry);
            final Map<String, String> qs = passwordManagementProperties.getLdap().getSecurityQuestionsAttributes();
            LOGGER.debug("Security question attributes are defined to be [{}]", qs);
            qs.forEach((k, v) -> {
                final LdapAttribute q = entry.getAttribute(k);
                final LdapAttribute a = entry.getAttribute(v);
                if (q != null && a != null && StringUtils.isNotBlank(q.getStringValue()) && StringUtils.isNotBlank(a.getStringValue())) {
                    LOGGER.debug("Added security question [{}]", q.getStringValue());
                    set.put(q.getStringValue(), a.getStringValue());
                }
            });
        } else {
            LOGGER.debug("LDAP response did not contain a result for security questions");
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return set;
}
Also used : ConnectionFactory(org.ldaptive.ConnectionFactory) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties) LdapAttribute(org.ldaptive.LdapAttribute) SearchFilter(org.ldaptive.SearchFilter) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with SearchFilter

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

the class LdapPasswordManagementService method findEmail.

@Override
public String findEmail(final String username) {
    try {
        final PasswordManagementProperties.Ldap ldap = passwordManagementProperties.getLdap();
        final SearchFilter filter = Beans.newLdaptiveSearchFilter(ldap.getUserFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(username));
        LOGGER.debug("Constructed LDAP filter [{}] to locate account email", filter);
        final ConnectionFactory factory = Beans.newLdaptivePooledConnectionFactory(ldap);
        final Response<SearchResult> response = LdapUtils.executeSearchOperation(factory, ldap.getBaseDn(), filter);
        LOGGER.debug("LDAP response to locate account email is [{}]", response);
        if (LdapUtils.containsResultEntry(response)) {
            final LdapEntry entry = response.getResult().getEntry();
            LOGGER.debug("Found LDAP entry [{}] to use for the account email", entry);
            final String attributeName = passwordManagementProperties.getReset().getEmailAttribute();
            final LdapAttribute attr = entry.getAttribute(attributeName);
            if (attr != null) {
                final String email = attr.getStringValue();
                LOGGER.debug("Found email address [{}] for user [{}]. Validating...", email, username);
                if (EmailValidator.getInstance().isValid(email)) {
                    LOGGER.debug("Email address [{}] matches a valid email address", email);
                    return email;
                } else {
                    LOGGER.error("Email [{}] is not a valid address", email);
                }
            } else {
                LOGGER.error("Could not locate an LDAP attribute [{}] for [{}] and base DN [{}]", attributeName, filter.format(), ldap.getBaseDn());
            }
            return null;
        } else {
            LOGGER.error("Could not locate an LDAP entry for [{}] and base DN [{}]", filter.format(), ldap.getBaseDn());
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : ConnectionFactory(org.ldaptive.ConnectionFactory) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties) LdapAttribute(org.ldaptive.LdapAttribute) SearchFilter(org.ldaptive.SearchFilter) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry)

Aggregations

SearchFilter (org.ldaptive.SearchFilter)7 ConnectionFactory (org.ldaptive.ConnectionFactory)4 PasswordManagementProperties (org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties)3 LdapAttribute (org.ldaptive.LdapAttribute)3 SearchResult (org.ldaptive.SearchResult)3 LdapEntry (org.ldaptive.LdapEntry)2 SearchRequest (org.ldaptive.SearchRequest)2 MongoCredential (com.mongodb.MongoCredential)1 LinkedHashMap (java.util.LinkedHashMap)1 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)1 GraphicalUserAuthenticationProperties (org.apereo.cas.configuration.model.support.gua.GraphicalUserAuthenticationProperties)1 AbstractLdapProperties (org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties)1 SpnegoProperties (org.apereo.cas.configuration.model.support.spnego.SpnegoProperties)1 LdapSpnegoKnownClientSystemsFilterAction (org.apereo.cas.web.flow.client.LdapSpnegoKnownClientSystemsFilterAction)1 Audit (org.apereo.inspektr.audit.annotation.Audit)1 BindRequest (org.ldaptive.BindRequest)1 CompareRequest (org.ldaptive.CompareRequest)1 Credential (org.ldaptive.Credential)1 DefaultConnectionFactory (org.ldaptive.DefaultConnectionFactory)1 BindPassivator (org.ldaptive.pool.BindPassivator)1