I know, but we do a bunch of other stuff around it...
# ktor
m
I know, but we do a bunch of other stuff around it:
Copy code
fun 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