Search in sources :

Example 6 with ConnectionFactory

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

Example 7 with ConnectionFactory

use of org.ldaptive.ConnectionFactory 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 = Beans.newLdaptivePooledConnectionFactory(spnegoProperties.getLdap());
    final SearchFilter filter = Beans.newLdaptiveSearchFilter(spnegoProperties.getLdap().getSearchFilter(), "host", Collections.emptyList());
    final SearchRequest searchRequest = Beans.newLdaptiveSearchRequest(spnegoProperties.getLdap().getBaseDn(), filter);
    return new LdapSpnegoKnownClientSystemsFilterAction(spnegoProperties.getIpsToCheckPattern(), spnegoProperties.getAlternativeRemoteHostAttribute(), spnegoProperties.getDnsTimeout(), 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 8 with ConnectionFactory

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

the class CasLdapUserDetailsManagerConfigurer method build.

private AuthorizationGenerator<CommonProfile> build() {
    final LdapAuthorizationProperties ldapAuthz = adminPagesSecurityProperties.getLdap().getLdapAuthz();
    final ConnectionFactory connectionFactory = Beans.newLdaptivePooledConnectionFactory(adminPagesSecurityProperties.getLdap());
    if (isGroupBasedAuthorization()) {
        LOGGER.debug("Handling LDAP authorization based on groups");
        return new LdapUserGroupsToRolesAuthorizationGenerator(connectionFactory, ldapAuthorizationGeneratorUserSearchExecutor(), ldapAuthz.isAllowMultipleResults(), ldapAuthz.getGroupAttribute(), ldapAuthz.getGroupPrefix(), ldapAuthorizationGeneratorGroupSearchExecutor());
    }
    LOGGER.debug("Handling LDAP authorization based on attributes and roles");
    return new LdapUserAttributesToRolesAuthorizationGenerator(connectionFactory, ldapAuthorizationGeneratorUserSearchExecutor(), ldapAuthz.isAllowMultipleResults(), ldapAuthz.getRoleAttribute(), ldapAuthz.getRolePrefix());
}
Also used : LdapUserAttributesToRolesAuthorizationGenerator(org.apereo.cas.authorization.LdapUserAttributesToRolesAuthorizationGenerator) LdapAuthorizationProperties(org.apereo.cas.configuration.model.support.ldap.LdapAuthorizationProperties) ConnectionFactory(org.ldaptive.ConnectionFactory) LdapUserGroupsToRolesAuthorizationGenerator(org.apereo.cas.authorization.LdapUserGroupsToRolesAuthorizationGenerator)

Example 9 with ConnectionFactory

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

the class LdapPasswordManagementService method change.

@Audit(action = "CHANGE_PASSWORD", actionResolverName = "CHANGE_PASSWORD_ACTION_RESOLVER", resourceResolverName = "CHANGE_PASSWORD_RESOURCE_RESOLVER")
@Override
public boolean change(final Credential credential, final PasswordChangeBean bean) {
    Assert.notNull(credential, "Credential cannot be null");
    Assert.notNull(bean, "PasswordChangeBean cannot be null");
    try {
        final PasswordManagementProperties.Ldap ldap = passwordManagementProperties.getLdap();
        final UsernamePasswordCredential c = (UsernamePasswordCredential) credential;
        final SearchFilter filter = Beans.newLdaptiveSearchFilter(ldap.getUserFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(c.getId()));
        LOGGER.debug("Constructed LDAP filter [{}] to update account password", filter);
        final ConnectionFactory factory = Beans.newLdaptivePooledConnectionFactory(ldap);
        final Response<SearchResult> response = LdapUtils.executeSearchOperation(factory, ldap.getBaseDn(), filter);
        LOGGER.debug("LDAP response to update password is [{}]", response);
        if (LdapUtils.containsResultEntry(response)) {
            final String dn = response.getResult().getEntry().getDn();
            LOGGER.debug("Updating account password for [{}]", dn);
            if (LdapUtils.executePasswordModifyOperation(dn, factory, c.getPassword(), bean.getPassword(), passwordManagementProperties.getLdap().getType())) {
                LOGGER.debug("Successfully updated the account password for [{}]", dn);
                return true;
            }
            LOGGER.error("Could not update the LDAP entry's password for [{}] and base DN [{}]", filter.format(), ldap.getBaseDn());
        } 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 false;
}
Also used : ConnectionFactory(org.ldaptive.ConnectionFactory) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties) SearchFilter(org.ldaptive.SearchFilter) SearchResult(org.ldaptive.SearchResult) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Audit(org.apereo.inspektr.audit.annotation.Audit)

Aggregations

ConnectionFactory (org.ldaptive.ConnectionFactory)9 SearchFilter (org.ldaptive.SearchFilter)5 SearchResult (org.ldaptive.SearchResult)4 RefreshScope (org.springframework.cloud.context.config.annotation.RefreshScope)4 Bean (org.springframework.context.annotation.Bean)4 PasswordManagementProperties (org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties)3 LdapAttribute (org.ldaptive.LdapAttribute)3 LdapEntry (org.ldaptive.LdapEntry)3 LdapUserAttributesToRolesAuthorizationGenerator (org.apereo.cas.authorization.LdapUserAttributesToRolesAuthorizationGenerator)2 LdapUserGroupsToRolesAuthorizationGenerator (org.apereo.cas.authorization.LdapUserGroupsToRolesAuthorizationGenerator)2 LdapAuthorizationProperties (org.apereo.cas.configuration.model.support.ldap.LdapAuthorizationProperties)2 URI (java.net.URI)1 URL (java.net.URL)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1