Search code examples
javakotlinactive-directoryldapldap-query

LDAP query - Include query filter on result


I am performing a LDAP query that looks something like this:

(&(|(objectClass=ExternalAccount)(objectClass=Person))(|(uid=SOMETHING)))

And the result looks like this

cn=something,ou=something,o=something,dc=something,dc=something

I would like to have the value I placed on uid on the query associated with the result. Something like this would be ideal:

  uid=SOMETHING,cn=something,ou=something,o=something,dc=something,dc=something

The reason why I need this is because in the case I need to do something like

(&(|(objectClass=ExternalAccount)(objectClass=Person))(|(uid=SOMETHING1)(uid=SOMETHING2)))

I would like to have the result associated with the parameter it matches with, like this:

uid=SOMETHING1,cn=value1,ou=value1,o=value1,dc=value1,dc=value1
uid=SOMETHING2,cn=value2,ou=value2,o=value2,dc=value2,dc=value2

Is this possible? If so, how can I achieve it?

I am using javax.naming.directory libraries in the actual code. To test queries with a gui I am using LDAPAdmin.

Here is a code sample, for a single uid:

        fun getFromLDAP(uid: String): String? {
        return InitialDirContext(connectionsProperties).let { context ->
        val searchControls =
            SearchControls().also { controls -> controls.searchScope = SearchControls.SUBTREE_SCOPE }
        val searchString = "(&(|(objectClass=ExternAccount)(objectClass=Person))(uid=$uid))"

        context.search(config.searchBase, searchString, searchControls)
            .toList()
            .first()
            .also { context.close() }
    }
}

The Desired result would for the attributes.all call for the result of this function to contain also de uid, which is not happening right now.


Solution

  • It was an issue with permissions on the user being used to connect to the LDAP.