10cf36ecee
后端(委托 Auth Broker,路径 A): - 删自建鉴权(OIDC exchange / 自签会话 / step-up / shortcut / web_sessions / accounts),cdrop 不再存任何凭证;鉴权中间件改读边缘注入的 X-Auth-Subject/Scope/Meta/Name/Roles 头(dev 旁路保留);Claims 加 Tier() / Guest() - internal/brokerclient:mint / revoke(带 X-Broker-App)/ refresh / ListSessions(R1 列举),直连内网、吊销幂等 统一会话模型“委派设备会话”(Delegated Device Sessions): - 每个客户端(浏览器 / 桌面 / 扫码设备)=一条带 meta(device_id) + label 的 broker 机器会话;Broker 作设备会话唯一注册表(R1 按用户+app 列举 + R2 按 (user,app,meta) 幂等铸造),cdrop 退化为薄覆盖层、不再自存权威会话表 - 新增代铸端点 POST /api/auth/device-session:凭边缘已验明的 X-Auth-Subject 委托 broker 铸 / 轮换设备会话(meta=device_id、按调用方 tier 防越权、sameOrigin CSRF、per-IP 限流);R2 幂等保证同一 device_id 重登原地轮换、不堆重复设备 - 会话列表=R1 权威 + 叠加 type(本地缓存)/ online(Hub presence,按设备名)/ current(meta 匹配本请求 X-Auth-Meta)+ 过滤 meta=""(device-authorize 引导会话残留);devices 表降级为 type/presence 薄缓存(非会话权威),device_id 主键、upsert 按 user 限定 - 吊销按 device_id → 缓存优先 / R1 兜底解析 sid → broker 吊销 + X-Broker-App;扫码登录保留三密钥编排,collect 改委托 broker 铸 + 落缓存行 Web 前端: - 登录走 broker 全局 SSO 代跳(/api/auth/login 302);bootstrap 经 /api/me 注入身份后代铸设备会话(稳定 device_id 存 localStorage、Web Locks 跨 tab 串行防重复铸造);refresh 走 /api/auth/refresh - 设备管理按 device_id;改名=同 device_id 重代铸(R2 原地轮换换 label、不产生重复行);登录页反应式守卫修登录回环 - 去 OIDC PKCE / step-up(删 oauth.callback / stepUp) 桌面客户端(Wails): - loopback PKCE(RFC 8252)改指 broker 设备授权流(/device/authorize + /device/token)拿引导令牌,再代铸出带 meta 的托管设备会话——与浏览器同模型、同管理、同吊销;身份取自代铸响应(修“显示名显示为 UUID”);refresh 保留显示名;稳定 device_id 入桌面配置 iOS 客户端(arch A,原生 SwiftUI + 离屏无头 WebView 引擎 + 原生↔JS 桥): - 引擎 / 文件管理 / 设备管理 / 应用图标 / 本地化(此前实现,随本次落入版本库) - 鉴权=引擎自刷(boot 注入 refresh_token)+ broker 轮换经 sessionRotated 回报原生更新 Keychain;去 cookie 同步;Session 加 refreshToken / deviceId 实时 / 健壮性: - presence 走 Hub union(设备表行 ∪ 表外实时连接,按名去重、live-only 标在线) - Hub 通道 close 一律在写锁内、非阻塞 send 一律在读锁内,消除 close-vs-send 闭通道 send panic(revoke 每次 Kick 后该路径变热) 配置 / 删旧栈: - config 改 broker 接入(CDROP_BROKER_* / CDROP_PUBLIC_URL / 按档 TTL),prod 强校验 broker 配置 + PUBLIC_URL(CSRF Origin 守卫不失效) - 删 auth.go / selftoken.go / shortcut.go / jwks.go + 三表(web_sessions / accounts / shortcut_tokens)及验证链;.env.example / compose.snippet.yaml / Caddyfile.snippet 更新为 broker 模型(人机分流 + 公开端点放行 + X-Auth-Meta 透传) - 测试全重写:QR / 会话含 mock broker(R1 列举 + R2 幂等);hub 加 close-vs-send 并发回归;config 加 prod 必填校验
513 lines
19 KiB
Go
513 lines
19 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"commilitia.net/cdrop/internal/brokerclient"
|
|
"commilitia.net/cdrop/internal/config"
|
|
"commilitia.net/cdrop/internal/db"
|
|
"commilitia.net/cdrop/internal/hub"
|
|
"commilitia.net/cdrop/internal/jwtauth"
|
|
)
|
|
|
|
// mockBrokerState records what the fake Auth Broker was asked to do, so tests can
|
|
// assert cdrop delegated correctly (mint params, revoke + its X-Broker-App scope).
|
|
type mockBrokerState struct {
|
|
mintCount int
|
|
lastMint map[string]any
|
|
revoked map[string]bool
|
|
lastRevokeApp string
|
|
// sessions holds the live (non-revoked) delegated sessions keyed by sid, so the mock
|
|
// can answer R1 (GET /internal/sessions) and enforce R2 idempotency (same user+app+meta
|
|
// → same sid).
|
|
sessions map[string]*mockSession
|
|
}
|
|
|
|
type mockSession struct {
|
|
sid, userID, app, meta, label, scope string
|
|
createdAt, lastUsedAt int64
|
|
}
|
|
|
|
// newMockBroker stands in for the Auth Broker's internal API: POST /internal/sessions
|
|
// mints (R2-idempotent by user+app+meta) a session, GET /internal/sessions lists the
|
|
// user's live machine sessions (R1), and DELETE /internal/sessions/{sid} revokes one.
|
|
func newMockBroker(t *testing.T) (*brokerclient.Client, *mockBrokerState) {
|
|
t.Helper()
|
|
st := &mockBrokerState{revoked: map[string]bool{}, sessions: map[string]*mockSession{}}
|
|
str := func(m map[string]any, k string) string {
|
|
if v, ok := m[k].(string); ok {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == http.MethodPost && r.URL.Path == "/internal/sessions":
|
|
st.mintCount += 1
|
|
var body map[string]any
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
st.lastMint = body
|
|
userID, app, meta, label, tier := str(body, "user_id"), str(body, "app"), str(body, "meta"), str(body, "label"), str(body, "tier")
|
|
scope := "app:" + app
|
|
if tier != "" {
|
|
scope += ":" + tier
|
|
}
|
|
// R2 idempotency: same (user, app, meta) with non-empty meta rotates in place.
|
|
sid := ""
|
|
if meta != "" {
|
|
for _, sess := range st.sessions {
|
|
if !st.revoked[sess.sid] && sess.userID == userID && sess.app == app && sess.meta == meta {
|
|
sid = sess.sid
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if sid == "" {
|
|
sid = fmt.Sprintf("sid-%d", st.mintCount)
|
|
}
|
|
now := time.Now().Unix()
|
|
created := now
|
|
if existing, ok := st.sessions[sid]; ok {
|
|
created = existing.createdAt // rotation preserves CreatedAt
|
|
}
|
|
st.sessions[sid] = &mockSession{sid: sid, userID: userID, app: app, meta: meta, label: label, scope: scope, createdAt: created, lastUsedAt: now}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
|
"id": sid, "app": "cdrop",
|
|
"access": "acc-" + sid, "refresh": "rtk-" + sid,
|
|
"access_expires": time.Now().Add(15 * time.Minute).Unix(),
|
|
"refresh_expires": time.Now().Add(24 * time.Hour).Unix(),
|
|
})
|
|
case r.Method == http.MethodGet && r.URL.Path == "/internal/sessions":
|
|
q := r.URL.Query()
|
|
userID, app := q.Get("user_id"), q.Get("app")
|
|
if userID == "" || app == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
out := []map[string]any{}
|
|
for _, sess := range st.sessions {
|
|
if st.revoked[sess.sid] || sess.userID != userID || sess.app != app {
|
|
continue
|
|
}
|
|
out = append(out, map[string]any{
|
|
"id": sess.sid, "scope": sess.scope, "label": sess.label, "meta": sess.meta,
|
|
"created_at": sess.createdAt, "last_used_at": sess.lastUsedAt,
|
|
"expires_at": time.Now().Add(24 * time.Hour).Unix(),
|
|
})
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"sessions": out})
|
|
case r.Method == http.MethodDelete && strings.HasPrefix(r.URL.Path, "/internal/sessions/"):
|
|
st.revoked[strings.TrimPrefix(r.URL.Path, "/internal/sessions/")] = true
|
|
st.lastRevokeApp = r.Header.Get("X-Broker-App")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return brokerclient.New(srv.URL, "test-key", "cdrop"), st
|
|
}
|
|
|
|
// newQRTestServer builds a Server with only the fields the scan-login + device
|
|
// handlers touch, backed by a fresh file-based sqlite and a mock broker.
|
|
func newQRTestServer(t *testing.T) (*Server, *mockBrokerState) {
|
|
t.Helper()
|
|
conn, err := db.Open(filepath.Join(t.TempDir(), "qr.db"))
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
if err := db.Bootstrap(context.Background(), conn); err != nil {
|
|
t.Fatalf("bootstrap: %v", err)
|
|
}
|
|
q := db.New(conn)
|
|
broker, st := newMockBroker(t)
|
|
s := &Server{
|
|
cfg: &config.Config{
|
|
AuthMode: "prod",
|
|
QRLoginEnabled: true,
|
|
QRRequestTTLSeconds: 120,
|
|
FullAccessTTLSeconds: 900,
|
|
FullRefreshTTLSeconds: 604800,
|
|
GuestAccessTTLSeconds: 900,
|
|
GuestRefreshTTLSeconds: 86400,
|
|
BrokerBaseURL: "http://broker", // non-empty so QRLoginOn() is true
|
|
},
|
|
queries: q,
|
|
hub: hub.New(q),
|
|
broker: broker,
|
|
siteOrigin: "https://drop.example.net",
|
|
}
|
|
return s, st
|
|
}
|
|
|
|
func qrStart(t *testing.T, s *Server, deviceName string) (qrStartResp, string) {
|
|
t.Helper()
|
|
body := fmt.Sprintf(`{"device_name":%q,"device_type":"browser"}`, deviceName)
|
|
r := httptest.NewRequest(http.MethodPost, "/api/auth/qr/start", strings.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
s.handleQRStart(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("qr/start: %d %s", w.Code, w.Body.String())
|
|
}
|
|
var resp qrStartResp
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode start: %v", err)
|
|
}
|
|
u, err := url.Parse(resp.QRPayload)
|
|
if err != nil {
|
|
t.Fatalf("qr_payload not a url: %v", err)
|
|
}
|
|
return resp, u.Query().Get("c")
|
|
}
|
|
|
|
func qrApprove(t *testing.T, s *Server, requestID, code, scope, persist, approver string) int {
|
|
t.Helper()
|
|
body := fmt.Sprintf(`{"request_id":%q,"approval_code":%q,"scope":%q,"persist":%q}`, requestID, code, scope, persist)
|
|
r := httptest.NewRequest(http.MethodPost, "/api/auth/qr/approve", strings.NewReader(body))
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: approver, Scope: "full"}))
|
|
w := httptest.NewRecorder()
|
|
s.handleQRApprove(w, r)
|
|
return w.Code
|
|
}
|
|
|
|
func qrStatus(s *Server, requestID, pollSecret string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(http.MethodGet, "/api/auth/qr/status?request_id="+url.QueryEscape(requestID), nil)
|
|
r.Header.Set("X-Poll-Secret", pollSecret)
|
|
w := httptest.NewRecorder()
|
|
s.handleQRStatus(w, r)
|
|
return w
|
|
}
|
|
|
|
func decodeStatus(t *testing.T, w *httptest.ResponseRecorder) qrStatusResp {
|
|
t.Helper()
|
|
var resp qrStatusResp
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode status: %v (%s)", err, w.Body.String())
|
|
}
|
|
return resp
|
|
}
|
|
|
|
// Happy path: a new device starts a request, the approver authorises it as a guest,
|
|
// and the new device collects a live guest session minted by the broker — broker
|
|
// access + refresh handed back, a device row recorded with the broker sid, the mint
|
|
// scoped to the device_id, and the request single-use thereafter.
|
|
func TestQRFlow_ApproveCollectGuestSingleUse(t *testing.T) {
|
|
s, st := newQRTestServer(t)
|
|
start, code := qrStart(t, s, "Borrowed Laptop")
|
|
|
|
if c := qrApprove(t, s, start.RequestID, code, "guest", "once", "approver-1"); c != http.StatusNoContent {
|
|
t.Fatalf("approve: got %d, want 204", c)
|
|
}
|
|
|
|
w := qrStatus(s, start.RequestID, start.PollSecret)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status: %d %s", w.Code, w.Body.String())
|
|
}
|
|
got := decodeStatus(t, w)
|
|
if got.Status != "approved" || got.AccessToken == "" || got.RefreshToken == "" {
|
|
t.Fatalf("collected status wrong: %+v", got)
|
|
}
|
|
if got.DeviceName != "Borrowed Laptop" || got.DeviceID == "" {
|
|
t.Fatalf("collected device wrong: %+v", got)
|
|
}
|
|
if !strings.HasPrefix(got.DeviceID, "dev_") {
|
|
t.Errorf("device_id not opaque dev_ token: %q", got.DeviceID)
|
|
}
|
|
|
|
// The broker was asked to mint at tier guest with our device_id as meta.
|
|
if st.mintCount != 1 {
|
|
t.Fatalf("mint count: got %d, want 1", st.mintCount)
|
|
}
|
|
if st.lastMint["tier"] != "guest" || st.lastMint["meta"] != got.DeviceID {
|
|
t.Errorf("mint params wrong: %+v", st.lastMint)
|
|
}
|
|
|
|
// A device row exists for the approver, tier guest, bound to the broker sid.
|
|
dev, err := s.queries.GetDevice(context.Background(), got.DeviceID)
|
|
if err != nil {
|
|
t.Fatalf("device row missing: %v", err)
|
|
}
|
|
if dev.UserID != "approver-1" || dev.Tier != "guest" || dev.BrokerSid != "sid-1" {
|
|
t.Errorf("device row wrong: %+v", dev)
|
|
}
|
|
|
|
// Single use: a second collection finds the request consumed.
|
|
if got2 := decodeStatus(t, qrStatus(s, start.RequestID, start.PollSecret)); got2.Status != "expired" {
|
|
t.Errorf("second collection: got %q, want expired (consumed)", got2.Status)
|
|
}
|
|
}
|
|
|
|
// A full approval mints a full-tier session.
|
|
func TestQRFlow_ApproveCollectFull(t *testing.T) {
|
|
s, st := newQRTestServer(t)
|
|
start, code := qrStart(t, s, "Home PC")
|
|
if c := qrApprove(t, s, start.RequestID, code, "full", "persist", "approver-2"); c != http.StatusNoContent {
|
|
t.Fatalf("approve: got %d, want 204", c)
|
|
}
|
|
got := decodeStatus(t, qrStatus(s, start.RequestID, start.PollSecret))
|
|
if got.Status != "approved" {
|
|
t.Fatalf("status: %+v", got)
|
|
}
|
|
if st.lastMint["tier"] != "full" {
|
|
t.Errorf("mint tier: got %v, want full", st.lastMint["tier"])
|
|
}
|
|
dev, err := s.queries.GetDevice(context.Background(), got.DeviceID)
|
|
if err != nil || dev.Tier != "full" {
|
|
t.Errorf("device row: %+v (err=%v)", dev, err)
|
|
}
|
|
}
|
|
|
|
func TestQRFlow_WrongPollSecretRejected(t *testing.T) {
|
|
s, _ := newQRTestServer(t)
|
|
start, code := qrStart(t, s, "Laptop")
|
|
_ = qrApprove(t, s, start.RequestID, code, "guest", "once", "approver-1")
|
|
w := qrStatus(s, start.RequestID, "not-the-secret")
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("wrong poll secret: got %d, want 403", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestQRFlow_Deny(t *testing.T) {
|
|
s, _ := newQRTestServer(t)
|
|
start, code := qrStart(t, s, "Laptop")
|
|
body := fmt.Sprintf(`{"request_id":%q,"approval_code":%q}`, start.RequestID, code)
|
|
r := httptest.NewRequest(http.MethodPost, "/api/auth/qr/deny", strings.NewReader(body))
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: "approver-1", Scope: "full"}))
|
|
w := httptest.NewRecorder()
|
|
s.handleQRDeny(w, r)
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("deny: got %d, want 204", w.Code)
|
|
}
|
|
if got := decodeStatus(t, qrStatus(s, start.RequestID, start.PollSecret)); got.Status != "denied" {
|
|
t.Errorf("after deny: got %q, want denied", got.Status)
|
|
}
|
|
}
|
|
|
|
func TestQRFlow_PendingLongPoll(t *testing.T) {
|
|
old := qrStatusPollWindow
|
|
qrStatusPollWindow = 50 * time.Millisecond
|
|
defer func() { qrStatusPollWindow = old }()
|
|
|
|
s, _ := newQRTestServer(t)
|
|
start, _ := qrStart(t, s, "Laptop")
|
|
got := decodeStatus(t, qrStatus(s, start.RequestID, start.PollSecret))
|
|
if got.Status != "pending" {
|
|
t.Errorf("unapproved long-poll: got %q, want pending", got.Status)
|
|
}
|
|
}
|
|
|
|
// requireFullSession lets a full session through and blocks a restricted guest.
|
|
func TestRequireFullSession_GuestBlockedFullPasses(t *testing.T) {
|
|
handler := requireFullSession(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
check := func(scope string) int {
|
|
r := httptest.NewRequest(http.MethodGet, "/x", nil)
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: "u", Scope: scope}))
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, r)
|
|
return w.Code
|
|
}
|
|
if c := check("app:cdrop:guest"); c != http.StatusForbidden {
|
|
t.Errorf("guest on full-only route: got %d, want 403", c)
|
|
}
|
|
if c := check("app:cdrop:full"); c != http.StatusOK {
|
|
t.Errorf("full on full-only route: got %d, want 200", c)
|
|
}
|
|
}
|
|
|
|
// Deleting a device revokes its broker session (scoped by X-Broker-App) and drops the
|
|
// local row.
|
|
func TestDeleteDevice_RevokesBrokerSession(t *testing.T) {
|
|
s, st := newQRTestServer(t)
|
|
start, code := qrStart(t, s, "Old Phone")
|
|
_ = qrApprove(t, s, start.RequestID, code, "full", "persist", "owner")
|
|
got := decodeStatus(t, qrStatus(s, start.RequestID, start.PollSecret))
|
|
if got.DeviceID == "" {
|
|
t.Fatal("no device_id from collect")
|
|
}
|
|
|
|
r := httptest.NewRequest(http.MethodDelete, "/api/devices/"+got.DeviceID, nil)
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: "owner", Scope: "full"}))
|
|
rctx := chi.NewRouteContext()
|
|
rctx.URLParams.Add("device_id", got.DeviceID)
|
|
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
|
|
w := httptest.NewRecorder()
|
|
s.handleDeleteDevice(w, r)
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("delete device: got %d, want 204 (%s)", w.Code, w.Body.String())
|
|
}
|
|
|
|
if !st.revoked["sid-1"] {
|
|
t.Error("broker session sid-1 was not revoked")
|
|
}
|
|
if st.lastRevokeApp != "cdrop" {
|
|
t.Errorf("revoke X-Broker-App: got %q, want cdrop", st.lastRevokeApp)
|
|
}
|
|
if _, err := s.queries.GetDevice(context.Background(), got.DeviceID); err == nil {
|
|
t.Error("device row should be gone after delete")
|
|
}
|
|
}
|
|
|
|
// The session list surfaces the user's devices with their tier as scope.
|
|
func TestSessionsList_ShowsDevices(t *testing.T) {
|
|
s, _ := newQRTestServer(t)
|
|
start, code := qrStart(t, s, "Tablet")
|
|
_ = qrApprove(t, s, start.RequestID, code, "guest", "once", "owner")
|
|
got := decodeStatus(t, qrStatus(s, start.RequestID, start.PollSecret))
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/api/auth/sessions", nil)
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: "owner", Scope: "app:cdrop:guest", DeviceID: got.DeviceID}))
|
|
w := httptest.NewRecorder()
|
|
s.handleSessionsList(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("sessions list: %d %s", w.Code, w.Body.String())
|
|
}
|
|
var resp struct {
|
|
Sessions []sessionView `json:"sessions"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if len(resp.Sessions) != 1 {
|
|
t.Fatalf("session count: got %d, want 1", len(resp.Sessions))
|
|
}
|
|
sv := resp.Sessions[0]
|
|
if sv.DeviceID != got.DeviceID || sv.Scope != "guest" || !sv.Current {
|
|
t.Errorf("session view wrong: %+v", sv)
|
|
}
|
|
}
|
|
|
|
// deviceSession drives the 代铸 endpoint with the given identity and returns the response.
|
|
func deviceSession(t *testing.T, s *Server, userID, scope, deviceID, name, dtype, origin string) deviceSessionResp {
|
|
t.Helper()
|
|
body := fmt.Sprintf(`{"device_id":%q,"device_name":%q,"device_type":%q}`, deviceID, name, dtype)
|
|
r := httptest.NewRequest(http.MethodPost, "/api/auth/device-session", strings.NewReader(body))
|
|
if origin != "" {
|
|
r.Header.Set("Origin", origin)
|
|
}
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: userID, Name: "Commilitia", Avatar: "https://example.net/a.png", Scope: scope}))
|
|
w := httptest.NewRecorder()
|
|
s.handleDeviceSession(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("device-session: %d %s", w.Code, w.Body.String())
|
|
}
|
|
var resp deviceSessionResp
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode device-session: %v", err)
|
|
}
|
|
return resp
|
|
}
|
|
|
|
// listSessions calls the session-management list for a caller riding device deviceID.
|
|
func listSessions(t *testing.T, s *Server, userID, scope, deviceID string) []sessionView {
|
|
t.Helper()
|
|
r := httptest.NewRequest(http.MethodGet, "/api/auth/sessions", nil)
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: userID, Scope: scope, DeviceID: deviceID}))
|
|
w := httptest.NewRecorder()
|
|
s.handleSessionsList(w, r)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("sessions list: %d %s", w.Code, w.Body.String())
|
|
}
|
|
var resp struct {
|
|
Sessions []sessionView `json:"sessions"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("decode list: %v", err)
|
|
}
|
|
return resp.Sessions
|
|
}
|
|
|
|
// 代铸 turns an edge-verified login into a managed device session that joins the unified
|
|
// list as the current device, with its type overlaid and the verified display name returned.
|
|
func TestDeviceSession_MintsManagedDevice(t *testing.T) {
|
|
s, st := newQRTestServer(t)
|
|
resp := deviceSession(t, s, "owner", "full", "dev_browser01", "Laptop", "browser", "")
|
|
if resp.AccessToken == "" || resp.RefreshToken == "" {
|
|
t.Fatal("device-session returned no tokens")
|
|
}
|
|
if resp.DeviceID != "dev_browser01" {
|
|
t.Errorf("device_id: got %q", resp.DeviceID)
|
|
}
|
|
if resp.Name != "Commilitia" {
|
|
t.Errorf("name: got %q, want the verified X-Auth-Name not the subject UUID", resp.Name)
|
|
}
|
|
if resp.Avatar != "https://example.net/a.png" {
|
|
t.Errorf("avatar: got %q, want the verified X-Auth-Avatar", resp.Avatar)
|
|
}
|
|
if st.lastMint["tier"] != "full" || st.lastMint["meta"] != "dev_browser01" || st.lastMint["label"] != "Laptop" {
|
|
t.Errorf("mint params: %+v", st.lastMint)
|
|
}
|
|
list := listSessions(t, s, "owner", "app:cdrop:full", "dev_browser01")
|
|
if len(list) != 1 || list[0].DeviceID != "dev_browser01" || list[0].Kind != "browser" || !list[0].Current {
|
|
t.Errorf("unified list wrong: %+v", list)
|
|
}
|
|
}
|
|
|
|
// Re-login with the same device_id rotates the one session (R2), not piling up duplicates,
|
|
// and adopts the latest label — the fix for the "duplicate phantom devices" regression.
|
|
func TestDeviceSession_Idempotent(t *testing.T) {
|
|
s, _ := newQRTestServer(t)
|
|
_ = deviceSession(t, s, "owner", "full", "dev_same01", "Laptop", "browser", "")
|
|
_ = deviceSession(t, s, "owner", "full", "dev_same01", "Laptop Renamed", "browser", "")
|
|
list := listSessions(t, s, "owner", "app:cdrop:full", "dev_same01")
|
|
if len(list) != 1 {
|
|
t.Fatalf("idempotent re-mint: got %d sessions, want 1", len(list))
|
|
}
|
|
if list[0].DeviceName != "Laptop Renamed" {
|
|
t.Errorf("rotation should adopt the latest label: got %q", list[0].DeviceName)
|
|
}
|
|
}
|
|
|
|
// A meta-less machine session (a desktop device-authorize bootstrap) must not surface as a
|
|
// phantom device — the unified list filters it out, leaving only真正的托管设备.
|
|
func TestSessionsList_FiltersMetalessBootstrap(t *testing.T) {
|
|
s, _ := newQRTestServer(t)
|
|
if _, err := s.broker.MintSession(context.Background(), brokerclient.MintParams{
|
|
UserID: "owner", Tier: "full", Label: "bootstrap",
|
|
}); err != nil {
|
|
t.Fatalf("bootstrap mint: %v", err)
|
|
}
|
|
_ = deviceSession(t, s, "owner", "full", "dev_real01", "Laptop", "browser", "")
|
|
list := listSessions(t, s, "owner", "app:cdrop:full", "dev_real01")
|
|
if len(list) != 1 || list[0].DeviceID != "dev_real01" {
|
|
t.Fatalf("metaless bootstrap not filtered: %+v", list)
|
|
}
|
|
}
|
|
|
|
// A cookie-authenticated mint must carry a same-origin Origin; a cross-site forgery (which
|
|
// would reintroduce phantom devices) is rejected.
|
|
func TestDeviceSession_RejectsCrossOrigin(t *testing.T) {
|
|
s, _ := newQRTestServer(t)
|
|
body := `{"device_id":"dev_x01","device_name":"X","device_type":"browser"}`
|
|
r := httptest.NewRequest(http.MethodPost, "/api/auth/device-session", strings.NewReader(body))
|
|
r.Header.Set("Origin", "https://evil.example.net")
|
|
r = r.WithContext(jwtauth.ContextWithClaims(r.Context(), &jwtauth.Claims{UserID: "owner", Scope: "full"}))
|
|
w := httptest.NewRecorder()
|
|
s.handleDeviceSession(w, r)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("cross-origin device-session: got %d, want 403", w.Code)
|
|
}
|
|
}
|
|
|
|
// A restricted guest minting its device session stays guest — no escalation to full.
|
|
func TestDeviceSession_GuestTierNotEscalated(t *testing.T) {
|
|
s, st := newQRTestServer(t)
|
|
_ = deviceSession(t, s, "owner", "app:cdrop:guest", "dev_guest01", "Borrowed", "browser", "")
|
|
if st.lastMint["tier"] != "guest" {
|
|
t.Errorf("guest caller minted tier %v, want guest", st.lastMint["tier"])
|
|
}
|
|
}
|