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.
This commit is contained in:
tgiachi 2026-11-13 14:43:48 +01:00
commit 695a012f6c
29 changed files with 3987 additions and 20 deletions

View file

@ -28,10 +28,11 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr
- **Your own settings.** `GET` and `PATCH /me/settings` hold choices that belong to you, not to the server. Everyone starts private: `discover_from_listening` is off, so this setting never sends your listening to a provider. When you turn it on, Discover may start from the artists you listen to most, and their names are sent to Last.fm (only if an admin switched it on). What Last.fm answers is kept in the shared database like any other lookup, so another local user can see, from `retrieved_at` of `GET /artists/{id}/similar`, that someone looked up an artist, but not who.
- **Tags.** `GET /artists/{id}/tags` shows what Last.fm and MusicBrainz say kind of music an artist plays, each with its own weights, and the genres of the artist's own files, read offline. See [External providers](#external-providers).
- **Similar artists.** With Last.fm switched on, `GET /artists/{id}/similar` lists artists similar to an artist, and says which of them you already have. See [External providers](#external-providers). Without it, Sonda makes no outbound network requests at all.
- **Friends (federation, first part).** Two Sonda servers can become friends. `PUT /federation` (admin) turns federation on, which is off by default, and gives this server a name and a public address (`https`; plain `http` only on a network of your own); the server then makes its own Ed25519 key, kept in the database and never returned. `POST /federation/invites` makes an invite that works once and expires in 24 hours (only a hash of its code is kept). The other admin uses it with `POST /federation/friends`: that server is asked to accept this one, the key it answers with must be the one named in the invite, and the friend waits as `outgoing` until the first admin approves it with `POST /federation/friends/{id}/approve`. Either side can remove a friend with `DELETE /federation/friends/{id}`. Nothing is sent until an admin makes or uses an invite, and then only this server's name, address and public key. Sharing libraries and playing a friend's music are not there yet: for now a friend is only recorded. The admin area has a Federation section for all of it.
## What is not there yet
Radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client is built into the binary (see [Web app](#web-app)), and its admin area covers everything the API can do for an admin: libraries and scanning, users, the external providers and the bulk jobs.
Playing a friend's libraries (federation: shared libraries, the read API and streaming), radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client is built into the binary (see [Web app](#web-app)), and its admin area covers everything the API can do for an admin: libraries and scanning, users, the external providers and the bulk jobs.
## Requirements

View file

@ -22,6 +22,7 @@ import (
"codeberg.org/squidd/sonda/internal/discover"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/duplicates"
"codeberg.org/squidd/sonda/internal/federation"
"codeberg.org/squidd/sonda/internal/feedback"
"codeberg.org/squidd/sonda/internal/images"
"codeberg.org/squidd/sonda/internal/jobs"
@ -315,6 +316,7 @@ func run() error {
About: descriptions,
Settings: userSettings,
Streams: streaming.NewService(libraryRepo),
Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()),
Web: webui.FS(),
})

View file

@ -21,6 +21,7 @@ import (
"codeberg.org/squidd/sonda/internal/discover"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/duplicates"
"codeberg.org/squidd/sonda/internal/federation"
"codeberg.org/squidd/sonda/internal/feedback"
"codeberg.org/squidd/sonda/internal/images"
"codeberg.org/squidd/sonda/internal/jobs"
@ -106,6 +107,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun
Taste: taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}),
Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)),
Streams: streaming.NewService(noFiles{}),
Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()),
}
feedbackSvc := feedback.NewService(storage.NewFeedbackRepo(db))
deps.Feedback = feedbackSvc

View file

@ -3,6 +3,7 @@ package httpapi
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"image"
"image/color"
@ -14,6 +15,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
@ -826,6 +828,7 @@ func TestResponsesMatchTheSpec(t *testing.T) {
c.mixes(t, token, libID)
c.smartPlaylists(t, token)
c.names(t, token)
c.federation(t, token)
// ---- every operation in the spec was exercised ----
var missing []string
@ -1122,3 +1125,79 @@ func (c *contract) names(t *testing.T, token string) {
c.expect(c.call("POST", "/smart-playlists/"+sid+"/suggest-name", token, map[string]any{"lang": "en"}), 200, "a name for a smart playlist")
c.expect(c.call("POST", "/smart-playlists/999999/suggest-name", token, nil), 404, "a name for an unknown smart playlist", "smart_playlist_not_found")
}
func (c *contract) federation(t *testing.T, token string) {
t.Helper()
for _, r := range []struct{ method, path string }{
{"GET", "/federation"}, {"PUT", "/federation"}, {"POST", "/federation/invites"}, {"POST", "/federation/friends"},
{"POST", "/federation/friends/1/approve"}, {"DELETE", "/federation/friends/1"},
} {
c.expect(c.call(r.method, r.path, "", map[string]any{}), 401, r.method+" "+r.path+" without a token", "unauthorized")
}
st := c.expect(c.call("GET", "/federation", token, nil), 200, "federation")
if st["enabled"] != false || st["key_id"] != "" {
t.Fatalf("federation = %v, want off and no key", st)
}
c.expect(c.call("POST", "/federation/invites", token, nil), 409, "an invite while federation is off", "federation_off")
c.expect(c.call("PUT", "/federation", token, map[string]any{"address": "http://music.example.org"}), 400, "a public http address", "invalid_input")
on := c.expect(c.call("PUT", "/federation", token, map[string]any{"enabled": true, "name": "Contract"}), 200, "turn federation on")
if on["enabled"] != true || len(on["key_id"].(string)) != 16 {
t.Fatalf("federation = %v", on)
}
c.expect(c.call("POST", "/federation/invites", token, nil), 409, "an invite without an address", "address_required")
c.expect(c.call("PUT", "/federation", token, map[string]any{"address": "https://contract.example.org"}), 200, "give the address")
inv := c.expect(c.call("POST", "/federation/invites", token, nil), 201, "an invite")
// Another server pairs with this one, then this one approves it and ends the friendship.
c.expect(c.call("POST", "/peer/pair", "", pairBody("wrong", "Other", "https://other.example.org")), 403, "pairing with a wrong code", "invalid_invite")
c.expect(c.call("POST", "/peer/pair", "", map[string]any{"code": "c", "name": "", "address": "https://o.example.org", "public_key": "AAAA"}), 400, "pairing with a bad key", "invalid_input")
code := inviteCode(t, inv["invite"].(string))
body := pairBody(code, "Other", "https://other.example.org")
reply := c.expect(c.call("POST", "/peer/pair", "", body), 200, "pairing with the invite")
if reply["name"] != "Contract" || reply["address"] != "https://contract.example.org" {
t.Fatalf("pair reply = %v", reply)
}
c.expect(c.call("POST", "/peer/pair", "", pairBody(code, "Other", "https://other.example.org")), 403, "using the invite twice", "invalid_invite")
c.expect(c.call("POST", "/peer/pair", "", body), 409, "pairing a known key", "already_friends")
list := c.expect(c.call("GET", "/federation", token, nil), 200, "federation with a request")["friends"].([]any)
if len(list) != 1 || list[0].(map[string]any)["state"] != "incoming" {
t.Fatalf("friends = %v", list)
}
id := idOf(list[0].(map[string]any))
c.expect(c.call("POST", "/federation/friends/"+id+"/approve", token, nil), 200, "approve")
c.expect(c.call("POST", "/federation/friends/"+id+"/approve", token, nil), 409, "approve twice", "not_waiting")
c.expect(c.call("POST", "/federation/friends/999999/approve", token, nil), 404, "approve a missing friend", "friend_not_found")
c.expect(c.call("DELETE", "/federation/friends/"+id, token, nil), 204, "end the friendship")
c.expect(c.call("DELETE", "/federation/friends/"+id, token, nil), 404, "end it twice", "friend_not_found")
// This server uses an invite of another one, which is a real server of its own.
other := newFriendServer(t, true)
other.setUp(t, "Anna", other.ts.URL)
theirs := other.call(t, "POST", "/federation/invites", nil)
c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": "hello"}), 400, "an invite that is not one", "invalid_invite")
friend := c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": theirs.Body["invite"]}), 201, "use an invite")
if friend["state"] != "outgoing" || friend["name"] != "Anna" {
t.Fatalf("friend = %v", friend)
}
c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": theirs.Body["invite"]}), 409, "use the invite of a friend", "already_friends")
c.expect(c.call("DELETE", "/federation/friends/"+idOf(friend), token, nil), 204, "withdraw the request")
again := other.call(t, "POST", "/federation/invites", nil)
other.ts.Close()
c.expect(c.call("POST", "/federation/friends", token, map[string]any{"invite": again.Body["invite"]}), 502, "use an invite of a server that is gone", "friend_unreachable")
}
// inviteCode reads the code out of an invite text, as a test that plays the other server needs to.
func inviteCode(t *testing.T, text string) string {
t.Helper()
raw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(text, "sonda1."))
if err != nil {
t.Fatal(err)
}
var p struct {
C string `json:"c"`
}
if err := json.Unmarshal(raw, &p); err != nil || p.C == "" {
t.Fatalf("invite %q has no code: %v", text, err)
}
return p.C
}

View file

@ -0,0 +1,301 @@
package httpapi
import (
"context"
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"strings"
"testing"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/federation"
"codeberg.org/squidd/sonda/internal/storage"
)
// friendServer is a whole Sonda that other tests can reach over HTTP, with its admin signed in.
type friendServer struct {
h http.Handler
db *bun.DB
ts *httptest.Server
admin string
}
func newFriendServer(t *testing.T, listen bool) *friendServer {
t.Helper()
h, db := newTestEnvDB(t, func(d *Deps, db *bun.DB) {
d.Federation = federation.NewService(storage.NewFederationRepo(db), federation.NewClient())
})
s := &friendServer{h: h, db: db, admin: registerAndLogin(t, h)}
if listen {
s.ts = httptest.NewServer(h)
t.Cleanup(s.ts.Close)
}
return s
}
func (s *friendServer) call(t *testing.T, method, path string, body any) resp {
t.Helper()
return do(t, s.h, method, "/api/v1"+path, s.admin, body)
}
func (s *friendServer) setUp(t *testing.T, name, address string) {
t.Helper()
r := s.call(t, "PUT", "/federation", map[string]any{"enabled": true, "name": name, "address": address})
if r.Code != http.StatusOK {
t.Fatalf("set up federation: %d %v", r.Code, r.Body)
}
}
func TestFederationRoutesAreForAdminsOnly(t *testing.T) {
h := newTestAPI(t)
admin := registerAndLogin(t, h)
_, bob := addUser(t, h, admin, "bob", false)
for _, c := range []struct{ method, path string }{
{"GET", "/api/v1/federation"},
{"PUT", "/api/v1/federation"},
{"POST", "/api/v1/federation/invites"},
{"POST", "/api/v1/federation/friends"},
{"POST", "/api/v1/federation/friends/1/approve"},
{"DELETE", "/api/v1/federation/friends/1"},
} {
if r := do(t, h, c.method, c.path, "", nil); r.Code != http.StatusUnauthorized {
t.Errorf("%s %s without a token: %d", c.method, c.path, r.Code)
}
r := do(t, h, c.method, c.path, bob, map[string]any{})
if r.Code != http.StatusForbidden || errCode(r) != "admin_required" {
t.Errorf("%s %s as a normal user: %d %v", c.method, c.path, r.Code, r.Body)
}
}
}
func TestFederationStartsOffAndSaysWhatItSends(t *testing.T) {
s := newFriendServer(t, false)
r := s.call(t, "GET", "/federation", nil)
if r.Code != http.StatusOK {
t.Fatalf("get: %d %v", r.Code, r.Body)
}
sameKeys(t, "federation", r.Body, "enabled", "name", "address", "key_id", "sends", "friends")
if r.Body["enabled"] != false || r.Body["key_id"] != "" || r.Body["address"] != "" {
t.Errorf("federation must start off with no key and no address: %v", r.Body)
}
if friends, ok := r.Body["friends"].([]any); !ok || len(friends) != 0 {
t.Errorf("friends = %#v, want an empty list", r.Body["friends"])
}
if !strings.Contains(r.Body["sends"].(string), "never learns who listens") {
t.Errorf("sends = %q", r.Body["sends"])
}
}
func TestSettingUpFederation(t *testing.T) {
s := newFriendServer(t, false)
// Nothing can be made while it is off, and an invite needs the address of this server.
if r := s.call(t, "POST", "/federation/invites", nil); r.Code != http.StatusConflict || errCode(r) != "federation_off" {
t.Errorf("invite while off: %d %v", r.Code, r.Body)
}
for name, body := range map[string]any{
"ftp address": map[string]any{"address": "ftp://x.example.org"},
"public http": map[string]any{"address": "http://music.example.org"},
"long name": map[string]any{"name": strings.Repeat("n", 61)},
"bad json": "{nope",
"wrong type": map[string]any{"enabled": "yes"},
} {
if r := s.call(t, "PUT", "/federation", body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("%s: %d %v", name, r.Code, r.Body)
}
}
r := s.call(t, "PUT", "/federation", map[string]any{"enabled": true})
if r.Code != http.StatusOK || r.Body["enabled"] != true || !regexp.MustCompile(`^[0-9a-f]{16}$`).MatchString(str(r.Body, "key_id")) {
t.Fatalf("turn on: %d %v", r.Code, r.Body)
}
if r := s.call(t, "POST", "/federation/invites", nil); r.Code != http.StatusConflict || errCode(r) != "address_required" {
t.Errorf("invite without an address: %d %v", r.Code, r.Body)
}
r = s.call(t, "PUT", "/federation", map[string]any{"address": "https://Music.Example.org/", "name": " Home "})
if r.Body["address"] != "https://music.example.org" || r.Body["name"] != "Home" || r.Body["enabled"] != true {
t.Errorf("update: %v (fields left out must be kept)", r.Body)
}
r = s.call(t, "POST", "/federation/invites", nil)
if r.Code != http.StatusCreated {
t.Fatalf("invite: %d %v", r.Code, r.Body)
}
sameKeys(t, "invite", r.Body, "invite", "expires_at")
if !strings.HasPrefix(str(r.Body, "invite"), "sonda1.") {
t.Errorf("invite = %v", r.Body["invite"])
}
if _, err := time.Parse(time.RFC3339, str(r.Body, "expires_at")); err != nil {
t.Errorf("expires_at = %v", r.Body["expires_at"])
}
}
func TestTwoServersBecomeFriendsOverHTTP(t *testing.T) {
a := newFriendServer(t, true)
b := newFriendServer(t, false)
a.setUp(t, "Anna", a.ts.URL)
b.setUp(t, "Bruno", "https://bruno.example.org")
inv := a.call(t, "POST", "/federation/invites", nil)
if inv.Code != http.StatusCreated {
t.Fatalf("invite: %d %v", inv.Code, inv.Body)
}
// Bruno's admin pastes the invite.
r := b.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]})
if r.Code != http.StatusCreated {
t.Fatalf("redeem: %d %v", r.Code, r.Body)
}
sameKeys(t, "friend", r.Body, "id", "name", "address", "key_id", "state", "created_at", "approved_at")
if r.Body["name"] != "Anna" || r.Body["address"] != a.ts.URL || r.Body["state"] != "outgoing" || r.Body["approved_at"] != nil {
t.Errorf("Bruno's friend = %v", r.Body)
}
fromB := r.Body
// Anna sees a request, and nothing is agreed yet.
st := a.call(t, "GET", "/federation", nil)
list := st.Body["friends"].([]any)
if len(list) != 1 {
t.Fatalf("Anna's friends = %v", list)
}
req := list[0].(map[string]any)
if req["name"] != "Bruno" || req["address"] != "https://bruno.example.org" || req["state"] != "incoming" {
t.Errorf("Anna's request = %v", req)
}
// Each side shows the key id of the other, so the two admins can compare them out of band.
if req["key_id"] != b.call(t, "GET", "/federation", nil).Body["key_id"] || fromB["key_id"] != st.Body["key_id"] {
t.Errorf("key ids do not match: %v / %v", req["key_id"], fromB["key_id"])
}
id := strconv.Itoa(int(req["id"].(float64)))
ok := a.call(t, "POST", "/federation/friends/"+id+"/approve", nil)
if ok.Code != http.StatusOK || ok.Body["state"] != "active" || ok.Body["approved_at"] == nil {
t.Fatalf("approve: %d %v", ok.Code, ok.Body)
}
if again := a.call(t, "POST", "/federation/friends/"+id+"/approve", nil); again.Code != http.StatusConflict || errCode(again) != "not_waiting" {
t.Errorf("approve twice: %d %v", again.Code, again.Body)
}
// The invite was good once.
c := newFriendServer(t, false)
c.setUp(t, "Carla", "https://carla.example.org")
if r := c.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}); r.Code != http.StatusUnprocessableEntity || errCode(r) != "invite_refused" {
t.Errorf("second use: %d %v", r.Code, r.Body)
}
// Revoking, on either side.
if r := b.call(t, "DELETE", "/federation/friends/"+strconv.Itoa(int(fromB["id"].(float64))), nil); r.Code != http.StatusNoContent {
t.Errorf("delete: %d %v", r.Code, r.Body)
}
if r := a.call(t, "DELETE", "/federation/friends/"+id, nil); r.Code != http.StatusNoContent {
t.Errorf("delete: %d %v", r.Code, r.Body)
}
if r := a.call(t, "DELETE", "/federation/friends/"+id, nil); r.Code != http.StatusNotFound || errCode(r) != "friend_not_found" {
t.Errorf("delete twice: %d %v", r.Code, r.Body)
}
}
func TestThePrivateKeyIsNeverReturned(t *testing.T) {
s := newFriendServer(t, false)
s.setUp(t, "Anna", "https://anna.example.org")
var priv string
if err := s.db.NewRaw("SELECT private_key FROM federation_settings").Scan(context.Background(), &priv); err != nil || priv == "" {
t.Fatalf("private key = %q, %v", priv, err)
}
for _, path := range []string{"/federation"} {
if body := fmt.Sprint(s.call(t, "GET", path, nil).Body); strings.Contains(body, priv) {
t.Errorf("GET %s returned the private key", path)
}
}
}
func TestRedeemChecksTheInvite(t *testing.T) {
s := newFriendServer(t, false)
if r := s.call(t, "POST", "/federation/friends", map[string]any{"invite": "sonda1.x"}); r.Code != http.StatusConflict || errCode(r) != "federation_off" {
t.Errorf("while off: %d %v", r.Code, r.Body)
}
s.setUp(t, "Bruno", "https://bruno.example.org")
for name, body := range map[string]any{
"garbage": map[string]any{"invite": "hello"},
"empty": map[string]any{"invite": ""},
"no field": map[string]any{},
"bad json": "[",
} {
if r := s.call(t, "POST", "/federation/friends", body); r.Code != http.StatusBadRequest || (errCode(r) != "invalid_invite" && errCode(r) != "invalid_input") {
t.Errorf("%s: %d %v", name, r.Code, r.Body)
}
}
// An invite whose server is gone.
a := newFriendServer(t, true)
a.setUp(t, "Anna", a.ts.URL)
inv := a.call(t, "POST", "/federation/invites", nil)
a.ts.Close()
if r := s.call(t, "POST", "/federation/friends", map[string]any{"invite": inv.Body["invite"]}); r.Code != http.StatusBadGateway || errCode(r) != "friend_unreachable" {
t.Errorf("server gone: %d %v", r.Code, r.Body)
}
if r := s.call(t, "GET", "/federation", nil); len(r.Body["friends"].([]any)) != 0 {
t.Errorf("a failed attempt left a friend: %v", r.Body["friends"])
}
}
func TestApprovingWhatIsNotWaiting(t *testing.T) {
s := newFriendServer(t, false)
if r := s.call(t, "POST", "/federation/friends/99/approve", nil); r.Code != http.StatusNotFound || errCode(r) != "friend_not_found" {
t.Errorf("approve missing: %d %v", r.Code, r.Body)
}
if r := s.call(t, "POST", "/federation/friends/zero/approve", nil); r.Code != http.StatusBadRequest {
t.Errorf("approve bad id: %d %v", r.Code, r.Body)
}
}
func pairBody(code, name, address string) map[string]any {
pub, _, _ := ed25519.GenerateKey(rand.Reader)
return map[string]any{"code": code, "name": name, "address": address, "public_key": base64.RawURLEncoding.EncodeToString(pub)}
}
func TestThePairingRouteIsPublicAndGivesNothingAway(t *testing.T) {
s := newFriendServer(t, false)
anon := func(body any) resp { return do(t, s.h, "POST", "/api/v1/peer/pair", "", body) }
// While federation is off the route is as good as missing.
if r := anon(pairBody("c", "B", "https://b.example.org")); r.Code != http.StatusNotFound || errCode(r) != "not_found" {
t.Errorf("while off: %d %v", r.Code, r.Body)
}
s.setUp(t, "Anna", "https://anna.example.org")
if r := anon(pairBody("not-an-invite", "B", "https://b.example.org")); r.Code != http.StatusForbidden || errCode(r) != "invalid_invite" {
t.Errorf("unknown code: %d %v", r.Code, r.Body)
}
for name, body := range map[string]any{
"no name": pairBody("c", "", "https://b.example.org"),
"bad address": pairBody("c", "B", "ftp://b"),
"bad key": map[string]any{"code": "c", "name": "B", "address": "https://b.example.org", "public_key": "AAAA"},
"not base64": map[string]any{"code": "c", "name": "B", "address": "https://b.example.org", "public_key": "!!"},
"bad json": "{",
} {
if r := anon(body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
t.Errorf("%s: %d %v", name, r.Code, r.Body)
}
}
}
func TestPairingIsSlowedDown(t *testing.T) {
s := newFriendServer(t, false)
s.setUp(t, "Anna", "https://anna.example.org")
var last resp
for i := 0; i < 11; i++ {
last = do(t, s.h, "POST", "/api/v1/peer/pair", "", pairBody("guess", "B", "https://b.example.org"))
}
if last.Code != http.StatusTooManyRequests || errCode(last) != "too_many_requests" {
t.Errorf("11th attempt: %d %v", last.Code, last.Body)
}
}

View file

@ -0,0 +1,221 @@
package httpapi
import (
"context"
"crypto/ed25519"
"encoding/base64"
"errors"
"net/http"
"github.com/gin-gonic/gin"
"codeberg.org/squidd/sonda/internal/federation"
)
type federationService interface {
Status(ctx context.Context) (federation.Status, error)
Update(ctx context.Context, p federation.Patch) (federation.Status, error)
CreateInvite(ctx context.Context) (federation.Invite, error)
Redeem(ctx context.Context, text string) (federation.Friend, error)
Pair(ctx context.Context, req federation.PairRequest) (federation.PairReply, error)
Approve(ctx context.Context, id int64) (federation.Friend, error)
Remove(ctx context.Context, id int64) error
}
// federationHandlers are the admin routes that set up federation and its friends, and the one public route
// another server calls to pair.
type federationHandlers struct{ svc federationService }
type friendJSON struct {
ID int64 `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
KeyID string `json:"key_id"`
State string `json:"state"`
CreatedAt *string `json:"created_at"`
ApprovedAt *string `json:"approved_at"`
}
func toFriendJSON(f federation.Friend) friendJSON {
out := friendJSON{
ID: f.ID, Name: f.Name, Address: f.Address, KeyID: federation.KeyID(f.PublicKey),
State: string(f.State), CreatedAt: rfc3339(f.CreatedAt),
}
if !f.ApprovedAt.IsZero() {
out.ApprovedAt = rfc3339(f.ApprovedAt)
}
return out
}
type federationJSON struct {
Enabled bool `json:"enabled"`
Name string `json:"name"`
Address string `json:"address"`
KeyID string `json:"key_id"`
Sends string `json:"sends"`
Friends []friendJSON `json:"friends"`
}
func toFederationJSON(st federation.Status) federationJSON {
out := federationJSON{
Enabled: st.Enabled, Name: st.Name, Address: st.Address, KeyID: st.KeyID, Sends: st.Sends,
Friends: make([]friendJSON, len(st.Friends)),
}
for i, f := range st.Friends {
out.Friends[i] = toFriendJSON(f)
}
return out
}
// writeFederationError maps what the service says to the standard envelope, for the routes of an admin.
func writeFederationError(c *gin.Context, err error) {
switch {
case errors.Is(err, federation.ErrOff):
writeError(c, http.StatusConflict, "federation_off", "turn federation on first")
case errors.Is(err, federation.ErrNoAddress):
writeError(c, http.StatusConflict, "address_required", "set the public address of this server first")
case errors.Is(err, federation.ErrInvalidInput):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
case errors.Is(err, federation.ErrInvalidInvite):
writeError(c, http.StatusBadRequest, "invalid_invite", "this is not an invite from a Sonda server")
case errors.Is(err, federation.ErrRefused):
writeError(c, http.StatusUnprocessableEntity, "invite_refused", err.Error())
case errors.Is(err, federation.ErrKeyMismatch):
writeError(c, http.StatusBadGateway, "key_mismatch", err.Error())
case errors.Is(err, federation.ErrUnreachable):
writeError(c, http.StatusBadGateway, "friend_unreachable", err.Error())
case errors.Is(err, federation.ErrAlreadyFriend):
writeError(c, http.StatusConflict, "already_friends", err.Error())
case errors.Is(err, federation.ErrTooMany):
writeError(c, http.StatusTooManyRequests, "too_many_requests", err.Error())
case errors.Is(err, federation.ErrFriendMissing):
writeError(c, http.StatusNotFound, "friend_not_found", "no such friend")
case errors.Is(err, federation.ErrWrongState):
writeError(c, http.StatusConflict, "not_waiting", err.Error())
default:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
}
}
func (h *federationHandlers) get(c *gin.Context) {
st, err := h.svc.Status(c.Request.Context())
if err != nil {
writeFederationError(c, err)
return
}
c.JSON(http.StatusOK, toFederationJSON(st))
}
type updateFederationRequest struct {
Enabled *bool `json:"enabled"` // pointers: an absent field is left alone
Name *string `json:"name"`
Address *string `json:"address"`
}
func (h *federationHandlers) update(c *gin.Context) {
var req updateFederationRequest
if !bindJSON(c, &req) {
return
}
st, err := h.svc.Update(c.Request.Context(), federation.Patch{Enabled: req.Enabled, Name: req.Name, Address: req.Address})
if err != nil {
writeFederationError(c, err)
return
}
c.JSON(http.StatusOK, toFederationJSON(st))
}
func (h *federationHandlers) invite(c *gin.Context) {
inv, err := h.svc.CreateInvite(c.Request.Context())
if err != nil {
writeFederationError(c, err)
return
}
c.JSON(http.StatusCreated, gin.H{"invite": inv.Text, "expires_at": rfc3339(inv.ExpiresAt)})
}
type redeemRequest struct {
Invite string `json:"invite"`
}
func (h *federationHandlers) redeem(c *gin.Context) {
var req redeemRequest
if !bindJSON(c, &req) {
return
}
f, err := h.svc.Redeem(c.Request.Context(), req.Invite)
if err != nil {
writeFederationError(c, err)
return
}
c.JSON(http.StatusCreated, toFriendJSON(f))
}
func (h *federationHandlers) approve(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
f, err := h.svc.Approve(c.Request.Context(), id)
if err != nil {
writeFederationError(c, err)
return
}
c.JSON(http.StatusOK, toFriendJSON(f))
}
func (h *federationHandlers) remove(c *gin.Context) {
id, ok := pathID(c)
if !ok {
return
}
if err := h.svc.Remove(c.Request.Context(), id); err != nil {
writeFederationError(c, err)
return
}
c.Status(http.StatusNoContent)
}
type pairRequestJSON struct {
Code string `json:"code"`
Name string `json:"name"`
Address string `json:"address"`
PublicKey string `json:"public_key"`
}
// pair is what another server calls when its admin pastes an invite of this one. It has no login: the code of
// the invite is the proof. While federation is off, or has no address, it is as good as a missing route.
func (h *federationHandlers) pair(c *gin.Context) {
var req pairRequestJSON
if !bindJSON(c, &req) {
return
}
key, err := base64.RawURLEncoding.DecodeString(req.PublicKey)
if err != nil || len(key) != ed25519.PublicKeySize {
writeError(c, http.StatusBadRequest, "invalid_input", "public_key is not an Ed25519 key")
return
}
reply, err := h.svc.Pair(c.Request.Context(), federation.PairRequest{
Code: req.Code, Name: req.Name, Address: req.Address, PublicKey: key,
})
switch {
case err == nil:
c.JSON(http.StatusOK, gin.H{
"name": reply.Name, "address": reply.Address, "public_key": base64.RawURLEncoding.EncodeToString(reply.PublicKey),
})
case errors.Is(err, federation.ErrOff), errors.Is(err, federation.ErrNoAddress):
writeError(c, http.StatusNotFound, "not_found", "no such route")
case errors.Is(err, federation.ErrInvalidInvite):
writeError(c, http.StatusForbidden, "invalid_invite", "the invite is not valid")
case errors.Is(err, federation.ErrInvalidInput):
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
case errors.Is(err, federation.ErrAlreadyFriend):
writeError(c, http.StatusConflict, "already_friends", "this server is already a friend")
case errors.Is(err, federation.ErrTooMany):
writeError(c, http.StatusTooManyRequests, "too_many_requests", "too many attempts: try again in a minute")
default:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
}
}

View file

@ -217,6 +217,7 @@ type Deps struct {
ArtistImages artistImageService
Settings userSettingsService
Streams streamService
Federation federationService
// Web is the built web app, served on / and for its own links. It is optional: with none (a build
// without the app) / shows a page that says how to build it, and the API works the same.
Web fs.FS
@ -259,6 +260,8 @@ func NewRouter(d Deps) *gin.Engine {
v1.POST("/auth/login", a.login)
v1.POST("/auth/refresh", a.refresh)
v1.POST("/auth/logout", a.logout)
// Another Sonda server pairs here, with the code of an invite instead of a login.
v1.POST("/peer/pair", (&federationHandlers{svc: d.Federation}).pair)
// Any signed-in user.
protected := v1.Group("", requireAuth(d.Tokens, d.Auth, false))
@ -388,6 +391,13 @@ func NewRouter(d Deps) *gin.Engine {
admin.POST("/artists/:id/lidarr", la.send)
admin.POST("/providers/:name/test", prov.test)
admin.DELETE("/providers/:name/data", prov.purge)
fed := &federationHandlers{svc: d.Federation}
admin.GET("/federation", fed.get)
admin.PUT("/federation", fed.update)
admin.POST("/federation/invites", fed.invite)
admin.POST("/federation/friends", fed.redeem)
admin.POST("/federation/friends/:id/approve", fed.approve)
admin.DELETE("/federation/friends/:id", fed.remove)
u := &usersHandlers{svc: d.Auth}
admin.GET("/users", u.list)
admin.POST("/users", u.create)

View file

@ -45,6 +45,8 @@ tags:
description: Music outside the library, from external providers an admin switched on, and suggestions scored for you.
- name: Providers
description: External services Sonda can ask about artists, set up by an admin.
- name: Federation
description: Friend Sonda servers, whose listeners can play the libraries an admin shares (admin).
paths:
/health:
@ -2945,6 +2947,221 @@ paths:
application/json:
schema: {$ref: '#/components/schemas/Error'}
/federation:
get:
tags: [Federation]
operationId: getFederation
summary: Federation, its key and its friends
description: |
Admin only. Federation is off until an admin turns it on. `sends` says in plain words what a friend's server is
sent. `key_id` names the key of this server (the first 16 hex characters of the SHA-256 of its public key); it is
empty until federation was first turned on. The private key is never returned. `name` is the one saved, or the
host name.
security: [{bearerAuth: []}]
responses:
'200':
description: The settings and the friends, oldest first.
content:
application/json:
schema: {$ref: '#/components/schemas/Federation'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
put:
tags: [Federation]
operationId: updateFederation
summary: Turn federation on or off, name this server, give its address
description: |
Admin only. Fields you leave out are kept. The key pair is made the first time `enabled` becomes true and is kept
after that, so turning it off and on again does not change the key. `address` is where friends reach this server:
`https://`, or `http://` only on a network of your own (a private, loopback or link-local address, or a name that
is local: no dot, or ending `.local`, `.lan`, `.home.arpa`); without user, query or fragment. A closing slash is
dropped; an empty address clears it. An empty `name` goes back to the host name (at most 60 characters).
security: [{bearerAuth: []}]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/FederationUpdate'}
responses:
'200':
description: The settings as they are now.
content:
application/json:
schema: {$ref: '#/components/schemas/Federation'}
'400': {$ref: '#/components/responses/InvalidInput'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
/federation/invites:
post:
tags: [Federation]
operationId: createFederationInvite
summary: Make an invite for another server
description: |
Admin only. The invite is one piece of text to give to the other admin, who pastes it into
`POST /federation/friends`. It works once and expires in 24 hours; only a hash of its code is kept here. It needs
federation on (`federation_off`) and an address (`address_required`).
security: [{bearerAuth: []}]
responses:
'201':
description: The invite.
content:
application/json:
schema: {$ref: '#/components/schemas/FederationInvite'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
'409':
description: Federation is off (`federation_off`) or this server has no address yet (`address_required`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
/federation/friends:
post:
tags: [Federation]
operationId: useFederationInvite
summary: Use an invite made by another server
description: |
Admin only. Asks the server the invite came from to accept this one, checks that the key it answers with is the one
the invite names, and keeps it as a friend in state `outgoing`: it waits for the other admin to approve. This is
the only request federation sends at an admin's command, and it sends this server's name, address and public key
and the code of the invite. It needs federation on and an address, like making an invite.
security: [{bearerAuth: []}]
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/FederationUseInvite'}
responses:
'201':
description: The new friend, waiting for approval.
content:
application/json:
schema: {$ref: '#/components/schemas/FederationFriend'}
'400':
description: Malformed body (`invalid_input`) or not an invite of a Sonda server (`invalid_invite`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
'409':
description: Federation is off (`federation_off`), no address (`address_required`) or the server is already a friend (`already_friends`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'422':
description: "The other server turned the invite down: used, expired, or federation off there (`invite_refused`)."
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'429':
description: The other server asked to slow down (`too_many_requests`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'502':
description: The other server could not be reached or did not answer as a Sonda server (`friend_unreachable`), or its key is not the one in the invite (`key_mismatch`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
/federation/friends/{id}:
parameters:
- {name: id, in: path, required: true, schema: {type: integer}}
delete:
tags: [Federation]
operationId: removeFederationFriend
summary: Decline, withdraw or end a friendship
description: |
Admin only. Removes the friend of any state. The other server is not told: its requests are no longer answered.
The same server can be invited again afterwards.
security: [{bearerAuth: []}]
responses:
'204': {description: Removed.}
'400': {$ref: '#/components/responses/InvalidInput'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
'404':
description: No such friend (`friend_not_found`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
/federation/friends/{id}/approve:
parameters:
- {name: id, in: path, required: true, schema: {type: integer}}
post:
tags: [Federation]
operationId: approveFederationFriend
summary: Approve a server that used an invite of this one
description: |
Admin only. The second confirmation: a friend in state `incoming` becomes `active`. A friend in any other state
is a `409` (`not_waiting`).
security: [{bearerAuth: []}]
responses:
'200':
description: The friend, now active.
content:
application/json:
schema: {$ref: '#/components/schemas/FederationFriend'}
'400': {$ref: '#/components/responses/InvalidInput'}
'401': {$ref: '#/components/responses/Unauthorized'}
'403': {$ref: '#/components/responses/AdminRequired'}
'404':
description: No such friend (`friend_not_found`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'409':
description: The friend is not waiting for approval (`not_waiting`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
/peer/pair:
post:
tags: [Federation]
operationId: peerPair
summary: Pair with this server (called by another Sonda server)
description: |
Not for people: another Sonda server calls this when its admin pastes an invite made here. There is no login: the
code of the invite is the proof, and it works once. The other server is kept as a friend in state `incoming`
until an admin here approves it. While federation is off, or has no address, the route answers `404 not_found`,
as a missing route does. At most 10 attempts a minute, from anyone.
requestBody:
required: true
content:
application/json:
schema: {$ref: '#/components/schemas/PeerPairRequest'}
responses:
'200':
description: This server's name, address and public key.
content:
application/json:
schema: {$ref: '#/components/schemas/PeerPairReply'}
'400': {$ref: '#/components/responses/InvalidInput'}
'403':
description: The code is unknown, used or expired (`invalid_invite`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'404':
description: Federation is off here (`not_found`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'409':
description: This key is already a friend (`already_friends`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
'429':
description: Too many attempts (`too_many_requests`).
content:
application/json:
schema: {$ref: '#/components/schemas/Error'}
components:
securitySchemes:
bearerAuth:
@ -4284,3 +4501,75 @@ components:
total: {type: integer, description: "Size of the whole result, not of this page."}
limit: {type: integer}
offset: {type: integer}
Federation:
type: object
additionalProperties: false
required: [enabled, name, address, key_id, sends, friends]
properties:
enabled: {type: boolean, description: Whether this server may pair with others. Off by default.}
name: {type: string, description: How friends see this server.}
address: {type: string, description: Where friends reach this server; empty until set.}
key_id: {type: string, description: "Names this server's key; empty until federation was first turned on. Compare it with the one the other admin sees."}
sends: {type: string, description: "What a friend's server is sent, and what it never learns."}
friends:
type: array
items: {$ref: '#/components/schemas/FederationFriend'}
FederationUpdate:
type: object
additionalProperties: false
anyOf:
- required: [enabled]
- required: [name]
- required: [address]
properties:
enabled: {type: boolean}
name: {type: string, maxLength: 240, description: "At most 60 characters, no control characters. Empty goes back to the host name."}
address: {type: string, maxLength: 300}
FederationInvite:
type: object
additionalProperties: false
required: [invite, expires_at]
properties:
invite: {type: string, description: One piece of text to give to the other admin. Starts with `sonda1.`.}
expires_at: {type: string, format: date-time}
FederationUseInvite:
type: object
additionalProperties: false
required: [invite]
properties:
invite: {type: string, maxLength: 1000}
FederationFriend:
type: object
additionalProperties: false
required: [id, name, address, key_id, state, created_at, approved_at]
properties:
id: {type: integer}
name: {type: string}
address: {type: string}
key_id: {type: string, description: Names the friend's key.}
state:
type: string
enum: [incoming, outgoing, active]
description: |
`incoming`: it used an invite of this server and waits for approval here. `outgoing`: this server used its invite
and waits for it. `active`: both agreed.
created_at: {type: string, format: date-time}
approved_at: {type: string, format: date-time, nullable: true}
PeerPairRequest:
type: object
additionalProperties: false
required: [code, name, address, public_key]
properties:
code: {type: string, maxLength: 64}
name: {type: string, maxLength: 240}
address: {type: string, maxLength: 300}
public_key: {type: string, description: "The Ed25519 public key, 32 bytes, base64url without padding."}
PeerPairReply:
type: object
additionalProperties: false
required: [name, address, public_key]
properties:
name: {type: string}
address: {type: string}
public_key: {type: string, description: "The Ed25519 public key, 32 bytes, base64url without padding."}

View file

@ -0,0 +1,99 @@
package federation
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
)
const (
pairPath = "/api/v1/peer/pair"
maxReplyBytes = 16 << 10
)
// Client talks to other Sonda servers. It is not the outbound client of the providers, which by design never sends a
// header that identifies anything: this one is for a friend, and it has the same bounds otherwise: no redirect is
// followed, no proxy of the environment is used, the answer has a size limit and every attempt has a time limit.
type Client struct{ http *http.Client }
// NewClient builds a Client.
func NewClient() *Client {
return &Client{http: &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
Proxy: nil,
DialContext: (&net.Dialer{Timeout: 5 * time.Second}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
DisableKeepAlives: true,
},
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}}
}
type pairBody struct {
Code string `json:"code"`
Name string `json:"name"`
Address string `json:"address"`
PublicKey string `json:"public_key"`
}
type pairReplyBody struct {
Name string `json:"name"`
Address string `json:"address"`
PublicKey string `json:"public_key"`
}
// Pair asks the server at address to accept this server as a friend, with the code of an invite.
func (c *Client) Pair(ctx context.Context, address string, req PairRequest) (PairReply, error) {
payload, err := json.Marshal(pairBody{
Code: req.Code, Name: req.Name, Address: req.Address, PublicKey: base64.RawURLEncoding.EncodeToString(req.PublicKey),
})
if err != nil {
return PairReply{}, err
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(address, "/")+pairPath, bytes.NewReader(payload))
if err != nil {
return PairReply{}, fmt.Errorf("%w: %v", ErrUnreachable, err)
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("User-Agent", "Sonda (federation)")
resp, err := c.http.Do(httpReq)
if err != nil {
return PairReply{}, fmt.Errorf("%w: %v", ErrUnreachable, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, maxReplyBytes+1))
if err != nil || len(body) > maxReplyBytes {
return PairReply{}, fmt.Errorf("%w: no usable answer", ErrUnreachable)
}
switch resp.StatusCode {
case http.StatusOK:
var r pairReplyBody
if json.Unmarshal(body, &r) != nil {
return PairReply{}, fmt.Errorf("%w: the answer is not that of a Sonda server", ErrUnreachable)
}
key, err := base64.RawURLEncoding.DecodeString(r.PublicKey)
if err != nil || r.Name == "" || len(key) != ed25519.PublicKeySize {
return PairReply{}, fmt.Errorf("%w: the answer is not that of a Sonda server", ErrUnreachable)
}
return PairReply{Name: r.Name, Address: r.Address, PublicKey: key}, nil
case http.StatusForbidden:
return PairReply{}, ErrRefused
case http.StatusConflict:
return PairReply{}, ErrAlreadyFriend
case http.StatusTooManyRequests:
return PairReply{}, ErrTooMany
default:
return PairReply{}, fmt.Errorf("%w: it answered %d", ErrUnreachable, resp.StatusCode)
}
}

View file

@ -0,0 +1,141 @@
package federation
import (
"bytes"
"context"
"crypto/ed25519"
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func serverKey(t *testing.T) ed25519.PublicKey {
t.Helper()
pub, _, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
return pub
}
func TestClientSendsThePairingRequestAndReadsTheReply(t *testing.T) {
mine, theirs := serverKey(t), serverKey(t)
var got struct {
Method, Path, Type, Agent, Auth string
Body map[string]string
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got.Method, got.Path, got.Type = r.Method, r.URL.Path, r.Header.Get("Content-Type")
got.Agent, got.Auth = r.Header.Get("User-Agent"), r.Header.Get("Authorization")
_ = json.NewDecoder(r.Body).Decode(&got.Body)
_ = json.NewEncoder(w).Encode(map[string]string{
"name": "Anna", "address": "https://anna.example.org", "public_key": base64.RawURLEncoding.EncodeToString(theirs),
})
}))
defer srv.Close()
reply, err := NewClient().Pair(context.Background(), srv.URL, PairRequest{
Code: "the-code", Name: "Bruno", Address: "https://bruno.example.org", PublicKey: mine,
})
if err != nil {
t.Fatal(err)
}
if got.Method != "POST" || got.Path != "/api/v1/peer/pair" || got.Type != "application/json" || got.Auth != "" || !strings.HasPrefix(got.Agent, "Sonda") {
t.Errorf("request = %+v", got)
}
if got.Body["code"] != "the-code" || got.Body["name"] != "Bruno" || got.Body["address"] != "https://bruno.example.org" ||
got.Body["public_key"] != base64.RawURLEncoding.EncodeToString(mine) {
t.Errorf("body = %v", got.Body)
}
if reply.Name != "Anna" || reply.Address != "https://anna.example.org" || !reply.PublicKey.Equal(theirs) {
t.Errorf("reply = %+v", reply)
}
}
func TestClientKeepsTheSubPathOfTheAddress(t *testing.T) {
var path string
srv := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { path = r.URL.Path }))
defer srv.Close()
_, _ = NewClient().Pair(context.Background(), srv.URL+"/sonda", PairRequest{Code: "c", PublicKey: serverKey(t)})
if path != "/sonda/api/v1/peer/pair" {
t.Errorf("path = %q", path)
}
}
func TestClientMapsWhatTheOtherServerSays(t *testing.T) {
for _, tc := range []struct {
status int
body string
want error
}{
{http.StatusForbidden, `{"error":{"code":"invalid_invite","message":"x"}}`, ErrRefused},
{http.StatusConflict, `{"error":{"code":"already_friends","message":"x"}}`, ErrAlreadyFriend},
{http.StatusTooManyRequests, `{"error":{"code":"too_many_requests","message":"x"}}`, ErrTooMany},
{http.StatusNotFound, `<html>not here</html>`, ErrUnreachable},
{http.StatusInternalServerError, `oops`, ErrUnreachable},
{http.StatusOK, `not json`, ErrUnreachable},
{http.StatusOK, `{"name":"A","address":"https://a.example.org","public_key":"!!"}`, ErrUnreachable},
{http.StatusOK, `{"name":"A","address":"https://a.example.org","public_key":"AAAA"}`, ErrUnreachable},
} {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tc.status)
_, _ = io.WriteString(w, tc.body)
}))
_, err := NewClient().Pair(context.Background(), srv.URL, PairRequest{Code: "c", PublicKey: serverKey(t)})
srv.Close()
if !errors.Is(err, tc.want) {
t.Errorf("%d %q: err = %v, want %v", tc.status, tc.body, err, tc.want)
}
}
}
func TestClientNeverFollowsARedirect(t *testing.T) {
elsewhere := 0
other := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { elsewhere++ }))
defer other.Close()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, other.URL, http.StatusTemporaryRedirect)
}))
defer srv.Close()
_, err := NewClient().Pair(context.Background(), srv.URL, PairRequest{Code: "c", PublicKey: serverKey(t)})
if !errors.Is(err, ErrUnreachable) || elsewhere != 0 {
t.Errorf("err = %v, redirected requests = %d: a redirect must end the attempt", err, elsewhere)
}
}
func TestClientBoundsTheAnswerAndTheWait(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(bytes.Repeat([]byte("x"), 1<<20))
}))
defer srv.Close()
if _, err := NewClient().Pair(context.Background(), srv.URL, PairRequest{Code: "c", PublicKey: serverKey(t)}); !errors.Is(err, ErrUnreachable) {
t.Errorf("a huge answer: err = %v, want ErrUnreachable", err)
}
// The handler waits for the test to end: it does not read the body, so it would not see the client leave.
release := make(chan struct{})
slow := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { <-release }))
defer slow.Close()
defer close(release)
c := NewClient()
c.http.Timeout = 50 * time.Millisecond
if _, err := c.Pair(context.Background(), slow.URL, PairRequest{Code: "c", PublicKey: serverKey(t)}); !errors.Is(err, ErrUnreachable) {
t.Errorf("a server that never answers: err = %v, want ErrUnreachable", err)
}
}
func TestClientDoesNotUseTheProxyOfTheEnvironment(t *testing.T) {
t.Setenv("HTTP_PROXY", "http://127.0.0.1:1")
t.Setenv("http_proxy", "http://127.0.0.1:1")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusForbidden) }))
defer srv.Close()
if _, err := NewClient().Pair(context.Background(), srv.URL, PairRequest{Code: "c", PublicKey: serverKey(t)}); !errors.Is(err, ErrRefused) {
t.Errorf("err = %v, want ErrRefused: the request must go straight to the server", err)
}
}

View file

@ -0,0 +1,89 @@
// Package federation lets two Sonda servers become friends: each one's listeners can play the libraries the
// other's admin chose to share, read only. Nothing leaves a server unless its admin decides it, so federation is off
// until an admin turns it on, and a friend is made only by an invite and two confirmations.
//
// This file holds the types and the contract of the store. The rules are in service.go, the call to the other
// server in client.go.
package federation
import (
"context"
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"errors"
"time"
)
// Errors returned by the service. Handlers map them to HTTP statuses.
var (
ErrOff = errors.New("federation is off")
ErrNoAddress = errors.New("this server has no public address yet")
ErrInvalidInput = errors.New("invalid federation input")
ErrInvalidInvite = errors.New("the invite is not valid")
ErrAlreadyFriend = errors.New("this server is already a friend")
ErrFriendMissing = errors.New("no such friend")
ErrWrongState = errors.New("the friend is not waiting for approval")
ErrTooMany = errors.New("too many pairing attempts: try again in a minute")
ErrUnreachable = errors.New("the other server could not be reached")
ErrRefused = errors.New("the other server refused the invite: it may be used, expired or federation off there")
ErrKeyMismatch = errors.New("the other server's key is not the one in the invite")
)
// State of a friend.
type State string
// The states a friend goes through.
const (
// Incoming: they used an invite of mine and wait for my approval.
Incoming State = "incoming"
// Outgoing: I used an invite of theirs and wait for their approval.
Outgoing State = "outgoing"
// Active: both sides agreed.
Active State = "active"
)
// Settings of this server's federation. The private key never leaves the service: the API shows the key id only.
type Settings struct {
Enabled bool
Name string // shown to friends; empty means the host name
Address string // where friends reach this server, normalised
PublicKey ed25519.PublicKey
PrivateKey ed25519.PrivateKey
UpdatedAt time.Time
}
// Friend is another Sonda server.
type Friend struct {
ID int64
Name string
Address string
PublicKey ed25519.PublicKey
State State
CreatedAt time.Time
ApprovedAt time.Time // zero until active
}
// KeyID is the short name of a key: the first 16 hex characters of its SHA-256.
func KeyID(pub ed25519.PublicKey) string {
sum := sha256.Sum256(pub)
return hex.EncodeToString(sum[:])[:16]
}
// Store persists federation state.
type Store interface {
// Settings returns found=false until they were first saved.
Settings(ctx context.Context) (s Settings, found bool, err error)
SaveSettings(ctx context.Context, s Settings) error
AddInvite(ctx context.Context, codeHash string, created, expires time.Time) error
// UseInvite marks an unused, unexpired invite as used and says whether there was one.
UseInvite(ctx context.Context, codeHash string, now time.Time) (bool, error)
Friends(ctx context.Context) ([]Friend, error)
Friend(ctx context.Context, id int64) (Friend, error) // ErrFriendMissing
FriendByKey(ctx context.Context, key ed25519.PublicKey) (Friend, bool, error)
AddFriend(ctx context.Context, f Friend) (Friend, error) // ErrAlreadyFriend when the key is known
Approve(ctx context.Context, id int64, now time.Time) error // incoming to active; ErrFriendMissing, ErrWrongState
DeleteFriend(ctx context.Context, id int64) error // ErrFriendMissing
}

View file

@ -0,0 +1,433 @@
package federation
import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"net"
"net/netip"
"net/url"
"os"
"strings"
"sync"
"time"
"unicode"
"unicode/utf8"
)
const (
inviteTTL = 24 * time.Hour
invitePrefix = "sonda1."
maxNameRunes = 60
maxAddressLen = 300
maxCodeLen = 64
pairAttempts = 10 // pairing requests allowed in one window, from anyone
pairWindow = time.Minute
keyIDHexLength = 16
)
// Sends is what an admin is told before turning federation on.
const Sends = "Nothing is sent until an admin makes an invite or uses one. A friend's server is then sent: this " +
"server's name and address, its public key, and the id of each track or cover its listeners open. It never " +
"learns who listens. Shared libraries can be played, and in practice copied, by the friend's listeners."
// PairRequest is what the server that uses an invite sends to the one that made it.
type PairRequest struct {
Code string
Name string
Address string
PublicKey ed25519.PublicKey
}
// PairReply is what the server that made the invite answers.
type PairReply struct {
Name string
Address string
PublicKey ed25519.PublicKey
}
// Remote talks to another Sonda server. It is only asked when an admin pastes an invite.
type Remote interface {
// Pair sends the request to the server at address. It returns ErrRefused when that server turned the invite down,
// and ErrUnreachable when it did not answer as a Sonda server with federation on.
Pair(ctx context.Context, address string, req PairRequest) (PairReply, error)
}
// Status is what an admin sees: the settings without the private key, and the friends.
type Status struct {
Enabled bool
Name string // the one saved, or the host name
Address string
KeyID string // empty until federation was first turned on
Friends []Friend
Sends string
}
// Patch changes some settings. A nil field is left alone; an empty Address clears it and an empty Name goes back to the host name.
type Patch struct {
Enabled *bool
Name *string
Address *string
}
// Invite is what an admin gives to the other one, once.
type Invite struct {
Text string
ExpiresAt time.Time
}
// Service holds the rules of federation.
type Service struct {
store Store
remote Remote
now func() time.Time
host func() string
mu sync.Mutex // an update reads then writes; the attempts list is shared
attempts []time.Time
}
// NewService builds a Service. remote may be nil in a server that never pastes an invite (tests).
func NewService(store Store, remote Remote) *Service {
return &Service{store: store, remote: remote, now: time.Now, host: hostName}
}
func hostName() string {
if h, err := os.Hostname(); err == nil && strings.TrimSpace(h) != "" {
return h
}
return "sonda"
}
func (s *Service) load(ctx context.Context) (Settings, error) {
set, _, err := s.store.Settings(ctx)
if err != nil {
return Settings{}, fmt.Errorf("read federation settings: %w", err)
}
return set, nil
}
func (s *Service) name(set Settings) string {
if set.Name != "" {
return set.Name
}
return s.host()
}
// Status returns the settings and the friends.
func (s *Service) Status(ctx context.Context) (Status, error) {
set, err := s.load(ctx)
if err != nil {
return Status{}, err
}
friends, err := s.store.Friends(ctx)
if err != nil {
return Status{}, fmt.Errorf("list friends: %w", err)
}
st := Status{Enabled: set.Enabled, Name: s.name(set), Address: set.Address, Friends: friends, Sends: Sends}
if set.PublicKey != nil {
st.KeyID = KeyID(set.PublicKey)
}
return st, nil
}
// Update changes the settings. The key pair is made the first time federation is turned on and is kept after that.
func (s *Service) Update(ctx context.Context, p Patch) (Status, error) {
s.mu.Lock()
set, err := s.load(ctx)
if err != nil {
s.mu.Unlock()
return Status{}, err
}
if p.Name != nil {
name := strings.TrimSpace(*p.Name)
if err := checkText(name, true); err != nil {
s.mu.Unlock()
return Status{}, err
}
set.Name = name
}
if p.Address != nil {
addr := strings.TrimSpace(*p.Address)
if addr != "" {
if addr, err = normalizeAddress(addr); err != nil {
s.mu.Unlock()
return Status{}, err
}
}
set.Address = addr
}
if p.Enabled != nil {
set.Enabled = *p.Enabled
if set.Enabled && set.PublicKey == nil {
if set.PublicKey, set.PrivateKey, err = ed25519.GenerateKey(rand.Reader); err != nil {
s.mu.Unlock()
return Status{}, fmt.Errorf("make the server key: %w", err)
}
}
}
set.UpdatedAt = s.now()
err = s.store.SaveSettings(ctx, set)
s.mu.Unlock()
if err != nil {
return Status{}, fmt.Errorf("save federation settings: %w", err)
}
return s.Status(ctx)
}
// ready returns the settings of a server that can pair: on, with a key and an address.
func (s *Service) ready(ctx context.Context) (Settings, error) {
set, err := s.load(ctx)
if err != nil {
return Settings{}, err
}
if !set.Enabled || set.PublicKey == nil {
return Settings{}, ErrOff
}
if set.Address == "" {
return Settings{}, ErrNoAddress
}
return set, nil
}
func hashCode(code string) string {
sum := sha256.Sum256([]byte(code))
return hex.EncodeToString(sum[:])
}
type invitePayload struct {
Address string `json:"a"`
Code string `json:"c"`
KeyID string `json:"k"`
}
// CreateInvite makes an invite that works once, for 24 hours. Only a hash of its code is kept.
func (s *Service) CreateInvite(ctx context.Context) (Invite, error) {
set, err := s.ready(ctx)
if err != nil {
return Invite{}, err
}
raw := make([]byte, 20)
if _, err := rand.Read(raw); err != nil {
return Invite{}, fmt.Errorf("make an invite code: %w", err)
}
code := base64.RawURLEncoding.EncodeToString(raw)
now := s.now()
expires := now.Add(inviteTTL)
if err := s.store.AddInvite(ctx, hashCode(code), now, expires); err != nil {
return Invite{}, fmt.Errorf("store the invite: %w", err)
}
body, err := json.Marshal(invitePayload{Address: set.Address, Code: code, KeyID: KeyID(set.PublicKey)})
if err != nil {
return Invite{}, err
}
return Invite{Text: invitePrefix + base64.RawURLEncoding.EncodeToString(body), ExpiresAt: expires}, nil
}
func parseInvite(text string) (invitePayload, error) {
var p invitePayload
rest, ok := strings.CutPrefix(strings.TrimSpace(text), invitePrefix)
if !ok {
return p, ErrInvalidInvite
}
body, err := base64.RawURLEncoding.DecodeString(rest)
if err != nil || json.Unmarshal(body, &p) != nil {
return p, ErrInvalidInvite
}
if p.Code == "" || len(p.Code) > maxCodeLen || len(p.KeyID) != keyIDHexLength {
return p, ErrInvalidInvite
}
if _, err := hex.DecodeString(p.KeyID); err != nil {
return p, ErrInvalidInvite
}
if p.Address, err = normalizeAddress(p.Address); err != nil {
return p, ErrInvalidInvite
}
return p, nil
}
// Redeem uses an invite made by another server: it asks that server to pair, checks that the key that comes back
// is the one the invite names, and keeps it as a friend that waits for the other admin's approval.
func (s *Service) Redeem(ctx context.Context, text string) (Friend, error) {
set, err := s.ready(ctx)
if err != nil {
return Friend{}, err
}
inv, err := parseInvite(text)
if err != nil {
return Friend{}, err
}
if inv.KeyID == KeyID(set.PublicKey) {
return Friend{}, fmt.Errorf("%w: that is an invite of this server", ErrInvalidInput)
}
reply, err := s.remote.Pair(ctx, inv.Address, PairRequest{
Code: inv.Code, Name: s.name(set), Address: set.Address, PublicKey: set.PublicKey,
})
if err != nil {
return Friend{}, err
}
if len(reply.PublicKey) != ed25519.PublicKeySize || KeyID(reply.PublicKey) != inv.KeyID {
return Friend{}, ErrKeyMismatch
}
name := strings.TrimSpace(reply.Name)
if checkText(name, false) != nil {
return Friend{}, fmt.Errorf("%w: the other server sent no usable name", ErrUnreachable)
}
// The address is the one the invite was made for: that is where it answered.
return s.store.AddFriend(ctx, Friend{
Name: name, Address: inv.Address, PublicKey: reply.PublicKey, State: Outgoing, CreatedAt: s.now(),
})
}
func (s *Service) tooMany() bool {
s.mu.Lock()
defer s.mu.Unlock()
now := s.now()
kept := s.attempts[:0]
for _, t := range s.attempts {
if now.Sub(t) < pairWindow {
kept = append(kept, t)
}
}
s.attempts = kept
if len(s.attempts) >= pairAttempts {
return true
}
s.attempts = append(s.attempts, now)
return false
}
// Pair answers another server that used an invite of this one. The friend is kept as incoming: an admin of this
// server still has to approve it.
func (s *Service) Pair(ctx context.Context, req PairRequest) (PairReply, error) {
set, err := s.load(ctx)
if err != nil {
return PairReply{}, err
}
if !set.Enabled || set.PublicKey == nil {
return PairReply{}, ErrOff
}
if s.tooMany() {
return PairReply{}, ErrTooMany
}
name := strings.TrimSpace(req.Name)
if err := checkText(name, false); err != nil {
return PairReply{}, err
}
addr, err := normalizeAddress(req.Address)
if err != nil {
return PairReply{}, err
}
if len(req.PublicKey) != ed25519.PublicKeySize || req.PublicKey.Equal(set.PublicKey) {
return PairReply{}, fmt.Errorf("%w: not a key of another server", ErrInvalidInput)
}
if set.Address == "" {
return PairReply{}, ErrNoAddress
}
if _, known, err := s.store.FriendByKey(ctx, req.PublicKey); err != nil {
return PairReply{}, fmt.Errorf("look up the friend: %w", err)
} else if known {
return PairReply{}, ErrAlreadyFriend
}
if req.Code == "" || len(req.Code) > maxCodeLen {
return PairReply{}, ErrInvalidInvite
}
used, err := s.store.UseInvite(ctx, hashCode(req.Code), s.now())
if err != nil {
return PairReply{}, fmt.Errorf("use the invite: %w", err)
}
if !used {
return PairReply{}, ErrInvalidInvite
}
if _, err := s.store.AddFriend(ctx, Friend{
Name: name, Address: addr, PublicKey: req.PublicKey, State: Incoming, CreatedAt: s.now(),
}); err != nil {
return PairReply{}, err
}
return PairReply{Name: s.name(set), Address: set.Address, PublicKey: set.PublicKey}, nil
}
// Approve is the second confirmation: a friend that used an invite of this server becomes active.
func (s *Service) Approve(ctx context.Context, id int64) (Friend, error) {
if err := s.store.Approve(ctx, id, s.now()); err != nil {
return Friend{}, err
}
return s.store.Friend(ctx, id)
}
// Remove declines a request, withdraws one, or ends a friendship. The other server is not told: its requests
// are simply no longer answered.
func (s *Service) Remove(ctx context.Context, id int64) error { return s.store.DeleteFriend(ctx, id) }
// checkText accepts a name: at most 60 characters, none of them a control character. An own name may be empty (it
// then is the host name); a friend's may not.
func checkText(name string, allowEmpty bool) error {
switch {
case name == "" && allowEmpty:
return nil
case name == "", utf8.RuneCountInString(name) > maxNameRunes, !utf8.ValidString(name):
return fmt.Errorf("%w: a name is 1 to %d characters", ErrInvalidInput, maxNameRunes)
}
for _, r := range name {
if unicode.IsControl(r) {
return fmt.Errorf("%w: a name has no control characters", ErrInvalidInput)
}
}
return nil
}
// normalizeAddress checks the address of a server and writes it one way: scheme and host in lower case, no trailing
// slash. It must be https; plain http is accepted only on a network of one's own (a private, loopback or link-local
// address, or a local name), for two servers in one house.
func normalizeAddress(s string) (string, error) {
bad := func(why string) (string, error) { return "", fmt.Errorf("%w: address %s", ErrInvalidInput, why) }
if s == "" || len(s) > maxAddressLen {
return bad("is empty or too long")
}
u, err := url.Parse(s)
if err != nil || u.Host == "" {
return bad("is not an http(s) address")
}
scheme := strings.ToLower(u.Scheme)
if scheme != "http" && scheme != "https" {
return bad("must start with https://")
}
if u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" {
return bad("has no user, query or fragment")
}
host := strings.ToLower(u.Hostname())
if host == "" {
return bad("has no host")
}
if scheme == "http" && !onOwnNetwork(host) {
return bad("must be https, unless it is on your own network")
}
hostPort := host
if strings.Contains(host, ":") {
hostPort = "[" + host + "]"
}
if port := u.Port(); port != "" {
hostPort = net.JoinHostPort(host, port)
}
return scheme + "://" + hostPort + strings.TrimRight(u.Path, "/"), nil
}
func onOwnNetwork(host string) bool {
if ip, err := netip.ParseAddr(host); err == nil {
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast()
}
if !strings.Contains(host, ".") {
return true // nas, localhost
}
for _, suffix := range []string{".local", ".lan", ".home.arpa"} {
if strings.HasSuffix(host, suffix) {
return true
}
}
return false
}

View file

@ -0,0 +1,515 @@
package federation
import (
"context"
"crypto/ed25519"
"errors"
"strings"
"testing"
"time"
)
// memStore is a Store in memory.
type memStore struct {
settings *Settings
invites map[string]memInvite
friends []Friend
nextID int64
}
type memInvite struct {
expires time.Time
used bool
}
func newMemStore() *memStore { return &memStore{invites: map[string]memInvite{}} }
func (m *memStore) Settings(context.Context) (Settings, bool, error) {
if m.settings == nil {
return Settings{}, false, nil
}
return *m.settings, true, nil
}
func (m *memStore) SaveSettings(_ context.Context, s Settings) error { m.settings = &s; return nil }
func (m *memStore) AddInvite(_ context.Context, hash string, _, expires time.Time) error {
m.invites[hash] = memInvite{expires: expires}
return nil
}
func (m *memStore) UseInvite(_ context.Context, hash string, now time.Time) (bool, error) {
inv, ok := m.invites[hash]
if !ok || inv.used || !now.Before(inv.expires) {
return false, nil
}
inv.used = true
m.invites[hash] = inv
return true, nil
}
func (m *memStore) Friends(context.Context) ([]Friend, error) {
return append([]Friend(nil), m.friends...), nil
}
func (m *memStore) Friend(_ context.Context, id int64) (Friend, error) {
for _, f := range m.friends {
if f.ID == id {
return f, nil
}
}
return Friend{}, ErrFriendMissing
}
func (m *memStore) FriendByKey(_ context.Context, key ed25519.PublicKey) (Friend, bool, error) {
for _, f := range m.friends {
if f.PublicKey.Equal(key) {
return f, true, nil
}
}
return Friend{}, false, nil
}
func (m *memStore) AddFriend(ctx context.Context, f Friend) (Friend, error) {
if _, found, _ := m.FriendByKey(ctx, f.PublicKey); found {
return Friend{}, ErrAlreadyFriend
}
m.nextID++
f.ID = m.nextID
m.friends = append(m.friends, f)
return f, nil
}
func (m *memStore) Approve(_ context.Context, id int64, now time.Time) error {
for i, f := range m.friends {
if f.ID != id {
continue
}
if f.State != Incoming {
return ErrWrongState
}
m.friends[i].State, m.friends[i].ApprovedAt = Active, now
return nil
}
return ErrFriendMissing
}
func (m *memStore) DeleteFriend(_ context.Context, id int64) error {
for i, f := range m.friends {
if f.ID == id {
m.friends = append(m.friends[:i], m.friends[i+1:]...)
return nil
}
}
return ErrFriendMissing
}
// direct is a Remote that reaches another Service in the same process, answering as the HTTP layer would.
type direct struct {
to *Service
calls int
}
func (d *direct) Pair(ctx context.Context, _ string, req PairRequest) (PairReply, error) {
d.calls++
reply, err := d.to.Pair(ctx, req)
switch {
case errors.Is(err, ErrInvalidInvite), errors.Is(err, ErrOff):
return PairReply{}, ErrRefused
case err != nil:
return PairReply{}, err
}
return reply, nil
}
type clock struct{ t time.Time }
func (c *clock) now() time.Time { return c.t }
func newServer(t *testing.T, name, address string, remote Remote) (*Service, *memStore, *clock) {
t.Helper()
st := newMemStore()
ck := &clock{t: time.Unix(1_700_000_000, 0)}
s := NewService(st, remote)
s.now = ck.now
s.host = func() string { return "host-" + name }
on, n, a := true, name, address
if _, err := s.Update(context.Background(), Patch{Enabled: &on, Name: &n, Address: &a}); err != nil {
t.Fatal(err)
}
return s, st, ck
}
func TestFederationStartsOffAndAsksNothing(t *testing.T) {
ctx := context.Background()
s := NewService(newMemStore(), nil)
s.host = func() string { return "nas" }
st, err := s.Status(ctx)
if err != nil {
t.Fatal(err)
}
if st.Enabled || st.KeyID != "" || len(st.Friends) != 0 || st.Address != "" || st.Name != "nas" {
t.Errorf("Status = %+v: federation must start off, with no key and no friend, named after the host", st)
}
if !strings.Contains(st.Sends, "never learns who listens") {
t.Errorf("Sends = %q, it must say what a friend is sent", st.Sends)
}
if _, err := s.CreateInvite(ctx); !errors.Is(err, ErrOff) {
t.Errorf("CreateInvite = %v, want ErrOff", err)
}
if _, err := s.Redeem(ctx, "sonda1.x"); !errors.Is(err, ErrOff) {
t.Errorf("Redeem = %v, want ErrOff", err)
}
if _, err := s.Pair(ctx, PairRequest{}); !errors.Is(err, ErrOff) {
t.Errorf("Pair = %v, want ErrOff", err)
}
}
func TestTheKeyIsMadeOnceOnFirstUse(t *testing.T) {
ctx := context.Background()
st := newMemStore()
s := NewService(st, nil)
on, off := true, false
got, err := s.Update(ctx, Patch{Enabled: &on})
if err != nil {
t.Fatal(err)
}
if !got.Enabled || len(got.KeyID) != 16 {
t.Fatalf("Status = %+v", got)
}
if len(st.settings.PrivateKey) != ed25519.PrivateKeySize || KeyID(st.settings.PublicKey) != got.KeyID {
t.Errorf("the stored pair does not match the key id")
}
if _, err := s.Update(ctx, Patch{Enabled: &off}); err != nil {
t.Fatal(err)
}
again, _ := s.Update(ctx, Patch{Enabled: &on})
if again.KeyID != got.KeyID {
t.Errorf("key id changed from %s to %s: turning it off and on must keep the key", got.KeyID, again.KeyID)
}
}
func TestUpdateChecksTheNameAndTheAddress(t *testing.T) {
ctx := context.Background()
s := NewService(newMemStore(), nil)
set := func(name, addr string) error {
_, err := s.Update(ctx, Patch{Name: &name, Address: &addr})
return err
}
for _, addr := range []string{
"ftp://music.example.org", "music.example.org", "https://", "https://user:pw@music.example.org",
"https://music.example.org/?a=b", "https://music.example.org/#x", "http://music.example.org",
"http://8.8.8.8", "http://" + strings.Repeat("a", 300) + ".example.org", "https://exa mple.org",
} {
if err := set("Home", addr); !errors.Is(err, ErrInvalidInput) {
t.Errorf("address %q: err = %v, want ErrInvalidInput", addr, err)
}
}
for _, name := range []string{strings.Repeat("n", 61), "line\nbreak", "tab\there"} {
if err := set(name, ""); !errors.Is(err, ErrInvalidInput) {
t.Errorf("name %q: err = %v, want ErrInvalidInput", name, err)
}
}
// https is fine anywhere. Plain http only on a network of one's own: a private or loopback address, or a local name.
for addr, want := range map[string]string{
"https://Music.Example.org/": "https://music.example.org",
"https://music.example.org/sonda/": "https://music.example.org/sonda",
"http://192.168.1.20:8080": "http://192.168.1.20:8080",
"http://10.0.0.5": "http://10.0.0.5",
"http://127.0.0.1:8081": "http://127.0.0.1:8081",
"http://[::1]:8081": "http://[::1]:8081",
"http://nas": "http://nas",
"http://nas.local:8080": "http://nas.local:8080",
"http://music.lan": "http://music.lan",
"http://router.home.arpa": "http://router.home.arpa",
"https://8.8.8.8": "https://8.8.8.8",
} {
if err := set("Home", addr); err != nil {
t.Errorf("address %q: %v", addr, err)
continue
}
if st, _ := s.Status(ctx); st.Address != want {
t.Errorf("address %q saved as %q, want %q", addr, st.Address, want)
}
}
// An empty address clears it.
if err := set(" Home ", ""); err != nil {
t.Fatal(err)
}
if st, _ := s.Status(ctx); st.Address != "" || st.Name != "Home" {
t.Errorf("Status = %+v", st)
}
}
func TestAnInviteNeedsAnAddressAndKeepsOnlyAHash(t *testing.T) {
ctx := context.Background()
st := newMemStore()
s := NewService(st, nil)
s.now = (&clock{t: time.Unix(1_700_000_000, 0)}).now
on := true
if _, err := s.Update(ctx, Patch{Enabled: &on}); err != nil {
t.Fatal(err)
}
if _, err := s.CreateInvite(ctx); !errors.Is(err, ErrNoAddress) {
t.Fatalf("CreateInvite without an address = %v, want ErrNoAddress", err)
}
addr := "https://a.example.org"
if _, err := s.Update(ctx, Patch{Address: &addr}); err != nil {
t.Fatal(err)
}
inv, err := s.CreateInvite(ctx)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(inv.Text, "sonda1.") || inv.ExpiresAt.Sub(s.now()) != 24*time.Hour {
t.Errorf("invite = %+v", inv)
}
if len(st.invites) != 1 {
t.Fatalf("stored invites = %d", len(st.invites))
}
for hash := range st.invites {
if len(hash) != 64 || strings.Contains(inv.Text, hash) {
t.Errorf("stored %q: want only the SHA-256 of the code", hash)
}
}
}
func TestTwoServersBecomeFriendsWithTwoConfirmations(t *testing.T) {
ctx := context.Background()
a, aStore, _ := newServer(t, "Anna", "https://anna.example.org", nil)
toA := &direct{to: a}
b, bStore, _ := newServer(t, "Bruno", "https://bruno.example.org", toA)
inv, err := a.CreateInvite(ctx)
if err != nil {
t.Fatal(err)
}
friend, err := b.Redeem(ctx, inv.Text)
if err != nil {
t.Fatal(err)
}
// B pasted the invite: that is its confirmation. It waits for Anna.
if friend.Name != "Anna" || friend.Address != "https://anna.example.org" || friend.State != Outgoing {
t.Errorf("B's friend = %+v", friend)
}
// A now has a request to approve, and has not agreed to anything yet.
list, _ := aStore.Friends(ctx)
if len(list) != 1 || list[0].State != Incoming || list[0].Name != "Bruno" || list[0].Address != "https://bruno.example.org" {
t.Fatalf("A's friends = %+v", list)
}
if got, _ := bStore.Friends(ctx); len(got) != 1 || got[0].State != Outgoing {
t.Errorf("B's friends = %+v", got)
}
approved, err := a.Approve(ctx, list[0].ID)
if err != nil {
t.Fatal(err)
}
if approved.State != Active || approved.ApprovedAt.IsZero() {
t.Errorf("approved = %+v", approved)
}
st, _ := a.Status(ctx)
if len(st.Friends) != 1 || st.Friends[0].State != Active {
t.Errorf("Status.Friends = %+v", st.Friends)
}
// The invite was good once.
c, _, _ := newServer(t, "Carla", "https://carla.example.org", toA)
if _, err := c.Redeem(ctx, inv.Text); !errors.Is(err, ErrRefused) {
t.Errorf("second use of the invite = %v, want ErrRefused", err)
}
}
func TestAnExpiredInviteIsRefused(t *testing.T) {
ctx := context.Background()
a, _, ack := newServer(t, "Anna", "https://anna.example.org", nil)
b, bStore, _ := newServer(t, "Bruno", "https://bruno.example.org", &direct{to: a})
inv, _ := a.CreateInvite(ctx)
ack.t = ack.t.Add(24*time.Hour + time.Second)
if _, err := b.Redeem(ctx, inv.Text); !errors.Is(err, ErrRefused) {
t.Fatalf("Redeem = %v, want ErrRefused", err)
}
if got, _ := bStore.Friends(ctx); len(got) != 0 {
t.Errorf("B kept %+v after a refusal", got)
}
}
// A server that answers with another key than the one in the invite is not the one the invite came from.
type swapKey struct{ real Remote }
func (s swapKey) Pair(ctx context.Context, addr string, req PairRequest) (PairReply, error) {
r, err := s.real.Pair(ctx, addr, req)
if err != nil {
return r, err
}
pub, _, _ := ed25519.GenerateKey(nil)
r.PublicKey = pub
return r, nil
}
func TestAKeyThatIsNotTheOneInTheInviteIsNotAFriend(t *testing.T) {
ctx := context.Background()
a, _, _ := newServer(t, "Anna", "https://anna.example.org", nil)
b, bStore, _ := newServer(t, "Bruno", "https://bruno.example.org", swapKey{&direct{to: a}})
inv, _ := a.CreateInvite(ctx)
if _, err := b.Redeem(ctx, inv.Text); !errors.Is(err, ErrKeyMismatch) {
t.Fatalf("Redeem = %v, want ErrKeyMismatch", err)
}
if got, _ := bStore.Friends(ctx); len(got) != 0 {
t.Errorf("B kept %+v", got)
}
}
func TestPairingChecksWhatItIsSent(t *testing.T) {
ctx := context.Background()
a, aStore, _ := newServer(t, "Anna", "https://anna.example.org", nil)
pub, _, _ := ed25519.GenerateKey(nil)
ownPub := aStore.settings.PublicKey
for name, req := range map[string]PairRequest{
"no name": {Code: "c", Name: "", Address: "https://b.example.org", PublicKey: pub},
"bad address": {Code: "c", Name: "B", Address: "ftp://b", PublicKey: pub},
"short key": {Code: "c", Name: "B", Address: "https://b.example.org", PublicKey: pub[:10]},
"its own key": {Code: "c", Name: "B", Address: "https://b.example.org", PublicKey: ownPub},
"no code at all": {Code: "", Name: "B", Address: "https://b.example.org", PublicKey: pub},
} {
_, err := a.Pair(ctx, req)
want := ErrInvalidInput
if name == "no code at all" {
want = ErrInvalidInvite
}
if !errors.Is(err, want) {
t.Errorf("%s: err = %v, want %v", name, err, want)
}
}
if got, _ := aStore.Friends(ctx); len(got) != 0 {
t.Errorf("A kept %+v", got)
}
}
func TestAKnownServerDoesNotBurnAnInvite(t *testing.T) {
ctx := context.Background()
a, _, _ := newServer(t, "Anna", "https://anna.example.org", nil)
b, _, _ := newServer(t, "Bruno", "https://bruno.example.org", &direct{to: a})
first, _ := a.CreateInvite(ctx)
friend, err := b.Redeem(ctx, first.Text)
if err != nil {
t.Fatal(err)
}
second, _ := a.CreateInvite(ctx)
if _, err := b.Redeem(ctx, second.Text); !errors.Is(err, ErrAlreadyFriend) {
t.Fatalf("second Redeem = %v, want ErrAlreadyFriend", err)
}
// The second invite was not used up by that attempt: once B's friend is removed on A's side it works.
list, _ := a.Status(ctx)
if err := a.Remove(ctx, list.Friends[0].ID); err != nil {
t.Fatal(err)
}
if err := b.Remove(ctx, friend.ID); err != nil {
t.Fatal(err)
}
if _, err := b.Redeem(ctx, second.Text); err != nil {
t.Errorf("Redeem after removal = %v, the invite must still be good", err)
}
}
func TestTooManyAttemptsAreSlowedDown(t *testing.T) {
ctx := context.Background()
a, _, ack := newServer(t, "Anna", "https://anna.example.org", nil)
pub, _, _ := ed25519.GenerateKey(nil)
req := PairRequest{Code: "guess", Name: "B", Address: "https://b.example.org", PublicKey: pub}
for i := 0; i < 10; i++ {
if _, err := a.Pair(ctx, req); !errors.Is(err, ErrInvalidInvite) {
t.Fatalf("attempt %d = %v, want ErrInvalidInvite", i, err)
}
}
if _, err := a.Pair(ctx, req); !errors.Is(err, ErrTooMany) {
t.Fatalf("attempt 11 = %v, want ErrTooMany", err)
}
ack.t = ack.t.Add(61 * time.Second)
if _, err := a.Pair(ctx, req); !errors.Is(err, ErrInvalidInvite) {
t.Errorf("after a minute = %v, want ErrInvalidInvite", err)
}
}
func TestPairingStopsWhenFederationIsTurnedOff(t *testing.T) {
ctx := context.Background()
a, _, _ := newServer(t, "Anna", "https://anna.example.org", nil)
b, _, _ := newServer(t, "Bruno", "https://bruno.example.org", &direct{to: a})
inv, _ := a.CreateInvite(ctx)
off := false
if _, err := a.Update(ctx, Patch{Enabled: &off}); err != nil {
t.Fatal(err)
}
if _, err := b.Redeem(ctx, inv.Text); !errors.Is(err, ErrRefused) {
t.Errorf("Redeem = %v, want ErrRefused while the other side is off", err)
}
}
func TestRedeemRefusesWhatIsNotAnInvite(t *testing.T) {
ctx := context.Background()
remote := &direct{}
b, _, _ := newServer(t, "Bruno", "https://bruno.example.org", remote)
for _, text := range []string{
"", "hello", "sonda1.", "sonda1.!!!", "sonda1.e30", // {}
"sonda2.e30",
} {
if _, err := b.Redeem(ctx, text); !errors.Is(err, ErrInvalidInvite) {
t.Errorf("Redeem(%q) = %v, want ErrInvalidInvite", text, err)
}
}
// Its own invite is no use to it.
own, _ := b.CreateInvite(ctx)
if _, err := b.Redeem(ctx, own.Text); !errors.Is(err, ErrInvalidInput) {
t.Errorf("Redeem(own invite) = %v, want ErrInvalidInput", err)
}
if remote.calls != 0 {
t.Errorf("the other server was called %d times for invites that could not be used", remote.calls)
}
}
func TestRedeemNeedsAnAddressOfItsOwn(t *testing.T) {
ctx := context.Background()
a, _, _ := newServer(t, "Anna", "https://anna.example.org", nil)
inv, _ := a.CreateInvite(ctx)
b := NewService(newMemStore(), &direct{to: a})
on := true
if _, err := b.Update(ctx, Patch{Enabled: &on}); err != nil {
t.Fatal(err)
}
if _, err := b.Redeem(ctx, inv.Text); !errors.Is(err, ErrNoAddress) {
t.Errorf("Redeem = %v, want ErrNoAddress: the friend must know where to reach it", err)
}
}
func TestApproveAndRemove(t *testing.T) {
ctx := context.Background()
a, _, _ := newServer(t, "Anna", "https://anna.example.org", nil)
b, _, _ := newServer(t, "Bruno", "https://bruno.example.org", &direct{to: a})
inv, _ := a.CreateInvite(ctx)
mine, err := b.Redeem(ctx, inv.Text)
if err != nil {
t.Fatal(err)
}
// B waits for A: it has nothing to approve.
if _, err := b.Approve(ctx, mine.ID); !errors.Is(err, ErrWrongState) {
t.Errorf("approve an outgoing friend = %v, want ErrWrongState", err)
}
if _, err := a.Approve(ctx, 999); !errors.Is(err, ErrFriendMissing) {
t.Errorf("approve a missing friend = %v, want ErrFriendMissing", err)
}
if err := a.Remove(ctx, 999); !errors.Is(err, ErrFriendMissing) {
t.Errorf("remove a missing friend = %v, want ErrFriendMissing", err)
}
if err := b.Remove(ctx, mine.ID); err != nil {
t.Errorf("a request one made can be withdrawn: %v", err)
}
}

View file

@ -0,0 +1,245 @@
package storage
import (
"context"
"crypto/ed25519"
"database/sql"
"encoding/base64"
"errors"
"fmt"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/federation"
)
// Keys are stored as base64url text; an empty string means "no key yet".
var keyEnc = base64.RawURLEncoding
type federationSettingsRow struct {
bun.BaseModel `bun:"table:federation_settings"`
ID int `bun:"id,pk"`
Enabled bool `bun:"enabled,notnull"`
Name string `bun:"name,notnull"`
Address string `bun:"address,notnull"`
PublicKey string `bun:"public_key,notnull"`
PrivateKey string `bun:"private_key,notnull"`
UpdatedAt int64 `bun:"updated_at,notnull"`
}
type federationInviteRow struct {
bun.BaseModel `bun:"table:federation_invites"`
ID int64 `bun:"id,pk,autoincrement"`
CodeHash string `bun:"code_hash,notnull"`
CreatedAt int64 `bun:"created_at,notnull"`
ExpiresAt int64 `bun:"expires_at,notnull"`
UsedAt *int64 `bun:"used_at"`
}
type federationFriendRow struct {
bun.BaseModel `bun:"table:federation_friends"`
ID int64 `bun:"id,pk,autoincrement"`
Name string `bun:"name,notnull"`
Address string `bun:"address,notnull"`
PublicKey string `bun:"public_key,notnull"`
State string `bun:"state,notnull"`
CreatedAt int64 `bun:"created_at,notnull"`
ApprovedAt *int64 `bun:"approved_at"`
}
func (r federationFriendRow) toFriend() (federation.Friend, error) {
key, err := keyEnc.DecodeString(r.PublicKey)
if err != nil || len(key) != ed25519.PublicKeySize {
return federation.Friend{}, fmt.Errorf("friend %d has a damaged key", r.ID)
}
f := federation.Friend{
ID: r.ID, Name: r.Name, Address: r.Address, PublicKey: key,
State: federation.State(r.State), CreatedAt: time.Unix(r.CreatedAt, 0),
}
if r.ApprovedAt != nil {
f.ApprovedAt = time.Unix(*r.ApprovedAt, 0)
}
return f, nil
}
// FederationRepo stores the keys, invites and friends of federation. It implements federation.Store.
type FederationRepo struct{ db *bun.DB }
// NewFederationRepo builds a FederationRepo.
func NewFederationRepo(db *bun.DB) *FederationRepo { return &FederationRepo{db: db} }
// Settings returns the stored settings, with found=false before the first save.
func (r *FederationRepo) Settings(ctx context.Context) (federation.Settings, bool, error) {
var row federationSettingsRow
err := r.db.NewSelect().Model(&row).Where("id = 1").Limit(1).Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
return federation.Settings{}, false, nil
}
if err != nil {
return federation.Settings{}, false, fmt.Errorf("select federation settings: %w", err)
}
s := federation.Settings{Enabled: row.Enabled, Name: row.Name, Address: row.Address, UpdatedAt: time.Unix(row.UpdatedAt, 0)}
if row.PublicKey != "" || row.PrivateKey != "" {
pub, perr := keyEnc.DecodeString(row.PublicKey)
priv, serr := keyEnc.DecodeString(row.PrivateKey)
if perr != nil || serr != nil || len(pub) != ed25519.PublicKeySize || len(priv) != ed25519.PrivateKeySize {
return federation.Settings{}, false, errors.New("the federation key pair is damaged")
}
s.PublicKey, s.PrivateKey = pub, priv
}
return s, true, nil
}
// SaveSettings inserts or replaces the one row of settings.
func (r *FederationRepo) SaveSettings(ctx context.Context, s federation.Settings) error {
row := &federationSettingsRow{
ID: 1, Enabled: s.Enabled, Name: s.Name, Address: s.Address, UpdatedAt: s.UpdatedAt.Unix(),
}
if s.PublicKey != nil {
row.PublicKey = keyEnc.EncodeToString(s.PublicKey)
}
if s.PrivateKey != nil {
row.PrivateKey = keyEnc.EncodeToString(s.PrivateKey)
}
_, err := r.db.NewInsert().Model(row).
On("CONFLICT (id) DO UPDATE").
Set("enabled = EXCLUDED.enabled").
Set("name = EXCLUDED.name").
Set("address = EXCLUDED.address").
Set("public_key = EXCLUDED.public_key").
Set("private_key = EXCLUDED.private_key").
Set("updated_at = EXCLUDED.updated_at").
Exec(ctx)
if err != nil {
return fmt.Errorf("save federation settings: %w", err)
}
return nil
}
// AddInvite stores the hash of a new invite code.
func (r *FederationRepo) AddInvite(ctx context.Context, codeHash string, created, expires time.Time) error {
row := &federationInviteRow{CodeHash: codeHash, CreatedAt: created.Unix(), ExpiresAt: expires.Unix()}
if _, err := r.db.NewInsert().Model(row).Exec(ctx); err != nil {
return fmt.Errorf("insert federation invite: %w", err)
}
return nil
}
// UseInvite marks an unused, unexpired invite as used. One statement does the check and the marking, so two
// requests with the same code cannot both succeed.
func (r *FederationRepo) UseInvite(ctx context.Context, codeHash string, now time.Time) (bool, error) {
res, err := r.db.NewUpdate().Model((*federationInviteRow)(nil)).
Set("used_at = ?", now.Unix()).
Where("code_hash = ?", codeHash).
Where("used_at IS NULL").
Where("expires_at > ?", now.Unix()).
Exec(ctx)
if err != nil {
return false, fmt.Errorf("use federation invite: %w", err)
}
n, err := res.RowsAffected()
if err != nil {
return false, fmt.Errorf("use federation invite: %w", err)
}
return n == 1, nil
}
// Friends lists every friend, oldest first.
func (r *FederationRepo) Friends(ctx context.Context) ([]federation.Friend, error) {
var rows []federationFriendRow
if err := r.db.NewSelect().Model(&rows).OrderExpr("created_at, id").Scan(ctx); err != nil {
return nil, fmt.Errorf("list federation friends: %w", err)
}
out := make([]federation.Friend, len(rows))
for i, row := range rows {
f, err := row.toFriend()
if err != nil {
return nil, err
}
out[i] = f
}
return out, nil
}
// Friend returns one friend.
func (r *FederationRepo) Friend(ctx context.Context, id int64) (federation.Friend, error) {
var row federationFriendRow
err := r.db.NewSelect().Model(&row).Where("id = ?", id).Limit(1).Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
return federation.Friend{}, federation.ErrFriendMissing
}
if err != nil {
return federation.Friend{}, fmt.Errorf("select federation friend: %w", err)
}
return row.toFriend()
}
// FriendByKey finds the friend that has a public key.
func (r *FederationRepo) FriendByKey(ctx context.Context, key ed25519.PublicKey) (federation.Friend, bool, error) {
var row federationFriendRow
err := r.db.NewSelect().Model(&row).Where("public_key = ?", keyEnc.EncodeToString(key)).Limit(1).Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
return federation.Friend{}, false, nil
}
if err != nil {
return federation.Friend{}, false, fmt.Errorf("select federation friend by key: %w", err)
}
f, err := row.toFriend()
return f, err == nil, err
}
// AddFriend stores a friend; a key that is already a friend's is ErrAlreadyFriend.
func (r *FederationRepo) AddFriend(ctx context.Context, f federation.Friend) (federation.Friend, error) {
row := &federationFriendRow{
Name: f.Name, Address: f.Address, PublicKey: keyEnc.EncodeToString(f.PublicKey),
State: string(f.State), CreatedAt: f.CreatedAt.Unix(),
}
if !f.ApprovedAt.IsZero() {
at := f.ApprovedAt.Unix()
row.ApprovedAt = &at
}
if _, err := r.db.NewInsert().Model(row).Exec(ctx); err != nil {
if isUniqueViolation(err) {
return federation.Friend{}, federation.ErrAlreadyFriend
}
return federation.Friend{}, fmt.Errorf("insert federation friend: %w", err)
}
f.ID = row.ID
return f, nil
}
// Approve turns an incoming friend into an active one.
func (r *FederationRepo) Approve(ctx context.Context, id int64, now time.Time) error {
res, err := r.db.NewUpdate().Model((*federationFriendRow)(nil)).
Set("state = ?", string(federation.Active)).
Set("approved_at = ?", now.Unix()).
Where("id = ?", id).
Where("state = ?", string(federation.Incoming)).
Exec(ctx)
if err != nil {
return fmt.Errorf("approve federation friend: %w", err)
}
if n, _ := res.RowsAffected(); n == 1 {
return nil
}
if _, err := r.Friend(ctx, id); err != nil {
return err
}
return federation.ErrWrongState
}
// DeleteFriend removes a friend: declining a request and revoking a friendship are the same thing.
func (r *FederationRepo) DeleteFriend(ctx context.Context, id int64) error {
res, err := r.db.NewDelete().Model((*federationFriendRow)(nil)).Where("id = ?", id).Exec(ctx)
if err != nil {
return fmt.Errorf("delete federation friend: %w", err)
}
if n, _ := res.RowsAffected(); n == 0 {
return federation.ErrFriendMissing
}
return nil
}

View file

@ -0,0 +1,180 @@
package storage
import (
"bytes"
"context"
"crypto/ed25519"
"errors"
"testing"
"time"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/internal/federation"
)
func testKey(t *testing.T, seed byte) ed25519.PublicKey {
t.Helper()
pub, _, err := ed25519.GenerateKey(bytes.NewReader(bytes.Repeat([]byte{seed}, 64)))
if err != nil {
t.Fatal(err)
}
return pub
}
func TestFederationSettings(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewFederationRepo(db)
if _, found, err := repo.Settings(ctx); err != nil || found {
t.Fatalf("Settings before any save = found %v, %v", found, err)
}
pub, priv, err := ed25519.GenerateKey(bytes.NewReader(bytes.Repeat([]byte{7}, 64)))
if err != nil {
t.Fatal(err)
}
want := federation.Settings{
Enabled: true, Name: "Home", Address: "https://music.example.org",
PublicKey: pub, PrivateKey: priv, UpdatedAt: time.Unix(1_700_000_000, 0),
}
if err := repo.SaveSettings(ctx, want); err != nil {
t.Fatal(err)
}
// A second save replaces the row, it does not add one.
want.Name = "Away"
want.Enabled = false
if err := repo.SaveSettings(ctx, want); err != nil {
t.Fatal(err)
}
got, found, err := repo.Settings(ctx)
if err != nil || !found {
t.Fatalf("Settings = found %v, %v", found, err)
}
if got.Enabled || got.Name != "Away" || got.Address != want.Address ||
!bytes.Equal(got.PublicKey, pub) || !bytes.Equal(got.PrivateKey, priv) || !got.UpdatedAt.Equal(want.UpdatedAt) {
t.Errorf("Settings = %+v, want %+v", got, want)
}
})
}
func TestFederationSettingsWithoutKeys(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewFederationRepo(db)
if err := repo.SaveSettings(ctx, federation.Settings{Name: "x", UpdatedAt: time.Unix(5, 0)}); err != nil {
t.Fatal(err)
}
got, found, err := repo.Settings(ctx)
if err != nil || !found || got.PublicKey != nil || got.PrivateKey != nil {
t.Fatalf("Settings = %+v, found %v, %v: a server with no keys yet must read back with none", got, found, err)
}
})
}
func TestFederationInvitesAreSingleUseAndExpire(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewFederationRepo(db)
now := time.Unix(1_700_000_000, 0)
if err := repo.AddInvite(ctx, "hash-a", now, now.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if ok, err := repo.UseInvite(ctx, "no-such", now); err != nil || ok {
t.Errorf("unknown invite: ok = %v, %v", ok, err)
}
if ok, err := repo.UseInvite(ctx, "hash-a", now.Add(2*time.Hour)); err != nil || ok {
t.Errorf("expired invite: ok = %v, %v", ok, err)
}
if ok, err := repo.UseInvite(ctx, "hash-a", now.Add(time.Minute)); err != nil || !ok {
t.Errorf("first use: ok = %v, %v", ok, err)
}
if ok, err := repo.UseInvite(ctx, "hash-a", now.Add(time.Minute)); err != nil || ok {
t.Errorf("second use: ok = %v, %v, an invite is good once", ok, err)
}
})
}
func TestFederationFriends(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
ctx := context.Background()
repo := NewFederationRepo(db)
now := time.Unix(1_700_000_000, 0)
if list, err := repo.Friends(ctx); err != nil || len(list) != 0 {
t.Fatalf("Friends = %v, %v", list, err)
}
a, err := repo.AddFriend(ctx, federation.Friend{
Name: "Anna", Address: "https://anna.example.org", PublicKey: testKey(t, 1), State: federation.Incoming, CreatedAt: now,
})
if err != nil {
t.Fatal(err)
}
if a.ID <= 0 {
t.Fatalf("AddFriend returned %+v", a)
}
if _, err := repo.AddFriend(ctx, federation.Friend{
Name: "Again", Address: "https://x.example.org", PublicKey: testKey(t, 1), State: federation.Outgoing, CreatedAt: now,
}); !errors.Is(err, federation.ErrAlreadyFriend) {
t.Errorf("same key twice: err = %v, want ErrAlreadyFriend", err)
}
b, err := repo.AddFriend(ctx, federation.Friend{
Name: "Bruno", Address: "https://bruno.example.org", PublicKey: testKey(t, 2), State: federation.Outgoing, CreatedAt: now.Add(time.Second),
})
if err != nil {
t.Fatal(err)
}
list, err := repo.Friends(ctx)
if err != nil || len(list) != 2 || list[0].ID != a.ID || list[1].ID != b.ID {
t.Fatalf("Friends = %+v, %v, want Anna then Bruno", list, err)
}
got, err := repo.Friend(ctx, a.ID)
if err != nil || got.Name != "Anna" || got.State != federation.Incoming || !got.ApprovedAt.IsZero() ||
!bytes.Equal(got.PublicKey, testKey(t, 1)) || !got.CreatedAt.Equal(now) {
t.Errorf("Friend = %+v, %v", got, err)
}
byKey, found, err := repo.FriendByKey(ctx, testKey(t, 2))
if err != nil || !found || byKey.ID != b.ID {
t.Errorf("FriendByKey = %+v, %v, %v", byKey, found, err)
}
if _, found, err := repo.FriendByKey(ctx, testKey(t, 9)); err != nil || found {
t.Errorf("FriendByKey unknown = %v, %v", found, err)
}
// Approving moves an incoming friend to active; the other one is not waiting for me.
if err := repo.Approve(ctx, a.ID, now.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if got, _ := repo.Friend(ctx, a.ID); got.State != federation.Active || !got.ApprovedAt.Equal(now.Add(time.Hour)) {
t.Errorf("after Approve: %+v", got)
}
if err := repo.Approve(ctx, a.ID, now); !errors.Is(err, federation.ErrWrongState) {
t.Errorf("approve an active friend: %v, want ErrWrongState", err)
}
if err := repo.Approve(ctx, b.ID, now); !errors.Is(err, federation.ErrWrongState) {
t.Errorf("approve an outgoing friend: %v, want ErrWrongState", err)
}
if err := repo.Approve(ctx, 999, now); !errors.Is(err, federation.ErrFriendMissing) {
t.Errorf("approve missing: %v, want ErrFriendMissing", err)
}
if err := repo.DeleteFriend(ctx, a.ID); err != nil {
t.Fatal(err)
}
if _, err := repo.Friend(ctx, a.ID); !errors.Is(err, federation.ErrFriendMissing) {
t.Errorf("Friend after delete: %v", err)
}
if err := repo.DeleteFriend(ctx, a.ID); !errors.Is(err, federation.ErrFriendMissing) {
t.Errorf("delete twice: %v, want ErrFriendMissing", err)
}
// The key is free again: the same server can be invited anew.
if _, err := repo.AddFriend(ctx, federation.Friend{
Name: "Anna", Address: "https://anna.example.org", PublicKey: testKey(t, 1), State: federation.Incoming, CreatedAt: now,
}); err != nil {
t.Errorf("add after delete: %v", err)
}
})
}

View file

@ -147,7 +147,7 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatalf("second Migrate: %v", err)
}
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions"} {
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions", "federation_settings", "federation_invites", "federation_friends"} {
if !tableExists(t, db, driver, table) {
t.Errorf("table %s missing", table)
}
@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
t.Fatal(err)
}
if applied != 22 {
t.Errorf("applied migrations = %d, want 22", applied)
if applied != 23 {
t.Errorf("applied migrations = %d, want 23", applied)
}
})
}

View file

@ -0,0 +1,29 @@
-- Federation between Sonda servers (see internal/federation). PostgreSQL flavour of sqlite/0023_federation.sql.
CREATE TABLE federation_settings (
id INTEGER PRIMARY KEY CHECK (id = 1),
enabled BOOLEAN NOT NULL DEFAULT FALSE,
name TEXT NOT NULL DEFAULT '',
address TEXT NOT NULL DEFAULT '',
public_key TEXT NOT NULL DEFAULT '',
private_key TEXT NOT NULL DEFAULT '',
updated_at BIGINT NOT NULL DEFAULT 0
);
CREATE TABLE federation_invites (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
code_hash TEXT NOT NULL UNIQUE,
created_at BIGINT NOT NULL,
expires_at BIGINT NOT NULL,
used_at BIGINT
);
CREATE TABLE federation_friends (
id BIGINT GENERATED BY DEFAULT AS IDENTITY 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 BIGINT NOT NULL,
approved_at BIGINT
);

View file

@ -0,0 +1,33 @@
-- 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
);

View file

@ -0,0 +1,62 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { api } from './client'
import type { components } from './schema'
export type Federation = components['schemas']['Federation']
export type FederationFriend = components['schemas']['FederationFriend']
export type FederationInvite = components['schemas']['FederationInvite']
/** What `PUT /federation` takes: any of these, and what is left out is kept. */
export interface FederationChanges {
enabled?: boolean
name?: string
address?: string
}
const FEDERATION = ['federation']
/** The federation settings of this server, with its friends. The private key is never in the answer. */
export function useFederation() {
return useQuery({ queryKey: FEDERATION, queryFn: () => api.get<Federation>('/federation') })
}
/** Turns federation on or off, names this server, gives its address. The first time it is on, the server makes its key. */
export function useUpdateFederation() {
const client = useQueryClient()
return useMutation({
mutationFn: (changes: FederationChanges) => api.put<Federation>('/federation', changes),
onSuccess: () => client.invalidateQueries({ queryKey: FEDERATION }),
})
}
/** Makes an invite for another server: one piece of text, good once, for 24 hours. */
export function useCreateInvite() {
return useMutation({ mutationFn: () => api.post<FederationInvite>('/federation/invites') })
}
/** Uses an invite made by another server: that server is asked, and the friend waits for its approval. */
export function useUseInvite() {
const client = useQueryClient()
return useMutation({
mutationFn: (invite: string) => api.post<FederationFriend>('/federation/friends', { invite }),
onSuccess: () => client.invalidateQueries({ queryKey: FEDERATION }),
})
}
/** The second confirmation: a server that used an invite of this one becomes a friend. */
export function useApproveFriend() {
const client = useQueryClient()
return useMutation({
mutationFn: (id: number) => api.post<FederationFriend>(`/federation/friends/${id}/approve`),
onSuccess: () => client.invalidateQueries({ queryKey: FEDERATION }),
})
}
/** Declines a request, withdraws one, or ends a friendship. The other server is not told. */
export function useRemoveFriend() {
const client = useQueryClient()
return useMutation({
mutationFn: (id: number) => api.del<void>(`/federation/friends/${id}`),
onSuccess: () => client.invalidateQueries({ queryKey: FEDERATION }),
})
}

View file

@ -2018,6 +2018,151 @@ export interface paths {
patch?: never;
trace?: never;
};
"/federation": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* Federation, its key and its friends
* @description Admin only. Federation is off until an admin turns it on. `sends` says in plain words what a friend's server is
* sent. `key_id` names the key of this server (the first 16 hex characters of the SHA-256 of its public key); it is
* empty until federation was first turned on. The private key is never returned. `name` is the one saved, or the
* host name.
*/
get: operations["getFederation"];
/**
* Turn federation on or off, name this server, give its address
* @description Admin only. Fields you leave out are kept. The key pair is made the first time `enabled` becomes true and is kept
* after that, so turning it off and on again does not change the key. `address` is where friends reach this server:
* `https://`, or `http://` only on a network of your own (a private, loopback or link-local address, or a name that
* is local: no dot, or ending `.local`, `.lan`, `.home.arpa`); without user, query or fragment. A closing slash is
* dropped; an empty address clears it. An empty `name` goes back to the host name (at most 60 characters).
*/
put: operations["updateFederation"];
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/federation/invites": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get?: never;
put?: never;
/**
* Make an invite for another server
* @description Admin only. The invite is one piece of text to give to the other admin, who pastes it into
* `POST /federation/friends`. It works once and expires in 24 hours; only a hash of its code is kept here. It needs
* federation on (`federation_off`) and an address (`address_required`).
*/
post: operations["createFederationInvite"];
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/federation/friends": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get?: never;
put?: never;
/**
* Use an invite made by another server
* @description Admin only. Asks the server the invite came from to accept this one, checks that the key it answers with is the one
* the invite names, and keeps it as a friend in state `outgoing`: it waits for the other admin to approve. This is
* the only request federation sends at an admin's command, and it sends this server's name, address and public key
* and the code of the invite. It needs federation on and an address, like making an invite.
*/
post: operations["useFederationInvite"];
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/federation/friends/{id}": {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
get?: never;
put?: never;
post?: never;
/**
* Decline, withdraw or end a friendship
* @description Admin only. Removes the friend of any state. The other server is not told: its requests are no longer answered.
* The same server can be invited again afterwards.
*/
delete: operations["removeFederationFriend"];
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/federation/friends/{id}/approve": {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
get?: never;
put?: never;
/**
* Approve a server that used an invite of this one
* @description Admin only. The second confirmation: a friend in state `incoming` becomes `active`. A friend in any other state
* is a `409` (`not_waiting`).
*/
post: operations["approveFederationFriend"];
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/peer/pair": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get?: never;
put?: never;
/**
* Pair with this server (called by another Sonda server)
* @description Not for people: another Sonda server calls this when its admin pastes an invite made here. There is no login: the
* code of the invite is the proof, and it works once. The other server is kept as a friend in state `incoming`
* until an admin here approves it. While federation is off, or has no address, the route answers `404 not_found`,
* as a missing route does. At most 10 attempts a minute, from anyone.
*/
post: operations["peerPair"];
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
}
export type webhooks = Record<string, never>;
export interface components {
@ -3051,6 +3196,64 @@ export interface components {
limit: number;
offset: number;
};
Federation: {
/** @description Whether this server may pair with others. Off by default. */
enabled: boolean;
/** @description How friends see this server. */
name: string;
/** @description Where friends reach this server; empty until set. */
address: string;
/** @description Names this server's key; empty until federation was first turned on. Compare it with the one the other admin sees. */
key_id: string;
/** @description What a friend's server is sent, and what it never learns. */
sends: string;
friends: components["schemas"]["FederationFriend"][];
};
FederationUpdate: {
enabled?: boolean;
/** @description At most 60 characters, no control characters. Empty goes back to the host name. */
name?: string;
address?: string;
} | unknown | unknown | unknown;
FederationInvite: {
/** @description One piece of text to give to the other admin. Starts with `sonda1.`. */
invite: string;
/** Format: date-time */
expires_at: string;
};
FederationUseInvite: {
invite: string;
};
FederationFriend: {
id: number;
name: string;
address: string;
/** @description Names the friend's key. */
key_id: string;
/**
* @description `incoming`: it used an invite of this server and waits for approval here. `outgoing`: this server used its invite
* and waits for it. `active`: both agreed.
* @enum {string}
*/
state: "incoming" | "outgoing" | "active";
/** Format: date-time */
created_at: string;
/** Format: date-time */
approved_at: string | null;
};
PeerPairRequest: {
code: string;
name: string;
address: string;
/** @description The Ed25519 public key, 32 bytes, base64url without padding. */
public_key: string;
};
PeerPairReply: {
name: string;
address: string;
/** @description The Ed25519 public key, 32 bytes, base64url without padding. */
public_key: string;
};
};
responses: {
/** @description The request is malformed or a value is out of range (`invalid_input`). */
@ -6550,4 +6753,291 @@ export interface operations {
};
};
};
getFederation: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description The settings and the friends, oldest first. */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Federation"];
};
};
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
};
};
updateFederation: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["FederationUpdate"];
};
};
responses: {
/** @description The settings as they are now. */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Federation"];
};
};
400: components["responses"]["InvalidInput"];
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
};
};
createFederationInvite: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description The invite. */
201: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["FederationInvite"];
};
};
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
/** @description Federation is off (`federation_off`) or this server has no address yet (`address_required`). */
409: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
};
};
useFederationInvite: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["FederationUseInvite"];
};
};
responses: {
/** @description The new friend, waiting for approval. */
201: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["FederationFriend"];
};
};
/** @description Malformed body (`invalid_input`) or not an invite of a Sonda server (`invalid_invite`). */
400: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
/** @description Federation is off (`federation_off`), no address (`address_required`) or the server is already a friend (`already_friends`). */
409: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description The other server turned the invite down: used, expired, or federation off there (`invite_refused`). */
422: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description The other server asked to slow down (`too_many_requests`). */
429: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description The other server could not be reached or did not answer as a Sonda server (`friend_unreachable`), or its key is not the one in the invite (`key_mismatch`). */
502: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
};
};
removeFederationFriend: {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description Removed. */
204: {
headers: {
[name: string]: unknown;
};
content?: never;
};
400: components["responses"]["InvalidInput"];
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
/** @description No such friend (`friend_not_found`). */
404: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
};
};
approveFederationFriend: {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description The friend, now active. */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["FederationFriend"];
};
};
400: components["responses"]["InvalidInput"];
401: components["responses"]["Unauthorized"];
403: components["responses"]["AdminRequired"];
/** @description No such friend (`friend_not_found`). */
404: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description The friend is not waiting for approval (`not_waiting`). */
409: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
};
};
peerPair: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["PeerPairRequest"];
};
};
responses: {
/** @description This server's name, address and public key. */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["PeerPairReply"];
};
};
400: components["responses"]["InvalidInput"];
/** @description The code is unknown, used or expired (`invalid_invite`). */
403: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description Federation is off here (`not_found`). */
404: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description This key is already a friend (`already_friends`). */
409: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
/** @description Too many attempts (`too_many_requests`). */
429: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
};
};
}

Some files were not shown because too many files have changed in this diff Show more