Search in sources :

Example 11 with LdapEntry

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

the class LdapServiceRegistry method insert.

private RegisteredService insert(final RegisteredService rs) {
    try {
        final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.baseDn, rs);
        LdapUtils.executeAddOperation(this.connectionFactory, entry);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return rs;
}
Also used : LdapEntry(org.ldaptive.LdapEntry) LdapException(org.ldaptive.LdapException)

Example 12 with LdapEntry

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

the class LdapServiceRegistry method update.

/**
 * Update the ldap entry with the given registered service.
 *
 * @param rs the rs
 * @return the registered service
 */
private RegisteredService update(final RegisteredService rs) {
    String currentDn = null;
    try {
        final Response<SearchResult> response = searchForServiceById(rs.getId());
        if (LdapUtils.containsResultEntry(response)) {
            currentDn = response.getResult().getEntry().getDn();
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    if (StringUtils.isNotBlank(currentDn)) {
        LOGGER.debug("Updating registered service at [{}]", currentDn);
        final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.baseDn, rs);
        LdapUtils.executeModifyOperation(currentDn, this.connectionFactory, entry);
    } else {
        LOGGER.debug("Failed to locate DN for registered service by id [{}]. Attempting to save the service anew", rs.getId());
        insert(rs);
    }
    return rs;
}
Also used : SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) ToString(lombok.ToString) LdapException(org.ldaptive.LdapException)

Example 13 with LdapEntry

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

the class LdapTestUtils method createLdapEntries.

/**
 * Creates the given LDAP entries.
 *
 * @param connection Open LDAP connection used to connect to directory.
 * @param entries    Collection of LDAP entries.
 */
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries) {
    try {
        for (final LdapEntry entry : entries) {
            final Collection<Attribute> attrs = new ArrayList<>(entry.getAttributeNames().length);
            attrs.addAll(entry.getAttributes().stream().map(a -> new Attribute(a.getName(), a.getStringValues())).collect(Collectors.toList()));
            final AddRequest ad = new AddRequest(entry.getDn(), attrs);
            connection.add(ad);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getLocalizedMessage());
    }
}
Also used : AddRequest(com.unboundid.ldap.sdk.AddRequest) Attribute(com.unboundid.ldap.sdk.Attribute) LdapAttribute(org.ldaptive.LdapAttribute) ArrayList(java.util.ArrayList) LdapEntry(org.ldaptive.LdapEntry) IOException(java.io.IOException)

Example 14 with LdapEntry

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

the class SurrogateLdapAuthenticationService method getEligibleAccountsForSurrogateToProxy.

@Override
public Collection<String> getEligibleAccountsForSurrogateToProxy(final String username) {
    final Collection<String> eligible = new LinkedHashSet<>();
    try {
        final SearchFilter filter = LdapUtils.newLdaptiveSearchFilter(ldapProperties.getSearchFilter(), CollectionUtils.wrap(username));
        LOGGER.debug("Using search filter: [{}]", filter);
        final Response<SearchResult> response = LdapUtils.executeSearchOperation(this.connectionFactory, ldapProperties.getBaseDn(), filter);
        LOGGER.debug("LDAP response: [{}]", response);
        if (!LdapUtils.containsResultEntry(response)) {
            return eligible;
        }
        final LdapEntry ldapEntry = response.getResult().getEntry();
        final LdapAttribute attribute = ldapEntry.getAttribute(ldapProperties.getMemberAttributeName());
        if (attribute == null || attribute.getStringValues().isEmpty()) {
            return eligible;
        }
        final Pattern pattern = RegexUtils.createPattern(ldapProperties.getMemberAttributeValueRegex());
        eligible.addAll(attribute.getStringValues().stream().map(pattern::matcher).filter(Matcher::matches).map(p -> p.group(1)).collect(Collectors.toList()));
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return eligible;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Pattern(java.util.regex.Pattern) LdapAttribute(org.ldaptive.LdapAttribute) SearchFilter(org.ldaptive.SearchFilter) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry)

Example 15 with LdapEntry

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

the class LdapConsentRepository method findConsentDecision.

@Override
public ConsentDecision findConsentDecision(final Service service, final RegisteredService registeredService, final Authentication authentication) {
    final String principal = authentication.getPrincipal().getId();
    final LdapEntry entry = readConsentEntry(principal);
    if (entry != null) {
        final LdapAttribute consentDecisions = entry.getAttribute(this.ldap.getConsentAttributeName());
        if (consentDecisions != null) {
            final Collection<String> values = consentDecisions.getStringValues();
            LOGGER.debug("Locating consent decision(s) for [{}] and service [{}]", principal, service.getId());
            return values.stream().map(LdapConsentRepository::mapFromJson).filter(d -> d.getService().equals(service.getId())).findFirst().orElse(null);
        }
    }
    return null;
}
Also used : LdapAttribute(org.ldaptive.LdapAttribute) LdapEntry(org.ldaptive.LdapEntry)

Aggregations

LdapEntry (org.ldaptive.LdapEntry)26 LdapAttribute (org.ldaptive.LdapAttribute)17 SearchResult (org.ldaptive.SearchResult)14 LdapException (org.ldaptive.LdapException)9 SearchFilter (org.ldaptive.SearchFilter)7 ArrayList (java.util.ArrayList)6 PasswordManagementProperties (org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties)4 ConnectionFactory (org.ldaptive.ConnectionFactory)4 AuthenticationResponse (org.ldaptive.auth.AuthenticationResponse)4 MessageDescriptor (org.apereo.cas.authentication.MessageDescriptor)3 Test (org.junit.Test)3 AccountState (org.ldaptive.auth.AccountState)3 LinkedHashMap (java.util.LinkedHashMap)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 AddRequest (com.unboundid.ldap.sdk.AddRequest)1 Attribute (com.unboundid.ldap.sdk.Attribute)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 CertificateException (java.security.cert.CertificateException)1 HashSet (java.util.HashSet)1