Skip to content

Commit

Permalink
isUnauthorizedResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
wkornewald committed Nov 8, 2024
1 parent b80c0cf commit 01843ea
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public final class io/ktor/client/plugins/auth/AuthConfig {
public fun <init> ()V
public final fun getProviders ()Ljava/util/List;
public final fun isUnauthorized ()Lkotlin/jvm/functions/Function2;
public final fun setUnauthorized (Lkotlin/jvm/functions/Function2;)V
public final fun isUnauthorizedResponse ()Lkotlin/jvm/functions/Function2;
public final fun setUnauthorizedResponse (Lkotlin/jvm/functions/Function2;)V
}

public final class io/ktor/client/plugins/auth/AuthKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ final class io.ktor.client.plugins.auth/AuthConfig { // io.ktor.client.plugins.a
final val providers // io.ktor.client.plugins.auth/AuthConfig.providers|{}providers[0]
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]

final var isUnauthorized // io.ktor.client.plugins.auth/AuthConfig.isUnauthorized|{}isUnauthorized[0]
final fun <get-isUnauthorized>(): kotlin.coroutines/SuspendFunction1<io.ktor.client.statement/HttpResponse, kotlin/Boolean> // io.ktor.client.plugins.auth/AuthConfig.isUnauthorized.<get-isUnauthorized>|<get-isUnauthorized>(){}[0]
final fun <set-isUnauthorized>(kotlin.coroutines/SuspendFunction1<io.ktor.client.statement/HttpResponse, kotlin/Boolean>) // io.ktor.client.plugins.auth/AuthConfig.isUnauthorized.<set-isUnauthorized>|<set-isUnauthorized>(kotlin.coroutines.SuspendFunction1<io.ktor.client.statement.HttpResponse,kotlin.Boolean>){}[0]
final var isUnauthorizedResponse // io.ktor.client.plugins.auth/AuthConfig.isUnauthorizedResponse|{}isUnauthorizedResponse[0]
final fun <get-isUnauthorizedResponse>(): kotlin.coroutines/SuspendFunction1<io.ktor.client.statement/HttpResponse, kotlin/Boolean> // io.ktor.client.plugins.auth/AuthConfig.isUnauthorizedResponse.<get-isUnauthorizedResponse>|<get-isUnauthorizedResponse>(){}[0]
final fun <set-isUnauthorizedResponse>(kotlin.coroutines/SuspendFunction1<io.ktor.client.statement/HttpResponse, kotlin/Boolean>) // io.ktor.client.plugins.auth/AuthConfig.isUnauthorizedResponse.<set-isUnauthorizedResponse>|<set-isUnauthorizedResponse>(kotlin.coroutines.SuspendFunction1<io.ktor.client.statement.HttpResponse,kotlin.Boolean>){}[0]
}

final val io.ktor.client.plugins.auth/Auth // io.ktor.client.plugins.auth/Auth|{}Auth[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package io.ktor.client.plugins.auth

import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.api.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
Expand Down Expand Up @@ -37,7 +36,7 @@ public class AuthConfig {
/**
* A lambda function to control whether a response is unauthorized and should trigger a refresh / re-auth.
*/
public var isUnauthorized: suspend (HttpResponse) -> Boolean = { it.status == HttpStatusCode.Unauthorized }
public var isUnauthorizedResponse: suspend (HttpResponse) -> Boolean = { it.status == HttpStatusCode.Unauthorized }
}

/**
Expand Down Expand Up @@ -140,14 +139,14 @@ public val Auth: ClientPlugin<AuthConfig> = createClientPlugin("Auth", ::AuthCon

on(Send) { originalRequest ->
val origin = proceed(originalRequest)
if (!pluginConfig.isUnauthorized(origin.response)) return@on origin
if (!pluginConfig.isUnauthorizedResponse(origin.response)) return@on origin
if (origin.request.attributes.contains(AuthCircuitBreaker)) return@on origin

var call = origin

val candidateProviders = HashSet(providers)

while (pluginConfig.isUnauthorized(call.response)) {
while (pluginConfig.isUnauthorizedResponse(call.response)) {
LOGGER.trace { "Unauthorized response for ${call.request.url}" }

val (provider, authHeader) = findProvider(call, candidateProviders) ?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ class AuthTest : ClientLoader() {
fun testForbiddenBearerAuthWithInvalidAccessAndValidRefreshTokens() = clientTests {
config {
install(Auth) {
isUnauthorized = { it.status == HttpStatusCode.Forbidden }
isUnauthorizedResponse = { it.status == HttpStatusCode.Forbidden }
bearer {
refreshTokens { BearerTokens("valid", "refresh") }
loadTokens { BearerTokens("invalid", "refresh") }
Expand Down

0 comments on commit 01843ea

Please sign in to comment.