feat(auth): add scoped JSON account and session queries for CLI clients
Build and publish container images / images (push) Successful in 18m18s
Build and publish container images / images (push) Successful in 18m18s
This commit is contained in:
1 parent
6f9752ea3a
commit
b650fe50d7
10 files changed
+640
No files matched your search
@@ -284,6 +284,12 @@ func (s *Server) withAuthorization(next http.Handler) http.Handler {
|
||||
// No process-wide principal/policy cache: revocation is observed by the
|
||||
// next request. Read snapshots never authorize mutating store operations.
|
||||
r = cacheRequestDomain(withReadPolicyContext(r))
|
||||
// These read-only JSON handlers require a live session even in legacy
|
||||
// mode; they also own method and domain-admin checks, without HTML redirects.
|
||||
if authQueryRoute(r.URL.Path) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/g" || strings.HasPrefix(r.URL.Path, "/g/") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeAuthQueryJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func authQueryRoute(path string) bool {
|
||||
switch path {
|
||||
case "/_/api/v1/auth/whoami", "/_/api/v1/auth/sessions", "/_/api/v1/auth/sessions/history":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) registerAuthQueryRoutes() {
|
||||
for _, path := range []string{"/_/api/v1/auth/whoami", "/_/api/v1/auth/sessions", "/_/api/v1/auth/sessions/history"} {
|
||||
s.mux.HandleFunc(path, s.handleAuthQuery)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleAuthQuery(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", "GET")
|
||||
writeAuthQueryJSON(w, 405, map[string]string{"error": "method_not_allowed"})
|
||||
return
|
||||
}
|
||||
p := s.requestPrincipal(r)
|
||||
if p == nil {
|
||||
writeAuthQueryJSON(w, 401, map[string]string{"error": "authentication_required"})
|
||||
return
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
current, err := s.store.CurrentAuthSession(ctx, domain, p.UUID, s.authTokenFromRequest(r))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
writeAuthQueryJSON(w, 401, map[string]string{"error": "authentication_required"})
|
||||
} else {
|
||||
writeAuthQueryJSON(w, 503, map[string]string{"error": "auth_query_unavailable"})
|
||||
}
|
||||
return
|
||||
}
|
||||
query, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_query"})
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/_/api/v1/auth/whoami" {
|
||||
if len(query) != 0 {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_query"})
|
||||
return
|
||||
}
|
||||
roles, groups, truncated, e := s.store.AuthMembershipNames(ctx, domain, p.UUID)
|
||||
if e != nil {
|
||||
writeAuthQueryJSON(w, 503, map[string]string{"error": "auth_query_unavailable"})
|
||||
return
|
||||
}
|
||||
writeAuthQueryJSON(w, 200, map[string]any{
|
||||
"principal_uuid": p.UUID, "username": p.Username, "display_name": p.DisplayName, "email": p.Email, "state": p.State,
|
||||
"roles": roles, "groups": groups, "memberships_truncated": truncated,
|
||||
"session": map[string]any{"uuid": current.UUID, "auth_time_ms": current.AuthTimeMS, "last_seen_at_ms": current.LastSeenAtMS, "expires_at_ms": current.ExpiresAtMS, "mfa_level": current.MFALevel},
|
||||
})
|
||||
return
|
||||
}
|
||||
for key, values := range query {
|
||||
if (key != "scope" && key != "limit") || len(values) != 1 {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_query"})
|
||||
return
|
||||
}
|
||||
}
|
||||
scope := query.Get("scope")
|
||||
principal := p.UUID
|
||||
switch scope {
|
||||
case "", "self":
|
||||
case "domain":
|
||||
if !s.store.IsBuiltinGroupAdmin(domain, p.UUID, "domain_admin") {
|
||||
writeAuthQueryJSON(w, 403, map[string]string{"error": "domain_admin_required"})
|
||||
return
|
||||
}
|
||||
principal = ""
|
||||
default:
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_scope"})
|
||||
return
|
||||
}
|
||||
history := r.URL.Path == "/_/api/v1/auth/sessions/history"
|
||||
limit := 100
|
||||
if history {
|
||||
limit = 20
|
||||
}
|
||||
if values, ok := query["limit"]; ok {
|
||||
limit, err = strconv.Atoi(values[0])
|
||||
if err != nil || limit < 1 || limit > 100 {
|
||||
writeAuthQueryJSON(w, 400, map[string]string{"error": "invalid_limit"})
|
||||
return
|
||||
}
|
||||
}
|
||||
sessions, truncated, err := s.store.QueryAuthSessions(ctx, domain, principal, current.UUID, history, limit)
|
||||
if err != nil {
|
||||
writeAuthQueryJSON(w, 503, map[string]string{"error": "auth_query_unavailable"})
|
||||
return
|
||||
}
|
||||
writeAuthQueryJSON(w, 200, map[string]any{"sessions": sessions, "truncated": truncated})
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -245,6 +245,7 @@ func (s *Server) mustLoadTemplates() {
|
||||
}
|
||||
|
||||
func (s *Server) registerRoutes() {
|
||||
s.registerAuthQueryRoutes()
|
||||
s.registerCalendarRoutes()
|
||||
s.registerTaskRoutes()
|
||||
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
||||
|
||||
Reference in new issue
Block a user