Skip to main content

Module auth

Module auth 

Source
Expand description

SDK-owned authentication: the session/token store and the auto-refresh engine.

This is the “SDK owns the complete user flow” foundation: native apps call AuthClient::login, then hand every request through Session::execute, then Session::logout — they never juggle raw access tokens. The store keeps the access + refresh token pair and:

  • runs a quick asynchronous pre-flight expiry check before each request and refreshes proactively when the access token is within the refresh skew of expiry (Session::valid_access_token);
  • single-flights refreshes — concurrent callers that all see a stale token coalesce onto one network refresh (Session::ensure_refreshed);
  • retries once on 401: an authenticated request that the server rejects is refreshed once and replayed, so a token that expired between the pre-flight check and the wire is transparently recovered.

It is hand-rolled over reqwest (rustls only) against the server’s own /v1/auth/{register,login,refresh,logout}. It does not route through the generated client, but the reason is no longer that spargen is parked — spargen ships and generates the typed surface today. What lives here is token orchestration: the pre-flight refresh, the 401-retry-once replay, and the session store they mutate. That is deliberately outside generated code, which owns parsing and serialization and nothing else. Auth requests are crate::net::RetryClass::Interactive; the full backoff ladder lives in crate::net, but the 401-retry-once and pre-flight refresh here are the parts the session store owns.

The OIDC legs (AuthClient::begin_oidc_login and AuthClient::complete_oidc_login) are not hand-rolled: neither is token orchestration, so the exemption above does not cover them, and they call the generated rest::Client — every body and every response parsed by generated code, with only the mapping into LoginOutcome and AuthError written here. The browser leg between the two is the platform’s: a loopback listener on the CLI, ASWebAuthenticationSession on iOS. The ceremony is bound to the client that began it: the begin leg mints a secret the OidcAuthorization keeps and sends only its hash, and the complete leg presents the secret and refuses a redirect carrying a state it did not begin (issue #517).

The device-grant legs (RFC 8628, issue #635: AuthClient::begin_oidc_device_login and AuthClient::poll_oidc_device_login) are the same kind of thing: two generated calls, the begin binding the ceremony to this client the same way, and the poll answering OidcDevicePoll — keep polling, slow down, or the LoginOutcome a password login gives. The wait between polls is the caller’s; these send one request each.

AuthClient::sign_in_options reads which sign-ins the server offers from its server-info record, also through the generated client, so a client can leave out a password form the server would refuse (auth.password_disabled, issue #1004). It is advice: the server’s own 403 error.auth.password_disabled stays the authority.

§Testing

The wire flows (login/refresh/logout, 401 recovery, error mapping) are proven against a focused in-process mock HTTP server rather than a booted server: booting one from this crate would invert the dependency — the server dev-depends on this crate, not the other way round — and a mock keeps every wire case deterministic. The round trip against the real router, over a real socket, lives where the dependency points: capsule-server/tests/sdk_client.rs. The pre-flight and single-flight guarantees are proven with an injected Clock — no sleeps, no wall-clock dependence.

Structs§

AuthClient
The unauthenticated entry point: turns credentials (or a persisted session) into an authenticated Session.
OidcAuthorization
A begun sign-in through the identity provider.
OidcDeviceAuthorization
A begun device sign-in through the identity provider (RFC 8628, issue #635).
PersistedSession
A session’s token material for handing to the platform’s secure storage so a session survives process restarts. The SDK owns the store and refresh logic; the platform owns only the at-rest bytes.
Session
An authenticated session. Cheaply cloneable — every clone shares one token store and one single-flight gate, so refreshes coalesce across clones and tasks.
SignInOptions
How a server signs people in, as its .well-known/capsule/server-info record publishes it.
SystemClock
The production Clock, backed by the system wall clock.

Enums§

AuthError
Everything the auth flows can fail with. Callers switch on the typed variant (never a bare HTTP status); AuthError::error_code yields the stable error.* catalog code for client localization where one applies.
LoginOutcome
What a password login answered with.
OidcDevicePoll
What one device poll answered.

Constants§

DEVICE_SLOW_DOWN_STEP_SECS
How much a slow_down raises a device sign-in’s polling interval by (RFC 8628 §3.5). The server raises its own copy by the same amount, which is why the new interval is never sent.

Traits§

Clock
Wall-clock seam so token-expiry logic is deterministically testable.