UNPKG

@kanadi/core

Version:

Multi-Layer CAPTCHA Framework with customizable validators and challenge bundles

62 lines (58 loc) 1.43 kB
import { Body, Controller, Get, Headers, HttpStatus, Ip, Param, Post, Res, ForbiddenException, } from "@nestjs/common"; import { KanadiGatewayService } from "./gateway.service"; import type { SiteVerifyRequest } from "./types"; @Controller() export class KanadiGatewayController { constructor(private readonly gatewayService: KanadiGatewayService) {} @Post(":namespace/challenge") async createChallenge( @Param("namespace") namespace: string, @Headers("user-agent") userAgent?: string, @Ip() ip?: string, @Headers("referer") referrer?: string, @Headers() headers?: Record<string, string | string[] | undefined>, @Body() body?: { clientFingerprint?: string }, @Res({ 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; } } @Post(":namespace/siteverify") async siteVerify( @Param("namespace") namespace: string, @Body() body: SiteVerifyRequest, ) { return await this.gatewayService.verifyChallenge(body); } }