Plaid webhook verification. Plaid signs each webhook with an ES256 JWT in the Plaid-Verification header. The JWT's kid names a public key you fetch from /webhook_verification_key/get. Its claims carry iat (reject anything older than 5 minutes) and request_body_sha256 (the SHA-256 of the raw body, compared in constant time). See https://plaid.com/docs/api/webhooks/webhook-verification/. Needs Hot 3.0.2+ for ES256.

Values

KEY_CACHE_NAME

KEY_CACHE_NAME: Str "plaid:webhook-verification-keys"

KEY_CACHE_TTL_SECONDS

KEY_CACHE_TTL_SECONDS: Int 86400

MAX_AGE_SECONDS

MAX_AGE_SECONDS: Int 300

VERIFICATION_HEADER

VERIFICATION_HEADER: Str "plaid-verification"

Functions

body-hash-matches

fn (raw-body: Str | Bytes, claimed: Any): Bool

True when claimed (the request_body_sha256 claim) equals the hex SHA-256 of the raw body, compared in constant time. Hash the body exactly as received: Plaid's JSON uses 2-space indentation, and any reformatting breaks the match. Exposed for testing.

Example

hash ::hot::hash/sha256("{}")
::plaid::webhooks/body-hash-matches("{}", hash)  // true
::plaid::webhooks/body-hash-matches("{ }", hash)  // false

cached-verification-key

fn (key-id: Str): Map
fn (key-id: Str, opts: Map): Map

The JWK for a kid, from a persistent store cache (refetched after a day). Falls back to an uncached fetch when stores aren't available.

Example

key ::plaid::webhooks/cached-verification-key("6c5516e1-92dc-479e-a8ff-5a51992e0001")
key.expired_at

check-key

fn (jwk: Map, now: Int): Bool

Reject a JWK that isn't an ES256 / P-256 key, lacks x/y, or whose expired_at (Unix seconds) has passed. Returns true or an Err. Exposed for testing.

Example

jwk {alg: "ES256", crv: "P-256", x: "x", y: "y"}
::plaid::webhooks/check-key(jwk, 1793000000)  // true
is-err(::plaid::webhooks/check-key({...jwk, expired_at: 1}, 1793000000))  // true

check-webhook

fn (request: Map, jwk: Map): Map
fn (request: Map, jwk: Map, options: Map): Map

Verify a webhook against a known JWK ({kid, alg, crv, x, y, expired_at} from /webhook_verification_key/get). Returns the JWT claims, or an Err saying what failed. Options: max-age (seconds, default 300), leeway (seconds of clock skew, default 0), now (Unix seconds, for tests).

Example

check (request) {
  kid ::plaid::webhooks/token-key-id(::plaid::webhooks/verification-token(request))
  jwk ::plaid::webhooks/cached-verification-key(kid)
  claims ::plaid::webhooks/check-webhook(request, jwk, {leeway: 5})
  if-err(claims, (reason) { ::hot::http/HttpResponse({status: 401, body: reason}) })
}

check-webhook-with

fn (request: Map, jwk: Map, options: Map, verify-token: Fn): Map

check-webhook with the JWT verifier injected: verify-token(token, x, y, options) returns the claims or an Err, like ::jwt/verify-es256. Exposed so the header, key, iat and body checks can be tested with a stub verifier.

Example

token "eyJhbGciOiJFUzI1NiIsImtpZCI6ImsxIn0.e30.c2ln"
stub (t, x, y, o) { {iat: 1793000000, request_body_sha256: ::hot::hash/sha256("{}")} }
request {headers: {plaid-verification: token}, body-raw: "{}"}
::plaid::webhooks/check-webhook-with(request, {kid: "k1", x: "x", y: "y"}, {now: 1793000010}, stub).iat  // 1793000000

event

fn (request: Map): Map

The routing fields of a webhook body: {webhook_type, webhook_code, item_id, environment, error} (null for absent ones). Call after verifying.

Example

body {webhook_type: "TRANSACTIONS", webhook_code: "SYNC_UPDATES_AVAILABLE", item_id: "item_123"}
::plaid::webhooks/event({body: body}).webhook_code  // "SYNC_UPDATES_AVAILABLE"
::plaid::webhooks/event({body: body}).error  // null

get-verification-key

fn (key-id: Str): Map
fn (key-id: Str, opts: Map): Map

POST /webhook_verification_key/get: the JWK for a kid, uncached. Returns the key map.

Example

key ::plaid::webhooks/get-verification-key("6c5516e1-92dc-479e-a8ff-5a51992e0001")
::plaid::webhooks/check-key(key, ::hot::time/epoch-seconds())

token-key-id

fn (token: Str): Str

Decode the token header (without verifying it), require alg ES256, and return its kid. Errs otherwise. Exposed for testing.

Example

// The token's header is {"alg":"ES256","kid":"k1"}
::plaid::webhooks/token-key-id("eyJhbGciOiJFUzI1NiIsImtpZCI6ImsxIn0.e30.c2ln")  // "k1"

verification-token

fn (request: Map): Str

The JWT from the Plaid-Verification header (looked up case-insensitively), or "" when absent.

Example

::plaid::webhooks/verification-token({headers: {Plaid-Verification: " abc "}})  // "abc"
::plaid::webhooks/verification-token({headers: {}})  // ""

verify-options

fn (options: Map): Map

Options for ::jwt/verify-es256: max-age 300 seconds (Plaid's five minutes) and no extra leeway, overridable. Exposed for testing.

Example

::plaid::webhooks/verify-options({leeway: 5})  // {max-age: 300, leeway: 5}

verify-webhook

fn (request: Map): Bool
fn (request: Map, jwk: Map): Bool
fn (request: Map, jwk: Map, options: Map): Bool
fn (request: Map, jwk: Map?, options: Map, opts: Map): Bool

Verify a Plaid webhook request. Returns true only when the header is present, the JWT is ES256 and signed by Plaid's key for its kid, iat is at most five minutes old, and request_body_sha256 matches the raw body. The 1-arity form fetches (and caches) the key with plaid.client-id / plaid.secret, failing when they are unset; the 2- and 3-arity forms take a JWK you fetched yourself, plus options as in check-webhook. The 4-arity form takes per-call credentials to fetch the key with: pass a null jwk. Use check-webhook to learn why a request failed.

Example

plaid-webhook
meta {webhook: {service: "plaid", path: "/plaid/webhook"}}
fn (request) {
  if(not(::plaid::webhooks/verify-webhook(request)),
    ::hot::http/HttpResponse({status: 401, body: "invalid signature"}),
    ::plaid::webhooks/event(request))
}

// With a tenant's credentials, fetching and caching the key
tenant-check (request) {
  ::plaid::webhooks/verify-webhook(request, null, {}, {client-id: ::hot::ctx/get("tenant.plaid.client-id"), secret: ::hot::ctx/get("tenant.plaid.secret")})
}
Context Vars: plaid.client-id, plaid.secret