diff --git a/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.api b/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.api index 209294981a..e2c695df71 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.api +++ b/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.api @@ -2,7 +2,7 @@ public final class io/ktor/client/plugins/auth/AuthConfig { public fun ()V public final fun getProviders ()Ljava/util/List; public final fun isUnauthorizedResponse ()Lkotlin/jvm/functions/Function2; - public final fun setUnauthorizedResponse (Lkotlin/jvm/functions/Function2;)V + public final fun reAuthorizeOnResponse (Lkotlin/jvm/functions/Function2;)V } public final class io/ktor/client/plugins/auth/AuthKt { diff --git a/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.klib.api b/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.klib.api index 63cead3b8f..a5f0797997 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.klib.api +++ b/ktor-client/ktor-client-plugins/ktor-client-auth/api/ktor-client-auth.klib.api @@ -158,7 +158,8 @@ final class io.ktor.client.plugins.auth/AuthConfig { // io.ktor.client.plugins.a final var isUnauthorizedResponse // io.ktor.client.plugins.auth/AuthConfig.isUnauthorizedResponse|{}isUnauthorizedResponse[0] final fun (): kotlin.coroutines/SuspendFunction1 // io.ktor.client.plugins.auth/AuthConfig.isUnauthorizedResponse.|(){}[0] - final fun (kotlin.coroutines/SuspendFunction1) // io.ktor.client.plugins.auth/AuthConfig.isUnauthorizedResponse.|(kotlin.coroutines.SuspendFunction1){}[0] + + final fun reAuthorizeOnResponse(kotlin.coroutines/SuspendFunction1) // io.ktor.client.plugins.auth/AuthConfig.reAuthorizeOnResponse|reAuthorizeOnResponse(kotlin.coroutines.SuspendFunction1){}[0] } final val io.ktor.client.plugins.auth/Auth // io.ktor.client.plugins.auth/Auth|{}Auth[0] diff --git a/ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/Auth.kt b/ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/Auth.kt index 31ef42a1f5..1316376dea 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/Auth.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/Auth.kt @@ -34,9 +34,23 @@ public class AuthConfig { public val providers: MutableList = mutableListOf() /** - * A lambda function to control whether a response is unauthorized and should trigger a refresh / re-auth. + * The currently set function to control whether a response is unauthorized and should trigger a refresh / re-auth. + * + * By default checks against HTTP status 401. + * + * You can set this value via [reAuthorizeOnResponse]. */ public var isUnauthorizedResponse: suspend (HttpResponse) -> Boolean = { it.status == HttpStatusCode.Unauthorized } + private set + + /** + * Sets a custom function to control whether a response is unauthorized and should trigger a refresh / re-auth. + * + * Use this to change the value of [isUnauthorizedResponse]. + */ + public fun reAuthorizeOnResponse(block: suspend (HttpResponse) -> Boolean) { + isUnauthorizedResponse = block + } } /** diff --git a/ktor-client/ktor-client-plugins/ktor-client-auth/common/test/io/ktor/client/plugins/auth/AuthTest.kt b/ktor-client/ktor-client-plugins/ktor-client-auth/common/test/io/ktor/client/plugins/auth/AuthTest.kt index 6c6a236f17..20107f77f5 100644 --- a/ktor-client/ktor-client-plugins/ktor-client-auth/common/test/io/ktor/client/plugins/auth/AuthTest.kt +++ b/ktor-client/ktor-client-plugins/ktor-client-auth/common/test/io/ktor/client/plugins/auth/AuthTest.kt @@ -407,7 +407,7 @@ class AuthTest : ClientLoader() { fun testForbiddenBearerAuthWithInvalidAccessAndValidRefreshTokens() = clientTests { config { install(Auth) { - isUnauthorizedResponse = { it.status == HttpStatusCode.Forbidden } + reAuthorizeOnResponse { it.status == HttpStatusCode.Forbidden } bearer { refreshTokens { BearerTokens("valid", "refresh") } loadTokens { BearerTokens("invalid", "refresh") }