feat(devices): P2 sessions 表 + 在线/最后登录/客户端版本
ci-pangolin / Lint — shellcheck (push) Successful in 8s
ci-pangolin / OpenAPI Sync Check (push) Successful in 18s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Successful in 24s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 5s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 4s
ci-pangolin / Go — build + test (push) Successful in 11s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m13s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 14s

migration 000016(mysql+sqlite,含 down):新增 sessions 表(绑 device+refresh JTI)
+ devices 加 client_version/totp_trusted_until。devices 唯一键改 + platform CHECK
加 linux(需 SQLite 表重建)拆出后续迁移,降风险。

后端:新 internal/sessions Store(Create/Rotate/Revoke/RevokeByDevice/
LastLoginByDevice);TokenManager 外露 refresh JTI(IssueWithJTI/RefreshWithJTI/
ParseRefreshJTI);auth.Service 注入 SessionStore——登录建会话、刷新轮换、登出吊销;
DeviceRegistrar 返回 deviceID;ReportUsage 心跳 touch devices.last_seen(在线判定);
devices.ListDevices 经 LastLoginSource 注入返回 online(last_seen<3min)/client_version/
last_login;RegisterIfAbsent 存 client_version。
客户端:Device model 加 online/clientVersion/lastLogin(fromJson 自动解析)。
测试:sessions store 3 例 + ListDevices 在线/最后登录 + device model 2 例 +
migration v16;全量 go test/flutter test 绿。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-29 00:50:24 +08:00
parent 8370ee1eb7
commit 2f298f0a0a
23 changed files with 709 additions and 159 deletions
@@ -0,0 +1,50 @@
package store_test
import (
"context"
"testing"
"time"
"github.com/wangjia/pangolin/server/internal/devices"
"github.com/wangjia/pangolin/server/internal/sessions"
)
// ListDevices should derive online (last_seen<3min), client_version, and
// last_login (most recent session) per device.
func TestSQLite_ListDevices_OnlineAndLastLogin(t *testing.T) {
ctx := context.Background()
db := openSQLite(t)
if _, err := db.Exec(
`INSERT INTO users (id, uuid, email, pw_hash, dp_uuid, status) VALUES (1,'u','u@e.com','h','dp','active')`); err != nil {
t.Fatalf("seed user: %v", err)
}
now := time.Now().UTC()
old := now.Add(-time.Hour)
// device 10: online (last_seen now) + client_version; device 20: offline.
db.Exec(`INSERT INTO devices (id, uuid, user_id, name, platform, last_seen, client_version) VALUES (10,'d10',1,'Mac','macos',?,?)`, now, "v1.0.10")
db.Exec(`INSERT INTO devices (id, uuid, user_id, name, platform, last_seen) VALUES (20,'d20',1,'PC','windows',?)`, old)
// one login session for device 10.
db.Exec(`INSERT INTO sessions (user_id, device_id, refresh_jti, created_at) VALUES (1,10,'j',?)`, now)
svc := devices.NewService(devices.NewStore(db), nil)
svc.SetLastLoginSource(sessions.NewStore(db))
list, apiErr := svc.ListDevices(ctx, 1)
if apiErr != nil {
t.Fatalf("ListDevices: %v", apiErr)
}
if len(list) != 2 {
t.Fatalf("want 2 devices, got %d", len(list))
}
by := map[string]devices.Device{}
for _, d := range list {
by[d.UUID] = d
}
if d := by["d10"]; !d.Online || d.ClientVersion != "v1.0.10" || d.LastLogin == nil {
t.Errorf("d10 wrong: online=%v ver=%q lastLogin=%v", d.Online, d.ClientVersion, d.LastLogin)
}
if d := by["d20"]; d.Online || d.LastLogin != nil {
t.Errorf("d20 wrong: online=%v lastLogin=%v", d.Online, d.LastLogin)
}
}
@@ -0,0 +1,124 @@
package store_test
import (
"context"
"database/sql"
"testing"
"time"
"github.com/wangjia/pangolin/server/internal/sessions"
)
// seedUserDevice inserts a user + device so sessions FKs are satisfiable.
func seedUserDevice(t *testing.T, db *sql.DB, userID, deviceID int64) {
t.Helper()
if _, err := db.Exec(
`INSERT INTO users (id, uuid, email, pw_hash, dp_uuid, status) VALUES (?,?,?,?,?,?)`,
userID, "u-uuid", "u@example.com", "h", "dp-uuid", "active"); err != nil {
t.Fatalf("seed user: %v", err)
}
if _, err := db.Exec(
`INSERT INTO devices (id, uuid, user_id, name, platform) VALUES (?,?,?,?,?)`,
deviceID, "d-uuid", userID, "MacBook", "macos"); err != nil {
t.Fatalf("seed device: %v", err)
}
}
func TestSQLite_Sessions_CreateRotateRevoke(t *testing.T) {
ctx := context.Background()
db := openSQLite(t)
seedUserDevice(t, db, 1, 10)
ss := sessions.NewStore(db)
if err := ss.Create(ctx, 1, 10, "jti-1", "1.2.3.4", "v1.0.10"); err != nil {
t.Fatalf("Create: %v", err)
}
var jti, ver, ip string
var revoked sql.NullTime
if err := db.QueryRow(
`SELECT refresh_jti, client_version, client_ip, revoked_at FROM sessions WHERE user_id=1`,
).Scan(&jti, &ver, &ip, &revoked); err != nil {
t.Fatalf("read: %v", err)
}
if jti != "jti-1" || ver != "v1.0.10" || ip != "1.2.3.4" || revoked.Valid {
t.Fatalf("bad session: jti=%s ver=%s ip=%s revoked=%v", jti, ver, ip, revoked.Valid)
}
// Rotate jti-1 → jti-2.
if err := ss.Rotate(ctx, "jti-1", "jti-2"); err != nil {
t.Fatalf("Rotate: %v", err)
}
var cnt int
db.QueryRow(`SELECT COUNT(*) FROM sessions WHERE refresh_jti='jti-2' AND revoked_at IS NULL`).Scan(&cnt)
if cnt != 1 {
t.Fatalf("rotate did not move jti: cnt=%d", cnt)
}
// Revoke jti-2.
if err := ss.Revoke(ctx, "jti-2"); err != nil {
t.Fatalf("Revoke: %v", err)
}
db.QueryRow(`SELECT COUNT(*) FROM sessions WHERE refresh_jti='jti-2' AND revoked_at IS NOT NULL`).Scan(&cnt)
if cnt != 1 {
t.Fatalf("revoke did not set revoked_at")
}
}
func TestSQLite_Sessions_RevokeByDevice(t *testing.T) {
ctx := context.Background()
db := openSQLite(t)
seedUserDevice(t, db, 1, 10)
ss := sessions.NewStore(db)
for _, j := range []string{"a", "b", "c"} {
if err := ss.Create(ctx, 1, 10, j, "", ""); err != nil {
t.Fatalf("create %s: %v", j, err)
}
}
// Pre-revoke one so it isn't returned/double-counted.
if err := ss.Revoke(ctx, "c"); err != nil {
t.Fatalf("pre-revoke: %v", err)
}
jtis, err := ss.RevokeByDevice(ctx, 1, 10)
if err != nil {
t.Fatalf("RevokeByDevice: %v", err)
}
if len(jtis) != 2 {
t.Fatalf("expected 2 active jtis revoked, got %v", jtis)
}
var active int
db.QueryRow(`SELECT COUNT(*) FROM sessions WHERE device_id=10 AND revoked_at IS NULL`).Scan(&active)
if active != 0 {
t.Fatalf("device still has %d active sessions", active)
}
}
func TestSQLite_Sessions_LastLoginByDevice(t *testing.T) {
ctx := context.Background()
db := openSQLite(t)
seedUserDevice(t, db, 1, 10)
// second device
db.Exec(`INSERT INTO devices (id, uuid, user_id, name, platform) VALUES (?,?,?,?,?)`,
20, "d-uuid-2", 1, "iPhone", "ios")
older := time.Date(2026, 6, 1, 8, 0, 0, 0, time.UTC)
newer := time.Date(2026, 6, 28, 9, 0, 0, 0, time.UTC)
// device 10: two logins, newer should win.
db.Exec(`INSERT INTO sessions (user_id, device_id, refresh_jti, created_at) VALUES (1,10,'o',?)`, older)
db.Exec(`INSERT INTO sessions (user_id, device_id, refresh_jti, created_at) VALUES (1,10,'n',?)`, newer)
// device 20: one login.
db.Exec(`INSERT INTO sessions (user_id, device_id, refresh_jti, created_at) VALUES (1,20,'x',?)`, older)
ss := sessions.NewStore(db)
m, err := ss.LastLoginByDevice(ctx, 1)
if err != nil {
t.Fatalf("LastLoginByDevice: %v", err)
}
if !m[10].Equal(newer) {
t.Fatalf("device 10 last login = %v, want %v", m[10], newer)
}
if !m[20].Equal(older) {
t.Fatalf("device 20 last login = %v, want %v", m[20], older)
}
}
+3 -3
View File
@@ -29,8 +29,8 @@ func TestSQLiteMigrateUpDown(t *testing.T) {
if dirty {
t.Fatalf("schema dirty after MigrateUp")
}
if v != 15 {
t.Errorf("version = %d, want 15", v)
if v != 16 {
t.Errorf("version = %d, want 16", v)
}
// 2. Core tables exist.
@@ -38,7 +38,7 @@ func TestSQLiteMigrateUpDown(t *testing.T) {
"users", "devices", "plans", "subscriptions", "code_batches", "codes",
"usage_daily", "audit_log", "providers", "nodes", "node_events",
"directory_version", "provision_idempotency", "replacements", "admins",
"connect_credentials", "usage_device_daily",
"connect_credentials", "usage_device_daily", "sessions",
} {
var name string
err := db.QueryRow(