feat(federation): shared libraries and the signed read API for friends

A friend's server can now read what an admin chose to share.

- A library has a shared flag, off by default (migration 0024), set with
  PATCH /libraries/{id}. Changing it never waits for a scan.
- Requests between friends are signed with each server's Ed25519 key: the
  method, the path and query from /api/v1/peer on, a date, a nonce and the
  hash of the empty body. A date more than two minutes off, a nonce used
  before, an unknown or removed key and a bad signature all get the same
  401. A friend still waiting for approval gets 403, and while federation is
  off the routes are missing.
- Routes under /api/v1/peer: info, artists, albums, tracks, covers and
  streaming with Range. The catalogue, the counts and the format come from
  the shared libraries only, and the file served is the one in a shared
  library. No path, library id, listen, taste, user or provider data is part
  of an answer.
- A friend never makes this server build a cover, read an audio file or ask
  a provider: only covers already built from the audio files are served.
- Per-friend limits, set by the admin: parallel streams (default 3) and KiB
  a second (default 4096, 0 for none), shared by all the streams of a friend.
- A log of the last requests friends made (route, status, bytes; never the
  query), kept for 7 days, in GET /federation/requests.
- POST /federation/friends/{id}/check lets the side that pasted an invite
  find out that the other admin approved; a signed call from a friend this
  server is waiting for makes it active too.
- Admin: a Shared with friends switch on each library, which asks first, and
  limits, recent requests and a Check button in the Federation section, in
  English and Italian.
- OpenAPI paths and schemas, contract test, two real servers checked end to
  end with tagged audio files.
This commit is contained in:
tgiachi 2026-11-13 15:04:07 +01:00
commit 89826fcd07

View file

@ -0,0 +1,18 @@
-- What a friend may read, and how much: a library is shared with friends only when an admin says so; the limits
-- are per friend; the log keeps the requests friends made (7 days, see internal/federation).
ALTER TABLE libraries ADD COLUMN shared INTEGER NOT NULL DEFAULT 0;
ALTER TABLE federation_settings ADD COLUMN max_streams INTEGER NOT NULL DEFAULT 3; -- parallel streams for one friend
ALTER TABLE federation_settings ADD COLUMN max_kib_per_sec INTEGER NOT NULL DEFAULT 4096; -- 0 = no limit
CREATE TABLE federation_requests (
id INTEGER PRIMARY KEY,
friend_id INTEGER NOT NULL REFERENCES federation_friends(id) ON DELETE CASCADE,
at INTEGER NOT NULL, -- unix seconds
route TEXT NOT NULL, -- the route pattern, never the query
status INTEGER NOT NULL,
bytes INTEGER NOT NULL
);
CREATE INDEX idx_federation_requests_at ON federation_requests(at);