mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-10-15 03:19:58 +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>
138 lines
4.3 KiB
Go
138 lines
4.3 KiB
Go
package users
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func TestDeviceSessionKindLabelScopes(t *testing.T) {
|
|
st, clk := newTestStore(t)
|
|
u := mustCreate(t, st, "dev@example.org", "Dev")
|
|
_, web, err := st.CreateSession(u.ID, time.Hour, "browser")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if web.Kind != SessionKindWeb || web.Label != "" || web.Scopes != nil || !web.HasScope("anything") {
|
|
t.Fatalf("web session = %+v", web)
|
|
}
|
|
|
|
raw, dev, err := st.CreateDeviceSession(u.ID, " Pixel\x07 8 ", []string{"rx", "account"}, "CoreDriveRX/1.0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if raw == "" || dev.Kind != SessionKindDevice || dev.Label != "Pixel 8" || dev.CSRFToken == "" ||
|
|
!reflect.DeepEqual(dev.Scopes, []string{"rx", "account"}) || !dev.ExpiresAt.Equal(clk.Now().Add(DeviceSessionTTL)) {
|
|
t.Fatalf("CreateDeviceSession = %+v", dev)
|
|
}
|
|
|
|
got, err := st.LookupSession(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.ID != dev.ID || got.Kind != SessionKindDevice || got.Label != "Pixel 8" || got.UserAgent != "CoreDriveRX/1.0" ||
|
|
!reflect.DeepEqual(got.Scopes, []string{"rx", "account"}) {
|
|
t.Fatalf("LookupSession = %+v", got)
|
|
}
|
|
if !got.HasScope("rx") || !got.HasScope("account") || got.HasScope("admin") || got.HasScope("") {
|
|
t.Fatalf("HasScope wrong for %v", got.Scopes)
|
|
}
|
|
|
|
list, err := st.ListSessions(u.ID)
|
|
if err != nil || len(list) != 2 {
|
|
t.Fatalf("ListSessions = %+v, %v", list, err)
|
|
}
|
|
kinds := map[string]string{}
|
|
for _, s := range list {
|
|
kinds[s.Kind] = s.Label
|
|
}
|
|
if l, ok := kinds[SessionKindDevice]; !ok || l != "Pixel 8" {
|
|
t.Fatalf("device row missing from list: %+v", list)
|
|
}
|
|
if l, ok := kinds[SessionKindWeb]; !ok || l != "" {
|
|
t.Fatalf("web row missing from list: %+v", list)
|
|
}
|
|
}
|
|
|
|
func TestDeviceSessionSlidingExpiry(t *testing.T) {
|
|
st, clk := newTestStore(t)
|
|
u := mustCreate(t, st, "slide@example.org", "Slide")
|
|
raw, dev, err := st.CreateDeviceSession(u.ID, "Phone", []string{"rx"}, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
clk.Advance(DeviceSessionTTL - time.Hour)
|
|
if err := st.ExtendSession(dev.ID, DeviceSessionTTL); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
clk.Advance(DeviceSessionTTL - time.Hour) // well past the original expiry
|
|
if _, err := st.LookupSession(raw); err != nil {
|
|
t.Fatalf("extended device session expired early: %v", err)
|
|
}
|
|
clk.Advance(2 * time.Hour)
|
|
if _, err := st.LookupSession(raw); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("expired device session err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeviceSessionRevoke(t *testing.T) {
|
|
st, _ := newTestStore(t)
|
|
u := mustCreate(t, st, "rev@example.org", "Rev")
|
|
other := mustCreate(t, st, "oth@example.org", "Oth")
|
|
raw1, d1, _ := st.CreateDeviceSession(u.ID, "One", []string{"rx"}, "")
|
|
raw2, _, _ := st.CreateDeviceSession(u.ID, "Two", []string{"rx"}, "")
|
|
|
|
if err := st.DeleteSession(other.ID, d1.ID); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("revoking someone else's device = %v", err)
|
|
}
|
|
if err := st.DeleteSession(u.ID, d1.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.LookupSession(raw1); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("revoked device token still resolves: %v", err)
|
|
}
|
|
if err := st.DeleteSessionByToken(raw2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.LookupSession(raw2); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("logged-out device token still resolves: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeviceSessionRejectsBadScopes(t *testing.T) {
|
|
st, _ := newTestStore(t)
|
|
u := mustCreate(t, st, "scope@example.org", "Scope")
|
|
if _, _, err := st.CreateDeviceSession(u.ID, "x", nil, ""); !errors.Is(err, ErrNoScopes) {
|
|
t.Fatalf("nil scopes err = %v", err)
|
|
}
|
|
for _, bad := range []string{"", "a,b", "Rx", "rx scope"} {
|
|
if _, _, err := st.CreateDeviceSession(u.ID, "x", []string{bad}, ""); err == nil {
|
|
t.Fatalf("scope %q accepted", bad)
|
|
}
|
|
}
|
|
if list, _ := st.ListSessions(u.ID); len(list) != 0 {
|
|
t.Fatalf("rejected device sessions were stored: %+v", list)
|
|
}
|
|
}
|
|
|
|
func TestCleanLabel(t *testing.T) {
|
|
cases := map[string]string{
|
|
" Car ": "Car",
|
|
"a\tb\nc\x00d": "abcd",
|
|
"bad\xffutf8": "badutf8",
|
|
"": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := CleanLabel(in); got != want {
|
|
t.Errorf("CleanLabel(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
long := CleanLabel(strings.Repeat("é", 70))
|
|
if utf8.RuneCountInString(long) != 64 {
|
|
t.Fatalf("CleanLabel cap = %d runes", utf8.RuneCountInString(long))
|
|
}
|
|
}
|