@kanadi/core
Version:
Multi-Layer CAPTCHA Framework with customizable validators and challenge bundles
130 lines (117 loc) • 2.43 kB
text/typescript
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";
("admin")
(ApiKeyGuard)
export class AdminController {
private banEngine: BanRuleEngine;
constructor() {
this.banEngine = new BanRuleEngine();
}
("bans")
async createManualBan(
()
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",
};
}
("bans")
async getActiveBans(("limit") limit?: string) {
const limitNum = limit ? parseInt(limit) : 100;
const bans = await this.banEngine.getActiveBans(limitNum);
return {
success: true,
count: bans.length,
bans,
};
}
("bans/stats")
async getBanStats() {
const stats = await this.banEngine.getStats();
return {
success: true,
stats,
};
}
("bans/:entityType/:entityId")
async getBanHistory(
("entityType") entityType: string,
("entityId") entityId: string,
("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,
};
}
("bans/:entityType/:entityId")
async removeBan(
("entityType") entityType: string,
("entityId") entityId: string,
) {
await this.banEngine.removeBan(entityType, entityId);
return {
success: true,
message: `Ban removed for ${entityType}:${entityId}`,
};
}
("bans/expire")
async expireBans() {
const count = await this.banEngine.expireBans();
return {
success: true,
message: `Expired ${count} ban(s)`,
count,
};
}
("bans/check")
async checkBan(
()
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,
};
}
}