From 1f44cd930cdff94bb8cf04f645a5b035507438d9 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Fri, 24 Oct 2025 19:10:29 -0400
Subject: Add an endpoint for creating push subscriptions.

The semantics of this endpoint are somewhat complex, and are incompletely captured in the associated docs change. For posterity, the intended workflow is:

1. Obtain Pilcrow's current VAPID key by connecting (it's in the events, either from boot or from the event stream).
2. Use the browser push APIs to create a push subscription, using that VAPID key.
3. Send Pilcrow the push subscription endpoint and keys, plus the VAPID key the client used to create it so that the server can detect race conditions with key rotation.
4. Wait for messages to arrive.

This commit does not introduce any actual messages, just subscription management endpoints.

When the server's VAPID key is rotated, all existing subscriptions are discarded. Without the VAPID key, the server cannot service those subscriptions. We can't exactly notify the broker to stop processing messages on those subscriptions, so this is an incomplete solution to what to do if the key is being rotated due to a compromise, but it's better than nothing.

The shape of the API endpoint is heavily informed by the [JSON payload][web-push-json] provided by browser Web Push implementations, to ease client development in a browser-based context. The idea is that a client can take that JSON and send it to the server verbatim, without needing to transform it in any way, to submit the subscription to the server for use.

[web-push-json]: https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription/toJSON

Push subscriptions are operationally associated with a specific _user agent_, and have no inherent relationship with a Pilcrow login or token (session). Taken as-is, a subscription created by user A could be reused by user B if they share a user agent, even if user A logs out before user B logs in. Pilcrow therefore _logically_ associates push subscriptions with specific tokens, and abandons those subscriptions when the token is invalidated by

* logging out,
* expiry, or
* changing passwords.

(There are no other token invalidation workflows at this time.)

Stored subscriptions are also abandoned when the server's VAPID key changes.
---
 migrations/20251009021241_push_subscriptions.sql | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 migrations/20251009021241_push_subscriptions.sql

(limited to 'migrations/20251009021241_push_subscriptions.sql')

diff --git a/migrations/20251009021241_push_subscriptions.sql b/migrations/20251009021241_push_subscriptions.sql
new file mode 100644
index 0000000..b42d122
--- /dev/null
+++ b/migrations/20251009021241_push_subscriptions.sql
@@ -0,0 +1,12 @@
+create table push_subscription (
+    endpoint text
+        primary key
+        not null,
+    token text
+        not null
+        references token (id),
+    p256dh text
+        not null,
+    auth text
+        not null
+);
-- 
cgit v1.2.3

