Box OAuth 2.0 (authorization code) helpers, built on hot.dev/oauth2.
Flow
- Redirect the user to
authorize-url(...). - In your callback,
exchange-code(...)turns?code=into tokens: an access token (about 60 minutes) and a refresh token (60 days, single use). - Keep the pair:
::box::auth/save-tokens(account, tokens)puts it in the store-backed keeper, and later calls pass{account}. Or store it yourself and userefresh-tokens/on-refresh, saving the new refresh token every time.
::oauth ::box::oauth
client-id ::hot::ctx/get("box.client.id")
url ::oauth/authorize-url(client-id, "https://app.example.com/oauth/box", "csrf-123")
// callback handler:
code "code-from-the-callback-query"
tokens ::oauth/exchange-code(client-id, ::hot::ctx/get("box.client.secret"), code, "https://app.example.com/oauth/box")
::box::auth/save-tokens("user:42", tokens)
Functions
authorize-url
fn (client-id: Str, redirect-uri: Str, state: Str): Str
fn (client-id: Str, redirect-uri: Str, state: Str, opts: Map): Str
Build the Box consent URL (https://account.box.com/api/oauth2/authorize).
state is your CSRF token. Options (all optional): scope (a Vec or
space-separated Str such as "root_readonly"; omit it for the app's
configured scopes), extra (any other parameters).
url ::box::oauth/authorize-url("abc123clientid", "https://app.example.com/oauth/box", "csrf-123")
exchange-code
fn (code: Str, redirect-uri: Str?, opts: Map): Map
fn (client-id: Str, client-secret: Str, code: Str, redirect-uri: Str?): Map
Exchange an authorization code for tokens (codes expire after 30
seconds). Returns Box's token response (access_token, expires_in,
refresh_token, token_type, restricted_to), or a
::box::auth/TokenError such as "box: token request failed: invalid_grant (Auth code doesn't exist or is invalid for the client)".
The 3-arity form takes the app keys from opts (client-id,
client-secret) or the box.client.id / box.client.secret context
variables.
Example
tokens ::box::oauth/exchange-code(::hot::ctx/get("box.client.id"), ::hot::ctx/get("box.client.secret"), "code-from-callback", "https://app.example.com/oauth/box")
::box::auth/save-tokens("user:42", tokens)
// App keys from context
tokens2 ::box::oauth/exchange-code("code-from-callback", "https://app.example.com/oauth/box", {})
tokens2.expires_in
refresh-tokens
fn (refresh-token: Str, opts: Map): Map
fn (client-id: Str, client-secret: Str, refresh-token: Str): Map
Spend a refresh token and return the new pair (Box's token
response: access_token, refresh_token, expires_in). The refresh
token you passed is dead afterwards, so persist refresh_token before
doing anything else. A used, revoked or expired token yields
error-code: "invalid_grant": ask the user to authorize again. The
2-arity form takes the app keys from opts or context.
saved ::box::auth/load-tokens("user:42")
tokens ::box::oauth/refresh-tokens(::hot::ctx/get("box.client.id"), ::hot::ctx/get("box.client.secret"), saved.refresh-token)
::box::auth/save-tokens("user:42", tokens) // do this first
::box::folders/list-items("0", {token: tokens.access_token})
revoke-body
fn (client-id: Str, client-secret: Str, token: Str): Map
The /oauth2/revoke form body (pure): client_id, client_secret,
token.
Example
body ::box::oauth/revoke-body(::hot::ctx/get("box.client.id"), ::hot::ctx/get("box.client.secret"), ::hot::ctx/get("box.access.token"))
body.client_id
revoke-token
fn (token: Str, opts: Map): Bool
fn (client-id: Str, client-secret: Str, token: Str): Bool
Revoke an access or refresh token (POST /oauth2/revoke; revoking
either ends the session). Returns true, or a TokenError. Pair it with
::box::auth/forget-tokens on disconnect. The 2-arity form takes the
app keys from opts or context.
Example
::box::oauth/revoke-token(::hot::ctx/get("box.client.id"), ::hot::ctx/get("box.client.secret"), ::hot::ctx/get("box.access.token"))
// App keys from context
::box::oauth/revoke-token(::hot::ctx/get("box.access.token"), {})
::box::auth/forget-tokens("tenant-42")