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 ServerGuesser method getBaseContexts.
public List<String> getBaseContexts() {
try {
ConnectionEntryReader reader = connection.search("", SearchScope.BASE_OBJECT, "(objectClass=*)", "namingContexts");
ArrayList<String> contexts = new ArrayList<>();
while (reader.hasNext()) {
SearchResultEntry entry = reader.readEntry();
if (entry.containsAttribute("namingContexts")) {
contexts.add(entry.getAttribute("namingContexts").firstValueAsString());
}
}
if (contexts.isEmpty()) {
contexts.add("");
}
return contexts;
} catch (LdapException | SearchResultReferenceIOException e) {
LOGGER.debug("Error getting baseContext", e);
return Collections.singletonList("");
}
}
use of org.forgerock.opendj.ldap.responses.SearchResultEntry in project admin-console-beta by connexta.
the class LdapQuery method performFunction.
@Override
public ListField<MapField> performFunction() {
LdapConnectionAttempt connectionAttempt = utils.bindUserToLdapConnection(conn, creds);
addResultMessages(connectionAttempt.messages());
if (!connectionAttempt.connection().isPresent()) {
return null;
}
List<SearchResultEntry> searchResults = utils.getLdapQueryResults(connectionAttempt.connection().get(), dn.getValue(), query.getValue(), SearchScope.WHOLE_SUBTREE, maxQueryResults.getValue());
List<MapField> convertedSearchResults = new ArrayList<>();
for (SearchResultEntry entry : searchResults) {
MapField entryMap = new MapField();
for (Attribute attri : entry.getAllAttributes()) {
entryMap.put("name", entry.getName().toString());
if (!attri.getAttributeDescriptionAsString().toLowerCase().contains("password")) {
List<String> attributeValueList = attri.parallelStream().map(ByteString::toString).collect(Collectors.toList());
String attributeValue = attributeValueList.size() == 1 ? attributeValueList.get(0) : attributeValueList.toString();
entryMap.put(attri.getAttributeDescriptionAsString(), attributeValue);
}
}
convertedSearchResults.add(entryMap);
}
return new ListFieldImpl<>(MapField.class).addAll(convertedSearchResults);
}
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;
}
Aggregations