Skip to content

Commit d6e7166

Browse files
committed
Make re-auth status codes configurable
Some services use 403 instead of 401. Changing them might be impossible. With this change Ktor can flexibly work with any broken service.
1 parent 3d71a28 commit d6e7166

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
public final class io/ktor/client/plugins/auth/AuthConfig {
22
public fun <init> ()V
33
public final fun getProviders ()Ljava/util/List;
4+
public final fun getReAuthStatusCodes ()Ljava/util/List;
45
}
56

67
public final class io/ktor/client/plugins/auth/AuthKt {

ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.klib.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ final class io.ktor.client.plugins.auth/AuthConfig { // io.ktor.client.plugins.a
155155

156156
final val providers // io.ktor.client.plugins.auth/AuthConfig.providers|{}providers[0]
157157
final fun <get-providers>(): kotlin.collections/MutableList<io.ktor.client.plugins.auth/AuthProvider> // io.ktor.client.plugins.auth/AuthConfig.providers.<get-providers>|<get-providers>(){}[0]
158+
final val reAuthStatusCodes // io.ktor.client.plugins.auth/AuthConfig.reAuthStatusCodes|{}reAuthStatusCodes[0]
159+
final fun <get-reAuthStatusCodes>(): kotlin.collections/MutableList<io.ktor.http/HttpStatusCode> // io.ktor.client.plugins.auth/AuthConfig.reAuthStatusCodes.<get-reAuthStatusCodes>|<get-reAuthStatusCodes>(){}[0]
158160
}
159161

160162
final val io.ktor.client.plugins.auth/Auth // io.ktor.client.plugins.auth/Auth|{}Auth[0]

ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/Auth.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ private class AtomicCounter {
2626
@KtorDsl
2727
public class AuthConfig {
2828
public val providers: MutableList<AuthProvider> = mutableListOf()
29+
public val reAuthStatusCodes: MutableList<HttpStatusCode> = mutableListOf(HttpStatusCode.Unauthorized)
2930
}
3031

3132
/**
@@ -40,6 +41,7 @@ public val AuthCircuitBreaker: AttributeKey<Unit> = AttributeKey("auth-request")
4041
* You can learn more from [Authentication and authorization](https://ktor.io/docs/auth.html).
4142
*
4243
* [providers] - list of auth providers to use.
44+
* [reAuthStatusCodes] - list of [HttpStatusCode] values which trigger a re-auth.
4345
*/
4446
public val Auth: ClientPlugin<AuthConfig> = createClientPlugin("Auth", ::AuthConfig) {
4547
val providers = pluginConfig.providers.toList()
@@ -128,14 +130,14 @@ public val Auth: ClientPlugin<AuthConfig> = createClientPlugin("Auth", ::AuthCon
128130

129131
on(Send) { originalRequest ->
130132
val origin = proceed(originalRequest)
131-
if (origin.response.status != HttpStatusCode.Unauthorized) return@on origin
133+
if (origin.response.status !in pluginConfig.reAuthStatusCodes) return@on origin
132134
if (origin.request.attributes.contains(AuthCircuitBreaker)) return@on origin
133135

134136
var call = origin
135137

136138
val candidateProviders = HashSet(providers)
137139

138-
while (call.response.status == HttpStatusCode.Unauthorized) {
140+
while (call.response.status in pluginConfig.reAuthStatusCodes) {
139141
LOGGER.trace("Received 401 for ${call.request.url}")
140142

141143
val (provider, authHeader) = findProvider(call, candidateProviders) ?: run {

0 commit comments

Comments
 (0)