feat(dcauth): 新增后台手机号与设备认证登录
登录链路改为账号密码 + 短信验证码 + DCAuth 设备签名,并新增后台手机号绑定、 登录日志、设备绑定与重置接口。
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
import { query } from '../db/client.js'
|
||||
|
||||
export type AdminDeviceChallengeRow = {
|
||||
id: number
|
||||
challenge_id: string
|
||||
app: string
|
||||
purpose: string
|
||||
challenge: string
|
||||
poll_token_hash: string
|
||||
status: string
|
||||
user_id: number | null
|
||||
device_id: string
|
||||
public_key: string
|
||||
machine_code_hash: string
|
||||
machine_code_mask: string
|
||||
client_version: string
|
||||
device_ticket_hash: string
|
||||
expires_at: string
|
||||
authorized_at: string | null
|
||||
ticket_issued_at: string | null
|
||||
consumed_at: string | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export type AdminUserDeviceRow = {
|
||||
id: number
|
||||
user_id: number
|
||||
device_id: string
|
||||
public_key: string
|
||||
machine_code_hash: string
|
||||
machine_code_mask: string
|
||||
client_version: string
|
||||
status: string
|
||||
bound_at: string
|
||||
last_used_at: string | null
|
||||
revoked_at: string | null
|
||||
revoked_by: number | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export async function createAdminDeviceChallenge(input: {
|
||||
challengeId: string
|
||||
app: string
|
||||
purpose: string
|
||||
challenge: string
|
||||
pollTokenHash: string
|
||||
userId: number | null
|
||||
expiresAt: string
|
||||
now: string
|
||||
}): Promise<AdminDeviceChallengeRow | null> {
|
||||
const result = await query<AdminDeviceChallengeRow>(
|
||||
`
|
||||
INSERT INTO admin_device_challenges (
|
||||
challenge_id, app, purpose, challenge, poll_token_hash, status, user_id,
|
||||
expires_at, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, $5, 'pending', $6, $7, $8, $8)
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.challengeId,
|
||||
input.app,
|
||||
input.purpose,
|
||||
input.challenge,
|
||||
input.pollTokenHash,
|
||||
input.userId,
|
||||
input.expiresAt,
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getAdminDeviceChallenge(
|
||||
challengeId: string,
|
||||
): Promise<AdminDeviceChallengeRow | null> {
|
||||
const result = await query<AdminDeviceChallengeRow>(
|
||||
`SELECT * FROM admin_device_challenges WHERE challenge_id = $1 LIMIT 1`,
|
||||
[challengeId],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function authorizeAdminDeviceChallenge(input: {
|
||||
challengeId: string
|
||||
app: string
|
||||
deviceId: string
|
||||
publicKey: string
|
||||
machineCodeHash: string
|
||||
machineCodeMask: string
|
||||
clientVersion: string
|
||||
now: string
|
||||
}): Promise<AdminDeviceChallengeRow | null> {
|
||||
const result = await query<AdminDeviceChallengeRow>(
|
||||
`
|
||||
UPDATE admin_device_challenges
|
||||
SET
|
||||
status = 'authorized',
|
||||
device_id = $3,
|
||||
public_key = $4,
|
||||
machine_code_hash = $5,
|
||||
machine_code_mask = $6,
|
||||
client_version = $7,
|
||||
authorized_at = $8,
|
||||
updated_at = $8
|
||||
WHERE challenge_id = $1
|
||||
AND app = $2
|
||||
AND status = 'pending'
|
||||
AND expires_at > $8
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.challengeId,
|
||||
input.app,
|
||||
input.deviceId,
|
||||
input.publicKey,
|
||||
input.machineCodeHash,
|
||||
input.machineCodeMask,
|
||||
input.clientVersion,
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function issueAdminDeviceTicket(input: {
|
||||
challengeId: string
|
||||
ticketHash: string
|
||||
now: string
|
||||
}): Promise<boolean> {
|
||||
const result = await query(
|
||||
`
|
||||
UPDATE admin_device_challenges
|
||||
SET
|
||||
status = 'ticket_issued',
|
||||
device_ticket_hash = $2,
|
||||
ticket_issued_at = $3,
|
||||
updated_at = $3
|
||||
WHERE challenge_id = $1
|
||||
AND status = 'authorized'
|
||||
AND expires_at > $3
|
||||
`,
|
||||
[input.challengeId, input.ticketHash, input.now],
|
||||
)
|
||||
return result.rowCount === 1
|
||||
}
|
||||
|
||||
export async function consumeAdminDeviceTicket(input: {
|
||||
challengeId: string
|
||||
pollTokenHash: string
|
||||
ticketHash: string
|
||||
now: string
|
||||
}): Promise<AdminDeviceChallengeRow | null> {
|
||||
const result = await query<AdminDeviceChallengeRow>(
|
||||
`
|
||||
UPDATE admin_device_challenges
|
||||
SET status = 'consumed', consumed_at = $4, updated_at = $4
|
||||
WHERE challenge_id = $1
|
||||
AND poll_token_hash = $2
|
||||
AND device_ticket_hash = $3
|
||||
AND status = 'ticket_issued'
|
||||
AND expires_at > $4
|
||||
RETURNING *
|
||||
`,
|
||||
[input.challengeId, input.pollTokenHash, input.ticketHash, input.now],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getActiveAdminUserDevice(
|
||||
userId: number | string,
|
||||
): Promise<AdminUserDeviceRow | null> {
|
||||
const result = await query<AdminUserDeviceRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM admin_user_devices
|
||||
WHERE user_id = $1 AND status = 'active'
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`,
|
||||
[Number(userId)],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function bindAdminUserDevice(input: {
|
||||
userId: number
|
||||
deviceId: string
|
||||
publicKey: string
|
||||
machineCodeHash: string
|
||||
machineCodeMask: string
|
||||
clientVersion: string
|
||||
now: string
|
||||
}): Promise<AdminUserDeviceRow | null> {
|
||||
const result = await query<AdminUserDeviceRow>(
|
||||
`
|
||||
INSERT INTO admin_user_devices (
|
||||
user_id, device_id, public_key, machine_code_hash, machine_code_mask,
|
||||
client_version, status, bound_at, last_used_at, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, 'active', $7, $7, $7, $7)
|
||||
ON CONFLICT (user_id) WHERE status = 'active'
|
||||
DO UPDATE SET
|
||||
device_id = EXCLUDED.device_id,
|
||||
public_key = EXCLUDED.public_key,
|
||||
machine_code_hash = EXCLUDED.machine_code_hash,
|
||||
machine_code_mask = EXCLUDED.machine_code_mask,
|
||||
client_version = EXCLUDED.client_version,
|
||||
bound_at = EXCLUDED.bound_at,
|
||||
last_used_at = EXCLUDED.last_used_at,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.userId,
|
||||
input.deviceId,
|
||||
input.publicKey,
|
||||
input.machineCodeHash,
|
||||
input.machineCodeMask,
|
||||
input.clientVersion,
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function touchAdminUserDevice(deviceId: string, now: string): Promise<void> {
|
||||
await query(
|
||||
`
|
||||
UPDATE admin_user_devices
|
||||
SET last_used_at = $2, updated_at = $2
|
||||
WHERE device_id = $1 AND status = 'active'
|
||||
`,
|
||||
[deviceId, now],
|
||||
)
|
||||
}
|
||||
|
||||
export async function resetAdminUserDevice(
|
||||
userId: number | string,
|
||||
revokedBy: number | null,
|
||||
now: string,
|
||||
): Promise<boolean> {
|
||||
const result = await query(
|
||||
`
|
||||
UPDATE admin_user_devices
|
||||
SET status = 'revoked', revoked_at = $2, revoked_by = $3, updated_at = $2
|
||||
WHERE user_id = $1 AND status = 'active'
|
||||
`,
|
||||
[Number(userId), now, revokedBy],
|
||||
)
|
||||
return Number(result.rowCount || 0) > 0
|
||||
}
|
||||
@@ -12,6 +12,8 @@ export type AdminLoginLogRow = {
|
||||
user_agent: string
|
||||
success: boolean
|
||||
failure_reason: string
|
||||
login_method: string
|
||||
machine_code_mask: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
@@ -24,6 +26,8 @@ type AdminLoginLogCreateInput = {
|
||||
userAgent?: string
|
||||
success?: boolean
|
||||
failureReason?: string
|
||||
loginMethod?: string
|
||||
machineCodeMask?: string
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
@@ -49,8 +53,10 @@ export async function createAdminLoginLog(
|
||||
user_agent,
|
||||
success,
|
||||
failure_reason,
|
||||
login_method,
|
||||
machine_code_mask,
|
||||
created_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
@@ -62,6 +68,8 @@ export async function createAdminLoginLog(
|
||||
String(input.userAgent || '').trim(),
|
||||
input.success !== false,
|
||||
String(input.failureReason || '').trim(),
|
||||
String(input.loginMethod || 'password').trim() || 'password',
|
||||
String(input.machineCodeMask || '').trim(),
|
||||
input.createdAt,
|
||||
],
|
||||
)
|
||||
|
||||
@@ -4,6 +4,7 @@ export type AdminSessionRow = {
|
||||
id: number
|
||||
session_id: string
|
||||
user_id: number
|
||||
device_id: string
|
||||
status: string
|
||||
issued_at: string
|
||||
last_seen_at: string
|
||||
@@ -16,6 +17,7 @@ export type AdminSessionRow = {
|
||||
export async function createAdminSession(input: {
|
||||
sessionId: string
|
||||
userId: number
|
||||
deviceId?: string
|
||||
issuedAt: string
|
||||
expiresAt: string
|
||||
now: string
|
||||
@@ -23,12 +25,19 @@ export async function createAdminSession(input: {
|
||||
const result = await query<AdminSessionRow>(
|
||||
`
|
||||
INSERT INTO admin_sessions (
|
||||
session_id, user_id, status, issued_at, last_seen_at,
|
||||
session_id, user_id, device_id, status, issued_at, last_seen_at,
|
||||
expires_at, revoked_at, created_at, updated_at
|
||||
) VALUES ($1, $2, 'active', $3, $3, $4, NULL, $5, $5)
|
||||
) VALUES ($1, $2, $3, 'active', $4, $4, $5, NULL, $6, $6)
|
||||
RETURNING *
|
||||
`,
|
||||
[input.sessionId, input.userId, input.issuedAt, input.expiresAt, input.now],
|
||||
[
|
||||
input.sessionId,
|
||||
input.userId,
|
||||
String(input.deviceId || '').trim(),
|
||||
input.issuedAt,
|
||||
input.expiresAt,
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { query } from '../db/client.js'
|
||||
|
||||
export type AdminSmsCodeRow = {
|
||||
id: number
|
||||
user_id: number
|
||||
phone: string
|
||||
purpose: string
|
||||
code_hash: string
|
||||
expires_at: string
|
||||
consumed_at: string | null
|
||||
failed_attempt_count: number
|
||||
request_ip: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export async function createAdminSmsCode(input: {
|
||||
userId: number
|
||||
phone: string
|
||||
purpose: string
|
||||
codeHash: string
|
||||
expiresAt: string
|
||||
requestIp: string
|
||||
now: string
|
||||
}): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
INSERT INTO admin_sms_codes (
|
||||
user_id, phone, purpose, code_hash, expires_at, consumed_at,
|
||||
failed_attempt_count, request_ip, created_at
|
||||
) VALUES ($1, $2, $3, $4, $5, NULL, 0, $6, $7)
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
input.userId,
|
||||
input.phone,
|
||||
input.purpose,
|
||||
input.codeHash,
|
||||
input.expiresAt,
|
||||
input.requestIp,
|
||||
input.now,
|
||||
],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getLatestAdminSmsCode(
|
||||
userId: number,
|
||||
purpose: string,
|
||||
): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
SELECT *
|
||||
FROM admin_sms_codes
|
||||
WHERE user_id = $1 AND purpose = $2
|
||||
ORDER BY id DESC
|
||||
LIMIT 1
|
||||
`,
|
||||
[userId, purpose],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function consumeAdminSmsCode(
|
||||
codeId: number,
|
||||
now: string,
|
||||
): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
UPDATE admin_sms_codes
|
||||
SET consumed_at = $1
|
||||
WHERE id = $2 AND consumed_at IS NULL
|
||||
RETURNING *
|
||||
`,
|
||||
[now, codeId],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function recordAdminSmsCodeFailure(
|
||||
codeId: number,
|
||||
now: string,
|
||||
maxAttempts: number,
|
||||
): Promise<AdminSmsCodeRow | null> {
|
||||
const result = await query<AdminSmsCodeRow>(
|
||||
`
|
||||
UPDATE admin_sms_codes
|
||||
SET
|
||||
failed_attempt_count = COALESCE(failed_attempt_count, 0) + 1,
|
||||
consumed_at = CASE
|
||||
WHEN COALESCE(failed_attempt_count, 0) + 1 >= $3 THEN $2
|
||||
ELSE consumed_at
|
||||
END
|
||||
WHERE id = $1 AND consumed_at IS NULL
|
||||
RETURNING *
|
||||
`,
|
||||
[codeId, now, Math.max(1, Math.floor(maxAttempts))],
|
||||
)
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function countAdminSmsCodesOnDay(
|
||||
userId: number,
|
||||
dayStartIso: string,
|
||||
dayEndIso: string,
|
||||
): Promise<number> {
|
||||
const result = await query<{ total: number }>(
|
||||
`
|
||||
SELECT COUNT(*)::int AS total
|
||||
FROM admin_sms_codes
|
||||
WHERE user_id = $1
|
||||
AND created_at >= $2
|
||||
AND created_at < $3
|
||||
`,
|
||||
[userId, dayStartIso, dayEndIso],
|
||||
)
|
||||
return Number(result.rows[0]?.total || 0)
|
||||
}
|
||||
@@ -7,9 +7,17 @@ type AdminUserRow = {
|
||||
password_hash: string
|
||||
role: string
|
||||
status: string
|
||||
phone: string
|
||||
phone_bound_at: string | null
|
||||
phone_bound_by: number | null
|
||||
test_account: boolean
|
||||
session_version: number
|
||||
created_at: string
|
||||
updated_at: string
|
||||
device_id?: string | null
|
||||
machine_code_mask?: string | null
|
||||
device_bound_at?: string | null
|
||||
device_last_used_at?: string | null
|
||||
}
|
||||
|
||||
type AdminUserCreateInput = {
|
||||
@@ -18,6 +26,10 @@ type AdminUserCreateInput = {
|
||||
passwordHash: string
|
||||
role: string
|
||||
status: string
|
||||
phone?: string
|
||||
phoneBoundAt?: string | null
|
||||
phoneBoundBy?: number | null
|
||||
testAccount?: boolean
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
}
|
||||
@@ -30,6 +42,10 @@ type AdminUserPatch = Partial<
|
||||
| 'password_hash'
|
||||
| 'role'
|
||||
| 'status'
|
||||
| 'phone'
|
||||
| 'phone_bound_at'
|
||||
| 'phone_bound_by'
|
||||
| 'test_account'
|
||||
| 'session_version'
|
||||
| 'updated_at'
|
||||
>
|
||||
@@ -49,8 +65,15 @@ type AdminUserListResult = {
|
||||
}
|
||||
|
||||
const ADMIN_USER_SELECT = `
|
||||
SELECT au.*
|
||||
SELECT
|
||||
au.*,
|
||||
d.device_id AS device_id,
|
||||
d.machine_code_mask AS machine_code_mask,
|
||||
d.bound_at AS device_bound_at,
|
||||
d.last_used_at AS device_last_used_at
|
||||
FROM admin_users au
|
||||
LEFT JOIN admin_user_devices d
|
||||
ON d.user_id = au.id AND d.status = 'active'
|
||||
`
|
||||
|
||||
export async function getAdminUserById(userId: number | string): Promise<AdminUserRow | null> {
|
||||
@@ -69,6 +92,13 @@ export async function getAdminUserByUsername(username: string): Promise<AdminUse
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function getAdminUserByPhone(phone: string): Promise<AdminUserRow | null> {
|
||||
const result = await query<AdminUserRow>(`${ADMIN_USER_SELECT} WHERE au.phone = $1 LIMIT 1`, [
|
||||
String(phone || '').trim(),
|
||||
])
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function createAdminUser(input: AdminUserCreateInput): Promise<AdminUserRow | null> {
|
||||
const result = await query<{ [column: string]: unknown; id: number }>(
|
||||
`
|
||||
@@ -78,10 +108,14 @@ export async function createAdminUser(input: AdminUserCreateInput): Promise<Admi
|
||||
password_hash,
|
||||
role,
|
||||
status,
|
||||
phone,
|
||||
phone_bound_at,
|
||||
phone_bound_by,
|
||||
test_account,
|
||||
session_version,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||
RETURNING id
|
||||
`,
|
||||
[
|
||||
@@ -90,6 +124,10 @@ export async function createAdminUser(input: AdminUserCreateInput): Promise<Admi
|
||||
input.passwordHash,
|
||||
input.role,
|
||||
input.status,
|
||||
String(input.phone || '').trim(),
|
||||
input.phoneBoundAt || null,
|
||||
input.phoneBoundBy ?? null,
|
||||
input.testAccount === true,
|
||||
1,
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
@@ -118,9 +156,13 @@ export async function updateAdminUser(
|
||||
password_hash = $3,
|
||||
role = $4,
|
||||
status = $5,
|
||||
session_version = $6,
|
||||
updated_at = $7
|
||||
WHERE id = $8
|
||||
phone = $6,
|
||||
phone_bound_at = $7,
|
||||
phone_bound_by = $8,
|
||||
test_account = $9,
|
||||
session_version = $10,
|
||||
updated_at = $11
|
||||
WHERE id = $12
|
||||
RETURNING id
|
||||
`,
|
||||
[
|
||||
@@ -129,6 +171,10 @@ export async function updateAdminUser(
|
||||
next.password_hash,
|
||||
next.role,
|
||||
next.status,
|
||||
String(next.phone || '').trim(),
|
||||
next.phone_bound_at || null,
|
||||
next.phone_bound_by ?? null,
|
||||
next.test_account === true,
|
||||
Number(next.session_version || 1),
|
||||
next.updated_at,
|
||||
Number(userId),
|
||||
@@ -151,7 +197,9 @@ export async function listAdminUsers({
|
||||
|
||||
if (keyword) {
|
||||
params.push(`%${keyword}%`)
|
||||
filters.push(`(au.username ILIKE $${params.length} OR au.display_name ILIKE $${params.length})`)
|
||||
filters.push(
|
||||
`(au.username ILIKE $${params.length} OR au.display_name ILIKE $${params.length} OR au.phone ILIKE $${params.length})`,
|
||||
)
|
||||
}
|
||||
|
||||
if (role) {
|
||||
|
||||
Reference in New Issue
Block a user