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