Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

first working PoC

Blue Mousebc9ddfe06cd9
diff --git a/src/captcha/captcha.controller.ts b/src/captcha/captcha.controller.ts
index 6e233897e..26eaf3cb5 100644
--- a/src/captcha/captcha.controller.ts
+++ b/src/captcha/captcha.controller.ts
@@ -2,12 +2,12 @@ import {
Controller, Post, Body, UseGuards, ForbiddenException, InternalServerErrorException,
Headers, Ip, Request, Req, Response, Res, HttpCode, Get,
} from '@nestjs/common'
-import { ApiResponse } from '@nestjs/swagger'
import { CaptchaService } from './captcha.service'
import { BotDetectionGuard } from '../common/guards/bot-detection.guard'
import { BotDetection } from '../common/decorators/bot-detection.decorator'
import { isDevelopment } from '../common/constants'
+import { ManualDTO } from './dtos/manual.dto'
const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
@@ -20,17 +20,90 @@ export class CaptchaController {
private readonly captchaService: CaptchaService,
) { }
- @Get('/auto-verifiable')
- @HttpCode(204)
+ @Post('/generate')
+ @HttpCode(200)
+ @UseGuards(BotDetectionGuard)
+ @BotDetection()
+ async generateCaptcha(
+ // @Req() request: Request,
+ // @Res({ passthrough: true }) response: Response,
+ ): Promise<any> {
+ return await this.captchaService.generateCaptcha()
+ }
+
+ @Post('/verify-manual')
+ @HttpCode(200)
+ @UseGuards(BotDetectionGuard)
+ @BotDetection()
+ async verifyManual(
+ @Body() manualDTO: ManualDTO,
+ @Req() request: Request,
+ @Res({ passthrough: true }) response: Response,
+ ): Promise<any> {
+ // @ts-ignore
+ const tokenCookie = request?.cookies?.[CAPTCHA_COOKIE_KEY]
+ const { code, hash } = manualDTO
+
+ if (!this.captchaService.verifyCaptcha(code, hash)) {
+ throw new ForbiddenException('Incorrect captcha')
+ }
+
+ let decrypted
+
+ try {
+ decrypted = await this.captchaService.decryptTokenCaptcha(tokenCookie)
+ } catch (e) {
+ decrypted = {
+ manuallyVerified: 0,
+ automaticallyVerified: 0,
+ }
+ }
+
+ console.log(decrypted)
+
+ const {
+ manuallyVerified, automaticallyVerified,
+ } = this.captchaService.incrementManuallyVerified(decrypted)
+
+ const newTokenCookie = this.captchaService.getTokenCaptcha(manuallyVerified, automaticallyVerified)
+
+ // Set the new cookie
+ if (isDevelopment) {
+ // @ts-ignore
+ response.cookie(CAPTCHA_COOKIE_KEY, newTokenCookie, {
+ httpOnly: true,
+ // 300 days
+ maxAge: 300 * 24 * 60 * 60 * 1000,
+ })
+ } else {
+ // @ts-ignore
+ response.cookie(CAPTCHA_COOKIE_KEY, newTokenCookie, {
+ httpOnly: true,
+ secure: true,
+ sameSite: 'none',
+ // 300 days
+ maxAge: 300 * 24 * 60 * 60 * 1000,
+ })
+ }
+
+ // TODO: return token
+ return {
+ success: true,
+ token: 'token',
+ }
+ }
+
+ @Post('/verify')
+ @HttpCode(200)
@UseGuards(BotDetectionGuard)
@BotDetection()
- @ApiResponse({ status: 204, description: 'Empty body' })
async autoVerifiable(
@Req() request: Request,
@Res({ passthrough: true }) response: Response,
): Promise<any> {
// @ts-ignore
const tokenCookie = request?.cookies?.[CAPTCHA_COOKIE_KEY]
+
let verifiable
try {
@@ -51,6 +124,8 @@ export class CaptchaController {
// @ts-ignore
response.cookie(CAPTCHA_COOKIE_KEY, newTokenCookie, {
httpOnly: true,
+ // 300 days
+ maxAge: 300 * 24 * 60 * 60 * 1000,
})
} else {
// @ts-ignore
@@ -58,6 +133,8 @@ export class CaptchaController {
httpOnly: true,
secure: true,
sameSite: 'none',
+ // 300 days
+ maxAge: 300 * 24 * 60 * 60 * 1000,
})
}
}
@@ -65,5 +142,13 @@ export class CaptchaController {
if (!verifiable) {
throw new ForbiddenException('Captcha required')
}
+
+ // TODO: Increase auto
+
+ // TODO: return token
+ return {
+ success: true,
+ token: 'token',
+ }
}
}
diff --git a/src/captcha/captcha.service.ts b/src/captcha/captcha.service.ts
index 184c46876..f822af26a 100644
--- a/src/captcha/captcha.service.ts
+++ b/src/captcha/captcha.service.ts
@@ -46,6 +46,7 @@ export class CaptchaService {
const captcha = svgCaptcha.create()
const hash = this.hashCaptcha(captcha.text)
+ console.log(captcha.text, hash)
return {
data: captcha.data,
hash,
@@ -54,6 +55,7 @@ export class CaptchaService {
verifyCaptcha(text: string, hash: string): boolean {
const hashedText = this.hashCaptcha(text)
+ console.log(text, hashedText)
return hashedText === hash
}
diff --git a/src/captcha/dtos/manual.dto.ts b/src/captcha/dtos/manual.dto.ts
new file mode 100644
index 000000000..6a0c764dd
--- /dev/null
+++ b/src/captcha/dtos/manual.dto.ts
@@ -0,0 +1,18 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNotEmpty } from 'class-validator'
+
+export class ManualDTO {
+ @ApiProperty({
+ required: true,
+ })
+ @IsNotEmpty()
+ hash?: string
+
+ @ApiProperty({
+ required: true,
+ example: '4vic2',
+ description: 'Captcha code',
+ })
+ @IsNotEmpty()
+ code?: string
+}