Search in sources :

Example 1 with LdapEntry

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

the class LdapUserGraphicalAuthenticationRepository method getGraphics.

@Override
public ByteSource getGraphics(final String username) {
    try {
        final GraphicalUserAuthenticationProperties gua = casProperties.getAuthn().getGua();
        final Response<SearchResult> response = searchForId(username);
        if (LdapUtils.containsResultEntry(response)) {
            final LdapEntry entry = response.getResult().getEntry();
            final LdapAttribute attribute = entry.getAttribute(gua.getLdap().getImageAttribute());
            if (attribute != null && attribute.isBinary()) {
                return ByteSource.wrap(attribute.getBinaryValue());
            }
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return ByteSource.empty();
}
Also used : LdapAttribute(org.ldaptive.LdapAttribute) SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) GraphicalUserAuthenticationProperties(org.apereo.cas.configuration.model.support.gua.GraphicalUserAuthenticationProperties) LdapException(org.ldaptive.LdapException)

Example 2 with LdapEntry

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

the class LdapServiceRegistryDao method save.

@Override
public RegisteredService save(final RegisteredService rs) {
    if (rs.getId() != RegisteredService.INITIAL_IDENTIFIER_VALUE) {
        return update(rs);
    }
    try {
        final LdapEntry entry = this.ldapServiceMapper.mapFromRegisteredService(this.baseDn, rs);
        LdapUtils.executeAddOperation(this.connectionFactory, entry);
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return rs;
}
Also used : LdapEntry(org.ldaptive.LdapEntry) LdapException(org.ldaptive.LdapException)

Example 3 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.
     * @throws Exception On LDAP errors.
     */
public static void createLdapEntries(final LDAPConnection connection, final Collection<LdapEntry> entries) throws Exception {
    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.warn(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 4 with LdapEntry

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

the class AbstractX509LdapTests method populateCertificateRevocationListAttribute.

/**
     * Populate certificate revocation list attribute.
     * Dynamically set the attribute value to the crl content.
     * Encode it as base64 first. Doing this in the code rather
     * than in the ldif file to ensure the attribute can be populated
     * without dependencies on the classpath and or filesystem.
     * @throws Exception the exception
     */
private static void populateCertificateRevocationListAttribute() throws Exception {
    final Collection<LdapEntry> col = getDirectory().getLdapEntries();
    for (final LdapEntry ldapEntry : col) {
        if (ldapEntry.getDn().equals(DN)) {
            final LdapAttribute attr = new LdapAttribute(true);
            byte[] value = new byte[1024];
            IOUtils.read(new ClassPathResource("userCA-valid.crl").getInputStream(), value);
            value = EncodingUtils.encodeBase64ToByteArray(value);
            attr.setName("certificateRevocationList");
            attr.addBinaryValue(value);
            LdapTestUtils.modifyLdapEntry(getDirectory().getConnection(), ldapEntry, attr);
        }
    }
}
Also used : LdapAttribute(org.ldaptive.LdapAttribute) LdapEntry(org.ldaptive.LdapEntry) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 5 with LdapEntry

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

Aggregations

LdapEntry (org.ldaptive.LdapEntry)13 LdapAttribute (org.ldaptive.LdapAttribute)9 SearchResult (org.ldaptive.SearchResult)8 LdapException (org.ldaptive.LdapException)6 ArrayList (java.util.ArrayList)3 PasswordManagementProperties (org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties)2 ConnectionFactory (org.ldaptive.ConnectionFactory)2 SearchFilter (org.ldaptive.SearchFilter)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 LinkedHashMap (java.util.LinkedHashMap)1 GraphicalUserAuthenticationProperties (org.apereo.cas.configuration.model.support.gua.GraphicalUserAuthenticationProperties)1 AbstractRegisteredService (org.apereo.cas.services.AbstractRegisteredService)1 AuthenticationRequest (org.ldaptive.auth.AuthenticationRequest)1 AuthenticationResponse (org.ldaptive.auth.AuthenticationResponse)1 Authenticator (org.ldaptive.auth.Authenticator)1 RequireAnyRoleAuthorizer (org.pac4j.core.authorization.authorizer.RequireAnyRoleAuthorizer)1