Skip to content

Commit d491930

Browse files
authored
KTOR-8107 Don't add auth header for requests with AuthCircuitBreaker attribute (#4815)
1 parent 5621540 commit d491930

File tree

2 files changed

+39
-1
lines changed
  • ktor-client/ktor-client-plugins/ktor-client-auth/common

2 files changed

+39
-1
lines changed

ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ public class BearerAuthProvider(
151151
if (contains(HttpHeaders.Authorization)) {
152152
remove(HttpHeaders.Authorization)
153153
}
154-
append(HttpHeaders.Authorization, tokenValue)
154+
if (request.attributes.contains(AuthCircuitBreaker).not()) {
155+
append(HttpHeaders.Authorization, tokenValue)
156+
}
155157
}
156158
}
157159

ktor-client/ktor-client-plugins/ktor-client-auth/common/test/io/ktor/client/plugins/auth/AuthTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,40 @@ class AuthTest : ClientLoader() {
765765
}
766766
}
767767
}
768+
769+
@Test
770+
fun testBearerAuthWithCircuitBreaker() = testWithEngine(MockEngine) {
771+
config {
772+
install(Auth) {
773+
bearer {
774+
loadTokens { BearerTokens("invalid", null) }
775+
}
776+
}
777+
engine {
778+
addHandler { request ->
779+
val authHeader = request.headers[HttpHeaders.Authorization]
780+
if (authHeader == null) {
781+
// no header - this is expected for refresh token requests
782+
respond("OK", HttpStatusCode.OK)
783+
} else {
784+
// header is invalid throw unauthorized
785+
respond("No Auth Header", HttpStatusCode.Unauthorized)
786+
}
787+
}
788+
}
789+
}
790+
791+
test { client ->
792+
// Test without circuit breaker - should add auth header with invalid token
793+
val response1 = client.get("/")
794+
assertEquals(HttpStatusCode.Unauthorized, response1.status)
795+
796+
// Test with circuit breaker - should not add auth header
797+
val response2 = client.get("/") {
798+
// add AuthCircuitBreaker like any refresh token request would have
799+
attributes.put(AuthCircuitBreaker, Unit)
800+
}
801+
assertEquals(HttpStatusCode.OK, response2.status)
802+
}
803+
}
768804
}

0 commit comments

Comments
 (0)