统一模板包 visual 契约并补齐完成证据;修复 release 版本一致性、管理员 token 撤销、CORS 与默认品牌配置;收敛依赖安全风险并完成全量回归验证。
25 lines
789 B
TypeScript
25 lines
789 B
TypeScript
import { ForbiddenException, UnauthorizedException } from "@nestjs/common";
|
|
import type { FastifyRequest } from "fastify";
|
|
import { readIdentity } from "../../auth.js";
|
|
|
|
export type AdminIdentity = {
|
|
subject: string;
|
|
brandId: string;
|
|
role: "admin";
|
|
tokenVersion: number;
|
|
};
|
|
|
|
export function requireAdminIdentity(request: FastifyRequest): AdminIdentity {
|
|
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
|
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
|
|
|
let identity;
|
|
try {
|
|
identity = readIdentity(token);
|
|
} catch {
|
|
throw new UnauthorizedException("管理员登录态无效或已过期");
|
|
}
|
|
if (identity.role !== "admin") throw new ForbiddenException("无管理员权限");
|
|
return identity as AdminIdentity;
|
|
}
|