Moved a code repeatation to format a ldap search filter string to a function

References #164
This commit is contained in:
Mahesh V Kelkar 2015-04-30 12:21:26 -04:00
parent d2d567dfbd
commit c1b8322748
2 changed files with 19 additions and 14 deletions

View File

@ -65,6 +65,19 @@ class LdapAuthenticator implements Authenticator<BasicCredentials, BasicCredenti
this.configuration = configuration this.configuration = configuration
} }
/**
* Format the LDAP search filter string for the user lookup
*
* @param userString username or user domain name, etc
* @return formatter string
*/
protected String formatUserFilterString(String userString) {
final String filter = String.format("(&(%s=%s)(objectClass=%s))",
configuration.userNamePrefix, userString,
configuration.userObjectClass)
return filter
}
/** /**
* Creates, connects to LDAP server using JNDI * Creates, connects to LDAP server using JNDI
* *
@ -239,13 +252,9 @@ class LdapAuthenticator implements Authenticator<BasicCredentials, BasicCredenti
* We are searching from the top i.e. baseDC; filter the output using username and * We are searching from the top i.e. baseDC; filter the output using username and
* ObjectClass that user belong to * ObjectClass that user belong to
*/ */
final String filter = String.format("(&(%s=%s)(objectClass=%s))",
configuration.userNamePrefix, credentials.username,
configuration.userObjectClass)
/** Search from baseDC */
Set<String> distinguishedNames = searchContext(context, configuration.baseDC, Set<String> distinguishedNames = searchContext(context, configuration.baseDC,
filter, configuration.distinguishedNamePrefix) formatUserFilterString(credentials.username),
configuration.distinguishedNamePrefix)
/** /**
* The search should yield us 1 user DN. If we received anything but that, then * The search should yield us 1 user DN. If we received anything but that, then

View File

@ -209,13 +209,11 @@ class LdapAuthenticatorWithArgsSpec extends Specification {
given: given:
LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(ldapConfiguration) LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(ldapConfiguration)
InitialDirContext context = ldapAuthenticator.bindContext() InitialDirContext context = ldapAuthenticator.bindContext()
final String filter = String.format("(&(%s=%s)(objectClass=%s))",
ldapConfiguration.userNamePrefix, "peter",
ldapConfiguration.userObjectClass)
when: when:
Set<String> attributes = ldapAuthenticator.searchContext( Set<String> attributes = ldapAuthenticator.searchContext(
context, ldapConfiguration.baseDC, filter, context, ldapConfiguration.baseDC,
ldapAuthenticator.formatUserFilterString("peter"),
ldapConfiguration.distinguishedNamePrefix) ldapConfiguration.distinguishedNamePrefix)
then: then:
@ -228,13 +226,11 @@ class LdapAuthenticatorWithArgsSpec extends Specification {
ldapConfiguration.distinguishedNamePrefix = "invalidDN" ldapConfiguration.distinguishedNamePrefix = "invalidDN"
LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(ldapConfiguration) LdapAuthenticator ldapAuthenticator = new LdapAuthenticator(ldapConfiguration)
InitialDirContext context = ldapAuthenticator.bindContext() InitialDirContext context = ldapAuthenticator.bindContext()
final String filter = String.format("(&(%s=%s)(objectClass=%s))",
ldapConfiguration.userNamePrefix, "peter",
ldapConfiguration.userObjectClass)
when: when:
Set<String> attributes = ldapAuthenticator.searchContext( Set<String> attributes = ldapAuthenticator.searchContext(
context, ldapConfiguration.baseDC, filter, context, ldapConfiguration.baseDC,
ldapAuthenticator.formatUserFilterString("peter"),
ldapConfiguration.distinguishedNamePrefix) ldapConfiguration.distinguishedNamePrefix)
then: then: