// The people a vendor's pages sign in (docs/contributing/architecture.md, "Who is on a screen"): a person is a `_person` // row, `person:`, keeping their password's hash, never the password, written by a sign-up door or page and read // by a sign-in, the same way in every pack. World-ui's session kit signs them in. import type { HandlerContext } from './derived-core.ts'; import { digest } from './signing.ts'; /** A person a vendor's pages know: their email, and whatever the vendor's sign-up keeps beside it. */ export type Person = Record & { email: string }; const PERSON = '_person'; /** A password as a World keeps it: its hash, salted by whose it is. */ export const passwordHash = (email: string, password: string): string => digest('sha256', `password:${email.toLowerCase()}:${password}`); /** A person, their password's hash and the fields the vendor's sign-up keeps, recorded. */ export async function recordPerson(ctx: HandlerContext, email: string, password: string, fields: Record = {}): Promise { const lower = email.toLowerCase(); const person = { ...fields, email: lower, password_sha256: passwordHash(lower, password) }; await ctx.record(PERSON, person, `person:${lower}`); return person; } /** The person with this email, or undefined. */ export const personOf = (ctx: HandlerContext, email: string): Person | undefined => ctx.rowsRaw(PERSON).find((p) => p.email === email.trim().toLowerCase()) as Person | undefined; /** A person's fields as a reader shows them (the CLI's log and diff, a diff's field changes): the password's hash as * `(set)`, never its value. A changeset keeps the hash, which its replay writes (ADR 0025). */ export function shownPersonFields(fields: Record): Record { return 'password_sha256' in fields ? { ...fields, password_sha256: '(set)' } : fields; } /** The person whose email and password these are, or undefined. */ export function personWith(ctx: HandlerContext, email: string, password: string): Person | undefined { const person = personOf(ctx, email); return person && person.password_sha256 === passwordHash(person.email, password) ? person : undefined; }