mkporwit
07/14/2017, 4:56 AMfun ldapAuth(credentials: UserEmailCredential) : Principal? {
<http://log.info|log.info>("attempting to authenticate [${credentials.email}] in LDAP")
val principal = ldapAuthenticate(credentials, ldapUrl, configure) {
val users = lookup(ldapUsersRoot) as LdapContext
val controls = SearchControls().apply {
searchScope = SearchControls.SUBTREE_SCOPE
returningAttributes = arrayOf("+", "*")
}
users.search("", "(mail=" + credentials.email + ")", controls).asSequence().firstOrNull {
val ldapPassword = (it.attributes.get("userPassword")?.get() as ByteArray?)?.toString(Charsets.ISO_8859_1)
checkPassword(ldapPassword, credentials.password)
}?.let {
Person(
it.attributes.get("cn")?.get() as String? ?: "",
it.attributes.get("sn")?.get() as String? ?: "",
UserEmailCredential(it.attributes.get("mail")?.get() as String? ?: credentials.email),
it.attributes.get("uid")?.get() as String? ?: ""
)
}
}
return principal