Connection to ActiveDirectory by LDAP
- Dominant language
- Kotlin
- Stars
- 14.5k
- Forks
- 1.3k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 49
Description
Hello,
I try connect to active directory but i don't get any information about connection or user data. When i try login nothin ain't happens, just displays a new login window in the browser. Here my code. Maybe i do something wrong?
```import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.auth.ldap.ldapAuthenticate
import io.ktor.features.*
import io.ktor.gson.GsonConverter
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import mu.KotlinLogging
import javax.naming.directory.SearchControls
import javax.naming.ldap.LdapContext
fun main(args: Array) {
embeddedServer(Netty, 8080) {
install(DefaultHeaders)
install(CORS) {
method(HttpMethod.Get)
header(HttpHeaders.Authorization)
allowCredentials = true
anyHost()
}
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
}
install(Authentication) {
basic("authActiveDirectory") {
validate { cred ->
realm = "IdapRealm"
ldapAuthenticate(cred, "ldap://100.20.1.1:389", "OU=TestCatalog,DC=op,DC=serv") {
val users = (lookup("OU=UsersCatalog") as LdapContext)
logger.debug { users }
val controls = SearchControls().apply {
searchScope = SearchControls.ONELEVEL_SCOPE
returningAttributes = arrayOf("+", "*")
}
users.search(cred.name, "", controls).asSequence().firstOrNull {
val ldapPassword = (it.attributes.get("userPassword")?.get() as ByteArray?)?.toString(Charsets.UTF_8)
ldapPassword == cred.password
}?.let { UserIdPrincipal(cred.name) }
}
}
}
}
routing {
authenticate("authActiveDirectory") {
get("/au") {
call.respondText(call.authentication.principal()?.name ?: "null")
}
}
}
}.start(wait = true)
}
--------------------------------------------------------
**Gradle:**
--------------------------------------------------------
buildscript {
ext.kotlin_version = '1.2.61'
ext.ktor_version = '0.9.3'
ext.logback_version = '1.2.1'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.2.60'
}
group 'com.ldap'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
jcenter()
maven { url "http://kotlin.bintray.com/ktor" }
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile group: 'io.ktor', name: 'ktor', version: "$ktor_version"
compile group: 'io.ktor', name: 'ktor-gson', version: "$ktor_version"
compile group: 'io.ktor', name: 'ktor-server-netty', version: "$ktor_version"
compile group: 'io.ktor', name: 'ktor-client-auth-basic', version: "$ktor_version"
compile group: 'io.ktor', name: 'ktor-auth-ldap', version: "$ktor_version"
compile "ch.qos.logback:logback-classic:$logback_version"
compile 'io.github.microutils:kotlin-logging:1.5.9'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
kotlin {
experimental {
coroutines "enable"
}
}
Contributor guide
Research direction
Start with the Authentication block in main, especially the authActiveDirectory configuration and ldapAuthenticate call, then exercise the /au route to reproduce the repeated login prompt. Compare the observed LDAP and principal behavior with the supplied credentials and search setup; done means the cause is identified and successful authentication returns the expected user name.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- authentication, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100