mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
captcha: refactor canvas size globals
This commit is contained in:
parent
6d09f69e26
commit
c81005a050
1 changed files with 14 additions and 10 deletions
|
|
@ -18,14 +18,17 @@ interface Dimensions {
|
|||
|
||||
const captchas = new TTLCache<string, Point>();
|
||||
|
||||
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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue