- crosscode.event/1 and crosscode.event-feed/1 (feed, event.changed webhook), with session and registration rules; parseEventFeed skips and reports bad events instead of failing the whole feed. - crosscode.church/1: branding, campuses, service times, contact, links. - crosscode.identity/1: sign-in claims and the user.updated / user.deleted / user.export back-channel calls and replies. - Sign-in return helpers (isSafeReturnPath, encodeState/decodeState, isRegisteredRedirectUri) so every product returns people to the page they came from in the same safe way. - Hub protocol v2: product on register/heartbeat, stats per product, install public key, linked products, customerId/product in licences. - @crosscode/contracts/signing (Node only): the Hub's HMAC scheme. - Samples for every contract in samples/, checked by the tests (120). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
134 lines
5.5 KiB
TypeScript
134 lines
5.5 KiB
TypeScript
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<typeof twoFactorMethodSchema>;
|
|
|
|
export const newsConsentSchema = z.object({
|
|
email: z.boolean(),
|
|
whatsapp: z.boolean(),
|
|
});
|
|
export type NewsConsent = z.infer<typeof newsConsentSchema>;
|
|
|
|
/** 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<typeof profileSchema>;
|
|
|
|
/**
|
|
* 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<typeof identityClaimsSchema>;
|
|
|
|
// ---------------------------------------------------------------------------------------------
|
|
// Back-channel calls: Accounts → product, `POST <product>/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<typeof accountsCallSchema>;
|
|
export type UserUpdated = z.infer<typeof userUpdatedSchema>;
|
|
export type UserDeleted = z.infer<typeof userDeletedSchema>;
|
|
export type UserExport = z.infer<typeof userExportSchema>;
|
|
|
|
/** 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<typeof userDeletedReplySchema>;
|
|
|
|
/** 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<typeof userExportReplySchema>;
|