117 lines
3.2 KiB
TypeScript
117 lines
3.2 KiB
TypeScript
import { query } from '../db/client.js'
|
|
|
|
export type AdminSessionRow = {
|
|
id: number
|
|
session_id: string
|
|
user_id: number
|
|
device_id: string
|
|
status: string
|
|
issued_at: string
|
|
last_seen_at: string
|
|
expires_at: string
|
|
revoked_at: string | null
|
|
created_at: string
|
|
updated_at: string
|
|
}
|
|
|
|
export async function createAdminSession(input: {
|
|
sessionId: string
|
|
userId: number
|
|
deviceId?: string
|
|
issuedAt: string
|
|
expiresAt: string
|
|
now: string
|
|
}): Promise<AdminSessionRow | null> {
|
|
const result = await query<AdminSessionRow>(
|
|
`
|
|
INSERT INTO admin_sessions (
|
|
session_id, user_id, device_id, status, issued_at, last_seen_at,
|
|
expires_at, revoked_at, created_at, updated_at
|
|
) VALUES ($1, $2, $3, 'active', $4, $4, $5, NULL, $6, $6)
|
|
RETURNING *
|
|
`,
|
|
[
|
|
input.sessionId,
|
|
input.userId,
|
|
String(input.deviceId || '').trim(),
|
|
input.issuedAt,
|
|
input.expiresAt,
|
|
input.now,
|
|
],
|
|
)
|
|
return result.rows[0] || null
|
|
}
|
|
|
|
export async function getActiveAdminSession(
|
|
sessionId: string,
|
|
now: string,
|
|
): Promise<AdminSessionRow | null> {
|
|
const result = await query<AdminSessionRow>(
|
|
`
|
|
SELECT *
|
|
FROM admin_sessions
|
|
WHERE session_id = $1
|
|
AND status = 'active'
|
|
AND expires_at > $2
|
|
LIMIT 1
|
|
`,
|
|
[sessionId, now],
|
|
)
|
|
return result.rows[0] || null
|
|
}
|
|
|
|
/** 活跃请求的写入节流:last_seen_at 与滑动过期最多 5 分钟刷新一次。 */
|
|
const ADMIN_SESSION_TOUCH_MIN_INTERVAL = '5 minutes'
|
|
|
|
/**
|
|
* 校验会话有效并滑动续期。
|
|
* 返回续期后的空闲到期时间;会话无效/已过期返回 null。
|
|
*/
|
|
export async function touchAdminSession(input: {
|
|
sessionId: string
|
|
now: string
|
|
idleExpiresAt: string
|
|
absoluteExpiresAt: string
|
|
}): Promise<string | null> {
|
|
const result = await query<{ expires_at: string | Date }>(
|
|
`
|
|
WITH target AS (
|
|
SELECT session_id, expires_at
|
|
FROM admin_sessions
|
|
WHERE session_id = $1 AND status = 'active' AND expires_at > $2
|
|
), touched AS (
|
|
UPDATE admin_sessions s
|
|
SET last_seen_at = $2,
|
|
updated_at = $2,
|
|
expires_at = LEAST($3::timestamptz, $4::timestamptz)
|
|
FROM target
|
|
WHERE s.session_id = target.session_id
|
|
AND s.last_seen_at < ($2::timestamptz - INTERVAL '${ADMIN_SESSION_TOUCH_MIN_INTERVAL}')
|
|
AND s.expires_at < LEAST($3::timestamptz, $4::timestamptz)
|
|
RETURNING s.expires_at
|
|
)
|
|
SELECT COALESCE(
|
|
(SELECT expires_at FROM touched),
|
|
(SELECT expires_at FROM target)
|
|
) AS expires_at
|
|
`,
|
|
[input.sessionId, input.now, input.idleExpiresAt, input.absoluteExpiresAt],
|
|
)
|
|
const expiresAt = result.rows[0]?.expires_at
|
|
if (!expiresAt) return null
|
|
const parsed = new Date(expiresAt).getTime()
|
|
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : null
|
|
}
|
|
|
|
export async function revokeAdminSession(sessionId: string, now: string): Promise<boolean> {
|
|
const result = await query(
|
|
`
|
|
UPDATE admin_sessions
|
|
SET status = 'revoked', revoked_at = $2, updated_at = $2
|
|
WHERE session_id = $1 AND status = 'active'
|
|
`,
|
|
[sessionId, now],
|
|
)
|
|
return result.rowCount === 1
|
|
}
|