Karta.Tracking.Token (Karta v1.0.0)

Signed tokens for the links Karta puts into outgoing mail: the open pixel (/o/<token>.gif), tracked links (/c/<token>) and one-click unsubscribe (/u/<token>).

A token is the unpadded base64url form of payload <> tag:

  • payload - the recipient's id (16 bytes), and for a link its position in the message (2 bytes, big-endian);
  • tag - HMAC-SHA256 over a label naming the token's kind and the payload, truncated to 16 bytes (128 bits). Forging a tag means guessing 128 bits online, one request per guess; the full 32 bytes would only make every link in every message longer.

Tokens are 43 characters (open, unsubscribe) or 46 (click). The label keeps one kind from being used as another: the open pixel, which every image proxy sees, is no unsubscribe link.

The HMAC key is derived from a vault key (Karta.Vault) with HKDF-SHA256 (Karta.Crypto.hkdf_sha256/4), so there is no further secret to manage. New tokens use the active vault key; a token verifies under the active key or any retired one, compared in constant time. After the vault key is rotated, links in mail already sent keep working for as long as the old key is kept among the retired keys.

Nothing is looked up before a token verifies, and a token that does not verify gives :error whatever is wrong with it.

Summary

Functions

The token of kind for a recipient; a click token also names the link's position (0..65535).

The recipient id (and for :click the position) a token of kind was made for, or :error.

Types

kind()

@type kind() :: :open | :click | :unsubscribe

Functions

sign(kind, recipient_id)

@spec sign(:open | :unsubscribe, Ecto.UUID.t()) :: String.t()

The token of kind for a recipient; a click token also names the link's position (0..65535).

sign(atom, recipient_id, position)

@spec sign(:click, Ecto.UUID.t(), non_neg_integer()) :: String.t()

verify(kind, token)

@spec verify(kind(), term()) ::
  {:ok, Ecto.UUID.t()} | {:ok, {Ecto.UUID.t(), integer()}} | :error

The recipient id (and for :click the position) a token of kind was made for, or :error.