Files
magpie/internal/access/admin_test.go
T
yetone 727e236378 gateway: a management API for providers, accounts and gateway keys, for a key set to manage (ljayic on X)
ljayic asked for a complete, stable management API so nothing needs a
hand-edited file. /v1/magpie/admin/ now serves the app's own /api handlers
(providers and their keys and accounts, OAuth sign-in start/poll/cancel,
gateway keys, account groups) to a gateway key set with
`magpie gateway-key admin <id> local|any`. Routes and actions not in
gui/admin.go's adminRoutes are 404, so a provider's saved key, a gateway
key's secret, sign-in export and actions on this computer's apps are not
served; "admin-key" is not served either, so the API can't raise its own
caller. Sign-in returns the vendor's page and code, opens nothing, takes no
password.

Guard (gateway/admin.go), in order: any Origin or Sec-Fetch-Site/-Mode is
403 with or without a key (Origin null, foreign, listed, *.localhost, an
app scheme); no key 401; a key without Key.Admin 403; from loopback the Host
must be ownHost (a rebound page's GET has no Origin); from the network the
key must be "any"; a proxied request counts as remote. Key.Admin is dropped
(adminOf) for a read-only, Browser, issued, expiring or default key, and
setting read-only clears it. The route is in readOnlyRefused and keySlotFree.

Docs: docs/reference.md "Management API", gateway-routing.md, and the
remote page in en, zh and ja. Routing Groups, sign-in export and a GUI
switch for the level are left to #6307.

Run (mica, the worktree before the rebase onto 9 newer commits): go vet of
access, cli, gateway, gui (darwin), GOOS=windows vet of the same four,
GOOS=linux -tags nogui vet of access, cli, gateway; gofmt -l empty;
go test -count=1 -timeout 45m ./internal/access ./internal/cli
./internal/gateway ./internal/gui: all ok but TestKeySlotEveryRoute, which
named the new route and is fixed here, then it and the read-only, admin and
guard tests pass again. After the rebase the same tests ran again.
Broken by hand, each red in TestAdminAPIGuards: browser check off (a
browser case served), Admin check off (a key without it served), Host check
off (a rebound name served), the "any" requirement off (a local-only key
from the network served, and a proxied request).
Tests: TestAdminAPIGuards (no key, unknown key, non-admin, read-only,
local/any, x-api-key, 8 browser headers x 3 callers, rebound Host, proxy,
no handler), TestAdminKey (access: levels, read-only, Browser, issued),
TestGatewayKeyCLIAdmin, TestAdminAPIGatewayKeys / Providers (a canary key
is never told) / SignIn, TestAdminRoutesEveryAPIRoute.
Not tried: a real sign-in against a vendor through the API, a request from
a second computer on the LAN, the GUI suite (no page or app.js changed), a
Linux/Windows run of the new tests.
2026-11-12 17:47:11 +08:00

77 lines
2.4 KiB
Go

package access
import (
"strings"
"testing"
"time"
)
// ljayic on X: a key can be given the management API, but only a key the
// user made for it, and only by the user.
func TestAdminKey(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("HOME", t.TempDir())
plainID, plain := addKey(t, "plain")
if who, ok := Authenticate(plain); !ok || who.Admin != "" {
t.Fatalf("a new key has Admin %q", who.Admin)
}
if _, err := Update("admin-key", Change{Key: plainID, Admin: "sometimes"}); err == nil {
t.Fatal("an unknown level was taken")
}
for in, want := range map[string]string{"local": AdminLocal, "any": AdminAny, "off": ""} {
if _, err := Update("admin-key", Change{Key: plainID, Admin: in}); err != nil {
t.Fatal(in, err)
}
if who, _ := Authenticate(plain); who.Admin != want {
t.Errorf("admin %s: Admin %q, want %q", in, who.Admin, want)
}
}
// read-only: refused, and read-only takes the level away
if _, err := Update("admin-key", Change{Key: plainID, Admin: "any"}); err != nil {
t.Fatal(err)
}
if _, err := Update("access-key", Change{Key: plainID, ReadOnly: true}); err != nil {
t.Fatal(err)
}
if who, _ := Authenticate(plain); who.Admin != "" {
t.Error("a key made read-only kept Admin")
}
if _, err := Update("admin-key", Change{Key: plainID, Admin: "local"}); err == nil {
t.Error("a read-only key was given Admin")
}
// the extension's key
if _, err := Update("add-key", Change{Name: "ext", Browser: true}); err != nil {
t.Fatal(err)
}
keys, _ := List()
if _, err := Update("admin-key", Change{Key: keys[len(keys)-1].ID, Admin: "local"}); err == nil {
t.Error("the extension's key was given Admin")
}
// an issued key, even when the file says otherwise
issuerID, _ := addKey(t, "issuer")
if _, err := Update("admin-key", Change{Key: issuerID, Admin: "any"}); err != nil {
t.Fatal(err)
}
if _, err := Update("issuer-key", Change{Key: issuerID, Issues: true}); err != nil {
t.Fatal(err)
}
k, err := IssueKey(Issue{Name: "sandbox", TTL: time.Hour, Issuer: issuerID})
if err != nil {
t.Fatal(err)
}
if who, ok := Authenticate(k.Key); !ok || who.Admin != "" {
t.Errorf("an issued key has Admin %q", who.Admin)
}
keys, _ = List()
for _, key := range keys {
if key.Issuer != "" {
if _, err := Update("admin-key", Change{Key: key.ID, Admin: "any"}); err == nil || !strings.Contains(err.Error(), "issued") {
t.Errorf("an issued key was given Admin: %v", err)
}
}
}
}