Ankusa.SDK.Signature (ankusa_sdk v0.6.0)

Copy Markdown View Source

Verify the Standard Webhooks signature an Ankusa.Sink.Http sink with a secret adds to every delivery.

webhook-signature holds space-separated v1,<base64> entries, each an HMAC-SHA256 over <webhook-id>.<webhook-timestamp>.<body>. A delivery passes when any v1 entry matches any configured secret (several during a rotation), compared in constant time, and webhook-timestamp is within the tolerance of now.

Secrets are whsec_ + base64, or any other string used as its own bytes. Ankusa.SDK.Receiver calls this for you when given :secret.

Summary

Types

A verified delivery's webhook-id and webhook-timestamp.

Functions

Verify one delivery. headers is a map or a list of {name, value} pairs (names case-insensitive; in a list the first occurrence wins); body must be the raw bytes received; secrets is one secret or a list.

Types

verified()

@type verified() :: %{id: String.t(), timestamp: non_neg_integer()}

A verified delivery's webhook-id and webhook-timestamp.

Functions

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

@spec verify(Enumerable.t(), binary(), String.t() | [String.t()], keyword()) ::
  {:ok, verified()} | {:error, Ankusa.SDK.InvalidSignatureError.t()}

Verify one delivery. headers is a map or a list of {name, value} pairs (names case-insensitive; in a list the first occurrence wins); body must be the raw bytes received; secrets is one secret or a list.

Options: :tolerance_seconds (default 300) and :now (unix seconds; default the system clock).

The checks run in this order, the first failure deciding the error's :code: "invalid_secret", "missing_header" (webhook-id, webhook-timestamp, webhook-signature), "invalid_timestamp", "timestamp_out_of_tolerance", "no_matching_signature".