Search in sources :

Example 6 with SearchRequest

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

the class Beans method newLdaptiveSearchRequest.

/**
     * Builds a new request.
     *
     * @param baseDn           the base dn
     * @param filter           the filter
     * @param binaryAttributes the binary attributes
     * @param returnAttributes the return attributes
     * @return the search request
     */
public static SearchRequest newLdaptiveSearchRequest(final String baseDn, final SearchFilter filter, final String[] binaryAttributes, final String[] returnAttributes) {
    final SearchRequest sr = new SearchRequest(baseDn, filter);
    sr.setBinaryAttributes(binaryAttributes);
    sr.setReturnAttributes(returnAttributes);
    sr.setSearchScope(SearchScope.SUBTREE);
    return sr;
}
Also used : SearchRequest(org.ldaptive.SearchRequest)

Example 7 with SearchRequest

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

the class LdapUtils 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(Beans.newDuration(l.getBlockWaitTime()));
    cp.setPoolConfig(pc);
    final IdlePruneStrategy strategy = new IdlePruneStrategy();
    strategy.setIdleTime(Beans.newDuration(l.getIdleTime()));
    strategy.setPrunePeriod(Beans.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(SearchScope.valueOf(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());
                LOGGER.debug("Created [{}] passivator for [{}]", l.getPoolPassivator(), l.getLdapUrl());
                break;
            case BIND:
                if (StringUtils.isNotBlank(l.getBindDn()) && StringUtils.isNoneBlank(l.getBindCredential())) {
                    final BindRequest bindRequest = new BindRequest();
                    bindRequest.setDn(l.getBindDn());
                    bindRequest.setCredential(new Credential(l.getBindCredential()));
                    cp.setPassivator(new BindPassivator(bindRequest));
                    LOGGER.debug("Created [{}] passivator for [{}]", l.getPoolPassivator(), l.getLdapUrl());
                } else {
                    final List values = Arrays.stream(AbstractLdapProperties.LdapConnectionPoolPassivator.values()).filter(v -> v != AbstractLdapProperties.LdapConnectionPoolPassivator.BIND).collect(Collectors.toList());
                    LOGGER.warn("[{}] pool passivator could not be created for [{}] given bind credentials are not specified. " + "If you are dealing with LDAP in such a way that does not require bind credentials, you may need to " + "set the pool passivator setting to one of [{}]", l.getPoolPassivator(), l.getLdapUrl(), values);
                }
                break;
            default:
                break;
        }
    }
    LOGGER.debug("Initializing ldap connection pool for [{}] and bindDn [{}]", l.getLdapUrl(), l.getBindDn());
    cp.initialize();
    return cp;
}
Also used : Arrays(java.util.Arrays) ConnectionFactory(org.ldaptive.ConnectionFactory) SearchOperation(org.ldaptive.SearchOperation) AddRequest(org.ldaptive.AddRequest) ExternalConfig(org.ldaptive.sasl.ExternalConfig) StringUtils(org.apache.commons.lang3.StringUtils) SearchEntryHandler(org.ldaptive.handler.SearchEntryHandler) ClassUtils(org.apache.commons.lang3.ClassUtils) ActivePassiveConnectionStrategy(org.ldaptive.ActivePassiveConnectionStrategy) FormatDnResolver(org.ldaptive.auth.FormatDnResolver) Map(java.util.Map) AbstractLdapAuthenticationProperties(org.apereo.cas.configuration.model.support.ldap.AbstractLdapAuthenticationProperties) PasswordPolicyControl(org.ldaptive.control.PasswordPolicyControl) ConnectionConfig(org.ldaptive.ConnectionConfig) BindPassivator(org.ldaptive.pool.BindPassivator) SaslConfig(org.ldaptive.sasl.SaslConfig) BindConnectionInitializer(org.ldaptive.BindConnectionInitializer) ModifyRequest(org.ldaptive.ModifyRequest) BlockingConnectionPool(org.ldaptive.pool.BlockingConnectionPool) Set(java.util.Set) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) DnsSrvConnectionStrategy(org.ldaptive.DnsSrvConnectionStrategy) SearchScope(org.ldaptive.SearchScope) Response(org.ldaptive.Response) StandardCharsets(java.nio.charset.StandardCharsets) Slf4j(lombok.extern.slf4j.Slf4j) AddOperation(org.ldaptive.AddOperation) LdapAttribute(org.ldaptive.LdapAttribute) LdapEntry(org.ldaptive.LdapEntry) CramMd5Config(org.ldaptive.sasl.CramMd5Config) ObjectGuidHandler(org.ldaptive.ad.handler.ObjectGuidHandler) SearchFilter(org.ldaptive.SearchFilter) RangeEntryHandler(org.ldaptive.ad.handler.RangeEntryHandler) ArrayList(java.util.ArrayList) UtilityClass(lombok.experimental.UtilityClass) IdlePruneStrategy(org.ldaptive.pool.IdlePruneStrategy) ModifyOperation(org.ldaptive.ModifyOperation) SearchResult(org.ldaptive.SearchResult) SearchValidator(org.ldaptive.pool.SearchValidator) CompareRequest(org.ldaptive.CompareRequest) AttributeModification(org.ldaptive.AttributeModification) SearchRequest(org.ldaptive.SearchRequest) DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) RoundRobinConnectionStrategy(org.ldaptive.RoundRobinConnectionStrategy) Mechanism(org.ldaptive.sasl.Mechanism) CaseChangeEntryHandler(org.ldaptive.handler.CaseChangeEntryHandler) KeyStoreCredentialConfig(org.ldaptive.ssl.KeyStoreCredentialConfig) LdapException(org.ldaptive.LdapException) SearchExecutor(org.ldaptive.SearchExecutor) ClosePassivator(org.ldaptive.pool.ClosePassivator) PooledSearchDnResolver(org.ldaptive.auth.PooledSearchDnResolver) UnicodePwdAttribute(org.ldaptive.ad.UnicodePwdAttribute) URL(java.net.URL) AttributeModificationType(org.ldaptive.AttributeModificationType) Beans(org.apereo.cas.configuration.support.Beans) PooledBindAuthenticationHandler(org.ldaptive.auth.PooledBindAuthenticationHandler) ConnectionPool(org.ldaptive.pool.ConnectionPool) DerefAliases(org.ldaptive.DerefAliases) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) DigestMd5Config(org.ldaptive.sasl.DigestMd5Config) URI(java.net.URI) DeleteRequest(org.ldaptive.DeleteRequest) PooledCompareAuthenticationHandler(org.ldaptive.auth.PooledCompareAuthenticationHandler) SslConfig(org.ldaptive.ssl.SslConfig) PoolConfig(org.ldaptive.pool.PoolConfig) PrimaryGroupIdHandler(org.ldaptive.ad.handler.PrimaryGroupIdHandler) X509CredentialConfig(org.ldaptive.ssl.X509CredentialConfig) AbstractLdapProperties(org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties) Collectors(java.util.stream.Collectors) DnAttributeEntryHandler(org.ldaptive.handler.DnAttributeEntryHandler) List(java.util.List) DeleteOperation(org.ldaptive.DeleteOperation) CompareValidator(org.ldaptive.pool.CompareValidator) BindRequest(org.ldaptive.BindRequest) IntStream(java.util.stream.IntStream) Provider(org.ldaptive.provider.Provider) ReturnAttributes(org.ldaptive.ReturnAttributes) DefaultConnectionStrategy(org.ldaptive.DefaultConnectionStrategy) HashSet(java.util.HashSet) EntryResolver(org.ldaptive.auth.EntryResolver) QualityOfProtection(org.ldaptive.sasl.QualityOfProtection) ModifyReferralHandler(org.ldaptive.referral.ModifyReferralHandler) RecursiveEntryHandler(org.ldaptive.handler.RecursiveEntryHandler) PooledSearchEntryResolver(org.ldaptive.auth.PooledSearchEntryResolver) RandomConnectionStrategy(org.ldaptive.RandomConnectionStrategy) ObjectSidHandler(org.ldaptive.ad.handler.ObjectSidHandler) ResultCode(org.ldaptive.ResultCode) FastBindOperation(org.ldaptive.ad.extended.FastBindOperation) GssApiConfig(org.ldaptive.sasl.GssApiConfig) Connection(org.ldaptive.Connection) Authenticator(org.ldaptive.auth.Authenticator) DeleteReferralHandler(org.ldaptive.referral.DeleteReferralHandler) Credential(org.ldaptive.Credential) MergeAttributeEntryHandler(org.ldaptive.handler.MergeAttributeEntryHandler) NumberUtils(org.apache.commons.lang3.math.NumberUtils) PooledConnectionFactory(org.ldaptive.pool.PooledConnectionFactory) SecurityStrength(org.ldaptive.sasl.SecurityStrength) DefaultConnectionFactory(org.ldaptive.DefaultConnectionFactory) IdlePruneStrategy(org.ldaptive.pool.IdlePruneStrategy) SearchRequest(org.ldaptive.SearchRequest) 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) ArrayList(java.util.ArrayList) List(java.util.List) SearchReferralHandler(org.ldaptive.referral.SearchReferralHandler)

Example 8 with SearchRequest

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

the class LdapUtils method newLdaptiveSearchRequest.

/**
 * Builds a new request.
 *
 * @param baseDn           the base dn
 * @param filter           the filter
 * @param binaryAttributes the binary attributes
 * @param returnAttributes the return attributes
 * @return the search request
 */
public static SearchRequest newLdaptiveSearchRequest(final String baseDn, final SearchFilter filter, final String[] binaryAttributes, final String[] returnAttributes) {
    final SearchRequest sr = new SearchRequest(baseDn, filter);
    sr.setBinaryAttributes(binaryAttributes);
    sr.setReturnAttributes(returnAttributes);
    sr.setSearchScope(SearchScope.SUBTREE);
    return sr;
}
Also used : SearchRequest(org.ldaptive.SearchRequest)

Example 9 with SearchRequest

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

the class SpnegoWebflowActionsConfiguration method ldapSpnegoClientAction.

@Lazy
@Bean
@RefreshScope
public Action ldapSpnegoClientAction() {
    final SpnegoProperties spnegoProperties = casProperties.getAuthn().getSpnego();
    final ConnectionFactory connectionFactory = LdapUtils.newLdaptivePooledConnectionFactory(spnegoProperties.getLdap());
    final SearchFilter filter = LdapUtils.newLdaptiveSearchFilter(spnegoProperties.getLdap().getSearchFilter(), "host", new ArrayList<>(0));
    final SearchRequest searchRequest = LdapUtils.newLdaptiveSearchRequest(spnegoProperties.getLdap().getBaseDn(), filter);
    return new LdapSpnegoKnownClientSystemsFilterAction(RegexUtils.createPattern(spnegoProperties.getIpsToCheckPattern()), spnegoProperties.getAlternativeRemoteHostAttribute(), Beans.newDuration(spnegoProperties.getDnsTimeout()).toMillis(), connectionFactory, searchRequest, spnegoProperties.getSpnegoAttributeName());
}
Also used : SearchRequest(org.ldaptive.SearchRequest) ConnectionFactory(org.ldaptive.ConnectionFactory) LdapSpnegoKnownClientSystemsFilterAction(org.apereo.cas.web.flow.client.LdapSpnegoKnownClientSystemsFilterAction) SpnegoProperties(org.apereo.cas.configuration.model.support.spnego.SpnegoProperties) SearchFilter(org.ldaptive.SearchFilter) Lazy(org.springframework.context.annotation.Lazy) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Bean(org.springframework.context.annotation.Bean)

Example 10 with SearchRequest

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

the class LdapUtils method newLdaptiveSearchRequest.

/**
 * New ldaptive search executor search executor.
 *
 * @param baseDn           the base dn
 * @param filterQuery      the filter query
 * @param params           the params
 * @param returnAttributes the return attributes
 * @return the search executor
 */
public static SearchRequest newLdaptiveSearchRequest(final String baseDn, final String filterQuery, final List<String> params, final String[] returnAttributes) {
    val request = new SearchRequest();
    request.setBaseDn(baseDn);
    request.setFilter(newLdaptiveSearchFilter(filterQuery, params));
    request.setReturnAttributes(returnAttributes);
    request.setSearchScope(SearchScope.SUBTREE);
    return request;
}
Also used : lombok.val(lombok.val) SearchRequest(org.ldaptive.SearchRequest)

Aggregations

SearchRequest (org.ldaptive.SearchRequest)10 lombok.val (lombok.val)3 AbstractLdapProperties (org.apereo.cas.configuration.model.support.ldap.AbstractLdapProperties)3 CompareRequest (org.ldaptive.CompareRequest)3 Credential (org.ldaptive.Credential)3 DefaultConnectionFactory (org.ldaptive.DefaultConnectionFactory)3 URI (java.net.URI)2 URL (java.net.URL)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 UtilityClass (lombok.experimental.UtilityClass)2 Slf4j (lombok.extern.slf4j.Slf4j)2 StringUtils (org.apache.commons.lang3.StringUtils)2