2f298f0a0a
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>
125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
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)
|
|
}
|
|
}
|