From 12a1e744f90fe7f39808caa3f97d6885a883f34a Mon Sep 17 00:00:00 2001 From: JustZvan Date: Wed, 8 Apr 2026 19:22:29 +0200 Subject: feat: use otp for kid link --- src/account/kid_link_code.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/account/kid_link_code.ts (limited to 'src/account') diff --git a/src/account/kid_link_code.ts b/src/account/kid_link_code.ts new file mode 100644 index 0000000..a9d3c63 --- /dev/null +++ b/src/account/kid_link_code.ts @@ -0,0 +1,23 @@ +import { randomInt } from "node:crypto"; + +const KID_LINK_CODE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +export const KID_LINK_CODE_TTL_SECONDS = 5 * 60; +export const KID_LINK_CODE_REGEX = /^[A-Z0-9]{3}-[A-Z0-9]{3}$/; + +export function generateKidLinkCode(): string { + const rawCode = Array.from({ length: 6 }, () => { + const index = randomInt(0, KID_LINK_CODE_CHARACTERS.length); + return KID_LINK_CODE_CHARACTERS[index]!; + }).join(""); + + return `${rawCode.slice(0, 3)}-${rawCode.slice(3)}`; +} + +export function normalizeKidLinkCode(value: string): string { + return value.trim().toUpperCase(); +} + +export function getKidLinkCodeRedisKey(code: string): string { + return `kid-link-code:${code}`; +} -- cgit v1.2.3