UNPKG

@kanadi/core

Version:

Multi-Layer CAPTCHA Framework with customizable validators and challenge bundles

130 lines (117 loc) 2.43 kB
import { Body, Controller, Delete, Get, Param, Post, Query, UseGuards, } from "@nestjs/common"; import { ApiKeyGuard } from "./guards/api-key.guard"; import { BanRuleEngine } from "../ban/ban-engine.service"; @Controller("admin") @UseGuards(ApiKeyGuard) export class AdminController { private banEngine: BanRuleEngine; constructor() { this.banEngine = new BanRuleEngine(); } @Post("bans") async createManualBan( @Body() body: { entityType: string; entityId: string; reason: string; durationMinutes?: number; }, ) { const expiresAt = body.durationMinutes ? new Date(Date.now() + body.durationMinutes * 60000) : undefined; return { success: true, message: "Manual ban created", }; } @Get("bans") async getActiveBans(@Query("limit") limit?: string) { const limitNum = limit ? parseInt(limit) : 100; const bans = await this.banEngine.getActiveBans(limitNum); return { success: true, count: bans.length, bans, }; } @Get("bans/stats") async getBanStats() { const stats = await this.banEngine.getStats(); return { success: true, stats, }; } @Get("bans/:entityType/:entityId") async getBanHistory( @Param("entityType") entityType: string, @Param("entityId") entityId: string, @Query("limit") limit?: string, ) { const limitNum = limit ? parseInt(limit) : 50; const history = await this.banEngine.getBanHistory( entityType, entityId, limitNum, ); return { success: true, count: history.length, history, }; } @Delete("bans/:entityType/:entityId") async removeBan( @Param("entityType") entityType: string, @Param("entityId") entityId: string, ) { await this.banEngine.removeBan(entityType, entityId); return { success: true, message: `Ban removed for ${entityType}:${entityId}`, }; } @Post("bans/expire") async expireBans() { const count = await this.banEngine.expireBans(); return { success: true, message: `Expired ${count} ban(s)`, count, }; } @Post("bans/check") async checkBan( @Body() body: { ip?: string; userId?: string; deviceId?: string; sessionId: string; fingerprint?: string; }, ) { const banContext = { ...body, timestamp: new Date(), metadata: {}, }; const result = await this.banEngine.checkBan(banContext); return { success: true, isBanned: !!result && result.decision !== "ALLOW", result, }; } }