sonda/migrations/sqlite/0023_federation.sql
tgiachi 695a012f6c feat(federation): keys, invites and pairing between Sonda servers
Two servers can become friends. Federation is off by default; turning it
on makes an Ed25519 key pair, kept in the database and never returned.

- Admin routes: GET and PUT /federation (on or off, name, public address),
  POST /federation/invites, POST /federation/friends (use an invite),
  POST /federation/friends/{id}/approve and DELETE /federation/friends/{id}.
- POST /api/v1/peer/pair, the one route without a login: the code of an
  invite is the proof. An invite works once and expires in 24 hours; only
  a hash of its code is stored. At most 10 attempts a minute.
- The server that uses an invite checks that the key it gets back is the
  one named in the invite. A friend is kept as incoming or outgoing until
  both admins have agreed.
- https only, except plain http on a network of one's own (private,
  loopback or link-local address, or a local name).
- A client of its own for friends, with no redirects, no proxy from the
  environment, a size limit and a time limit; the providers' client stays
  as it is.
- Migration 0023 for SQLite and PostgreSQL, OpenAPI paths and schemas,
  contract test with two real servers.
- Admin > Federation in the web app, in English and Italian, and a
  section in the README.
2026-11-13 14:43:48 +01:00

33 lines
1.4 KiB
SQL

-- Federation between Sonda servers (see internal/federation). Off until an admin turns it on; the key pair is made then.
-- The private key stays in this table and is never returned by the API.
CREATE TABLE federation_settings (
id INTEGER PRIMARY KEY CHECK (id = 1), -- one row
enabled INTEGER NOT NULL DEFAULT 0,
name TEXT NOT NULL DEFAULT '', -- shown to friends; '' = the host name
address TEXT NOT NULL DEFAULT '', -- where friends reach this server
public_key TEXT NOT NULL DEFAULT '', -- Ed25519, base64url; '' until first enabled
private_key TEXT NOT NULL DEFAULT '',
updated_at INTEGER NOT NULL DEFAULT 0 -- unix seconds
);
-- Single-use invites. Only the SHA-256 of the code is kept.
CREATE TABLE federation_invites (
id INTEGER PRIMARY KEY,
code_hash TEXT NOT NULL UNIQUE,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
used_at INTEGER
);
-- The other servers. state: incoming (they used my invite, I have not approved), outgoing (I used theirs, they have
-- not approved), active.
CREATE TABLE federation_friends (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
address TEXT NOT NULL,
public_key TEXT NOT NULL UNIQUE,
state TEXT NOT NULL CHECK (state IN ('incoming', 'outgoing', 'active')),
created_at INTEGER NOT NULL,
approved_at INTEGER
);