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));
}
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));
}
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));
}
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);
}
}
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);
}
}
Aggregations