DpopAuthProvider

class DpopAuthProvider(session: OAuthSession, signer: DpopSigner, sessionStore: OAuthSessionStore, refreshTransport: TokenEndpointTransport, json: Json = Json { ignoreUnknownKeys = true }, onPersistFailure: (Throwable) -> Unit = {}) : AuthProvider

AuthProvider implementation that attaches DPoP proof-of-possession headers on every XRPC request and handles token refresh transparently.

On each request, produces:

  • Authorization: DPoP <access_token>

  • DPoP: <signed-jwt-proof> (with ath, htm, htu, nonce)

When the PDS responds with HTTP 401:

  • If DPoP-Nonce header is present → stores the nonce, retries

  • If the access token is expired, or the 401 challenges it with WWW-Authenticate: DPoP error="invalid_token" → refreshes via the token endpoint with the DPoP-bound refresh token, retries. Any other 401 does not refresh: every rotation spends a single-use token, and a rotation whose response is lost ends the session on the next replay

  • If the request was sent with an access token the session has since replaced (a refresh finished while it was in flight) → retries with the current token and does not refresh again

  • If the refresh token is revoked (error=invalid_grant) → clears the session, throws OAuthSessionExpiredException

  • Otherwise (network error, 5xx/429, unparseable/captive-portal body, or any non-invalid_grant error) → throws the retryable OAuthRefreshFailedException and LEAVES THE SESSION INTACT, so a flaky connection can't sign the user out

Refresh operations are serialized with one Mutex per account (DID), shared by every provider in the process, so two clients built from the same session cannot spend the same refresh token at once.

Constructors

Link copied to clipboard
constructor(session: OAuthSession, signer: DpopSigner, sessionStore: OAuthSessionStore, refreshTransport: TokenEndpointTransport, json: Json = Json { ignoreUnknownKeys = true }, onPersistFailure: (Throwable) -> Unit = {})
constructor(session: OAuthSession, signer: DpopSigner, sessionStore: OAuthSessionStore, refreshClient: HttpClient, json: Json = Json { ignoreUnknownKeys = true }, onPersistFailure: (Throwable) -> Unit = {})

Refreshes by posting directly to the token endpoint with refreshClient — the behaviour of a public client. Equivalent to passing a DirectTokenEndpointTransport built from that client, so the same requirements apply to it: no silent re-sends, and a request timeout.

Functions

Link copied to clipboard
open suspend override fun authHeaders(method: String, url: String): Map<String, String>
Link copied to clipboard
open suspend override fun onUnauthorized(responseHeaders: Map<String, String>): Boolean

A 401 reported without the headers its request carried. The token that was rejected is then unknown and taken to be the session's current one. XrpcClient calls the two-argument overload; prefer it.

open suspend override fun onUnauthorized(responseHeaders: Map<String, String>, requestAuthHeaders: Map<String, String>): Boolean

Called by XrpcClient on HTTP 401. Recovers every recoverable cause in one call so the single retry that XrpcClient performs always carries fresh state. Control flow: