Search in sources :

Example 51 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 52 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 53 with SearchResultEntry

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

Example 54 with SearchResultEntry

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);
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) ArrayList(java.util.ArrayList) ByteString(org.forgerock.opendj.ldap.ByteString) LdapConnectionAttempt(org.codice.ddf.admin.ldap.commons.LdapConnectionAttempt) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) MapField(org.codice.ddf.admin.common.fields.common.MapField)

Example 55 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)

Aggregations

SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)55 LdapException (org.forgerock.opendj.ldap.LdapException)43 Connection (org.forgerock.opendj.ldap.Connection)39 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)38 ByteString (org.forgerock.opendj.ldap.ByteString)36 Attribute (org.forgerock.opendj.ldap.Attribute)28 HashSet (java.util.HashSet)22 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)20 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)19 IOException (java.io.IOException)16 PolicyException (com.sun.identity.policy.PolicyException)15 ResultCode (org.forgerock.opendj.ldap.ResultCode)15 SSOException (com.iplanet.sso.SSOException)14 InvalidNameException (com.sun.identity.policy.InvalidNameException)10 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)10 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)8 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)7 FileNotFoundException (java.io.FileNotFoundException)7 ArrayList (java.util.ArrayList)7 LinkedHashSet (java.util.LinkedHashSet)7