Karta. Webhooks. Sendgrid
(Karta v1.0.0)
Webhook requests in the format of SendGrid's Event Webhook, for receivers written for it. The body is a JSON array of events followed by a carriage return and line feed, as SendGrid's bodies are:
[{"email":"bob@example.net","timestamp":1796378400,"smtp-id":"<...>",
"event":"delivered","category":["welcome"],"sg_event_id":"...",
"sg_message_id":"...","response":"250 2.0.0 Ok"}]Karta sends one event per request: SendGrid collects events for up
to 30 seconds, so its receivers take arrays of any length, one
included; each event keeps its own delivery, retries and replays
(Karta.Webhooks.Sender).
Signature (SendGrid's Signed Event Webhook). Each webhook has a key
pair of its own: ECDSA on the P-256 curve. The private key is stored
encrypted as the webhook's secret and never shown; the public key
(public_key/1: the base64 of its DER SubjectPublicKeyInfo, as SendGrid
shows its verification key) is shown with the webhook, and receivers
give it to SendGrid's libraries as it is written. A request carries
X-Twilio-Email-Event-Webhook-Timestamp (seconds) and
X-Twilio-Email-Event-Webhook-Signature: the base64 of the DER ECDSA
signature, with SHA-256, of the timestamp as written followed by the
body (verify/3 is the receivers' check, as SendGrid's libraries make
it). A rotation makes a new key pair, which signs at once.
Events (event), from Karta's types:
message.accepted-processed;message.suppressed-dropped, withreasonBounced Address,Unsubscribed AddressorSpam Reporting Addressandstatus5.0.0;message.delivered-delivered, withresponse(the server's reply);message.deferred-deferred, withresponse,attempt,domainandfrom;message.bounced-bounce, withreason(the server's reply),status(its enhanced code) andtype:bouncewhen the server refused the message,blockedwhen the retry period ran out;message.opened,message.clicked-open,click, withuseragent,ip(the client's network, as Karta keeps it) and for clicksurl;sg_machine_openisfalse;message.unsubscribed-unsubscribe;message.complained-spamreport: the recipient marked the message as spam; the address is now on the server's suppression list.
Every event has email (the recipient), timestamp (seconds),
smtp-id (the Message-ID, when the message has one), event,
category (the message's tags, when it has some), sg_event_id (from
Karta's event id: the same on every attempt and replay) and
sg_message_id: the Message-ID without its angle brackets (what Karta's
SendGrid-compatible API answered as X-Message-Id), a dot, and the
recipient's id; the part before the last dot is the X-Message-Id.
The message's variables (SendGrid's custom arguments) are fields of
their own, unless an event field has their name. Not sent: pool,
asm_group_id, url_offset, bounce_classification, tls,
cert_err and marketing campaign fields. webhook.test has event
webhook.test, timestamp, sg_event_id, webhook_id and
description.
Summary
Functions
The body of a request about event (a stored Karta.Webhooks.Event).
The event types after SendGrid's settings given (a map of names to
booleans; others are ignored) change those of a webhook that receives
events (nil: every type). :unchanged when given names none of
the settings that have a type.
The signature headers of a request with body, made at now.
A new private key (ECDSA, P-256), in PEM.
The public key of a private key (PEM): the base64 of its DER SubjectPublicKeyInfo, as SendGrid shows the verification key.
SendGrid's event settings (delivered, bounce, ...: on or off) of a
webhook that receives events (nil: every type), in SendGrid's order.
group_unsubscribe and group_resubscribe have no Karta type and are
always off.
The signature of body sent with timestamp, by the private key
(PEM): the base64 of the DER ECDSA signature of SHA-256 over the
timestamp and the body. ECDSA signatures differ every time.
Karta's event types that have a SendGrid event.
A receiver's check of a request, as SendGrid's libraries make it
(EventWebhook#verify_signature of sendgrid-ruby): the signature header
is a valid ECDSA signature, by the public key (base64, as shown), of the
timestamp header followed by the body, as received. Like those
libraries it does not look at the timestamp's age: a receiver that wants
to refuse old requests compares it with its clock.
Functions
The body of a request about event (a stored Karta.Webhooks.Event).
The event types after SendGrid's settings given (a map of names to
booleans; others are ignored) change those of a webhook that receives
events (nil: every type). :unchanged when given names none of
the settings that have a type.
@spec headers(String.t(), String.t(), DateTime.t()) :: [{String.t(), String.t()}]
The signature headers of a request with body, made at now.
@spec new_key() :: String.t()
A new private key (ECDSA, P-256), in PEM.
The public key of a private key (PEM): the base64 of its DER SubjectPublicKeyInfo, as SendGrid shows the verification key.
SendGrid's event settings (delivered, bounce, ...: on or off) of a
webhook that receives events (nil: every type), in SendGrid's order.
group_unsubscribe and group_resubscribe have no Karta type and are
always off.
The signature of body sent with timestamp, by the private key
(PEM): the base64 of the DER ECDSA signature of SHA-256 over the
timestamp and the body. ECDSA signatures differ every time.
@spec types() :: [String.t()]
Karta's event types that have a SendGrid event.
A receiver's check of a request, as SendGrid's libraries make it
(EventWebhook#verify_signature of sendgrid-ruby): the signature header
is a valid ECDSA signature, by the public key (base64, as shown), of the
timestamp header followed by the body, as received. Like those
libraries it does not look at the timestamp's age: a receiver that wants
to refuse old requests compares it with its clock.