From c81005a050082e0bc536aaaa6bae81dbd7fef7bc Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 4 Oct 2024 15:56:32 -0500 Subject: [PATCH] captcha: refactor canvas size globals --- src/controllers/api/captcha.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/controllers/api/captcha.ts b/src/controllers/api/captcha.ts index 72c5fe7c..4dfaacc7 100644 --- a/src/controllers/api/captcha.ts +++ b/src/controllers/api/captcha.ts @@ -18,14 +18,17 @@ interface Dimensions { const captchas = new TTLCache(); +const BG_SIZE = { w: 370, h: 400 }; +const PUZZLE_SIZE = { w: 65, h: 65 }; + /** Puzzle captcha controller. */ export const captchaController: AppController = async (c) => { const { bg, puzzle, solution } = await generateCaptcha( await Deno.readFile(new URL('../../assets/captcha/bg/tj-holowaychuk.jpg', import.meta.url)), await Deno.readFile(new URL('../../assets/captcha/puzzle-mask.png', import.meta.url)), await Deno.readFile(new URL('../../assets/captcha/puzzle-hole.png', import.meta.url)), - { w: 370, h: 400 }, - { w: 65, h: 65 }, + BG_SIZE, + PUZZLE_SIZE, ); const id = crypto.randomUUID(); @@ -110,15 +113,9 @@ export const captchaVerifyController: AppController = async (c) => { return c.json({ error: 'Captcha expired' }, { status: 410 }); } - const dim = { w: 65, h: 65 }; - const point = result.data; + const solved = verifySolution(PUZZLE_SIZE, result.data, solution); - const success = areIntersecting( - { ...point, ...dim }, - { ...solution, ...dim }, - ); - - if (success) { + if (solved) { captchas.delete(id); await createAdminEvent({ @@ -136,6 +133,13 @@ export const captchaVerifyController: AppController = async (c) => { return c.json({ error: 'Incorrect solution' }, { status: 400 }); }; +function verifySolution(puzzleSize: Dimensions, point: Point, solution: Point): boolean { + return areIntersecting( + { ...point, ...puzzleSize }, + { ...solution, ...puzzleSize }, + ); +} + type Rectangle = Point & Dimensions; function areIntersecting(rect1: Rectangle, rect2: Rectangle, threshold = 0.5) {