OAuthAuthorizationProvider

class OAuthAuthorizationProvider(initialAccessToken: String, refreshTokenHandler: suspend () -> String?) : AuthorizationProvider

An AuthorizationProvider that holds an OAuth bearer access token and refreshes it via refreshTokenHandler. Mirrors SwiftyNetwork's OAuthAuthorizationProvider.

Concurrent refreshes are coalesced: if several requests 401 at once, refreshTokenHandler runs exactly once and all callers await the same result — the Kotlin analog of SwiftyNetwork's in-flight refresh Task, and of Novalingo's sign-in mutex choke point.

Parameters

initialAccessToken

the token to start with.

refreshTokenHandler

mints a fresh access token, or returns null if refresh failed.

Constructors

Link copied to clipboard
constructor(initialAccessToken: String, refreshTokenHandler: suspend () -> String?)

Functions

Link copied to clipboard
fun close()

Cancels the internal coroutine scope used to coalesce refreshes. Call when the provider is discarded; any in-flight refresh is cancelled and subsequent refreshes will fail.

Link copied to clipboard
suspend fun currentAccessToken(): String

The current access token.

Link copied to clipboard
open suspend override fun currentAuthorization(): AuthorizationType

The authorization to attach to the next request.

Link copied to clipboard
open suspend override fun refreshAuthorizationIfNeeded(): Boolean

Attempts to refresh the credential after a 401. Returns true if a fresh credential is now available (and the request should be retried), false otherwise.