A friend's server can now read what an admin chose to share.
- A library has a shared flag, off by default (migration 0024), set with
PATCH /libraries/{id}. Changing it never waits for a scan.
- Requests between friends are signed with each server's Ed25519 key: the
method, the path and query from /api/v1/peer on, a date, a nonce and the
hash of the empty body. A date more than two minutes off, a nonce used
before, an unknown or removed key and a bad signature all get the same
401. A friend still waiting for approval gets 403, and while federation is
off the routes are missing.
- Routes under /api/v1/peer: info, artists, albums, tracks, covers and
streaming with Range. The catalogue, the counts and the format come from
the shared libraries only, and the file served is the one in a shared
library. No path, library id, listen, taste, user or provider data is part
of an answer.
- A friend never makes this server build a cover, read an audio file or ask
a provider: only covers already built from the audio files are served.
- Per-friend limits, set by the admin: parallel streams (default 3) and KiB
a second (default 4096, 0 for none), shared by all the streams of a friend.
- A log of the last requests friends made (route, status, bytes; never the
query), kept for 7 days, in GET /federation/requests.
- POST /federation/friends/{id}/check lets the side that pasted an invite
find out that the other admin approved; a signed call from a friend this
server is waiting for makes it active too.
- Admin: a Shared with friends switch on each library, which asks first, and
limits, recent requests and a Check button in the Federation section, in
English and Italian.
- OpenAPI paths and schemas, contract test, two real servers checked end to
end with tagged audio files.
50 lines
1.8 KiB
Go
50 lines
1.8 KiB
Go
package federation
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// The headers of a request from a friend.
|
|
const (
|
|
HeaderKeyID = "Sonda-Key-Id"
|
|
HeaderDate = "Sonda-Date"
|
|
HeaderNonce = "Sonda-Nonce"
|
|
HeaderSignature = "Sonda-Signature"
|
|
)
|
|
|
|
// Headers are what a signed request adds, besides the key id.
|
|
type Headers struct{ Date, Nonce, Signature string }
|
|
|
|
// Signed is what a server read of a request, to find out whether a friend made it.
|
|
type Signed struct {
|
|
KeyID, Date, Nonce, Signature string
|
|
Method string
|
|
URI string // the path from /api/v1/peer on, and the query, as sent
|
|
}
|
|
|
|
// The body of a request from a friend is always empty (they only read), and its hash is part of what is signed.
|
|
var emptyBodyHash = func() string { sum := sha256.Sum256(nil); return hex.EncodeToString(sum[:]) }()
|
|
|
|
func signingString(method, uri, date, nonce string) []byte {
|
|
return []byte(method + "\n" + uri + "\n" + date + "\n" + nonce + "\n" + emptyBodyHash)
|
|
}
|
|
|
|
// Sign signs a request: method, path and query, date, nonce and the hash of the (empty) body.
|
|
func Sign(priv ed25519.PrivateKey, method, uri string, now time.Time, nonce string) Headers {
|
|
date := strconv.FormatInt(now.Unix(), 10)
|
|
sig := ed25519.Sign(priv, signingString(method, uri, date, nonce))
|
|
return Headers{Date: date, Nonce: nonce, Signature: base64.RawURLEncoding.EncodeToString(sig)}
|
|
}
|
|
|
|
func verify(pub ed25519.PublicKey, method, uri, date, nonce, signature string) bool {
|
|
sig, err := base64.RawURLEncoding.DecodeString(signature)
|
|
if err != nil || len(sig) != ed25519.SignatureSize || len(pub) != ed25519.PublicKeySize {
|
|
return false
|
|
}
|
|
return ed25519.Verify(pub, signingString(method, uri, date, nonce), sig)
|
|
}
|