import { z } from "zod"; import { dateTimeSchema, emailSchema, idSchema, phoneSchema, productSchema } from "./common"; import { schemaName } from "./schema-name"; /** * `crosscode.identity/1` — what Accounts tells the products about a person (ROADMAP-v3 §3): * the claims in the sign-in token (and userinfo), and the back-channel calls Accounts makes to * each linked product (`user.updated`, `user.deleted`, `user.export`). * * Roles are never in here: each product keeps its own, keyed by `sub`. * Back-channel calls are signed (see `@crosscode/contracts/signing`) and retried through pg-boss. */ export const IDENTITY_SCHEMA = schemaName("identity", 1); /** 2FA methods (Cross Events 2.2.0 adds `totp`, the authenticator app). */ export const TWO_FACTOR_METHODS = ["email", "whatsapp", "totp"] as const; export const twoFactorMethodSchema = z.enum(TWO_FACTOR_METHODS); export type TwoFactorMethod = z.infer; export const newsConsentSchema = z.object({ email: z.boolean(), whatsapp: z.boolean(), }); export type NewsConsent = z.infer; /** The profile every product keeps a local copy of (for lists and messages). */ export const profileSchema = z.object({ name: z.string().trim().min(1).max(200), given_name: z.string().trim().max(100).nullable(), family_name: z.string().trim().max(100).nullable(), email: emailSchema, email_verified: z.boolean(), phone_number: phoneSchema.nullable(), phone_number_verified: z.boolean(), /** Whether the church may send this person news, per channel. */ news_consent: newsConsentSchema, }); export type Profile = z.infer; /** * The `crosscode.identity/1` claims in the ID token and the userinfo response, next to the * standard JWT claims (`iss`, `aud`, `exp`, …) that openid-client checks. */ export const identityClaimsSchema = profileSchema.extend({ schema: z.literal(IDENTITY_SCHEMA), /** The account id. Each product's user record uses it as its id. */ sub: idSchema, /** How this sign-in was done (RFC 8176): `pwd`, plus `otp` / `mfa` when 2FA was used. */ amr: z.array(z.string().min(1).max(20)).max(10), /** The account's 2FA method, or `null` when 2FA is off. Admins see it per staff account. */ two_factor: twoFactorMethodSchema.nullable(), /** The privacy notice version the person has accepted. Accounts asks again before returning when there's a newer one. */ privacy_notice_version: z.int().positive(), }); export type IdentityClaims = z.infer; // --------------------------------------------------------------------------------------------- // Back-channel calls: Accounts → product, `POST /api/accounts/v1/events` // --------------------------------------------------------------------------------------------- const callBase = z.object({ schema: z.literal(IDENTITY_SCHEMA), /** Unique per call; repeated on a retry, so products can ignore a call they've already handled. */ callId: idSchema, occurredAt: dateTimeSchema, sub: idSchema, }); /** The person's profile or consent changed. Products update their local copy. */ export const userUpdatedSchema = callBase.extend({ type: z.literal("user.updated"), profile: profileSchema, }); /** * An admin approved deleting the account. Products anonymise their data for `sub` (as Cross * Events does today) and answer with {@link userDeletedReplySchema}. */ export const userDeletedSchema = callBase.extend({ type: z.literal("user.deleted"), approvedAt: dateTimeSchema, }); /** "Download my data": each product sends its part, which Accounts bundles. */ export const userExportSchema = callBase.extend({ type: z.literal("user.export"), /** The export this part belongs to. */ exportId: idSchema, }); export const accountsCallSchema = z.discriminatedUnion("type", [userUpdatedSchema, userDeletedSchema, userExportSchema]); export type AccountsCall = z.infer; export type UserUpdated = z.infer; export type UserDeleted = z.infer; export type UserExport = z.infer; /** The reply to `user.updated`. */ export const userUpdatedReplySchema = z.object({ schema: z.literal(IDENTITY_SCHEMA), ok: z.literal(true) }); /** * The reply to `user.deleted`. `deferred` when the product can't anonymise yet (Cross Events: * an open registration for an upcoming event); Accounts asks again after `retryAfter`. */ export const userDeletedReplySchema = z.discriminatedUnion("status", [ z.object({ schema: z.literal(IDENTITY_SCHEMA), status: z.literal("deleted") }), z.object({ schema: z.literal(IDENTITY_SCHEMA), status: z.literal("deferred"), /** Shown to the admin, e.g. "Has a registration for Youth Camp (14 March)". */ reason: z.string().trim().min(1).max(500), retryAfter: dateTimeSchema, }), ]); export type UserDeletedReply = z.infer; /** One part of a product's export, e.g. "Registrations" or "Prayer commitments". */ export const exportSectionSchema = z.object({ key: z.string().regex(/^[a-z][a-z0-9_]*$/).max(50), title: z.string().trim().min(1).max(100), records: z.array(z.record(z.string(), z.unknown())), }); /** The reply to `user.export`: the product's part of "Download my data". */ export const userExportReplySchema = z.object({ schema: z.literal(IDENTITY_SCHEMA), exportId: idSchema, sub: idSchema, product: productSchema, generatedAt: dateTimeSchema, sections: z.array(exportSectionSchema).max(100), }); export type UserExportReply = z.infer;