OAuth 2.0 client flows.

Covers the grants that real integrations need: authorization-code (with PKCE), refresh-token, client-credentials, and JWT-bearer (RFC 7523 — Google service accounts and similar).

Authorization-code + PKCE

::oauth2 ::oauth2

client-id "my-app"
client-secret ::hot::ctx/get("oauth.client.secret")
state "af0ifjsldkj"
code "4/P7q7W91a-oMsCeLvIaQm6bTrgtp7"

verifier ::oauth2/pkce-verifier()
url ::oauth2/authorize-url({
  authorize-url: "https://accounts.google.com/o/oauth2/v2/auth",
  client-id: client-id,
  redirect-uri: "https://myapp.hot.dev/oauth/callback",
  scope: "openid email",
  state: state,
  code-challenge: ::oauth2/pkce-challenge(verifier)
})
// redirect the user to `url`; in the callback:
tokens ::oauth2/exchange-code("https://oauth2.googleapis.com/token", {
  client-id: client-id,
  client-secret: client-secret,
  code: code,
  redirect-uri: "https://myapp.hot.dev/oauth/callback",
  code-verifier: verifier
})
tokens.access_token

Token calls return the provider's decoded JSON body on success, or an Err carrying {status, body} on failure.

Masking. Client secrets, codes, refresh tokens, PKCE verifiers and JWT assertions are marked with ::hot::ctx/mark-secret on the way in, and access_token, refresh_token and id_token on the way out, so they are masked in the run's call records, including when a provider echoes them back in an error. Values are returned unchanged. Records of your own calls that finished before a value reached this package may already be written, so mark a secret your code fetches as soon as you have it.

Values

SECRET_FIELDS

SECRET_FIELDS: Vec [
  "client_secret",
  "code",
  "refresh_token",
  "code_verifier",
  "assertion",
  "client_assertion",
  "password"
]

SECRET_OPTIONS

SECRET_OPTIONS: Vec [
  "client-secret",
  "code",
  "refresh-token",
  "code-verifier"
]

TOKEN_FIELDS

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

Functions

authorize-url

fn (config: Map): Str

Build the authorization redirect URL. Config fields: authorize-url (required), client-id (required), redirect-uri (required), scope, state, code-challenge (S256 assumed), extra (Map of provider-specific params, e.g. {access_type: "offline", prompt: "consent"}).

Example

::oauth2/authorize-url({
  authorize-url: "https://auth.example.com/authorize",
  client-id: "my-app",
  redirect-uri: "https://myapp.hot.dev/cb",
  scope: "email"
})  // "https://auth.example.com/authorize?response_type=code&client_id=my-app&redirect_uri=https%3A%2F%2Fmyapp.hot.dev%2Fcb&scope=email"

build-client-credentials-body

fn (options: Map): Map

Build the client_credentials grant body. Fields: client-id, client-secret (required); scope, audience (optional).

Example

body ::oauth2/build-client-credentials-body({client-id: "my-app", client-secret: "test-secret", scope: "read:orders"})
body.grant_type  // "client_credentials"
body.scope  // "read:orders"

build-exchange-body

fn (options: Map): Map

Build the authorization_code grant body. Fields: client-id, code, redirect-uri (required); client-secret, code-verifier (optional — PKCE public clients omit the secret).

Example

body ::oauth2/build-exchange-body({client-id: "my-app", code: "abc", redirect-uri: "https://myapp.hot.dev/cb"})
body.grant_type  // "authorization_code"
keys(body)  // ["grant_type", "code", "client_id", "redirect_uri"]

build-jwt-bearer-body

fn (assertion: Str): Map

Build the RFC 7523 JWT-bearer grant body from a signed assertion (e.g. a Google service-account JWT).

Example

::oauth2/build-jwt-bearer-body("header.claims.signature").grant_type  // "urn:ietf:params:oauth:grant-type:jwt-bearer"

build-refresh-body

fn (options: Map): Map

Build the refresh_token grant body. Fields: client-id, refresh-token (required); client-secret, scope (optional).

Example

::oauth2/build-refresh-body({client-id: "my-app", refresh-token: "test-refresh"})
// {grant_type: "refresh_token", refresh_token: "test-refresh", client_id: "my-app"}

client-credentials

fn (token-url: Str, options: Map): Map

Machine-to-machine tokens via the client_credentials grant. See build-client-credentials-body for the options.

Example

tokens ::oauth2/client-credentials("https://auth.example.com/token", {
  client-id: "my-app",
  client-secret: ::hot::ctx/get("oauth.client.secret"),
  scope: "read:orders",
  audience: "https://api.example.com"
})
tokens.access_token

exchange-code

fn (token-url: Str, options: Map): Map

Exchange an authorization code for tokens. See build-exchange-body for the options.

Example

code "4/P7q7W91a-oMsCeLvIaQm6bTrgtp7"
tokens ::oauth2/exchange-code("https://oauth2.googleapis.com/token", {
  client-id: "my-app",
  client-secret: ::hot::ctx/get("oauth.client.secret"),
  code: code,
  redirect-uri: "https://myapp.hot.dev/oauth/callback"
})
tokens.access_token
tokens.refresh_token

jwt-bearer

fn (token-url: Str, assertion: Str): Map

RFC 7523 JWT-bearer grant: exchange a signed assertion for an access token. This is how Google service accounts authenticate.

Example

// A JWT signed with the service account's key, e.g. by `hot.dev/jwt`.
assertion ::hot::ctx/get("google.sa.assertion")

tokens ::oauth2/jwt-bearer("https://oauth2.googleapis.com/token", assertion)
tokens.access_token

mark-tokens

fn (reply: Any): Any

Mask the tokens in a token endpoint's reply and return it unchanged: access_token, refresh_token and id_token of a Map, or the whole reply if it isn't JSON.

Example

reply ::oauth2/mark-tokens({access_token: "test-token", expires_in: 3600})
reply.expires_in  // 3600

pkce-challenge

fn (verifier: Str): Str

Compute the S256 code challenge for a PKCE verifier: BASE64URL(SHA256(verifier)).

Example (RFC 7636 appendix B vector)

::oauth2/pkce-challenge("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk")
// => "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"

pkce-verifier

fn (): Str

Generate a high-entropy PKCE code verifier (43 characters, base64url of 32 random bytes). Store it for the token exchange.

Example

length(::oauth2/pkce-verifier())  // 43

refresh

fn (token-url: Str, options: Map): Map

Refresh an access token. See build-refresh-body for the options.

Example

tokens ::oauth2/refresh("https://oauth2.googleapis.com/token", {
  client-id: "my-app",
  client-secret: ::hot::ctx/get("oauth.client.secret"),
  refresh-token: ::hot::ctx/get("oauth.refresh.token")
})
tokens.access_token
tokens.expires_in

token-request

fn (token-url: Str, body: Map): Map

POST a form-encoded grant body to a token endpoint and decode the JSON response. Returns the token response Map (access_token, expires_in, ...) or an Err {status, body} for a non-2xx reply. The grant-specific helpers below are usually more convenient. Credentials in body and tokens in the reply are masked in the run's call records.

Example

tokens ::oauth2/token-request("https://auth.example.com/token", {
  grant_type: "client_credentials",
  client_id: "my-app",
  client_secret: ::hot::ctx/get("oauth.client.secret")
})
tokens.access_token

token-result

fn (response: Result): Map

What token-request returns for an HTTP response: the decoded reply with its tokens masked, an Err {status, body} for a non-2xx reply, or the transport Err. Exposed for testing.

Example

granted ::hot::http/HttpResponse({status: 200, headers: {}, body: {access_token: "test-token", expires_in: 3600}})
::oauth2/token-result(granted).expires_in  // 3600

denied ::hot::http/HttpResponse({status: 400, headers: {}, body: {error: "invalid_grant"}})
is-err(::oauth2/token-result(denied))  // true