use of org.forgerock.opendj.ldap.responses.SearchResultEntry in project ddf by codice.
the class RoleClaimsHandler method retrieveClaimValues.
@Override
public ProcessedClaimCollection retrieveClaimValues(ClaimCollection claims, ClaimsParameters parameters) {
String[] attributes = { groupNameAttribute, memberNameAttribute };
ProcessedClaimCollection claimsColl = new ProcessedClaimCollection();
Connection connection = null;
try {
Principal principal = parameters.getPrincipal();
String user = AttributeMapLoader.getUser(principal);
if (user == null) {
LOGGER.info("Could not determine user name, possible authentication error. Returning no claims.");
return new ProcessedClaimCollection();
}
connection = connectionFactory.getConnection();
if (connection != null) {
BindRequest request = BindMethodChooser.selectBindMethod(bindMethod, bindUserDN, bindUserCredentials, kerberosRealm, kdcAddress);
BindResult bindResult = connection.bind(request);
String membershipValue = user;
AndFilter filter;
ConnectionEntryReader entryReader;
if (!membershipUserAttribute.equals(loginUserAttribute)) {
String baseDN = AttributeMapLoader.getBaseDN(principal, userBaseDn, overrideCertDn);
filter = new AndFilter();
filter.and(new EqualsFilter(this.getLoginUserAttribute(), user));
entryReader = connection.search(baseDN, SearchScope.WHOLE_SUBTREE, filter.toString(), membershipUserAttribute);
while (entryReader.hasNext()) {
SearchResultEntry entry = entryReader.readEntry();
Attribute attr = entry.getAttribute(membershipUserAttribute);
if (attr != null) {
for (ByteString value : attr) {
membershipValue = value.toString();
}
}
}
}
filter = new AndFilter();
String userBaseDN = AttributeMapLoader.getBaseDN(principal, getUserBaseDn(), overrideCertDn);
filter.and(new EqualsFilter("objectClass", getObjectClass())).and(new EqualsFilter(getMemberNameAttribute(), getMembershipUserAttribute() + "=" + membershipValue + "," + userBaseDN));
if (bindResult.isSuccess()) {
LOGGER.trace("Executing ldap search with base dn of {} and filter of {}", groupBaseDn, filter.toString());
entryReader = connection.search(groupBaseDn, SearchScope.WHOLE_SUBTREE, filter.toString(), attributes);
SearchResultEntry entry;
while (entryReader.hasNext()) {
entry = entryReader.readEntry();
Attribute attr = entry.getAttribute(groupNameAttribute);
if (attr == null) {
LOGGER.trace("Claim '{}' is null", roleClaimType);
} else {
ProcessedClaim c = new ProcessedClaim();
c.setClaimType(getRoleURI());
c.setPrincipal(principal);
for (ByteString value : attr) {
String itemValue = value.toString();
c.addValue(itemValue);
}
claimsColl.add(c);
}
}
} else {
LOGGER.info("LDAP Connection failed.");
}
}
} catch (LdapException e) {
LOGGER.info("Cannot connect to server, therefore unable to set role claims. Set log level for \"ddf.security.sts.claimsHandler\" to DEBUG for more information.");
LOGGER.debug("Cannot connect to server, therefore unable to set role claims.", e);
} catch (SearchResultReferenceIOException e) {
LOGGER.info("Unable to set role claims. Set log level for \"ddf.security.sts.claimsHandler\" to DEBUG for more information.");
LOGGER.debug("Unable to set role claims.", e);
} finally {
if (connection != null) {
connection.close();
}
}
return claimsColl;
}
use of org.forgerock.opendj.ldap.responses.SearchResultEntry in project admin-console-beta by connexta.
the class ServerGuesser method getChoices.
private List<String> getChoices(String query) {
List<String> baseContexts = getBaseContexts();
List<String> choices = new ArrayList<>();
for (String baseContext : baseContexts) {
try (ConnectionEntryReader reader = connection.search(baseContext, SearchScope.WHOLE_SUBTREE, query)) {
while (reader.hasNext()) {
if (!reader.isReference()) {
SearchResultEntry resultEntry = reader.readEntry();
choices.add(resultEntry.getName().toString());
} else {
// TODO RAP 07 Dec 16: What do we need to do with remote references?
reader.readReference();
}
}
} catch (IOException e) {
LOGGER.debug("Error getting choices", e);
}
}
return choices;
}
use of org.forgerock.opendj.ldap.responses.SearchResultEntry in project admin-console-beta by connexta.
the class LdapTestingUtils method getLdapQueryResults.
/**
* Executes a query against the ldap connection
*
* @param ldapConnection Ldap connection to run query on
* @param ldapSearchBaseDN Base DN to run the query on
* @param ldapQuery Query to perform
* @param searchScope Scope of query
* @param maxResults Max number of results to return from query. Use -1 for all results
* @param attributes Optional list of attributes for return projection; if null, then all
* attributes will be returned
* @return list of results
*/
public List<SearchResultEntry> getLdapQueryResults(Connection ldapConnection, String ldapSearchBaseDN, String ldapQuery, SearchScope searchScope, int maxResults, String... attributes) {
ConnectionEntryReader reader;
if (attributes == null) {
reader = ldapConnection.search(ldapSearchBaseDN, searchScope, ldapQuery);
} else {
reader = ldapConnection.search(ldapSearchBaseDN, searchScope, ldapQuery, attributes);
}
List<SearchResultEntry> entries = new ArrayList<>();
try {
while (entries.size() < maxResults && reader.hasNext()) {
if (!reader.isReference()) {
SearchResultEntry resultEntry = reader.readEntry();
entries.add(resultEntry);
} else {
reader.readReference();
}
}
} catch (IOException e) {
reader.close();
}
reader.close();
return entries;
}
use of org.forgerock.opendj.ldap.responses.SearchResultEntry in project admin-console-beta by connexta.
the class LdapTestDirectorySettings method checkReferencedUser.
boolean checkReferencedUser(Connection ldapConnection, SearchResultEntry group) {
boolean hasReferencedMember = false;
for (ByteString memberRef : group.getAttribute(settings.groupAttributeHoldingMember()).toArray()) {
// This memberRef will be in the format:
// memberAttributeReferencedInGroup + username + baseUserDN
// Strip the baseUserDN and query for the remainder as a Filter
// beneath the baseUserDN
List<String> split = Arrays.asList(memberRef.toString().split(","));
String userFilter = split.get(0);
String checkUserBase = String.join(",", split.subList(1, split.size()));
// found in a matched group
if (checkUserBase.toUpperCase().endsWith(settings.baseUserDn().toUpperCase()) && userFilter.split("=")[0].equalsIgnoreCase(settings.memberAttributeReferencedInGroup())) {
List<SearchResultEntry> foundMember = utils.getLdapQueryResults(ldapConnection, settings.baseUserDn(), userFilter, SearchScope.WHOLE_SUBTREE, 1);
if (!foundMember.isEmpty()) {
hasReferencedMember = true;
break;
}
}
}
return hasReferencedMember;
}
use of org.forgerock.opendj.ldap.responses.SearchResultEntry in project admin-console-beta by connexta.
the class ServerGuesser method getResults.
private List<SearchResultEntry> getResults(String query) {
List<String> baseContexts = getBaseContexts();
List<SearchResultEntry> results = new ArrayList<>();
for (String baseContext : baseContexts) {
try (ConnectionEntryReader reader = connection.search(baseContext, SearchScope.WHOLE_SUBTREE, query)) {
while (reader.hasNext()) {
if (!reader.isReference()) {
results.add(reader.readEntry());
} else {
// TODO RAP 07 Dec 16: What do we need to do with remote references?
reader.readReference();
}
}
} catch (IOException e) {
LOGGER.debug("Error getting query results", e);
}
}
return results;
}
Aggregations