feat(auth): add scoped JSON account and session queries for CLI clients
Build and publish container images / images (push) Successful in 18m18s

This commit is contained in:
Anya Tokugawa committed 2026-12-01 08:17:56 +09:00
1 parent 6f9752ea3a
commit b650fe50d7
10 files changed
+640

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
package server
import (
"encoding/json"
"net/http"
"strings"
"testing"
"pageswiki/internal/store"
)
func TestAuthQueryAPI(t *testing.T) {
s, owner, domain := configFixture(t)
s.registerAuthQueryRoutes()
member, err := s.store.CreatePasswordAccount(domain, "reader", "synthetic auth query password", "Reader", "reader@example.test")
if err != nil {
t.Fatal(err)
}
token, err := s.store.CreateAuthSession(domain, member.UUID)
if err != nil {
t.Fatal(err)
}
for _, mode := range []string{"enforcing", "legacy"} {
if err = s.store.SetConfigInDomain(domain, "authorization_mode", mode); err != nil {
t.Fatal(err)
}
for _, path := range []string{"whoami", "sessions", "sessions/history"} {
url := "/_/api/v1/auth/" + path
for _, bad := range []string{"", "invalid"} {
w := configRequest(s, bad, "GET", url, nil)
if w.Code != 401 || !strings.Contains(w.Body.String(), "authentication_required") {
t.Fatalf("%s %s: %d %s", mode, path, w.Code, w.Body.String())
}
}
w := configRequest(s, token, "GET", url, nil)
if w.Code != 200 || w.Header().Get("Cache-Control") != "private, no-store" || !strings.Contains(w.Header().Get("Content-Type"), "application/json") {
t.Fatalf("%s %s: %d %s", mode, path, w.Code, w.Body.String())
}
for _, secret := range []string{token, "token_hash", "secret_hash", "browser_hash", "password"} {
if strings.Contains(w.Body.String(), secret) {
t.Fatalf("secret field leaked: %s", secret)
}
}
w = configRequest(s, token, "POST", url, nil)
if w.Code != 405 || w.Header().Get("Allow") != "GET" {
t.Fatal("method not closed", w.Code)
}
}
}
w := configRequest(s, token, "GET", "/_/api/v1/auth/whoami", nil)
var self struct {
Principal string `json:"principal_uuid"`
Username string `json:"username"`
Email string `json:"email"`
Roles []string `json:"roles"`
Groups []string `json:"groups"`
Session struct {
UUID string `json:"uuid"`
} `json:"session"`
}
if err = json.Unmarshal(w.Body.Bytes(), &self); err != nil || self.Principal != member.UUID || self.Username != "reader" || self.Email != "reader@example.test" || self.Session.UUID == "" || self.Roles == nil || self.Groups == nil {
t.Fatal("invalid self", w.Body.String(), err)
}
for _, path := range []string{"sessions", "sessions/history"} {
url := "/_/api/v1/auth/" + path
w = configRequest(s, token, "GET", url+"?scope=domain", nil)
if w.Code != 403 {
t.Fatal("ordinary member domain enumeration", w.Code)
}
w = configRequest(s, owner, "GET", url+"?scope=domain", nil)
if w.Code != 200 || !strings.Contains(w.Body.String(), member.UUID) {
t.Fatal("admin list", w.Code, w.Body.String())
}
w = configRequest(s, token, "GET", url, nil)
var list struct {
Sessions []store.AuthSessionView `json:"sessions"`
}
if err = json.Unmarshal(w.Body.Bytes(), &list); err != nil || len(list.Sessions) != 1 || list.Sessions[0].PrincipalUUID != member.UUID || !list.Sessions[0].IsCurrent {
t.Fatal("self scope", w.Body.String(), err)
}
for _, query := range []string{"limit=0", "limit=101", "limit=-1", "limit=", "limit=wat", "limit=1&limit=2", "scope=outside", "username=reader", "scope=domain;limit=1"} {
w = configRequest(s, owner, "GET", url+"?"+query, nil)
if w.Code != 400 {
t.Fatal("invalid query accepted", query, w.Code)
}
}
}
s.store.ReadOnly = true
w = configRequest(s, token, "GET", "/_/api/v1/auth/whoami", nil)
if w.Code != 200 {
t.Fatal("read-only store must permit self inquiry", w.Code)
}
s.store.ReadOnly = false
if err = s.store.RevokeAuthSession(token); err != nil {
t.Fatal(err)
}
w = configRequest(s, token, "GET", "/_/api/v1/auth/whoami", nil)
if w.Code != http.StatusUnauthorized {
t.Fatal("revoked token accepted", w.Code)
}
token, err = s.store.CreateAuthSession(domain, member.UUID)
if err != nil {
t.Fatal(err)
}
if err = s.store.SetPrincipalState(domain, member.UUID, "suspended"); err != nil {
t.Fatal(err)
}
w = configRequest(s, token, "GET", "/_/api/v1/auth/sessions", nil)
if w.Code != 401 {
t.Fatal("suspended user admitted", w.Code)
}
}