Skip to content

Commit

Permalink
KTOR-3391 Fix Digest Auth: algorithm isn't specified in the Authoriza…
Browse files Browse the repository at this point in the history
…tion header (#3732)

(cherry picked from commit 0d63c4d)
  • Loading branch information
marychatte authored and e5l committed Aug 30, 2023
1 parent ddc3ff3 commit d720899
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
15 changes: 15 additions & 0 deletions buildSrc/src/main/kotlin/test/server/tests/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ internal fun Application.authTestServer() {
}
}

digest("digest-SHA256") {
val password = "Circle Of Life"
algorithmName = "SHA-256"
realm = "[email protected]"

digestProvider { userName, realm ->
digest(MessageDigest.getInstance(algorithmName), "$userName:$realm:$password")
}
}

basic("basic") {
validate { credential ->
check("MyUser" == credential.name)
Expand Down Expand Up @@ -86,6 +96,11 @@ internal fun Application.authTestServer() {
call.respondText("ok")
}
}
authenticate("digest-SHA256") {
get("digest-SHA256") {
call.respondText("ok")
}
}
authenticate("basic") {
get("basic-fixed") {
call.respondText("ok")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public class DigestAuthProvider(
this["uri"] = url.fullPath
actualQop?.let { this["qop"] = it }
this["nc"] = nonceCount.toString()
this["algorithm"] = algorithmName
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import io.ktor.client.tests.utils.*
import io.ktor.http.*
import io.ktor.http.auth.*
import io.ktor.test.dispatcher.*
import io.ktor.util.*
import io.ktor.utils.io.errors.*
import kotlinx.coroutines.*
import kotlin.test.*
Expand Down Expand Up @@ -83,6 +82,22 @@ class AuthTest : ClientLoader() {
}
}

@Test
fun testDigestAuthSHA256() = clientTests(listOf("Js", "native")) {
config {
install(Auth) {
digest {
algorithmName = "SHA-256"
credentials { DigestAuthCredentials("MyName", "Circle Of Life") }
realm = "[email protected]"
}
}
}
test { client ->
assertTrue(client.get("$TEST_SERVER/auth/digest-SHA256").status.isSuccess())
}
}

@Suppress("DEPRECATION")
@Test
fun testBasicAuthLegacy() = clientTests(listOf("Js")) {
Expand Down

0 comments on commit d720899

Please sign in to comment.