Search in sources :

Example 56 with SearchResultEntry

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;
}
Also used : ProcessedClaimCollection(org.apache.cxf.sts.claims.ProcessedClaimCollection) Attribute(org.forgerock.opendj.ldap.Attribute) ByteString(org.forgerock.opendj.ldap.ByteString) Connection(org.forgerock.opendj.ldap.Connection) BindRequest(org.forgerock.opendj.ldap.requests.BindRequest) ByteString(org.forgerock.opendj.ldap.ByteString) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) AndFilter(org.springframework.ldap.filter.AndFilter) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ProcessedClaim(org.apache.cxf.sts.claims.ProcessedClaim) BindResult(org.forgerock.opendj.ldap.responses.BindResult) EqualsFilter(org.springframework.ldap.filter.EqualsFilter) LdapException(org.forgerock.opendj.ldap.LdapException) Principal(java.security.Principal) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 57 with SearchResultEntry

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;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 58 with SearchResultEntry

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;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 59 with SearchResultEntry

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;
}
Also used : ByteString(org.forgerock.opendj.ldap.ByteString) ByteString(org.forgerock.opendj.ldap.ByteString) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 60 with SearchResultEntry

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;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ArrayList(java.util.ArrayList) ByteString(org.forgerock.opendj.ldap.ByteString) IOException(java.io.IOException) SearchResultReferenceIOException(org.forgerock.opendj.ldap.SearchResultReferenceIOException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Aggregations

SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)62 LdapException (org.forgerock.opendj.ldap.LdapException)46 ByteString (org.forgerock.opendj.ldap.ByteString)43 Connection (org.forgerock.opendj.ldap.Connection)43 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)43 Attribute (org.forgerock.opendj.ldap.Attribute)30 HashSet (java.util.HashSet)25 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)24 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)19 IOException (java.io.IOException)18 PolicyException (com.sun.identity.policy.PolicyException)15 ResultCode (org.forgerock.opendj.ldap.ResultCode)15 SSOException (com.iplanet.sso.SSOException)14 DN (org.forgerock.opendj.ldap.DN)11 InvalidNameException (com.sun.identity.policy.InvalidNameException)10 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)10 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)10 ArrayList (java.util.ArrayList)9 BindResult (org.forgerock.opendj.ldap.responses.BindResult)8 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)7