Karta.Webhooks.Signature (Karta v1.0.0)

Standard Webhooks signatures (https://www.standardwebhooks.com/), as Karta signs every webhook request in its own format:

  • webhook-id - the event's id, the same on every attempt and replay (receivers use it to drop duplicates);
  • webhook-timestamp - when the request was signed, in seconds since 1970 (receivers refuse requests older or newer than a few minutes);
  • webhook-signature - v1, and the base64 HMAC-SHA256 of <id>.<timestamp>.<body> under the webhook's secret. While a rotated secret is still valid, the request carries one signature per secret, separated by spaces; a receiver accepts the request when any of them is valid.

A secret is whsec_ and the base64 of 32 random bytes; the HMAC key is those bytes (the prefix is optional when a secret is given).

verify/4 is the check a receiver makes, as the reference libraries make it: Karta's tests use it, and it documents what receivers do.

Summary

Functions

The three headers of a request: its id, the time (seconds) and one signature per secret (the current one first).

A new secret: whsec_ and the base64 of 32 random bytes.

The signature of body sent with id at timestamp (seconds), as v1,<base64>. Raises ArgumentError for a secret that is empty or not base64.

A receiver's check of a request: the three headers present, the timestamp within five minutes of now (option, in seconds; default the clock's), and a v1 signature among those given that matches under secret. Entries that are not v1,<base64> are skipped. headers are the request's (lower-case names).

Functions

headers(secrets, id, timestamp, body)

@spec headers([String.t(), ...], String.t(), integer(), iodata()) :: [
  {String.t(), String.t()}
]

The three headers of a request: its id, the time (seconds) and one signature per secret (the current one first).

new_secret()

@spec new_secret() :: String.t()

A new secret: whsec_ and the base64 of 32 random bytes.

sign(secret, id, timestamp, body)

@spec sign(String.t(), String.t(), integer(), iodata()) :: String.t()

The signature of body sent with id at timestamp (seconds), as v1,<base64>. Raises ArgumentError for a secret that is empty or not base64.

verify(secret, headers, body, opts \\ [])

@spec verify(String.t(), [{String.t(), String.t()}], iodata(), keyword()) ::
  :ok | {:error, String.t()}

A receiver's check of a request: the three headers present, the timestamp within five minutes of now (option, in seconds; default the clock's), and a v1 signature among those given that matches under secret. Entries that are not v1,<base64> are skipped. headers are the request's (lower-case names).