Ankusa.SDK.Webhook (ankusa_sdk v0.6.0)

Copy Markdown View Source

The headers every receiver of Ankusa's HTTP sink needs, parsed off a request.

Ankusa.Sink.Http sends these headers; the hook id is the only one that is always there:

HeaderFieldDefault
x-ankusa-ididrequired — its absence means the request isn't a delivery
x-ankusa-sourcesource""
x-ankusa-tenanttenantnil
content-typecontent_typenil
x-ankusa-dedupe-keydedupe_keynil (also when empty)
x-ankusa-replay-idreplay_idnil (also when empty)
x-ankusa-idempotency-keyidempotency_keynil (also when empty)
x-ankusa-received-atreceived_atnil (absent, empty, or not 1 to 15 decimal digits); unix milliseconds the edge accepted the hook
x-ankusa-body-sha256body_sha256nil (absent, or not exactly 64 lowercase hex characters); the SHA-256 of the body sent

The parsed struct also carries every request header in headers (lowercased), so a receiver can read the provider's own headers the sink forwarded.

Names are matched case-insensitively, so anything from a Plug req_headers list to a hand-built map works. In a list the first occurrence of a name wins, matching HTTP's rule for repeated headers.

receive_delivery/3 is the receiver-side decision on top of them: it checks the delivery's signature (HTTP sinks are signed by default) and then parses the headers. Ankusa.SDK.Receiver is a Plug around it.

Summary

Functions

Parse the Ankusa headers out of headers, a map or a list of {name, value} pairs (conn.req_headers is already the latter).

Decide whether to accept one HTTP-sink delivery, and parse its headers.

Functions

parse_headers(headers)

@spec parse_headers(term()) ::
  {:ok, Ankusa.SDK.Webhook.Headers.t()}
  | {:error, Ankusa.SDK.MissingHookIdError.t()}

Parse the Ankusa headers out of headers, a map or a list of {name, value} pairs (conn.req_headers is already the latter).

Returns {:error, %Ankusa.SDK.MissingHookIdError{}} when x-ankusa-id is absent or empty.

receive_delivery(headers, body, opts \\ [])

@spec receive_delivery(term(), binary(), keyword()) ::
  {:ok, %{headers: Ankusa.SDK.Webhook.Headers.t(), verified: boolean()}}
  | {:error,
     Ankusa.SDK.InvalidSignatureError.t() | Ankusa.SDK.MissingHookIdError.t()}

Decide whether to accept one HTTP-sink delivery, and parse its headers.

Ankusa.Sink.Http signs every delivery unless it is configured allow_unsigned: true, so a receiver is closed unless it is told otherwise. Options:

  • :secrets — the sink's secret (whsec_ + base64, or any other string used as its own bytes), or a list of them during a rotation. With any, the delivery MUST verify (Ankusa.SDK.Signature.verify/4, same errors, :tolerance_seconds and :now); :allow_unsigned does not weaken it.
  • :allow_unsigned — true accepts a delivery with no secret configured, unverified. Default false: with no :secrets every delivery is refused with an invalid_secret Ankusa.SDK.InvalidSignatureError.
  • :tolerance_seconds, :now — see Ankusa.SDK.Signature.verify/4.

Returns {:ok, %{headers: headers, verified: verified?}} (verified is false only for an allowed unsigned delivery), {:error, %Ankusa.SDK.InvalidSignatureError{}} (answer 401), or {:error, %Ankusa.SDK.MissingHookIdError{}} (400). The signature is judged first, so a delivery that fails it is a 401 even when it also lacks x-ankusa-id. body must be the raw bytes received.