mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-10-12 05:37:39 +00:00
Part 1 of 3 for #2173 (companion linking). This PR adds only the `users.db` side: the schema and store methods the server (part 2) and ingestor (part 3) build on. No route, no UI and no behaviour change for a running server. ## Changes in this PR - **Design spec** `docs/specs/2026-10-08-companion-linking-design.md`, including the optional linked-only ingest. - **Schema v6** (`internal/users/schema.go`): - `sessions` gains `kind` (`'web'` or `'device'`), `label` and `scopes` (`''` = full, for web sessions). - `companion_links (pubkey PRIMARY KEY, user_id, name, linked_at)`, cascading on user delete. - `link_challenges`: single-use challenges bound to a user and a pubkey, with an expiry. - **Device sessions** (`sessions.go`): create a device session with a label and scopes, list sessions with kind and label. - **Challenges** (`challenges.go`): issue and consume a challenge exactly once; consumed, expired, or bound to another user or pubkey all fail. - **Companion links** (`companions.go`): link, list, unlink. Linking a pubkey that belongs to another user moves it and reports the previous owner (transfer detection), so the server can audit and mail. - **Janitor**: the existing janitor also prunes expired challenges (`cmd/server/auth_service.go`, 3 lines). ## Upgrade note The store migrates `users.db` to v6 on start. A binary that only knows v5 refuses to open a v6 file (`users: database schema version 6 is newer than this binary supports (5)`), so rolling back after this lands needs the pre-upgrade `users.db` backup. ## Verification - `internal/users`: new tests for the v6 migration from v5, device sessions, challenges (single use, expiry, binding) and companion links (transfer, cascade on user delete). `go vet` and `go test ./...` green. - `cmd/server` full suite green against the v6 store. ## Not in this PR - Routes, bearer auth and the link handshake: part 2. - Ingestor filter, account page and "My coverage": part 3. --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
package users
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNormalizePubkey(t *testing.T) {
|
|
pk := strings.Repeat("ab", 32)
|
|
if got, err := NormalizePubkey(" " + strings.ToUpper(pk) + " "); err != nil || got != pk {
|
|
t.Fatalf("NormalizePubkey = %q, %v", got, err)
|
|
}
|
|
for _, bad := range []string{"", pk[:62], pk + "00", strings.Repeat("zz", 32)} {
|
|
if _, err := NormalizePubkey(bad); !errors.Is(err, ErrBadPubkey) {
|
|
t.Fatalf("NormalizePubkey(%q) err = %v", bad, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLinkChallengeLifecycle(t *testing.T) {
|
|
st, clk := newTestStore(t)
|
|
a := mustCreate(t, st, "a@example.org", "A")
|
|
pk := strings.Repeat("ab", 32)
|
|
ch, exp, err := st.CreateLinkChallenge(a.ID, strings.ToUpper(pk))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := hex.DecodeString(ch)
|
|
if err != nil || len(raw) != 32 || ch != strings.ToLower(ch) || !exp.Equal(clk.Now().Add(LinkChallengeTTL)) {
|
|
t.Fatalf("CreateLinkChallenge = %q, %v, %v", ch, exp, err)
|
|
}
|
|
var n int
|
|
if err := st.db.QueryRow(`SELECT COUNT(*) FROM link_challenges WHERE challenge_hash = ? AND pubkey = ? AND user_id = ?`,
|
|
challengeHash(raw), pk, a.ID).Scan(&n); err != nil || n != 1 {
|
|
t.Fatalf("stored challenge rows = %d, %v (want SHA-256 of the raw bytes)", n, err)
|
|
}
|
|
if _, _, err := st.CreateLinkChallenge(a.ID, "nothex"); !errors.Is(err, ErrBadPubkey) {
|
|
t.Fatalf("bad pubkey err = %v", err)
|
|
}
|
|
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, ch); err != nil {
|
|
t.Fatalf("first consume: %v", err)
|
|
}
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, ch); !errors.Is(err, ErrChallengeMissing) {
|
|
t.Fatalf("second consume err = %v, want single use", err)
|
|
}
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, "zz"); !errors.Is(err, ErrChallengeMissing) {
|
|
t.Fatalf("malformed challenge err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLinkChallengeBindingAndExpiry(t *testing.T) {
|
|
st, clk := newTestStore(t)
|
|
a := mustCreate(t, st, "a@example.org", "A")
|
|
b := mustCreate(t, st, "b@example.org", "B")
|
|
pk := strings.Repeat("ab", 32)
|
|
otherPk := strings.Repeat("cd", 32)
|
|
|
|
ch, _, _ := st.CreateLinkChallenge(a.ID, pk)
|
|
if err := st.ConsumeLinkChallenge(b.ID, pk, ch); !errors.Is(err, ErrChallengeMismatch) {
|
|
t.Fatalf("other user err = %v", err)
|
|
}
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, ch); !errors.Is(err, ErrChallengeMissing) {
|
|
t.Fatalf("mismatch did not consume: %v", err)
|
|
}
|
|
|
|
ch, _, _ = st.CreateLinkChallenge(a.ID, pk)
|
|
if err := st.ConsumeLinkChallenge(a.ID, otherPk, ch); !errors.Is(err, ErrChallengeMismatch) {
|
|
t.Fatalf("other pubkey err = %v", err)
|
|
}
|
|
|
|
ch, _, _ = st.CreateLinkChallenge(a.ID, pk)
|
|
clk.Advance(LinkChallengeTTL)
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, ch); !errors.Is(err, ErrChallengeExpired) {
|
|
t.Fatalf("expired err = %v", err)
|
|
}
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, ch); !errors.Is(err, ErrChallengeMissing) {
|
|
t.Fatalf("expired challenge not consumed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPruneLinkChallenges(t *testing.T) {
|
|
st, clk := newTestStore(t)
|
|
a := mustCreate(t, st, "a@example.org", "A")
|
|
pk := strings.Repeat("ab", 32)
|
|
old, _, _ := st.CreateLinkChallenge(a.ID, pk)
|
|
clk.Advance(3 * time.Minute)
|
|
fresh, _, _ := st.CreateLinkChallenge(a.ID, pk)
|
|
clk.Advance(3 * time.Minute) // old expired, fresh has 2 minutes left
|
|
|
|
n, err := st.PruneLinkChallenges()
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("PruneLinkChallenges = %d, %v", n, err)
|
|
}
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, old); !errors.Is(err, ErrChallengeMissing) {
|
|
t.Fatalf("pruned challenge err = %v", err)
|
|
}
|
|
if err := st.ConsumeLinkChallenge(a.ID, pk, fresh); err != nil {
|
|
t.Fatalf("live challenge pruned: %v", err)
|
|
}
|
|
}
|