public class LdapManager extends Object
getInstance() method
should be used to get an instace. The following properties configure this manager:
| Modifier and Type | Field and Description |
|---|---|
static SystemProperty<Integer> |
LDAP_PAGE_SIZE |
| Constructor and Description |
|---|
LdapManager(Map<String,String> properties)
Constructs a new LdapManager instance.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
checkAuthentication(Rdn[] userRDN,
String password)
Returns true if the user is able to successfully authenticate against
the LDAP server.
|
static LdapName |
createNewAbsolute(LdapName base,
Rdn[] relative) |
static LdapName |
escapeForJNDI(Rdn... rdn)
Returns a JNDI Name for an array of RDNs that is suitable to use to
access LDAP through JNDI.
|
LdapName |
findGroupAbsoluteDN(String groupname)
Like
findGroupRDN(String) but returns the absolute DN of a group |
Rdn[] |
findGroupRDN(String groupname)
Finds a groups's RDN using it's group name.
|
Rdn[] |
findGroupRDN(String groupname,
LdapName baseDN)
Finds a groups's dn using it's group name.
|
Rdn[] |
findUserRDN(String username)
Finds a user's RDN using their username.
|
Rdn[] |
findUserRDN(String username,
LdapName baseDN)
Finds a user's RDN using their username in the specified baseDN.
|
String |
getAdminDN()
Returns the starting admin DN that searches for admins will performed with.
|
String |
getAdminPassword()
Returns the starting admin DN that searches for admins will performed with.
|
LdapName |
getAlternateBaseDN()
Returns the alternate starting DN that searches for users will performed with.
|
LdapName |
getBaseDN()
Returns the starting DN that searches for users will performed with.
|
LdapContext |
getContext()
Returns a DirContext for the LDAP server that can be used to perform
lookups and searches using the default base DN.
|
LdapContext |
getContext(LdapName baseDN)
Returns a DirContext for the LDAP server that can be used to perform
lookups and searches using the specified base DN.
|
String |
getEmailField()
Returns the LDAP field name that the user's email address is stored in.
|
String |
getGroupDescriptionField()
Return the field used to describe a group.
|
String |
getGroupMemberField()
Return the field used to list members within a group.
|
String |
getGroupNameField()
Returns the field name used for groups.
|
LdapName |
getGroupsBaseDN(String groupname)
Returns the BaseDN for the given groupname.
|
String |
getGroupSearchFilter()
Returns the filter used for searching the directory for groups, which includes
the default filter plus any custom-defined search filter.
|
Collection<String> |
getHosts()
Returns the LDAP servers hosts; e.g.
|
static LdapManager |
getInstance()
Provides singleton access to an instance of the LdapManager class.
|
LdapUserTester.PropertyMapping |
getNameField()
Returns the LDAP field name that the user's name is stored in.
|
int |
getPort()
Returns the LDAP server port number.
|
static Rdn[] |
getRelativeDNFromResult(SearchResult answer)
Returns a RDN from the search result answer.
|
String |
getSearchFilter()
Returns the filter used for searching the directory for users, which includes
the default filter (username field search) plus any custom-defined search filter.
|
String |
getUsernameField()
Returns the LDAP field name that the username lookup will be performed
on.
|
String |
getUsernameSuffix()
Returns the suffix appended to the username when LDAP lookups are performed.
|
LdapName |
getUsersBaseDN(String username)
Returns the BaseDN for the given username.
|
boolean |
isConnectionPoolEnabled()
Returns whether an LDAP connection pool should be used or not.
|
boolean |
isDebugEnabled()
Returns true if LDAP connection debugging is turned on.
|
boolean |
isFindUsersFromGroupsEnabled() |
boolean |
isFlattenNestedGroups()
Returns true if nested / complex / hierarchic groups should be should be flattened.
|
boolean |
isFollowAliasReferralsEnabled()
Returns true if LDAP alias referrals will automatically be followed when found.
|
boolean |
isFollowReferralsEnabled()
Returns true if LDAP referrals will automatically be followed when found.
|
boolean |
isGroupDN(LdapName dn)
Check if the given DN matches the group search filter
|
boolean |
isPosixMode()
Return true if the LDAP server is operating in Posix mode.
|
boolean |
isSslEnabled()
Returns true if LDAP connection is via SSL or not.
|
boolean |
isStartTlsEnabled()
Returns true if LDAP connection is via START or not.
|
boolean |
isSubTreeSearch()
Returns true if the entire tree under the base DN will be searched (recursive search)
when doing LDAP queries (finding users, groups, etc).
|
static String |
joinFilter(char operator,
List<String> parts)
Joins individual strings into one, formatted as an LDAP filter, such as
(&(part-a)(part-b)(part-c)). |
LdapName |
parseAsLdapNameOrLog(String value)
Attempts to parse a string value as an LdapName.
|
List<String> |
retrieveAttributeOf(String attributeName,
LdapName dn)
Reads the attribute values of an entry with the given DN.
|
List<String> |
retrieveList(String attribute,
String searchFilter,
int startIndex,
int numResults,
String suffixToTrim)
Generic routine for retrieving a list of results from the LDAP server.
|
List<String> |
retrieveList(String attribute,
String searchFilter,
int startIndex,
int numResults,
String suffixToTrim,
boolean escapeJIDs)
Generic routine for retrieving a list of results from the LDAP server.
|
Integer |
retrieveListCount(String attribute,
String searchFilter)
Generic routine for retrieving the number of available results from the LDAP server that
match the passed search filter.
|
String |
retrieveSingle(String attribute,
String searchFilter,
boolean failOnMultipleResults)
Generic routine for retrieving a single element from the LDAP server.
|
String |
retrieveSingle(String attribute,
String searchFilter,
boolean failOnMultipleResults,
LdapName baseDN)
Generic routine for retrieving a single element from the LDAP server.
|
static String |
sanitizeSearchFilter(String value)
Escapes any special chars (RFC 4515) from a string representing
a search filter assertion value.
|
static String |
sanitizeSearchFilter(String value,
boolean acceptWildcard)
Escapes any special chars (RFC 4514/4515) from a string representing
a search filter assertion value, with the exception of the '*' wildcard sign
|
void |
setAdminDN(String adminDN)
Sets the starting admin DN that searches for admins will performed with.
|
void |
setAdminPassword(String adminPassword)
Sets the admin password for the LDAP server we're connecting to.
|
void |
setAlternateBaseDN(LdapName alternateBaseDN)
Sets the alternate starting DN that searches for users will performed with.
|
void |
setBaseDN(LdapName baseDN)
Sets the starting DN that searches for users will performed with.
|
void |
setConnectionPoolEnabled(boolean connectionPoolEnabled)
Sets whether an LDAP connection pool should be used or not.
|
void |
setDebugEnabled(boolean debugEnabled)
Sets whether LDAP connection debugging is turned on.
|
void |
setEmailField(String emailField)
Sets the LDAP field name that the user's email address is stored in.
|
void |
setFlattenNestedGroups(boolean flattenNestedGroups)
Set whether nested / complex / hierarchic groups should be should be flattened.
|
void |
setFollowAliasReferralsEnabled(boolean followAliasReferrals)
Sets whether LDAP alias referrals should be automatically followed.
|
void |
setFollowReferralsEnabled(boolean followReferrals)
Sets whether LDAP referrals should be automatically followed.
|
void |
setGroupDescriptionField(String groupDescriptionField)
Sets the field used to describe a group.
|
void |
setGroupMemberField(String groupMemberField)
Sets the field used to list members within a group.
|
void |
setGroupNameField(String groupNameField)
Sets the field name used for groups.
|
void |
setGroupSearchFilter(String groupSearchFilter)
Sets the search filter appended to the default filter when searching for groups.
|
void |
setHosts(Collection<String> hosts)
Sets the list of LDAP servers host; e.g.,
localhost or
machine.example.com, etc. |
static void |
setInstance(LdapManager instance)
Exposed for test use only
|
void |
setNameField(LdapUserTester.PropertyMapping nameField)
Sets the LDAP field name that the user's name is stored in.
|
void |
setPort(int port)
Sets the LDAP server port number.
|
void |
setPosixMode(boolean posixMode)
Sets whether the LDAP server is operating in Posix mode.
|
void |
setSearchFilter(String searchFilter)
Sets the search filter appended to the default filter when searching for users.
|
void |
setSslEnabled(boolean sslEnabled)
Sets whether the connection to the LDAP server should be made via ssl or not.
|
void |
setStartTlsEnabled(boolean startTlsEnabled)
Sets whether the connection to the LDAP server should be made via StartTLS or not.
|
void |
setSubTreeSearch(boolean subTreeSearch)
Sets whether the entire tree under the base DN will be searched (recursive search)
when doing LDAP queries (finding users, groups, etc).
|
void |
setUsernameField(String usernameField)
Sets the LDAP field name that the username lookup will be performed on.
|
void |
setUsernameSuffix(String usernameSuffix)
Set the suffix appended to the username whenever LDAP lookups are performed.
|
static List<String> |
splitFilter(String input)
Splits a string formatted as an LDAP filter, such as
(&(part-a)(part-b)(part-c)), in separate parts. |
public static final SystemProperty<Integer> LDAP_PAGE_SIZE
public LdapManager(Map<String,String> properties)
getInstance() should be
called instead of this method. LdapManager instances should only be created directly
for testing purposes.properties - the Map that contains properties used by the LDAP manager, such as
LDAP host and base DN.public static void setInstance(LdapManager instance)
public static LdapManager getInstance()
public static List<String> splitFilter(String input)
(&(part-a)(part-b)(part-c)), in separate parts.
When the provided input cannot be parsed as an LDAP filter, the returned collection contains one element: the
original input.input - The value to be split.public static String joinFilter(char operator, List<String> parts)
(&(part-a)(part-b)(part-c)).operator - the second character of the resulting string.parts - The parts to be joined into one string.public LdapName parseAsLdapNameOrLog(String value)
value - The value to be parsed (can be null or empty).public static Rdn[] getRelativeDNFromResult(SearchResult answer) throws NamingException
answer - The result of the search (cannot be null).NamingException - When the search result value cannot be used to form a valid RDN value.public LdapContext getContext() throws NamingException
NamingException using base DN. The context uses the
admin login that is defined by adminDN and adminPassword.NamingException - if there is an error making the LDAP connection.public LdapContext getContext(LdapName baseDN) throws NamingException
adminDN and adminPassword.baseDN - the base DN to use for the context.NamingException - if there is an error making the LDAP connection.public boolean checkAuthentication(Rdn[] userRDN, String password)
userRDN - the user's rdn to authenticate (relative to baseDN).password - the user's password.public boolean isFindUsersFromGroupsEnabled()
public Rdn[] findUserRDN(String username) throws Exception
usernameField property.
Searches are performed over all subtrees relative to the baseDN.
If the search fails in the baseDN then another search will be
performed in the alternateBaseDN. For example, if the baseDN
is "o=jivesoftware, o=com" and we do a search for "mtucker", then we might
find a userDN of "uid=mtucker,ou=People". This kind of searching is a good
thing since it doesn't make the assumption that all user records are stored
in a flat structure. However, it does add the requirement that "uid" field
(or the other field specified) must be unique over the entire subtree from
the baseDN. For example, it's entirely possible to create two dn's
in your LDAP directory with the same uid: "uid=mtucker,ou=People" and
"uid=mtucker,ou=Administrators". In such a case, it's not possible to
uniquely identify a user, so this method will throw an error.
The dn that's returned is relative to the default baseDN.
username - the username to lookup the dn for.username.Exception - if the search for the dn fails.public Rdn[] findUserRDN(String username, LdapName baseDN) throws Exception
usernameField property.
Searches are performed over all sub-trees relative to the baseDN unless
sub-tree searching has been disabled. For example, if the baseDN is
"o=jivesoftware, o=com" and we do a search for "mtucker", then we might find a userDN of
"uid=mtucker,ou=People". This kind of searching is a good thing since
it doesn't make the assumption that all user records are stored in a flat
structure. However, it does add the requirement that "uid" field (or the
other field specified) must be unique over the entire subtree from the
baseDN. For example, it's entirely possible to create two dn's
in your LDAP directory with the same uid: "uid=mtucker,ou=People" and
"uid=mtucker,ou=Administrators". In such a case, it's not possible to
uniquely identify a user, so this method will throw an error.
The RDN that's returned is relative to the baseDN.
username - the username to lookup the dn for.baseDN - the base DN to use for this search.username.Exception - if the search for the RDN fails.to search using the default baseDN and alternateBaseDN.public Rdn[] findGroupRDN(String groupname) throws Exception
groupNameField property.
Searches are performed over all subtrees relative to the baseDN.
If the search fails in the baseDN then another search will be
performed in the alternateBaseDN. For example, if the baseDN
is "o=jivesoftware, o=com" and we do a search for "managers", then we might
find a groupDN of "uid=managers,ou=Groups". This kind of searching is a good
thing since it doesn't make the assumption that all user records are stored
in a flat structure. However, it does add the requirement that "cn" field
(or the other field specified) must be unique over the entire subtree from
the baseDN. For example, it's entirely possible to create two dn's
in your LDAP directory with the same cn: "cn=managers,ou=Financial" and
"cn=managers,ou=Engineers". In such a case, it's not possible to
uniquely identify a group, so this method will throw an error.
The RDN that's returned is relative to the default baseDN.
groupname - the groupname to lookup the RDN for.groupname.Exception - if the search for the RDN fails.public LdapName findGroupAbsoluteDN(String groupname) throws Exception
findGroupRDN(String) but returns the absolute DN of a groupExceptionpublic Rdn[] findGroupRDN(String groupname, LdapName baseDN) throws Exception
groupNameField property.
Searches are performed over all subtrees relative to the baseDN.
If the search fails in the baseDN then another search will be
performed in the alternateBaseDN. For example, if the baseDN
is "o=jivesoftware, o=com" and we do a search for "managers", then we might
find a groupDN of "uid=managers,ou=Groups". This kind of searching is a good
thing since it doesn't make the assumption that all user records are stored
in a flat structure. However, it does add the requirement that "cn" field
(or the other field specified) must be unique over the entire subtree from
the baseDN. For example, it's entirely possible to create two dn's
in your LDAP directory with the same cn: "cn=managers,ou=Financial" and
"cn=managers,ou=Engineers". In such a case, it's not possible to
uniquely identify a group, so this method will throw an error.
The dn that's returned is relative to the default baseDN.
groupname - the groupname to lookup the dn for.baseDN - the base DN to use for this search.groupname.Exception - if the search for the dn fails.to search using the default baseDN and alternateBaseDN.public boolean isGroupDN(LdapName dn) throws NamingException
dn - the absolute DN of the node to checkNamingException - if the search for the dn fails.public Collection<String> getHosts()
localhost or
machine.example.com, etc. This value is stored as the Jive
Property ldap.host.public void setHosts(Collection<String> hosts)
localhost or
machine.example.com, etc. This value is store as the Jive
Property ldap.host using a comma as a delimiter for each host.Note that all LDAP servers have to share the same configuration.
hosts - the LDAP servers host names.public int getPort()
ldap.port.public void setPort(int port)
ldap.port.port - the LDAP server port number.public boolean isDebugEnabled()
public void setDebugEnabled(boolean debugEnabled)
debugEnabled - true if debugging should be turned on.public boolean isSslEnabled()
public void setSslEnabled(boolean sslEnabled)
sslEnabled - true if ssl should be enabled, false otherwise.public boolean isStartTlsEnabled()
public void setStartTlsEnabled(boolean startTlsEnabled)
startTlsEnabled - true if StartTLS should be used, false otherwise.public String getUsernameField()
public String getUsernameSuffix()
public void setUsernameField(String usernameField)
usernameField - the LDAP field that the username lookup will be
performed on.public void setUsernameSuffix(String usernameSuffix)
usernameSuffix - the String to append to usernames for lookupspublic LdapUserTester.PropertyMapping getNameField()
public void setNameField(LdapUserTester.PropertyMapping nameField)
nameField - the LDAP field that that corresponds to the user's name.public String getEmailField()
public void setEmailField(String emailField)
emailField - the LDAP field that that corresponds to the user's
email address.public LdapName getBaseDN()
public void setBaseDN(LdapName baseDN)
baseDN - the starting DN used for performing searches.public LdapName getAlternateBaseDN()
null.public void setAlternateBaseDN(LdapName alternateBaseDN)
alternateBaseDN - the alternate starting DN used for performing searches.public LdapName getUsersBaseDN(String username)
username - username to return its base DN.null.public LdapName getGroupsBaseDN(String groupname)
groupname - groupname to return its base DN.null.public String getAdminDN()
public void setAdminDN(String adminDN)
adminDN - the starting DN used for performing admin searches.public String getAdminPassword()
public void setAdminPassword(String adminPassword)
adminPassword - the admin password for the LDAP server we're
connecting to.public void setConnectionPoolEnabled(boolean connectionPoolEnabled)
connectionPoolEnabled - true if an LDAP connection pool should be used.public boolean isConnectionPoolEnabled()
public String getSearchFilter()
public void setSearchFilter(String searchFilter)
searchFilter - the search filter appended to the default filter
when searching for users.public boolean isSubTreeSearch()
true which is the best
option for most LDAP setups. In only a few cases will the directory be setup in such
a way that it's better to do single level searching.public void setSubTreeSearch(boolean subTreeSearch)
true which is the best
option for most LDAP setups. In only a few cases will the directory be setup in such
a way that it's better to do single level searching.subTreeSearch - true if the entire tree under the base DN will be searched.public boolean isFollowReferralsEnabled()
public void setFollowReferralsEnabled(boolean followReferrals)
followReferrals - true if LDAP referrals should be automatically followed.public boolean isFollowAliasReferralsEnabled()
public void setFollowAliasReferralsEnabled(boolean followAliasReferrals)
followAliasReferrals - true if LDAP alias referrals should be automatically followed.public String getGroupNameField()
public void setGroupNameField(String groupNameField)
groupNameField - the field used for groups.public String getGroupMemberField()
public void setGroupMemberField(String groupMemberField)
groupMemberField - the field used to list members within a group.public String getGroupDescriptionField()
public void setGroupDescriptionField(String groupDescriptionField)
groupDescriptionField - the field used to describe a group.public boolean isPosixMode()
public void setPosixMode(boolean posixMode)
posixMode - true if posix mode is being used by the LDAP server.public String getGroupSearchFilter()
public boolean isFlattenNestedGroups()
This means: if group A is member of group B, the members of group A will also be members of group B
public void setFlattenNestedGroups(boolean flattenNestedGroups)
isFlattenNestedGroups()public void setGroupSearchFilter(String groupSearchFilter)
groupSearchFilter - the search filter appended to the default filter
when searching for groups.public List<String> retrieveList(String attribute, String searchFilter, int startIndex, int numResults, String suffixToTrim)
attribute - LDAP attribute to be pulled from each result and placed in the return results.
Typically pulled from this manager.searchFilter - Filter to use to perform the search. Typically pulled from this manager.startIndex - Number/index of first result to include in results. (-1 for no limit)numResults - Number of results to include. (-1 for no limit)suffixToTrim - An arbitrary string to trim from the end of every attribute returned. null to disable.public List<String> retrieveList(String attribute, String searchFilter, int startIndex, int numResults, String suffixToTrim, boolean escapeJIDs)
attribute - LDAP attribute to be pulled from each result and placed in the return results.
Typically pulled from this manager.searchFilter - Filter to use to perform the search. Typically pulled from this manager.startIndex - Number/index of first result to include in results. (-1 for no limit)numResults - Number of results to include. (-1 for no limit)suffixToTrim - An arbitrary string to trim from the end of every attribute returned. null to disable.escapeJIDs - Use JID-escaping for returned results (e.g. usernames)public String retrieveSingle(String attribute, String searchFilter, boolean failOnMultipleResults)
The passed in filter string needs to be pre-prepared! In other words, nothing will be changed in the string before it is used as a string.
attribute - LDAP attribute to be pulled from each result and placed in the return results.
Typically pulled from this manager. Null means the the absolute DN is returned.searchFilter - Filter to use to perform the search. Typically pulled from this manager.failOnMultipleResults - It true, an IllegalStateException will be thrown, if the
search result is not unique. If false, just the first result will be returned.public String retrieveSingle(String attribute, String searchFilter, boolean failOnMultipleResults, LdapName baseDN)
The passed in filter string needs to be pre-prepared! In other words, nothing will be changed in the string before it is used as a string.
attribute - LDAP attribute to be pulled from each result and placed in the return results.
Typically pulled from this manager. Null means the the absolute DN is returned.searchFilter - Filter to use to perform the search. Typically pulled from this manager.failOnMultipleResults - It true, an IllegalStateException will be thrown, if the
search result is not unique. If false, just the first result will be returned.baseDN - DN where to start the search. Typically getBaseDN() or getAlternateBaseDN().public List<String> retrieveAttributeOf(String attributeName, LdapName dn) throws NamingException
attributeName - LDAP attribute to be read.dn - DN of the entry.NamingExceptionpublic Integer retrieveListCount(String attribute, String searchFilter)
attribute - LDAP attribute to be pulled from each result and used in the query.
Typically pulled from this manager.searchFilter - Filter to use to perform the search. Typically pulled from this manager.public static String sanitizeSearchFilter(String value)
value - The input string.public static LdapName escapeForJNDI(Rdn... rdn)
When using the JNDI to access an LDAP service, you should be aware that the forward slash character ("/") in a string name has special meaning to the JNDI. If the LDAP entry's name contains this character, then you need to escape it (using the backslash character ("\")). For example, an LDAP entry with the name "cn=O/R" must be presented as the string "cn=O\\/R" to the JNDI context methods.
rdn - The names to escape (cannot be null).public static String sanitizeSearchFilter(String value, boolean acceptWildcard)
value - The input string.acceptWildcard - true to accept wildcards, otherwise falseCopyright © 2003–2020 Ignite Realtime. All rights reserved.