Coffrify.Plug.VerifyWebhook (Coffrify v0.9.0)

View Source

Plug that verifies a Coffrify webhook signature and assigns the parsed event to conn.assigns[:coffrify_event].

Usage in a plain Plug pipeline

plug Coffrify.Plug.VerifyWebhook,
  secret: {System, :fetch_env!, ["COFFRIFY_WEBHOOK_SECRET"]}

Usage in a Phoenix Endpoint (BEFORE the JSON parser)

plug Plug.Parsers,
  parsers: [{:json, json_decoder: Jason}],
  body_reader: {Coffrify.Plug.VerifyWebhook, :cache_raw_body, []},
  only: ~w(application/json)

plug Coffrify.Plug.VerifyWebhook,
  secret: System.fetch_env!("COFFRIFY_WEBHOOK_SECRET")

The plug:

  • Reads the raw request body (caching it for downstream parsers).
  • Verifies the webhook-id/webhook-timestamp/webhook-signature headers via Coffrify.Webhook.Verification.verify/4.
  • Halts the connection with 400 Bad Request on failure (or a custom status configured via :on_reject).
  • Optionally deduplicates events via a Coffrify.Runtime.WebhookReplay store when one is configured.

Options

  • :secretString.t() | [String.t()] | {mod, fun, args} (required). List form supports key rotation.

  • :tolerance_seconds — clock skew tolerance (default 300).
  • :replay_store — optional dedup store.
  • :replay_ttl_ms — TTL when adding to the replay store (default 24h).
  • :on_reject:halt_400 (default) or &fun/2 that receives (conn, reason) and returns the conn.

Summary

Functions

Body reader compatible with Plug.Parsers — stashes the raw body in :raw_body private storage so subsequent calls (including this plug) can verify the original bytes.

Functions

cache_raw_body(conn, opts)

Body reader compatible with Plug.Parsers — stashes the raw body in :raw_body private storage so subsequent calls (including this plug) can verify the original bytes.