PUT host: verify the token with Base before it counts
SetHost fetches the board with the offered token (5 s, outside the transaction) and commits the host and the board together on a 200, or on a legacy Base's 404 with operations 200 while that origin has never answered the board. Anything else is 409 host_unverified, and a move to another allowed origin while the current one still honours the person is 409 host_elsewhere; the earlier registration and board stand. The thintest FakeBase now answers GET /api/v1/abilities (every ability on unless SetBoard says otherwise; SetLegacy makes it an old Base), and the allowlist contract test expects an unreachable origin to be refused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+345
−13
internal/equipo/chat/abilities_harness_test.go+18 −2internal/equipo/chat/abilities_host_test.go+149 −0internal/equipo/chat/assistant_http.go+10 −2internal/equipo/chat/assistant_tools.go+63 −1internal/thintest/assistant_ops_contract_test.go+12 −7internal/thintest/fakebase.go+93 −1
diff --git a/internal/equipo/chat/abilities_harness_test.go b/internal/equipo/chat/abilities_harness_test.go| index c5976421..3835e011 100644 |
| --- a/internal/equipo/chat/abilities_harness_test.go |
| +++ b/internal/equipo/chat/abilities_harness_test.go |
| @@ -110,6 +110,7 @@ func (f *fakeBoardBase) holdNext() (entered <-chan struct{}, release func()) { |
| // with a clock the test moves and an audit it reads. |
| type abilitiesHarness struct { |
| s *Store |
| + auth Authority |
| mu sync.Mutex |
| at time.Time |
| audits []string |
| @@ -141,7 +142,7 @@ func newAbilitiesHarness(t *testing.T, hosts ...string) *abilitiesHarness { |
| for i, o := range hosts { |
| lowered[i] = strings.ToLower(o) |
| } |
| - h.s = NewStore(d, Authority{ |
| + h.auth = Authority{ |
| CurrentPerson: func(context.Context, *gorm.DB) error { return nil }, |
| PrimaryRunner: func(*gorm.DB, string) (string, error) { return "RUNNER1", nil }, |
| Audit: func(_ *gorm.DB, actor, kind, record string) error { |
| @@ -157,11 +158,26 @@ func newAbilitiesHarness(t *testing.T, hosts ...string) *abilitiesHarness { |
| return string(raw), err |
| }, |
| Open: func(_ string, sealed string, v any) error { return json.Unmarshal([]byte(sealed), v) }, |
| - }, nil) |
| + } |
| + h.s = NewStore(d, h.auth, nil) |
| h.s.SetClock(h.now) |
| return h |
| } |
| +// withHosts is another store on the same database whose operator allows |
| +// only hosts, sharing the clock and the audit. |
| +func (h *abilitiesHarness) withHosts(hosts ...string) *abilitiesHarness { |
| + auth := h.auth |
| + auth.AssistantHosts = nil |
| + for _, o := range hosts { |
| + auth.AssistantHosts = append(auth.AssistantHosts, strings.ToLower(o)) |
| + } |
| + h2 := &abilitiesHarness{auth: auth, at: h.now()} |
| + h2.s = NewStore(h.s.db, auth, nil) |
| + h2.s.SetClock(h2.now) |
| + return h2 |
| +} |
| + |
| func (h *abilitiesHarness) now() time.Time { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
diff --git a/internal/equipo/chat/abilities_host_test.go b/internal/equipo/chat/abilities_host_test.go| new file mode 100644 |
| index 00000000..493caefd |
| --- /dev/null |
| +++ b/internal/equipo/chat/abilities_host_test.go |
| @@ -0,0 +1,149 @@ |
| +package chat |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +func putHost(h *abilitiesHarness, origin, token string) (wire.AssistantHost, error) { |
| + return h.s.Lane(wire.LaneAssistant).SetHost(context.Background(), abPerson, wire.AssistantHost{Origin: origin, Token: token}) |
| +} |
| + |
| +// hostOf is the person's registered origin and token. |
| +func hostOf(t *testing.T, h *abilitiesHarness) (string, string) { |
| + t.Helper() |
| + origin, token, ok, err := h.s.Host(context.Background(), abPerson) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !ok { |
| + return "", "" |
| + } |
| + return origin, token |
| +} |
| + |
| +// PUT host fetches the board with the offered token before it counts, |
| +// and stores the board with the host. |
| +func TestAbilitiesFetchOnHost(t *testing.T) { |
| + base := startBoardBase(t, "host-token-0123456789abcdef") |
| + h := newAbilitiesHarness(t, base.origin()) |
| + base.set(func(f *fakeBoardBase) { f.board.Revision, f.board.Abilities["web"] = 3, false }) |
| + if _, err := putHost(h, base.origin(), base.token); err != nil { |
| + t.Fatal(err) |
| + } |
| + if o, tok := hostOf(t, h); o != base.origin() || tok != base.token { |
| + t.Errorf("host %q %q", o, tok) |
| + } |
| + r := h.row(t, abPerson, base.origin()) |
| + if b, _ := r.board(); r.Revision != 3 || b.Abilities["web"] || r.ConfirmedAt == nil { |
| + t.Errorf("board %+v", r) |
| + } |
| + if n, _ := base.counts(); n != 1 { |
| + t.Errorf("board fetched %d times", n) |
| + } |
| + if got := h.audited(auditAssistantHost); len(got) != 1 { |
| + t.Errorf("audits %v", got) |
| + } |
| +} |
| + |
| +// A token Base doesn't honour, an error, or a 404 once a board was stored |
| +// is refused host_unverified, and the earlier registration and board stay. |
| +func TestHostUnverifiedRefused(t *testing.T) { |
| + base := startBoardBase(t, "host-token-0123456789abcdef") |
| + h := newAbilitiesHarness(t, base.origin()) |
| + base.set(func(f *fakeBoardBase) { f.board.Abilities["build"] = false }) |
| + if _, err := putHost(h, base.origin(), base.token); err != nil { |
| + t.Fatal(err) |
| + } |
| + before := h.row(t, abPerson, base.origin()) |
| + for name, c := range map[string]struct { |
| + token string |
| + setup func(f *fakeBoardBase) |
| + }{ |
| + "an invalid token": {"forged-token-0123456789abc", func(*fakeBoardBase) {}}, |
| + "a 401": {"other-token-0123456789abcd", func(f *fakeBoardBase) {}}, |
| + "a 5xx": {base.token, func(f *fakeBoardBase) { f.status = 503 }}, |
| + "a 404 after a board": {base.token, func(f *fakeBoardBase) { |
| + f.status = 404 |
| + }}, |
| + } { |
| + base.set(c.setup) |
| + if _, err := putHost(h, base.origin(), c.token); !errors.Is(err, ErrHostUnverified) { |
| + t.Errorf("%s: %v, want ErrHostUnverified", name, err) |
| + } |
| + base.set(func(f *fakeBoardBase) { f.status = 200 }) |
| + if o, tok := hostOf(t, h); o != base.origin() || tok != base.token { |
| + t.Errorf("%s: the registration moved: %q %q", name, o, tok) |
| + } |
| + if r := h.row(t, abPerson, base.origin()); r.Abilities != before.Abilities || r.RevokedAt != nil { |
| + t.Errorf("%s: the board changed: %+v", name, r) |
| + } |
| + } |
| +} |
| + |
| +// An old Base (board 404, operations 200) is accepted, with the defaults |
| +// stored as legacy. |
| +func TestHostLegacyAccepted(t *testing.T) { |
| + base := startBoardBase(t, "host-token-0123456789abcdef") |
| + base.set(func(f *fakeBoardBase) { f.status = 404 }) |
| + h := newAbilitiesHarness(t, base.origin()) |
| + if _, err := putHost(h, base.origin(), base.token); err != nil { |
| + t.Fatal(err) |
| + } |
| + r := h.row(t, abPerson, base.origin()) |
| + b, _ := r.board() |
| + if r.Source != SourceLegacy || r.AuthoritativeSeen || len(b.Abilities) != len(wire.Abilities) { |
| + t.Errorf("legacy row %+v %+v", r, b) |
| + } |
| + for _, k := range wire.Abilities { |
| + if !b.Abilities[k] { |
| + t.Errorf("%s off in the defaults", k) |
| + } |
| + } |
| +} |
| + |
| +// One Base at a time: with two allowed origins, moving while the first |
| +// still honours the person is refused host_elsewhere; after its 401, or |
| +// once the operator takes it off the list, the move succeeds. Each time |
| +// the person keeps one copy Equipo can refresh past 10 minutes. |
| +func TestHostElsewhereRefused(t *testing.T) { |
| + a := startBoardBase(t, "token-a-0123456789abcdefg") |
| + b := startBoardBase(t, "token-b-0123456789abcdefg") |
| + h := newAbilitiesHarness(t, a.origin(), b.origin()) |
| + if _, err := putHost(h, a.origin(), a.token); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := putHost(h, b.origin(), b.token); !errors.Is(err, ErrHostElsewhere) { |
| + t.Fatalf("moving while A honours them: %v", err) |
| + } |
| + if o, _ := hostOf(t, h); o != a.origin() { |
| + t.Fatalf("host moved to %q", o) |
| + } |
| + // A answers 401 (the member left A): the move succeeds. |
| + a.set(func(f *fakeBoardBase) { f.token = "gone-0123456789abcdefghij" }) |
| + if _, _, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := putHost(h, b.origin(), b.token); err != nil { |
| + t.Fatalf("moving after A's 401: %v", err) |
| + } |
| + h.advance(11 * time.Minute) |
| + if _, fresh, err := h.s.RefreshAbilities(context.Background(), abPerson); err != nil || !fresh { |
| + t.Errorf("B's copy not refreshable: %v %v", fresh, err) |
| + } |
| + |
| + // The operator takes B off the list: the person may move to A again. |
| + a.set(func(f *fakeBoardBase) { f.token = "token-a-0123456789abcdefg" }) |
| + h2 := h.withHosts(a.origin()) |
| + if _, err := h2.s.Lane(wire.LaneAssistant).SetHost(context.Background(), abPerson, wire.AssistantHost{Origin: a.origin(), Token: a.token}); err != nil { |
| + t.Fatalf("moving after B left the list: %v", err) |
| + } |
| + h2.advance(11 * time.Minute) |
| + if _, fresh, err := h2.s.RefreshAbilities(context.Background(), abPerson); err != nil || !fresh { |
| + t.Errorf("A's copy not refreshable: %v %v", fresh, err) |
| + } |
| +} |
diff --git a/internal/equipo/chat/assistant_http.go b/internal/equipo/chat/assistant_http.go| index a2af53ee..b521f230 100644 |
| --- a/internal/equipo/chat/assistant_http.go |
| +++ b/internal/equipo/chat/assistant_http.go |
| @@ -131,7 +131,8 @@ func (h *HTTP) warmAssistant(r *http.Request, p personapi.PersonPrincipal) { |
| // assistantHost is PUT host {origin, token}: 200 {origin, revision}, the |
| // token never given back; 403 host_not_allowed for an origin the |
| -// operator has not allowed. |
| +// operator has not allowed; 409 host_unverified when Base didn't honour |
| +// the token, host_elsewhere when the person's Base is another. |
| func (h *HTTP) assistantHost(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal, lane *LaneStore) { |
| var in wire.AssistantHost |
| if err := readJSON(r, &in); err != nil { |
| @@ -139,9 +140,16 @@ func (h *HTTP) assistantHost(w http.ResponseWriter, r *http.Request, p personapi |
| return |
| } |
| out, err := lane.SetHost(r.Context(), p.Address, in) |
| - if errors.Is(err, ErrHostNotAllowed) { |
| + switch { |
| + case errors.Is(err, ErrHostNotAllowed): |
| personapi.WriteError(w, r, http.StatusForbidden, "host_not_allowed", nil) |
| return |
| + case errors.Is(err, ErrHostUnverified): |
| + personapi.WriteError(w, r, http.StatusConflict, wire.CodeHostUnverified, nil) |
| + return |
| + case errors.Is(err, ErrHostElsewhere): |
| + personapi.WriteError(w, r, http.StatusConflict, wire.CodeHostElsewhere, nil) |
| + return |
| } |
| if err != nil { |
| h.fail(w, r, err) |
diff --git a/internal/equipo/chat/assistant_tools.go b/internal/equipo/chat/assistant_tools.go| index 79514758..22b5902c 100644 |
| --- a/internal/equipo/chat/assistant_tools.go |
| +++ b/internal/equipo/chat/assistant_tools.go |
| @@ -86,6 +86,15 @@ const ( |
| // host_not_allowed). |
| var ErrHostNotAllowed = errors.New("chat: host not allowed") |
| +// ErrHostUnverified: Base did not honour the offered token (409 |
| +// host_unverified); ErrHostElsewhere: the person's Base is another one, |
| +// which still honours them (409 host_elsewhere). Either way the earlier |
| +// registration and board stand (spec 2026-11-26-assistant-abilities §5.3). |
| +var ( |
| + ErrHostUnverified = errors.New("chat: host unverified") |
| + ErrHostElsewhere = errors.New("chat: host elsewhere") |
| +) |
| + |
| // assistantHostRecord is where a person's Base operations live, with the |
| // token Base minted for them, sealed. |
| type assistantHostRecord struct { |
| @@ -104,7 +113,14 @@ func (assistantHostRecord) TableName() string { return "chat_assistant_hosts" } |
| // SetHost registers the person's Base host, replacing any before: an |
| // https origin with no path (http on loopback), on the operator's |
| // allowlist, and a token of 20 to 512 visible ASCII characters. The |
| -// token is stored sealed and never given back. |
| +// token is stored sealed and never given back. It counts only once Base |
| +// honours it (spec 2026-11-26-assistant-abilities §5.3): the board is |
| +// fetched with it first, outside the transaction, and the host and the |
| +// board are committed together on a 200, or on a legacy Base's 404 with |
| +// its operations' 200 while that origin has never answered the board. |
| +// Anything else is ErrHostUnverified. A person moves to another origin |
| +// only once the one they have no longer honours them (its last answer a |
| +// 401) or the operator has taken it off the list; else ErrHostElsewhere. |
| func (l *LaneStore) SetHost(ctx context.Context, person string, in wire.AssistantHost) (out wire.AssistantHost, err error) { |
| origin, ok := hostOrigin(in.Origin) |
| if !ok || !hostToken(in.Token) { |
| @@ -120,10 +136,32 @@ func (l *LaneStore) SetHost(ctx context.Context, person string, in wire.Assistan |
| if err != nil { |
| return out, err |
| } |
| + m := l.s.personLock(person) |
| + m.Lock() |
| + defer m.Unlock() |
| + var seen []bool |
| + if err := l.s.read(ctx, func(tx *gorm.DB) error { |
| + t := &txn{s: l.s, ctx: ctx, db: tx} |
| + if err := t.hostElsewhere(person, origin); err != nil { |
| + return err |
| + } |
| + return tx.Model(&abilitiesRecord{}).Where("person = ? AND origin = ?", person, origin).Pluck("coalesce(authoritative_seen, 0)", &seen).Error |
| + }); err != nil { |
| + return out, err |
| + } |
| + seq := l.s.nextFetchSeq() |
| + board, outcome, ferr := l.s.fetchBoard(ctx, origin, in.Token, len(seen) == 0 || !seen[0]) |
| + if outcome != fetchOK && outcome != fetchLegacy { |
| + l.s.logAbilities(person, ferr) |
| + return out, ErrHostUnverified |
| + } |
| err = l.s.tx(ctx, func(t *txn) error { |
| if err := l.s.authority.CurrentPerson(ctx, t.db); err != nil { |
| return err |
| } |
| + if err := t.hostElsewhere(person, origin); err != nil { |
| + return err |
| + } |
| var h assistantHostRecord |
| if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil { |
| return err |
| @@ -132,6 +170,9 @@ func (l *LaneStore) SetHost(ctx context.Context, person string, in wire.Assistan |
| if err := t.db.Save(&h).Error; err != nil { |
| return err |
| } |
| + if _, err := t.applyBoard(person, origin, seq, board, outcome); err != nil { |
| + return err |
| + } |
| out = wire.AssistantHost{Origin: origin, Revision: h.Revision} |
| return t.s.authority.Audit(t.db, person, auditAssistantHost, origin) |
| }) |
| @@ -143,6 +184,27 @@ func (l *LaneStore) SetHost(ctx context.Context, person string, in wire.Assistan |
| return out, err |
| } |
| +// hostElsewhere refuses registering origin while the person's host is |
| +// another origin that is still allowed and whose last answer wasn't a 401 |
| +// (spec §5.3, "One Base at a time"). |
| +func (t *txn) hostElsewhere(person, origin string) error { |
| + var h assistantHostRecord |
| + if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil { |
| + return err |
| + } |
| + if h.Person == "" || h.Origin == origin || !slices.Contains(t.s.authority.AssistantHosts, h.Origin) { |
| + return nil |
| + } |
| + var r abilitiesRecord |
| + if err := t.db.Where("person = ? AND origin = ?", person, h.Origin).Limit(1).Find(&r).Error; err != nil { |
| + return err |
| + } |
| + if r.RevokedAt != nil { |
| + return nil |
| + } |
| + return ErrHostElsewhere |
| +} |
| + |
| // Host is where the person's Base operations live and the token to call |
| // them with; not ok when they registered none, or the operator has since |
| // taken the origin off the allowlist. |
diff --git a/internal/thintest/assistant_ops_contract_test.go b/internal/thintest/assistant_ops_contract_test.go| index e2e8025d..3fddb6c0 100644 |
| --- a/internal/thintest/assistant_ops_contract_test.go |
| +++ b/internal/thintest/assistant_ops_contract_test.go |
| @@ -250,13 +250,16 @@ func TestContractHostAllowlist(t *testing.T) { |
| return status, e, raw |
| } |
| token := "tok-" + strings.Repeat("a", 30) |
| - status, _, body := put("https://Base.Example.com", token) |
| - var out wire.AssistantHost |
| - if status != 200 || json.Unmarshal(body, &out) != nil || out.Origin != "https://base.example.com" || out.Revision != 1 || strings.Contains(string(body), token) || strings.Contains(string(body), `"token"`) { |
| + // An allowed origin (case aside) that doesn't honour the token, here |
| + // one that doesn't answer at all, doesn't count (abilities §5.3). |
| + status, e, body := put("https://Base.Example.com", token) |
| + if status != 409 || e.Code != wire.CodeHostUnverified || strings.Contains(string(body), token) { |
| t.Fatalf("first put %d %s", status, body) |
| } |
| + var out wire.AssistantHost |
| second := "second-" + strings.Repeat("b", 30) |
| - if status, _, body = put(base.Origin, second); status != 200 || json.Unmarshal(body, &out) != nil || out.Origin != base.Origin || out.Revision != 2 || strings.Contains(string(body), second) { |
| + base.Accept(second) |
| + if status, _, body = put(base.Origin, second); status != 200 || json.Unmarshal(body, &out) != nil || out.Origin != base.Origin || out.Revision != 1 || strings.Contains(string(body), second) || strings.Contains(string(body), `"token"`) { |
| t.Fatalf("second put %d %s", status, body) |
| } |
| if n := rows("SELECT count(*) FROM chat_assistant_hosts"); n != 1 { |
| @@ -295,10 +298,12 @@ func TestContractHostAllowlist(t *testing.T) { |
| t.Fatalf("%q %q: %d %s", c.origin, c.token, status, body) |
| } |
| } |
| - if status, _, _ := put("https://base.example.com", strings.Repeat("a", 20)); status != 200 { |
| + base.Accept(strings.Repeat("a", 20)) |
| + base.Accept(strings.Repeat("a", 512)) |
| + if status, _, _ := put(base.Origin, strings.Repeat("a", 20)); status != 200 { |
| t.Fatalf("a 20-character token %d", status) |
| } |
| - if status, _, _ := put("https://base.example.com", strings.Repeat("a", 512)); status != 200 { |
| + if status, _, _ := put(base.Origin, strings.Repeat("a", 512)); status != 200 { |
| t.Fatalf("a 512-character token %d", status) |
| } |
| if status, e, body := put("https://other.example.com", token); status != 403 || e.Code != "host_not_allowed" || e.CopyKey != "api.host_not_allowed" { |
| @@ -317,7 +322,7 @@ func TestContractHostAllowlist(t *testing.T) { |
| if n := rows("SELECT count(*) FROM audit_events WHERE record_id LIKE ? OR record_id LIKE ?", "%"+token[:12]+"%", "%"+second[:12]+"%"); n != 0 { |
| t.Fatalf("%d audits carry a token", n) |
| } |
| - if n := rows("SELECT count(*) FROM audit_events WHERE kind = 'assistant.host' AND actor = ?", Owner); n != 4 { |
| + if n := rows("SELECT count(*) FROM audit_events WHERE kind = 'assistant.host' AND actor = ?", Owner); n != 3 { |
| t.Fatalf("%d host audits", n) |
| } |
diff --git a/internal/thintest/fakebase.go b/internal/thintest/fakebase.go| index 9a87f131..48a43dac 100644 |
| --- a/internal/thintest/fakebase.go |
| +++ b/internal/thintest/fakebase.go |
| @@ -6,11 +6,13 @@ import ( |
| "io" |
| "net" |
| "net/http" |
| + "slices" |
| "strings" |
| "sync" |
| "testing" |
| "amadan.net/oficina/equipo/internal/equipo/assist" |
| + "amadan.net/oficina/equipo/internal/wire" |
| ) |
| // FakeBase is Base's operations endpoint as the Assistant's base provider |
| @@ -22,6 +24,10 @@ type FakeBase struct { |
| Origin string |
| // Token is one accepted bearer; Mint makes more. |
| Token string |
| + // It also answers GET /api/v1/abilities (spec |
| + // 2026-11-26-assistant-abilities §5.3) with each member's board: |
| + // every ability on unless SetBoard says otherwise. Legacy makes it a |
| + // Base older than the board, which answers that route 404. |
| mu sync.Mutex |
| ops []assist.Operation |
| @@ -30,6 +36,13 @@ type FakeBase struct { |
| fail map[string]string |
| hold map[string]chan struct{} |
| output map[string]string |
| + // board is each token's board (boardFor), boardStatus what the board |
| + // route answers instead when set, legacy a Base without the route, |
| + // boardCalls the tokens it was asked with. |
| + board map[string]wire.AbilityBoard |
| + boardStatus int |
| + legacy bool |
| + boardCalls []string |
| } |
| type fakeCall struct{ token, key, input string } |
| @@ -37,7 +50,7 @@ type fakeCall struct{ token, key, input string } |
| // StartFakeBase serves a fake Base on 127.0.0.1 until the test ends. |
| func StartFakeBase(t testing.TB) *FakeBase { |
| t.Helper() |
| - f := &FakeBase{Token: "base-" + rand.Text(), tokens: map[string]bool{}, calls: map[string][]fakeCall{}, fail: map[string]string{}, hold: map[string]chan struct{}{}, output: map[string]string{}} |
| + f := &FakeBase{Token: "base-" + rand.Text(), tokens: map[string]bool{}, calls: map[string][]fakeCall{}, fail: map[string]string{}, hold: map[string]chan struct{}{}, output: map[string]string{}, board: map[string]wire.AbilityBoard{}} |
| f.tokens[f.Token] = true |
| object := json.RawMessage(`{"type":"object","properties":{},"additionalProperties":false}`) |
| f.ops = []assist.Operation{ |
| @@ -152,7 +165,86 @@ func (f *FakeBase) Hold(name string) (release func()) { |
| } |
| } |
| +// Accept accepts token as a bearer, as if Base had minted it. |
| +func (f *FakeBase) Accept(token string) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.tokens[token] = true |
| +} |
| + |
| +// SetBoard is the board the member with token gets: off lists the |
| +// abilities turned off, at revision, with ask as who can change them. |
| +func (f *FakeBase) SetBoard(token string, revision int64, ask string, off ...string) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + b := wire.AbilityBoard{Revision: revision, Ask: ask, Abilities: map[string]bool{}} |
| + for _, k := range wire.Abilities { |
| + b.Abilities[k] = !slices.Contains(off, k) |
| + } |
| + f.board[token] = b |
| +} |
| + |
| +// SetLegacy makes the fake a Base older than the board (on), whose board |
| +// route answers 404, or a current one again (off). |
| +func (f *FakeBase) SetLegacy(on bool) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.legacy = on |
| +} |
| + |
| +// FailBoard makes the board route answer status (0 stops). |
| +func (f *FakeBase) FailBoard(status int) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.boardStatus = status |
| +} |
| + |
| +// BoardCalls are the tokens the board was asked with, in order. |
| +func (f *FakeBase) BoardCalls() []string { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + return slices.Clone(f.boardCalls) |
| +} |
| + |
| +// Forget stops accepting token, as Base does once its member disconnects. |
| +func (f *FakeBase) Forget(token string) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + delete(f.tokens, token) |
| +} |
| + |
| +// serveBoard is GET /api/v1/abilities. |
| +func (f *FakeBase) serveBoard(w http.ResponseWriter, r *http.Request) { |
| + token, _ := strings.CutPrefix(r.Header.Get("Authorization"), "Bearer ") |
| + f.mu.Lock() |
| + f.boardCalls = append(f.boardCalls, token) |
| + accepted, legacy, status := f.tokens[token], f.legacy, f.boardStatus |
| + b, ok := f.board[token] |
| + f.mu.Unlock() |
| + if !ok { |
| + b = wire.AbilityBoard{Revision: 1, Ask: "Owner", Abilities: map[string]bool{}} |
| + for _, k := range wire.Abilities { |
| + b.Abilities[k] = true |
| + } |
| + } |
| + w.Header().Set("Content-Type", "application/json") |
| + switch { |
| + case legacy: |
| + http.NotFound(w, r) |
| + case !accepted: |
| + w.WriteHeader(http.StatusUnauthorized) |
| + case status != 0: |
| + w.WriteHeader(status) |
| + default: |
| + _ = json.NewEncoder(w).Encode(b) |
| + } |
| +} |
| + |
| func (f *FakeBase) serve(w http.ResponseWriter, r *http.Request) { |
| + if r.Method == http.MethodGet && r.URL.Path == "/api/v1/abilities" { |
| + f.serveBoard(w, r) |
| + return |
| + } |
| name, ok := "", false |
| switch { |
| case r.Method == http.MethodGet && r.URL.Path == "/api/v1/operations": |