Accounts 1.0.0 backend: OIDC provider, sign-in steps, account and admin API

oidc-provider 9.13.3 with PKCE, ES256, crosscode.identity/1 claims and
back-channel logout; clients are the linked products (Connect). Sign-in
steps (password, sign-up, email check, 2FA and its setup, privacy notice,
news question, password reset) survive to the product. Account page and
admin API, church profile, export and deletion fan-out, Hub v2 client,
jobs. End-to-end tests with a test product on openid-client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 13:33:49 +02:00
co-authored by Claude Opus 5.5
parent 45c6539bf1
commit 069b66fd30
51 changed files with 4863 additions and 9 deletions
@@ -0,0 +1,342 @@
-- Accounts 1.0.0: the first schema (docs/ACCOUNTS-1.0-PLAN.md §2).
-- CreateEnum
CREATE TYPE "AccountRole" AS ENUM ('member', 'admin');
-- CreateEnum
CREATE TYPE "AccountStatus" AS ENUM ('active', 'deactivated', 'deleted');
-- CreateEnum
CREATE TYPE "TwoFactorMethod" AS ENUM ('email', 'whatsapp', 'totp');
-- CreateTable
CREATE TABLE "Account" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"givenName" TEXT,
"familyName" TEXT,
"email" TEXT NOT NULL,
"emailVerified" BOOLEAN NOT NULL DEFAULT false,
"phone" TEXT,
"phoneVerified" BOOLEAN NOT NULL DEFAULT false,
"passwordHash" TEXT,
"role" "AccountRole" NOT NULL DEFAULT 'member',
"status" "AccountStatus" NOT NULL DEFAULT 'active',
"twoFactorMethod" "TwoFactorMethod",
"totpSecret" TEXT,
"totpLastStep" INTEGER,
"newsEmail" BOOLEAN NOT NULL DEFAULT false,
"newsWhatsApp" BOOLEAN NOT NULL DEFAULT false,
"newsAskedAt" TIMESTAMP(3),
"noticeVersionAccepted" INTEGER NOT NULL DEFAULT 0,
"failedSignIns" INTEGER NOT NULL DEFAULT 0,
"lockedUntil" TIMESTAMP(3),
"lastSignInAt" TIMESTAMP(3),
"deactivatedAt" TIMESTAMP(3),
"anonymisedAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "RecoveryCode" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"codeHash" TEXT NOT NULL,
"usedAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "RecoveryCode_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "PendingSignIn" (
"uid" TEXT NOT NULL,
"accountId" TEXT,
"stage" TEXT NOT NULL,
"codeHash" TEXT,
"codeSentAt" TIMESTAMP(3),
"attempts" INTEGER NOT NULL DEFAULT 0,
"amr" TEXT[],
"pendingTotpSecret" TEXT,
"remember" BOOLEAN NOT NULL DEFAULT false,
"expiresAt" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "PendingSignIn_pkey" PRIMARY KEY ("uid")
);
-- CreateTable
CREATE TABLE "EmailToken" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"purpose" TEXT NOT NULL,
"tokenHash" TEXT NOT NULL,
"interactionUid" TEXT,
"expiresAt" TIMESTAMP(3) NOT NULL,
"usedAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "EmailToken_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "LoginEvent" (
"id" TEXT NOT NULL,
"accountId" TEXT,
"type" TEXT NOT NULL,
"clientId" TEXT,
"ip" TEXT,
"userAgent" TEXT,
"detail" JSONB,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "LoginEvent_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "PrivacyNotice" (
"version" INTEGER NOT NULL,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"publishedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"publishedById" TEXT,
CONSTRAINT "PrivacyNotice_pkey" PRIMARY KEY ("version")
);
-- CreateTable
CREATE TABLE "NoticeAcceptance" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"version" INTEGER NOT NULL,
"acceptedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "NoticeAcceptance_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "ConsentChange" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"channel" TEXT NOT NULL,
"granted" BOOLEAN NOT NULL,
"source" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "ConsentChange_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "AccessLog" (
"id" TEXT NOT NULL,
"actorId" TEXT NOT NULL,
"targetId" TEXT NOT NULL,
"action" TEXT NOT NULL,
"ip" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "AccessLog_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "AuditLog" (
"id" TEXT NOT NULL,
"actorId" TEXT,
"actorRole" TEXT NOT NULL,
"action" TEXT NOT NULL,
"targetType" TEXT NOT NULL,
"targetId" TEXT,
"metadata" JSONB,
"ip" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "AuditLog_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "AppSetting" (
"key" TEXT NOT NULL,
"value" TEXT NOT NULL,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "AppSetting_pkey" PRIMARY KEY ("key")
);
-- CreateTable
CREATE TABLE "ProductLink" (
"installId" TEXT NOT NULL,
"product" TEXT NOT NULL,
"url" TEXT NOT NULL,
"publicKey" TEXT NOT NULL,
"secret" TEXT NOT NULL,
"connectedAt" TIMESTAMP(3) NOT NULL,
"connectedById" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "ProductLink_pkey" PRIMARY KEY ("installId")
);
-- CreateTable
CREATE TABLE "LinkNonce" (
"nonce" TEXT NOT NULL,
"seenAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "LinkNonce_pkey" PRIMARY KEY ("nonce")
);
-- CreateTable
CREATE TABLE "OidcPayload" (
"id" TEXT NOT NULL,
"model" TEXT NOT NULL,
"payload" JSONB NOT NULL,
"grantId" TEXT,
"userCode" TEXT,
"uid" TEXT,
"accountId" TEXT,
"expiresAt" TIMESTAMP(3),
"consumedAt" TIMESTAMP(3),
CONSTRAINT "OidcPayload_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "Delivery" (
"id" TEXT NOT NULL,
"linkId" TEXT NOT NULL,
"callId" TEXT NOT NULL,
"type" TEXT NOT NULL,
"path" TEXT NOT NULL,
"body" JSONB NOT NULL,
"status" TEXT NOT NULL DEFAULT 'pending',
"attempts" INTEGER NOT NULL DEFAULT 0,
"nextAttemptAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"lastError" TEXT,
"reply" JSONB,
"deliveredAt" TIMESTAMP(3),
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Delivery_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "DataExport" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"status" TEXT NOT NULL DEFAULT 'collecting',
"parts" JSONB NOT NULL DEFAULT '{}',
"file" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"readyAt" TIMESTAMP(3),
"expiresAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "DataExport_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "DeletionRequest" (
"id" TEXT NOT NULL,
"accountId" TEXT NOT NULL,
"status" TEXT NOT NULL DEFAULT 'waiting_approval',
"requestedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"approvedAt" TIMESTAMP(3),
"approvedById" TEXT,
"products" JSONB NOT NULL DEFAULT '{}',
"completedAt" TIMESTAMP(3),
CONSTRAINT "DeletionRequest_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Account_email_key" ON "Account"("email");
-- CreateIndex
CREATE INDEX "Account_status_idx" ON "Account"("status");
-- CreateIndex
CREATE UNIQUE INDEX "RecoveryCode_codeHash_key" ON "RecoveryCode"("codeHash");
-- CreateIndex
CREATE INDEX "RecoveryCode_accountId_idx" ON "RecoveryCode"("accountId");
-- CreateIndex
CREATE INDEX "PendingSignIn_expiresAt_idx" ON "PendingSignIn"("expiresAt");
-- CreateIndex
CREATE UNIQUE INDEX "EmailToken_tokenHash_key" ON "EmailToken"("tokenHash");
-- CreateIndex
CREATE INDEX "EmailToken_accountId_purpose_idx" ON "EmailToken"("accountId", "purpose");
-- CreateIndex
CREATE INDEX "LoginEvent_accountId_createdAt_idx" ON "LoginEvent"("accountId", "createdAt");
-- CreateIndex
CREATE INDEX "NoticeAcceptance_accountId_idx" ON "NoticeAcceptance"("accountId");
-- CreateIndex
CREATE INDEX "ConsentChange_accountId_idx" ON "ConsentChange"("accountId");
-- CreateIndex
CREATE INDEX "AccessLog_targetId_createdAt_idx" ON "AccessLog"("targetId", "createdAt");
-- CreateIndex
CREATE INDEX "AuditLog_createdAt_idx" ON "AuditLog"("createdAt");
-- CreateIndex
CREATE INDEX "LinkNonce_seenAt_idx" ON "LinkNonce"("seenAt");
-- CreateIndex
CREATE INDEX "OidcPayload_grantId_idx" ON "OidcPayload"("grantId");
-- CreateIndex
CREATE INDEX "OidcPayload_uid_idx" ON "OidcPayload"("uid");
-- CreateIndex
CREATE INDEX "OidcPayload_accountId_idx" ON "OidcPayload"("accountId");
-- CreateIndex
CREATE INDEX "OidcPayload_expiresAt_idx" ON "OidcPayload"("expiresAt");
-- CreateIndex
CREATE UNIQUE INDEX "Delivery_callId_key" ON "Delivery"("callId");
-- CreateIndex
CREATE INDEX "Delivery_status_nextAttemptAt_idx" ON "Delivery"("status", "nextAttemptAt");
-- CreateIndex
CREATE INDEX "Delivery_linkId_idx" ON "Delivery"("linkId");
-- CreateIndex
CREATE INDEX "DataExport_accountId_idx" ON "DataExport"("accountId");
-- CreateIndex
CREATE INDEX "DeletionRequest_status_idx" ON "DeletionRequest"("status");
-- AddForeignKey
ALTER TABLE "RecoveryCode" ADD CONSTRAINT "RecoveryCode_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "EmailToken" ADD CONSTRAINT "EmailToken_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "LoginEvent" ADD CONSTRAINT "LoginEvent_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "NoticeAcceptance" ADD CONSTRAINT "NoticeAcceptance_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ConsentChange" ADD CONSTRAINT "ConsentChange_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Delivery" ADD CONSTRAINT "Delivery_linkId_fkey" FOREIGN KEY ("linkId") REFERENCES "ProductLink"("installId") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DataExport" ADD CONSTRAINT "DataExport_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "DeletionRequest" ADD CONSTRAINT "DeletionRequest_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "Account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"
+287
View File
@@ -0,0 +1,287 @@
// Accounts 1.0.0 (docs/ACCOUNTS-1.0-PLAN.md). One database per church.
generator client {
provider = "prisma-client"
// Its own output, so hoisted clients of other apps never overwrite it (EVENTS-2.2-PLAN §2).
output = "../src/generated/prisma"
moduleFormat = "esm"
importFileExtension = "js"
}
datasource db {
provider = "postgresql"
}
enum AccountRole {
member
admin
}
enum AccountStatus {
active
deactivated
deleted
}
enum TwoFactorMethod {
email
whatsapp
totp
}
// A person. `id` is the OIDC `sub` every product keys its own records on.
model Account {
id String @id @default(uuid())
name String
givenName String?
familyName String?
email String @unique // lower-case
emailVerified Boolean @default(false)
phone String?
phoneVerified Boolean @default(false)
// Argon2id; bcrypt hashes imported from Cross Events verify too and are re-hashed at sign-in.
passwordHash String?
role AccountRole @default(member)
status AccountStatus @default(active)
twoFactorMethod TwoFactorMethod?
totpSecret String? // encrypted
totpLastStep Int?
newsEmail Boolean @default(false)
newsWhatsApp Boolean @default(false)
newsAskedAt DateTime?
noticeVersionAccepted Int @default(0)
failedSignIns Int @default(0)
lockedUntil DateTime?
lastSignInAt DateTime?
deactivatedAt DateTime?
anonymisedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
recoveryCodes RecoveryCode[]
tokens EmailToken[]
loginEvents LoginEvent[]
acceptances NoticeAcceptance[]
consentChanges ConsentChange[]
exports DataExport[]
deletionRequests DeletionRequest[]
@@index([status])
}
model RecoveryCode {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
codeHash String @unique
usedAt DateTime?
createdAt DateTime @default(now())
@@index([accountId])
}
// One sign-in in progress (an oidc-provider interaction), through every step: password, 2FA,
// verifying a new email, the news question and the privacy notice. Keyed by the interaction uid.
model PendingSignIn {
uid String @id
accountId String?
stage String // password | two_factor | setup_two_factor | verify_email | notice | news | done
codeHash String?
codeSentAt DateTime?
attempts Int @default(0)
amr String[]
pendingTotpSecret String? // encrypted, while setting up an authenticator app during sign-in
remember Boolean @default(false)
expiresAt DateTime
createdAt DateTime @default(now())
@@index([expiresAt])
}
// Links sent by email: verify an address, reset a password. Only the hash is kept.
model EmailToken {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
purpose String // verify_email | reset_password
tokenHash String @unique
// The sign-in the link belongs to, so it carries on to the product afterwards (§5).
interactionUid String?
expiresAt DateTime
usedAt DateTime?
createdAt DateTime @default(now())
@@index([accountId, purpose])
}
// Sign-ins and changes to how someone signs in (the person and admins see these).
model LoginEvent {
id String @id @default(uuid())
accountId String?
account Account? @relation(fields: [accountId], references: [id], onDelete: Cascade)
type String // sign_in | sign_in_failed | locked | password_reset | password_changed | two_factor_on | two_factor_off | recovery_code_used | recovery_codes_replaced | signed_out_everywhere
clientId String? // the product it was for
ip String?
userAgent String?
detail Json?
createdAt DateTime @default(now())
@@index([accountId, createdAt])
}
model PrivacyNotice {
version Int @id
title String
body String
publishedAt DateTime @default(now())
publishedById String?
}
model NoticeAcceptance {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
version Int
acceptedAt DateTime @default(now())
@@index([accountId])
}
// Proof of news consent (POPIA): every change, with where it was given.
model ConsentChange {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
channel String // email | whatsapp
granted Boolean
source String // sign_up | news_question | account_page | admin
createdAt DateTime @default(now())
@@index([accountId])
}
// Admins viewing or exporting a person.
model AccessLog {
id String @id @default(uuid())
actorId String
targetId String
action String // view | export
ip String?
createdAt DateTime @default(now())
@@index([targetId, createdAt])
}
model AuditLog {
id String @id @default(uuid())
actorId String?
actorRole String
action String
targetType String
targetId String?
metadata Json?
ip String?
createdAt DateTime @default(now())
@@index([createdAt])
}
model AppSetting {
key String @id
value String
updatedAt DateTime @updatedAt
}
// A linked product (Connect, crosscode.link/1). Each is also an OIDC client (§4): client_id is
// installId, the client secret is derived from `secret`, the addresses are fixed paths on `url`.
model ProductLink {
installId String @id
product String // events | website | app
url String
publicKey String
secret String // encrypted
connectedAt DateTime
connectedById String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deliveries Delivery[]
}
model LinkNonce {
nonce String @id
seenAt DateTime @default(now())
@@index([seenAt])
}
// oidc-provider's storage (sessions, grants, codes, interactions…), one row per model and id.
model OidcPayload {
id String @id // "<model>:<id>"
model String
payload Json
grantId String?
userCode String?
uid String?
// Sessions only: whose, so "sign out everywhere" can find them.
accountId String?
expiresAt DateTime?
consumedAt DateTime?
@@index([grantId])
@@index([uid])
@@index([accountId])
@@index([expiresAt])
}
// Back-channel calls to linked products (user.updated, user.deleted, user.export, church.changed),
// retried until they're answered.
model Delivery {
id String @id @default(uuid())
linkId String
link ProductLink @relation(fields: [linkId], references: [installId], onDelete: Cascade)
callId String @unique
type String
path String
body Json
status String @default("pending") // pending | delivered | failed
attempts Int @default(0)
nextAttemptAt DateTime @default(now())
lastError String?
reply Json?
deliveredAt DateTime?
createdAt DateTime @default(now())
@@index([status, nextAttemptAt])
@@index([linkId])
}
// "Download my data": Accounts' part plus each product's (user.export), bundled into one file.
model DataExport {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
status String @default("collecting") // collecting | ready | expired
parts Json @default("{}")
file String?
createdAt DateTime @default(now())
readyAt DateTime?
expiresAt DateTime
@@index([accountId])
}
// "Delete my account": deactivated at once, then an admin approves, then every product answers.
model DeletionRequest {
id String @id @default(uuid())
accountId String
account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)
status String @default("waiting_approval") // waiting_approval | approved | done | cancelled
requestedAt DateTime @default(now())
approvedAt DateTime?
approvedById String?
// { [installId]: { status: "pending" | "deleted" | "deferred", reason?, retryAfter? } }
products Json @default("{}")
completedAt DateTime?
@@index([status])
}