Dropbox credential resolution and the per-run access-token cache. Usually used indirectly through ::dropbox::api.

Credential sources. Per-call opts win over context. When opts carries a primary credential (token or refresh-token), context credentials are ignored for that call. Within one source an access token wins over a refresh token.

A credential key (token, refresh-token, app-key, app-secret) that is in opts but blank or null is an error naming the key, never a fallback to context. Credentials from opts are marked as secrets (::hot::ctx/mark-secret), so call records mask them; they are never written to context. Leave app-secret out for a PKCE client.

Credentialopts keysContext variables
Access tokentokendropbox.access.token
Refresh tokenrefresh-token, app-key, app-secretdropbox.refresh.token, dropbox.app.key, dropbox.app.secret

The app secret is optional for refresh tokens issued through a PKCE flow (public clients); confidential apps must send it.

Token cache. Access tokens minted from a refresh token (they last about 4 hours) are cached in the run's execution context (::hot::ctx, stored as secrets) under a key derived from the app key and refresh token, until expires_in minus 300 seconds. The cache lives for one run. Explicit access tokens are never cached. Every token the token endpoint returns is marked as a secret before it leaves this package.

Values

BLANK_CHECKED_KEYS

BLANK_CHECKED_KEYS: Vec [
  "token",
  "refresh-token",
  "app-key",
  "app-secret"
]

CACHE_PREFIX

CACHE_PREFIX: Str "dropbox.token-cache."

CREDENTIAL_KEYS

CREDENTIAL_KEYS: Vec [
  "token",
  "refresh-token",
  "app-key",
  "app-secret",
  "select-user",
  "select-admin",
  "path-root",
  "retry"
]

EXPIRY_MARGIN_SECONDS

EXPIRY_MARGIN_SECONDS: Int 300

SECRET_KEYS

SECRET_KEYS: Vec [
  "token",
  "refresh-token",
  "app-secret"
]

TOKEN_RESPONSE_SECRETS

TOKEN_RESPONSE_SECRETS: Vec [
  "access_token",
  "refresh_token",
  "id_token"
]

Functions

access-token

fn (): Str
fn (opts: Map): Str

Resolve an access token from opts and context: the explicit token, or one minted from the refresh token (cached for the run). Returns the token string or a TokenError.

token ::dropbox::auth/access-token()

// Per call: one tenant's stored refresh token
token2 ::dropbox::auth/access-token({
  refresh-token: "tenant-refresh-token",
  app-key: "abc123appkey",
  app-secret: "app-secret"
})

bearer

fn (creds: Map): Map

Resolve a bearer token for resolved credentials, using the per-run cache. Returns {token, cache-key, cached} (cached is true when the token came from the cache, so a 401 should evict and retry), or a TokenError.

blank-credential

fn (opts: Map): Str?

The first credential key opts has with a blank or null value, else null (pure).

cache-entry

fn (tokens: Map, now: Int): Map

The cached form of a token response: {access-token, expires-at} (epoch ms), expiring EXPIRY_MARGIN_SECONDS early. Dropbox's expires_in is 14400 (4 hours) today.

credentials-from

fn (source: Map): Map

Turn a credential source Map (the opts shape) into resolved credentials: {kind: "token", token} or {kind: "refresh", refresh-token, app-key, app-secret?}. Returns a TokenError when nothing usable is set or a refresh token lacks its app key. Pure.

ctx-source

fn (): Map

The credential source read from context, in the same shape as opts.

Context Vars: dropbox.access.token, dropbox.refresh.token, dropbox.app.key, dropbox.app.secret

evict-token

fn (cache-key: Str?): Bool

Drop a cached token (e.g. after a 401). No-op for a null key.

has-primary-credential

fn (source: Map): Bool

True when source carries a primary credential: token or refresh-token.

is-fresh

fn (entry: Any, now: Int): Bool

True when a cache entry exists and has not reached its (margin-adjusted) expiry.

mark-tokens

fn (tokens: Map): Map

Mark the tokens of a token response as secrets and return the response unchanged.

mark-values

fn (values: Vec): Vec

Register the non-blank values as secrets for this run's call records (::hot::ctx/mark-secret).

merge-sources

fn (opts: Map, ctx: Map): Map

Pick the credential source for a call: opts when it carries a primary credential, otherwise ctx. A refresh token in opts without an app key borrows the context app key and secret. Pure; resolve-credentials passes the live context.

now-ms

fn (): Int

refresh-token-grant

fn (app-key: Str, app-secret: Str?, refresh-token: Str): Map

Exchange a refresh token for an access token at https://api.dropboxapi.com/oauth2/token (no cache). Returns the token response (access_token, expires_in, token_type) or a TokenError.

resolve-credentials

fn (): Map
fn (opts: Map): Map

Resolve the credentials for a call from opts and context (see the namespace doc for precedence). Credentials from opts are marked as secrets first, and never written to context.

resolve-from

fn (opts: Map, ctx: Map): Map

The one resolver, pure: credentials for a call from opts first, then ctx (a source Map, see ctx-source). An Err when a credential key is in opts but blank or null; otherwise as credentials-from(merge-sources(opts, ctx)). Exposed for testing; resolve-credentials feeds it the context.

token-cache-key

fn (creds: Map): Str?

The context key a minted token is cached under (a SHA-256 of the app key and refresh token, so neither appears in the key), or null for explicit access tokens.

token-error

fn (message: Str): TokenError
fn (prefix: Str, payload: Any): TokenError

Build a TokenError. The 1-arity form wraps a plain message; the 2-arity form prefixes the payload of a failed ::oauth2 call ({status, body} or a transport error).

Types

TokenError

TokenError type {
    message: Str,
    status: Int?,
    error-code: Str?,
    error-description: Str?,
    body: Any?
}

A credential or token-endpoint failure. message is readable, e.g. "dropbox: token refresh failed: invalid_grant (refresh token is invalid or revoked)"; error-code and error-description carry the OAuth error / error_description when the token endpoint answered. Tokens are never included.