Kantoor.Push.Vapid (Kantoor v1.0.0)

The installation's VAPID key pair and the sub claim web push needs.

Where the keys come from

  1. The environment, when both are set: VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY (base64url, no padding; VAPID_SUBJECT optional). Nothing is written to the database then.
  2. Otherwise one pair is generated on first use and kept in push_keys (row 1). The private key is encrypted with Kantoor.Vault when KANTOOR_VAULT_KEY is configured (scheme: "vault"), and stored as is when it is not (scheme: "plain", which the settings page warns about).

Why a stored pair is acceptable: the key is the installation's own, made here; no third party is involved and no account is created. Rotating it invalidates every existing browser subscription, so rotate/0 is a deliberate, logged operation, not something that happens by itself.

Caching: the pair is the same for the life of a release, so it is read once and kept in :persistent_term (disable with config :kantoor, push_key_cache: false, which tests do).

Summary

Functions

Base64url without padding, the encoding web push uses.

Makes a new pair unless one already exists, and returns it. Safe to call from several replicas at once: the insert is guarded by the primary key.

Forgets a cached pair (after rotate/0, and between tests).

The pair and subject, as %{public:, private:, subject:}.

The base64url public key a browser subscribes with.

Replaces the stored pair with a new one. Every existing subscription stops working (the browsers must subscribe again with the new public key), so this is for an operator who has decided to. Returns the new pair.

Whether the stored private key is encrypted, plain, or not stored at all.

The VAPID sub claim (a mailto: address the push service may contact).

Functions

b64(value)

@spec b64(binary()) :: String.t()

Base64url without padding, the encoding web push uses.

ensure_pair()

@spec ensure_pair() :: %{public: String.t(), private: String.t(), subject: String.t()}

Makes a new pair unless one already exists, and returns it. Safe to call from several replicas at once: the insert is guarded by the primary key.

forget()

@spec forget() :: :ok

Forgets a cached pair (after rotate/0, and between tests).

keys()

@spec keys() :: %{public: String.t(), private: String.t(), subject: String.t()}

The pair and subject, as %{public:, private:, subject:}.

public_key()

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

The base64url public key a browser subscribes with.

rotate()

@spec rotate() :: %{public: String.t(), private: String.t(), subject: String.t()}

Replaces the stored pair with a new one. Every existing subscription stops working (the browsers must subscribe again with the new public key), so this is for an operator who has decided to. Returns the new pair.

storage()

@spec storage() :: :vault | :plain | :none | :environment

Whether the stored private key is encrypted, plain, or not stored at all.

subject()

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

The VAPID sub claim (a mailto: address the push service may contact).