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. When a late 401 rejects a token another request has already replaced, NetworkClient reuses the current token without refreshing it again. This check and refresh admission share the mutex.

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 (e.g. on sign-out): any in-flight refresh is cancelled, and it and every subsequent refreshAuthorizationIfNeeded report false, so requests fail with NetworkError.AuthorizationRefreshFailed instead of refreshing.

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.