Search in sources :

Example 11 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project midpoint by Evolveum.

the class AbstractLdapTest method assertLdapPassword.

protected void assertLdapPassword(UserLdapConnectionConfig config, Entry entry, String password) throws LdapException, IOException, CursorException {
    LdapNetworkConnection conn = ldapConnect(config, entry.getDn().toString(), password);
    assertTrue("Not connected", conn.isConnected());
    assertTrue("Not authenticated", conn.isAuthenticated());
    // AD sometimes pretends to bind successfuly. Even though success is indicated, the bind in fact fails silently.
    // Therefore try to read my own entry.
    EntryCursor cursor = conn.search(entry.getDn(), "(objectclass=*)", SearchScope.OBJECT, "*");
    int foundEntries = 0;
    while (cursor.next()) {
        Entry entryFound = cursor.get();
        logger.trace("Search-after-auth found: {}", entryFound);
        foundEntries++;
    }
    cursor.close();
    logger.debug("Search-after-auth found {} entries", foundEntries);
    ldapDisconnect(conn);
    if (foundEntries != 1) {
        throw new SecurityException("Cannot read my own entry (" + entry.getDn() + ")");
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection)

Example 12 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project Singularity by HubSpot.

the class SingularityLDAPDatastore method getUser.

@Override
public Optional<SingularityUser> getUser(String user) {
    if (configuration.isStripUserEmailDomain()) {
        user = user.split("@")[0];
    }
    if (ldapCache.isPresent()) {
        Optional<SingularityUser> cachedResult = ldapCache.get().getIfPresent(user);
        if (cachedResult != null) {
            return cachedResult;
        }
    }
    final Set<String> groups = new HashSet<>();
    try {
        final LdapConnection connection = connectionPool.getConnection();
        try {
            checkState(connection.isConnected(), "not connected");
            checkState(connection.isAuthenticated(), "not authenticated");
            connection.bind();
            final long startTime = System.currentTimeMillis();
            try {
                final EntryCursor userCursor = connection.search(configuration.getUserBaseDN(), String.format(configuration.getUserFilter(), user), SearchScope.ONELEVEL, configuration.getUserNameAttribute(), configuration.getUserEmailAttribute());
                if (!userCursor.next()) {
                    if (ldapCache.isPresent()) {
                        ldapCache.get().put(user, Optional.empty());
                    }
                    return Optional.empty();
                }
                final Entry userEntry = userCursor.get();
                // get group info
                final EntryCursor cursor = connection.search(configuration.getGroupBaseDN(), String.format(configuration.getGroupFilter(), user), configuration.getGroupSearchScope(), configuration.getGroupNameAttribute());
                while (cursor.next()) {
                    groups.add(cursor.get().get(configuration.getGroupNameAttribute()).getString());
                }
                Optional<SingularityUser> result = Optional.of(new SingularityUser(user, Optional.ofNullable(Strings.emptyToNull(userEntry.get(configuration.getUserNameAttribute()).getString())), Optional.ofNullable(Strings.emptyToNull(userEntry.get(configuration.getUserEmailAttribute()).getString())), groups));
                if (ldapCache.isPresent()) {
                    ldapCache.get().put(user, result);
                }
                return result;
            } finally {
                LOG.trace("Loaded {}'s user data in {}", user, JavaUtils.duration(startTime));
                connection.unBind();
            }
        } finally {
            connectionPool.releaseConnection(connection);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) SingularityUser(com.hubspot.singularity.SingularityUser) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) HashSet(java.util.HashSet) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 13 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method readStringAttributes.

public Map<String, String> readStringAttributes(final String entryDN, final Set<String> attributes) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().readStringAttributes(entryDN, attributes);
    try {
        final EntryCursor entries = connection.search(entryDN, ChaiConstant.FILTER_OBJECTCLASS_ANY, org.apache.directory.api.ldap.model.message.SearchScope.OBJECT, attributes.toArray(new String[attributes.size()]));
        final Entry entry = entries.iterator().next();
        final Collection<Attribute> attrs = entry.getAttributes();
        final Map<String, String> returnMap = new LinkedHashMap<>();
        for (final Attribute attr : attrs) {
            final String name = attr.getId();
            final String value = attr.getString();
            returnMap.put(name, value);
        }
        return returnMap;
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project ldapchai by ldapchai.

the class ApacheLdapProviderImpl method readMultiAttribute.

private List<Value> readMultiAttribute(final String entryDN, final String attribute) throws ChaiOperationException {
    try {
        final EntryCursor entries = connection.search(entryDN, ChaiConstant.FILTER_OBJECTCLASS_ANY, org.apache.directory.api.ldap.model.message.SearchScope.OBJECT, attribute);
        final Entry entry = entries.iterator().next();
        final List<Value> returnSet = new ArrayList<>();
        final Attribute attr = entry.get(attribute);
        if (attr == null) {
            return null;
        }
        for (final Value value : attr) {
            if (value != null) {
                returnSet.add(value);
            }
        }
        return Collections.unmodifiableList(returnSet);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) BinaryValue(org.apache.directory.api.ldap.model.entry.BinaryValue) Value(org.apache.directory.api.ldap.model.entry.Value) StringValue(org.apache.directory.api.ldap.model.entry.StringValue) ArrayList(java.util.ArrayList) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 15 with EntryCursor

use of org.apache.directory.api.ldap.model.cursor.EntryCursor in project mxisd by kamax-io.

the class LdapThreePidProvider method lookup.

private Optional<String> lookup(LdapConnection conn, String medium, String value) {
    Optional<String> tPidQueryOpt = getCfg().getIdentity().getQuery(medium);
    if (!tPidQueryOpt.isPresent()) {
        log.warn("{} is not a configured 3PID type for LDAP lookup", medium);
        return Optional.empty();
    }
    // we merge 3PID specific query with global/specific filter, if one exists.
    String tPidQuery = tPidQueryOpt.get().replaceAll(getCfg().getIdentity().getToken(), value);
    String searchQuery = buildWithFilter(tPidQuery, getCfg().getIdentity().getFilter());
    log.debug("Base DN: {}", getBaseDn());
    log.debug("Query: {}", searchQuery);
    log.debug("Attributes: {}", GsonUtil.build().toJson(getUidAtt()));
    try (EntryCursor cursor = conn.search(getBaseDn(), searchQuery, SearchScope.SUBTREE, getUidAtt())) {
        while (cursor.next()) {
            Entry entry = cursor.get();
            log.info("Found possible match, DN: {}", entry.getDn().getName());
            Optional<String> data = getAttribute(entry, getUidAtt());
            if (!data.isPresent()) {
                continue;
            }
            log.info("DN {} is a valid match", entry.getDn().getName());
            return Optional.of(buildMatrixIdFromUid(data.get()));
        }
    } catch (CursorLdapReferralException e) {
        log.warn("3PID {} is only available via referral, skipping", value);
    } catch (IOException | LdapException | CursorException e) {
        throw new InternalServerError(e);
    }
    return Optional.empty();
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Aggregations

EntryCursor (org.apache.directory.api.ldap.model.cursor.EntryCursor)19 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)15 Entry (org.apache.directory.api.ldap.model.entry.Entry)14 IOException (java.io.IOException)11 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)10 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)7 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)5 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)5 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)5 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)4 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)3 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)3 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)3 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)2 InternalServerError (io.kamax.mxisd.exception.InternalServerError)2 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)2 SearchRequestImpl (org.apache.directory.api.ldap.model.message.SearchRequestImpl)2