codes.ts
This file
Path
apps/platform/src/auth/codes.ts
105 lines · 2.8 KiB · TypeScript
Last changed by
Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c
History of this fileTake it
Sign people in by a proven email, with a display name, and Google's emailoscabrielbefore adoption125964c
lines 1–105why, one step back1// A mailed code proves a person reads an address. The code itself is kept2// nowhere: the form carries its HMAC under the cookie key, bound to what it3// is for and to the address, so the browser holding the form cannot guess it4// offline, and a code mailed for one purpose or address opens no other.5import { Effect, Option, Redacted, Schema } from "effect";67import { emailKey } from "./accounts.ts";8import type { Email } from "./accounts.ts";910export const CODE_DIGITS = 6;1112const SPACE = 10 ** CODE_DIGITS;1314// The largest multiple of SPACE a Uint32 holds, so every code is as likely.15const FAIR = Math.floor(2 ** 32 / SPACE) * SPACE;1617export const freshCode = Effect.sync(() => {18 const draw = new Uint32Array(1);1920 do {21 crypto.getRandomValues(draw);22 } while ((draw[0] ?? FAIR) >= FAIR);2324 return ((draw[0] ?? 0) % SPACE).toString().padStart(CODE_DIGITS, "0");25});2627export type CodePurpose = "sign-up" | "add-email" | "reset";2829const Tag = Schema.Uint8ArrayFromBase64Url;3031const encodeTag = Schema.encodeEffect(Tag);3233const decodeTag = Schema.decodeEffect(Tag);3435const utf8 = (text: string) => new TextEncoder().encode(text);3637const hmacKey = (key: Redacted.Redacted) =>38 Effect.promise(() =>39 crypto.subtle.importKey(40 "raw",41 utf8(Redacted.value(key)),42 { hash: "SHA-256", name: "HMAC" },43 false,44 ["sign", "verify"]45 )46 );4748const bound = (purpose: CodePurpose, email: Email, code: string) =>49 utf8(`code.${purpose}.${emailKey(email)}.${code}`);5051export const codeTag = (52 key: Redacted.Redacted,53 purpose: CodePurpose,54 email: Email,55 code: string56) =>57 Effect.gen(function* () {58 const hmac = yield* hmacKey(key);5960 const signature = yield* Effect.promise(() =>61 crypto.subtle.sign("HMAC", hmac, bound(purpose, email, code))62 );6364 return yield* encodeTag(new Uint8Array(signature)).pipe(Effect.orDie);65 });6667// A tag no code opens, for a form that must look like one that mailed a code.68export const deadTag = Effect.sync(() =>69 crypto.getRandomValues(new Uint8Array(32))70).pipe(Effect.flatMap(encodeTag), Effect.orDie);7172// People paste codes with spaces or dashes in them.73const typed = (entered: string) => entered.replaceAll(/[\s-]/gu, "");7475export const codeMatches = (76 key: Redacted.Redacted,77 purpose: CodePurpose,78 email: Email,79 tag: string,80 entered: string81) =>82 Effect.gen(function* () {83 const code = typed(entered);8485 if (!new RegExp(`^\\d{${CODE_DIGITS}}$`, "u").test(code)) {86 return false;87 }8889 const expected = yield* decodeTag(tag).pipe(Effect.option);9091 if (Option.isNone(expected)) {92 return false;93 }9495 const hmac = yield* hmacKey(key);9697 return yield* Effect.promise(() =>98 crypto.subtle.verify(99 "HMAC",100 hmac,101 new Uint8Array(expected.value),102 bound(purpose, email, code)103 )104 );105 });