refactor: 收口 Admin 会话与授权边界
This commit is contained in:
1 parent
0788480ce3
commit
4ef4be2071
67 files changed
+1890
-392
No files matched your search
@@ -1,11 +1,13 @@
|
||||
import { Body, Controller, Post, Query, Req, UnauthorizedException } from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { Body, Controller, Get, Post, Query, Req, Res } from "@nestjs/common";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { WechatAuthService } from "../../wechat-auth.js";
|
||||
import { AdminBrand, AdminPublic, AdminTokenOnly } from "../../admin-auth.decorators.js";
|
||||
import { clearAdminSessionCookie, setAdminSessionCookie } from "../../admin-session-cookie.js";
|
||||
import { AdminAuthService } from "../../admin-auth.service.js";
|
||||
import { requireAdminIdentity } from "../../admin-access.identity.js";
|
||||
import { AdminLoginDto, WechatLoginDto } from "../../admin-auth.dto.js";
|
||||
import { CustomerSessionService } from "./customer-session.service.js";
|
||||
import { ChangeAdminPasswordDto } from "../../admin.dto.js";
|
||||
import { readIdentity } from "../../auth.js";
|
||||
|
||||
@Controller("customer/session")
|
||||
export class CustomerSessionController {
|
||||
@@ -28,37 +30,55 @@ export class CustomerSessionController {
|
||||
export class AdminSessionController {
|
||||
constructor(private readonly auth: AdminAuthService) {}
|
||||
|
||||
@AdminPublic()
|
||||
@Post("login")
|
||||
login(@Body() body: AdminLoginDto): Promise<{
|
||||
token: string;
|
||||
async login(
|
||||
@Body() body: AdminLoginDto,
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{
|
||||
displayName: string;
|
||||
mustChangePassword: boolean;
|
||||
role: string;
|
||||
platformTemplateRole: string;
|
||||
platformTemplateRole: "none" | "template_admin";
|
||||
}> {
|
||||
return this.auth.login(body.username, body.password);
|
||||
const { session, token } = await this.auth.login(body.username, body.password);
|
||||
setAdminSessionCookie(reply, token);
|
||||
return session;
|
||||
}
|
||||
|
||||
@AdminBrand()
|
||||
@Get("current")
|
||||
async current(@Req() request: FastifyRequest) {
|
||||
return this.auth.currentSession(request);
|
||||
}
|
||||
|
||||
@AdminTokenOnly()
|
||||
@Post("logout")
|
||||
async logout(@Req() request: FastifyRequest): Promise<{ loggedOut: true }> {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
const identity = readIdentity(token);
|
||||
if (identity.role !== "admin") throw new UnauthorizedException("管理员登录态无效");
|
||||
async logout(
|
||||
@Req() request: FastifyRequest,
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{ loggedOut: true }> {
|
||||
const identity = requireAdminIdentity(request);
|
||||
await this.auth.logout(identity.subject);
|
||||
clearAdminSessionCookie(reply);
|
||||
return { loggedOut: true };
|
||||
}
|
||||
|
||||
@AdminTokenOnly()
|
||||
@Post("change-password")
|
||||
async changePassword(
|
||||
@Req() request: FastifyRequest,
|
||||
@Body() body: ChangeAdminPasswordDto,
|
||||
@Res({ passthrough: true }) reply: FastifyReply,
|
||||
): Promise<{ changed: true }> {
|
||||
const token = request.headers.authorization?.replace(/^Bearer\s+/i, "");
|
||||
if (!token) throw new UnauthorizedException("缺少管理员登录态");
|
||||
const identity = readIdentity(token);
|
||||
if (identity.role !== "admin") throw new UnauthorizedException("管理员登录态无效");
|
||||
await this.auth.changePassword(identity.subject, body.currentPassword, body.newPassword);
|
||||
const identity = requireAdminIdentity(request);
|
||||
const { token } = await this.auth.changePassword(
|
||||
identity.subject,
|
||||
identity.brandId,
|
||||
body.currentPassword,
|
||||
body.newPassword,
|
||||
);
|
||||
setAdminSessionCookie(reply, token);
|
||||
return { changed: true };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user