Search in sources :

Example 11 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project jackrabbit-oak by apache.

the class UnboundLookupConnectionValidatorTest method testValidateNotConnectedLookupReturnsNull.

@Test
public void testValidateNotConnectedLookupReturnsNull() throws Exception {
    LdapConnection connection = Mockito.mock(LdapConnection.class);
    when(connection.isConnected()).thenReturn(false);
    when(connection.lookup(Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE)).thenReturn(null);
    assertFalse(validator.validate(connection));
}
Also used : LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) Test(org.junit.Test)

Example 12 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project jackrabbit-oak by apache.

the class UnboundLookupConnectionValidatorTest method testValidate.

@Test
public void testValidate() throws Exception {
    LdapConnection connection = Mockito.mock(LdapConnection.class);
    when(connection.isConnected()).thenReturn(true);
    when(connection.lookup(Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE)).thenReturn(Mockito.mock(Entry.class));
    assertTrue(validator.validate(connection));
}
Also used : Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) Test(org.junit.Test)

Example 13 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project jackrabbit-oak by apache.

the class UnboundLookupConnectionValidatorTest method testValidateLookupReturnsNull.

@Test
public void testValidateLookupReturnsNull() throws Exception {
    LdapConnection connection = Mockito.mock(LdapConnection.class);
    when(connection.isConnected()).thenReturn(true);
    when(connection.lookup(Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE)).thenReturn(null);
    assertFalse(validator.validate(connection));
}
Also used : LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) Test(org.junit.Test)

Example 14 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project jackrabbit-oak by apache.

the class LdapIdentityProvider method getGroup.

@Override
public ExternalGroup getGroup(@Nonnull String name) throws ExternalIdentityException {
    DebugTimer timer = new DebugTimer();
    LdapConnection connection = connect();
    timer.mark("connect");
    try {
        Entry entry = getEntry(connection, config.getGroupConfig(), name, config.getCustomAttributes());
        timer.mark("lookup");
        if (log.isDebugEnabled()) {
            log.debug("getGroup({}) {}", name, timer.getString());
        }
        if (entry != null) {
            return createGroup(entry, name);
        } else {
            return null;
        }
    } catch (LdapException | CursorException e) {
        throw lookupFailedException(e, timer);
    } finally {
        disconnect(connection);
    }
}
Also used : DebugTimer(org.apache.jackrabbit.oak.commons.DebugTimer) Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 15 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project jackrabbit-oak by apache.

the class LdapIdentityProvider method getDeclaredGroupRefs.

// -----------------------------------------------------------< internal >---
/**
 * Collects the declared (direct) groups of an identity
 * @param ref reference to the identity
 * @return map of identities where the key is the DN of the LDAP entity
 */
Map<String, ExternalIdentityRef> getDeclaredGroupRefs(ExternalIdentityRef ref) throws ExternalIdentityException {
    if (!isMyRef(ref)) {
        return Collections.emptyMap();
    }
    String searchFilter = config.getMemberOfSearchFilter(ref.getId());
    LdapConnection connection = null;
    SearchCursor searchCursor = null;
    try {
        // Create the SearchRequest object
        SearchRequest req = new SearchRequestImpl();
        req.setScope(SearchScope.SUBTREE);
        String idAttribute = config.getGroupConfig().getIdAttribute();
        req.addAttributes(idAttribute == null ? SchemaConstants.NO_ATTRIBUTE : idAttribute);
        req.setTimeLimit((int) config.getSearchTimeout());
        req.setBase(new Dn(config.getGroupConfig().getBaseDN()));
        req.setFilter(searchFilter);
        if (log.isDebugEnabled()) {
            log.debug("getDeclaredGroupRefs: using SearchRequest {}.", req);
        }
        Map<String, ExternalIdentityRef> groups = new HashMap<String, ExternalIdentityRef>();
        DebugTimer timer = new DebugTimer();
        connection = connect();
        timer.mark("connect");
        searchCursor = connection.search(req);
        timer.mark("search");
        while (searchCursor.next()) {
            Response response = searchCursor.get();
            if (response instanceof SearchResultEntry) {
                Entry resultEntry = ((SearchResultEntry) response).getEntry();
                ExternalIdentityRef groupRef = new ExternalIdentityRef(resultEntry.getDn().toString(), this.getName());
                groups.put(groupRef.getId(), groupRef);
            }
        }
        timer.mark("iterate");
        if (log.isDebugEnabled()) {
            log.debug("getDeclaredGroupRefs: search below {} with {} found {} entries. {}", config.getGroupConfig().getBaseDN(), searchFilter, groups.size(), timer.getString());
        }
        return groups;
    } catch (Exception e) {
        log.error("Error during ldap membership search.", e);
        throw new ExternalIdentityException("Error during ldap membership search.", e);
    } finally {
        if (searchCursor != null) {
            try {
                searchCursor.close();
            } catch (IOException e) {
                log.warn("Failed to close search cursor.", e);
            }
        }
        disconnect(connection);
    }
}
Also used : SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) ExternalIdentityRef(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef) HashMap(java.util.HashMap) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) IOException(java.io.IOException) LoginException(javax.security.auth.login.LoginException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ExternalIdentityException(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) DebugTimer(org.apache.jackrabbit.oak.commons.DebugTimer) Response(org.apache.directory.api.ldap.model.message.Response) Entry(org.apache.directory.api.ldap.model.entry.Entry) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry) ExternalIdentityException(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry)

Aggregations

LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)178 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)164 ArrayList (java.util.ArrayList)89 FinderException (org.apache.directory.fortress.core.FinderException)73 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)63 Entry (org.apache.directory.api.ldap.model.entry.Entry)50 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)49 Modification (org.apache.directory.api.ldap.model.entry.Modification)43 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)41 UpdateException (org.apache.directory.fortress.core.UpdateException)41 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)37 LdapNoSuchObjectException (org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException)20 CreateException (org.apache.directory.fortress.core.CreateException)17 RemoveException (org.apache.directory.fortress.core.RemoveException)17 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)14 IOException (java.io.IOException)12 Permission (org.apache.directory.fortress.core.model.Permission)9 Dn (org.apache.directory.api.ldap.model.name.Dn)7 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)6 SecurityException (org.apache.directory.fortress.core.SecurityException)6