Assist_Design/apps/bff/src/modules/auth/guards/auth-throttle.guard.ts

28 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-08-21 15:24:40 +09:00
import { Injectable } from "@nestjs/common";
import { ThrottlerGuard } from "@nestjs/throttler";
import { createHash } from "crypto";
2025-08-23 18:02:05 +09:00
import type { Request } from "express";
@Injectable()
export class AuthThrottleGuard extends ThrottlerGuard {
protected override async getTracker(req: Request): Promise<string> {
// Track by IP address + User Agent for better security on sensitive auth operations
2025-08-22 17:02:49 +09:00
const forwarded = req.headers["x-forwarded-for"];
const forwardedIp = Array.isArray(forwarded) ? forwarded[0] : forwarded;
2025-08-21 15:24:40 +09:00
const ip =
2025-08-23 18:02:05 +09:00
(typeof forwardedIp === "string" ? forwardedIp.split(",")[0]?.trim() : undefined) ||
2025-08-22 17:02:49 +09:00
(req.headers["x-real-ip"] as string | undefined) ||
2025-08-27 10:54:05 +09:00
req.socket?.remoteAddress ||
req.ip ||
2025-08-21 15:24:40 +09:00
"unknown";
const userAgent = req.headers["user-agent"] || "unknown";
const userAgentHash = createHash("sha256").update(userAgent).digest("hex").slice(0, 16);
const normalizedIp = ip.replace(/^::ffff:/, "");
const resolvedIp = await Promise.resolve(normalizedIp);
return `auth_${resolvedIp}_${userAgentHash}`;
}
}