feat(api): password change endpoint and source device id on uploads
P3 Task 3 契约增补批次(单一事实来源 openapi.yaml + cn 实现 + e2e):
- POST /v1/auth/password-change(operationId changePassword,需求 4.1 的
P1 契约缺口补齐):Bearer + {oldPassword 1-128, newPassword 8-128} → 204;
旧密码错误 401 AUTH_INVALID_CREDENTIALS;成功后事务内更新 argon2 哈希、
撤销该用户除当前会话外的全部其他会话(当前会话保留,本人不下线)、写
审计 auth.password_change。
- sourceDeviceId(A19 设备来源语义):UploadInitRequest 可选 8-64 位设备
标识;upload_sessions 落列(VARCHAR(64) utf8mb4_0900_bin,可空,存量
行 NULL 兼容);GET /uploads 条目回显;幂等重放不比对(first-writer,
首次落库值不随重放漂移)。
- e2e:auth 增 4 例(未认证 401 / 新密码过短 400 / 旧密码错误 401 且会话
无副作用 / 成功后他端会话撤销当前保留 + 新旧密码登录裁决 + 审计);
uploads 增 1 例(携带落库回显、换设备重放保留首值、缺省 null、长度
越界 400)。api e2e 179 → 184;contracts gen/redocly/单测、worker、
sync 门全绿。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// services/api/src/auth/auth.service.ts —— 登录 / 滑动续期 / 注销核心逻辑(Task 13)。
|
||||
// services/api/src/auth/auth.service.ts —— 登录 / 滑动续期 / 注销 / 修改密码核心逻辑
|
||||
// (Task 13;changePassword 为 P3 Task 3 契约增补)。
|
||||
// 会话模型:长效单令牌 + 滑动续期(用户决策 2026-09-17,不做 access/refresh 双层)。
|
||||
// 令牌 = 32B randomBytes hex(仅登录响应出现一次);库内只存 sha256(token)。
|
||||
// 安全要点:
|
||||
@@ -9,7 +10,8 @@
|
||||
// 成功登录清零。进程内存实现:P1a 单实例足够;P5 多实例需换 Redis 后端限速器
|
||||
// (本类私有接口不变,替换实现即可)。
|
||||
// · 审计——登录成功写 audit_logs(action='auth.login', actor=用户);
|
||||
// 注销不写审计(会话行 revokedAt 本身即持久的注销凭据)。
|
||||
// 注销不写审计(会话行 revokedAt 本身即持久的注销凭据);
|
||||
// 修改密码写 audit_logs(action='auth.password_change')(敏感凭据变更)。
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { ErrorCodes } from '@photo/contracts';
|
||||
@@ -206,4 +208,61 @@ export class AuthService {
|
||||
data: { revokedAt: new Date() },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码(P3 Task 3 契约增补,需求 4.1):
|
||||
* ① 校验旧密码——错误时 401 AUTH_INVALID_CREDENTIALS(契约该端点 401 语义
|
||||
* 即「旧密码错误」)。调用方已认证(SessionGuard),无需 dummy-verify
|
||||
* 拉平时延——用户身份已知,不存在账号枚举面。
|
||||
* ② 事务三写:更新 passwordHash + 撤销该用户除当前会话外的全部其他会话
|
||||
* (改密不应把本人踢下线,但旧凭据泄露面要即刻收敛)+ 审计
|
||||
* auth.password_change。哈希在事务外先算好(argon2 耗时,不占连接)。
|
||||
*/
|
||||
async changePassword(
|
||||
userId: string,
|
||||
sessionId: string,
|
||||
oldPassword: string,
|
||||
newPassword: string,
|
||||
): Promise<void> {
|
||||
const user = await this.prisma.users.findUnique({
|
||||
where: { id: userId },
|
||||
select: { passwordHash: true },
|
||||
});
|
||||
if (user === null) {
|
||||
// 守卫已确保用户存在且资格合格;不可达分支按同码兜底(不泄露差异)。
|
||||
throw new ApiException(
|
||||
ErrorCodes.AUTH_INVALID_CREDENTIALS,
|
||||
'Old password is incorrect',
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
);
|
||||
}
|
||||
if (!(await verifyPassword(user.passwordHash, oldPassword))) {
|
||||
throw new ApiException(
|
||||
ErrorCodes.AUTH_INVALID_CREDENTIALS,
|
||||
'Old password is incorrect',
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
);
|
||||
}
|
||||
|
||||
const passwordHash = await hashPassword(newPassword);
|
||||
const revokedAt = new Date();
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.users.update({
|
||||
where: { id: userId },
|
||||
data: { passwordHash },
|
||||
}),
|
||||
// 只撤销「其他」会话且只动尚未撤销的行(幂等;当前会话保留)。
|
||||
this.prisma.sessions.updateMany({
|
||||
where: { userId, id: { not: sessionId }, revokedAt: null },
|
||||
data: { revokedAt },
|
||||
}),
|
||||
this.prisma.audit_logs.create({
|
||||
data: {
|
||||
actorId: userId,
|
||||
action: 'auth.password_change',
|
||||
resourceId: userId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user