refactor: 收口 Admin 会话与授权边界

This commit is contained in:
que01 committed 2026-10-19 10:13:13 +08:00
1 parent 0788480ce3
commit 4ef4be2071
67 files changed
+1890 -392

No files matched your search

+8 -1
View File
@@ -1,5 +1,6 @@
import { ForbiddenException, UnauthorizedException } from "@nestjs/common";
import type { FastifyRequest } from "fastify";
import { ADMIN_SESSION_COOKIE } from "./admin-session-cookie.js";
import { readIdentity } from "./auth.js";
export type AdminIdentity = {
@@ -9,8 +10,14 @@ export type AdminIdentity = {
tokenVersion: number;
};
export function readAdminToken(request: FastifyRequest): string | null {
const header = request.headers.authorization?.replace(/^Bearer\s+/i, "");
if (header) return header;
return request.cookies?.[ADMIN_SESSION_COOKIE] ?? null;
}
export function requireAdminIdentity(request: FastifyRequest): AdminIdentity {
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
const token = readAdminToken(request);
if (!token) throw new UnauthorizedException("缺少管理员登录态");
let identity;