https://kotlinlang.org logo
#ktor
Title
# ktor
t

Trey

04/27/2022, 1:49 PM
We use token authentication with our app. Can somebody point me to an example of how to configure the bearer section so that the auth/refresh token can be shared across more than one http client? We have two different servers (dept1.mycompany.com and dept2.mycompany.com) that use the same auth token.
a

Aleksei Tirman [JB]

04/28/2022, 12:46 PM
You can create a
BearerAuthProvider
object and add it to a list of providers for both clients. In this case a token holder will be shared so this solution should work in principle:
Copy code
val provider = BearerAuthProvider({
    // refresh token logic
    BearerTokens("", "")
}, {
    // load token logic
    BearerTokens("", "")
}, realm = null)

val client1 = HttpClient(OkHttp) {
    install(Auth) {
        providers.add(provider)
    }
}

val client2 = HttpClient(OkHttp) {
    install(Auth) {
        providers.add(provider)
    }
}
t

Trey

04/28/2022, 1:55 PM
This is cool. Thank you!
4 Views