@kanadi/core
Version:
Multi-Layer CAPTCHA Framework with customizable validators and challenge bundles
62 lines (58 loc) • 1.43 kB
text/typescript
import {
Body,
Controller,
Get,
Headers,
HttpStatus,
Ip,
Param,
Post,
Res,
ForbiddenException,
} from "@nestjs/common";
import { KanadiGatewayService } from "./gateway.service";
import type { SiteVerifyRequest } from "./types";
()
export class KanadiGatewayController {
constructor(private readonly gatewayService: KanadiGatewayService) {}
(":namespace/challenge")
async createChallenge(
("namespace") namespace: string,
("user-agent") userAgent?: string,
() ip?: string,
("referer") referrer?: string,
() headers?: Record<string, string | string[] | undefined>,
() body?: { clientFingerprint?: string },
({ passthrough: true }) res?: any,
) {
try {
return await this.gatewayService.createChallenge(
userAgent,
ip,
referrer,
headers,
body?.clientFingerprint,
);
} catch (error) {
if (error instanceof ForbiddenException) {
const payload = error.getResponse();
const body =
typeof payload === "object"
? (payload as Record<string, any>)
: { reasonId: payload, ruleId: payload };
if (res) {
res.status(HttpStatus.FORBIDDEN);
return body;
}
}
throw error;
}
}
(":namespace/siteverify")
async siteVerify(
("namespace") namespace: string,
() body: SiteVerifyRequest,
) {
return await this.gatewayService.verifyChallenge(body);
}
}