JSON Web Tokens (RFC 7519) for Hot.

Supports HS256 (HMAC-SHA256, shared secret) and RS256 (RSASSA-PKCS1-v1_5, RSA key pair) for signing and verification — the two algorithms that cover almost every real integration: session tokens, OIDC providers, GitHub App tokens, and Google service accounts. ES256 (ECDSA P-256) is supported for verification, as used by Plaid webhooks and Apple.

Quick start

::jwt ::jwt

secret ::hot::ctx/get("jwt.secret")

// Sign (HS256)
token ::jwt/sign({sub: "user-42", exp: add(::jwt/now-seconds(), 3600)}, secret)

// Verify + validate claims
claims ::jwt/verify(token, secret)
claims.sub

// RS256 with a PEM private key (Google service accounts, GitHub Apps)
private-key-pem ::hot::ctx/get("jwt.private.key")
rs-token ::jwt/sign(claims, private-key-pem, "RS256")

// RS256 verification from JWKS components
rsa-jwk ::hot::ctx/get("jwt.rsa.jwk")
rs-claims ::jwt/verify-rs256(rs-token, rsa-jwk.n, rsa-jwk.e)

// ES256 verification from an EC JWK
ec-jwk ::hot::ctx/get("jwt.ec.jwk")
es-token ::hot::ctx/get("jwt.es.token")
es-claims ::jwt/verify-es256(es-token, ec-jwk.x, ec-jwk.y, {max-age: 300})

Verification returns the payload claims on success and an Err describing the failure (bad signature, expired, wrong issuer, ...) otherwise — branch with is-err / if-err.

Values

DEFAULT_LEEWAY_SECONDS

DEFAULT_LEEWAY_SECONDS: Int 60

Functions

decode

fn (token: Str): Map

Split and decode a JWT without verifying it. Returns {header, payload, signing-input, signature} where signature is the raw base64url segment. Use this to inspect an untrusted token (e.g. to pick a JWKS key by kid); never trust its payload without verify.

Example

decoded ::jwt/decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyLTQyIn0.c2ln")
decoded.header  // {alg: "HS256", typ: "JWT"}
decoded.payload  // {sub: "user-42"}

now-seconds

fn (): Int

Current Unix time in whole seconds. Convenient for building exp / iat / nbf claims.

Example

now ::jwt/now-seconds()
{iat: now, exp: add(now, 3600)}

sign

fn (claims: Map, key: Str): Str
fn (claims: Map, key: Str, algorithm: Str): Str

Sign claims into a compact JWT. The 2-arity form signs with HS256 and a shared secret; pass "RS256" and a PEM private key (PKCS#8 BEGIN PRIVATE KEY or PKCS#1 BEGIN RSA PRIVATE KEY) for RSA.

The key and the signed token are masked in the run's call records. The token comes back unchanged.

Example

secret ::hot::ctx/get("jwt.secret")
token ::jwt/sign({sub: "user-42", exp: add(::jwt/now-seconds(), 3600)}, secret)

// Google service-account assertion
sa ::hot::ctx/get("google.service.account")
assertion ::jwt/sign({
  iss: sa.client_email,
  scope: "https://www.googleapis.com/auth/gmail.send",
  aud: "https://oauth2.googleapis.com/token",
  iat: ::jwt/now-seconds(),
  exp: add(::jwt/now-seconds(), 3600)
}, sa.private_key, "RS256")

verify

fn (token: Str, secret: Str): Any
fn (token: Str, secret: Str, options: Map): Any

Verify an HS256 token and validate its claims. Returns the payload claims, or an Err describing the failure.

Options (3-arity): leeway (seconds of clock skew tolerance, default 60), issuer (expected iss), audience (expected aud), max-age (reject tokens whose iat is more than this many seconds old; the token must carry iat), now (override the clock, mainly for tests). The shared secret is masked in the run's call records.

Example

token ::hot::ctx/get("session.token")
secret ::hot::ctx/get("jwt.secret")
claims ::jwt/verify(token, secret, {issuer: "https://auth.example.com"})
if-err(claims, (e) { ::hot::http/HttpResponse({status: 401, body: "invalid token"}) })

verify-es256

fn (token: Str, x: Any, y: Any): Any
fn (token: Str, x: Any, y: Any, options: Map): Any

Verify an ES256 (ECDSA P-256 / SHA-256) token against an EC public key and validate its claims. x and y are the base64url-encoded JWK coordinate fields (strings) or raw Bytes. Options as in verify. Requires Hot 3.0.2 or later (::hot::crypto/ecdsa-p256-sha256-verify).

Example

// Plaid webhooks: the token is in a header, and the JWK is looked up by kid
on-webhook fn (request) {
  token get(request.headers, "plaid-verification")
  decoded ::jwt/decode(token)
  key ::hot::ctx/get(`plaid.jwk.${get(decoded.header, "kid")}`)
  ::jwt/verify-es256(token, key.x, key.y, {max-age: 300})
}

verify-rs256

fn (token: Str, n: Any, e: Any): Any
fn (token: Str, n: Any, e: Any, options: Map): Any

Verify an RS256 token against RSA public key components and validate its claims. n and e are the base64url-encoded JWKS fields (strings) or raw Bytes. Options as in verify.

Example

token ::hot::ctx/get("oidc.id.token")
jwks ::hot::http/get("https://www.googleapis.com/oauth2/v3/certs").body

// Pick the key by kid
decoded ::jwt/decode(token)
key first(filter(jwks.keys, (k) { eq(k.kid, get(decoded.header, "kid")) }))
claims ::jwt/verify-rs256(token, key.n, key.e, {issuer: "https://accounts.google.com"})