Karta.Webhooks.Mailgun (Karta v1.0.0)

Webhook requests in Mailgun's format, for receivers written for Mailgun's webhooks. The body is a JSON object:

{
  "signature": {
    "timestamp": "1796378400",
    "token": "<50 random hexadecimal digits>",
    "signature": "<hex HMAC-SHA256 of timestamp and token>"
  },
  "event-data": {"id": ..., "event": "delivered", ...}
}

Signature. signature is the hexadecimal HMAC-SHA256 of the timestamp (seconds, as written) followed by the token, keyed with the webhook's signing key as it is written (64 hexadecimal digits, shown once, like Mailgun's HTTP webhook signing key). Receivers compare it, refuse old timestamps, and may refuse a token they saw before (verify/3 is that check, as Rails' Action Mailbox makes it for Mailgun). The body itself is not signed, as with Mailgun. Each request has a timestamp and token of its own; a rotated key signs at once (there is room for one signature only).

Events (event-data.event), from Karta's types:

  • message.accepted - accepted;
  • message.delivered - delivered, with delivery-status;
  • message.deferred - failed, severity temporary, reason generic, with delivery-status (and its retry-seconds);
  • message.bounced - failed, severity permanent, reason bounce (or old when the retry period ran out);
  • message.suppressed - failed, severity permanent, reason suppress-bounce, suppress-unsubscribe or suppress-complaint (codes 605, 606, 607);
  • message.opened, message.clicked - opened, clicked, with ip (the client's network, as Karta keeps it), client-info.user-agent and for clicks url;
  • message.unsubscribed - unsubscribed;
  • message.complained - complained, severity permanent, with the reason Mailgun gives a complaint about a suppressed address (suppress-complaint). Mailgun's own complained events carry no delivery status; the two fields say the same as the failed events Karta sends for a suppressed recipient.

Every event has id (from Karta's event id: the same on every attempt and replay), timestamp (seconds, with microseconds), log-level, recipient, recipient-domain, tags, user-variables, flags, envelope and message (headers to, message-id, from, subject; size). Karta does not keep the message's To header for each event: message.headers.to is the recipient's address. Not sent: geolocation, the client's type and system, storage and attachments (always []). webhook.test has id, event webhook.test, timestamp, log-level, webhook-id and description.

Summary

Functions

The body of a request about event (a stored Karta.Webhooks.Event) made at now, signed with key; token is new for every request.

A new signing key: 64 hexadecimal digits.

A token for one request: 50 random hexadecimal digits.

The body without its signature: what every request about event has in common.

The hexadecimal HMAC-SHA256 of timestamp and token, keyed with key.

Karta's event types that have a Mailgun event.

A receiver's check of the signature object of a request, as Rails' Action Mailbox checks Mailgun's: the signature equals the HMAC of the timestamp and token under key (compared in constant time), and the timestamp is at most two minutes old at now (option, seconds; default the clock's). A token seen before is the receiver's to refuse.

Functions

body(event, key, now, token \\ new_token())

@spec body(map(), String.t(), DateTime.t(), String.t()) :: String.t()

The body of a request about event (a stored Karta.Webhooks.Event) made at now, signed with key; token is new for every request.

new_key()

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

A new signing key: 64 hexadecimal digits.

new_token()

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

A token for one request: 50 random hexadecimal digits.

preview(event)

@spec preview(map()) :: String.t()

The body without its signature: what every request about event has in common.

sign(key, timestamp, token)

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

The hexadecimal HMAC-SHA256 of timestamp and token, keyed with key.

types()

@spec types() :: [String.t()]

Karta's event types that have a Mailgun event.

verify(key, signature, opts \\ [])

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

A receiver's check of the signature object of a request, as Rails' Action Mailbox checks Mailgun's: the signature equals the HMAC of the timestamp and token under key (compared in constant time), and the timestamp is at most two minutes old at now (option, seconds; default the clock's). A token seen before is the receiver's to refuse.