Files
crosscode-platform/apps/accounts/backend/src/oidc/adapter.ts
T
joshuaandClaude Opus 5.5 069b66fd30 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>
2026-11-18 13:33:49 +02:00

70 lines
2.9 KiB
TypeScript

/**
* oidc-provider's storage in Postgres (`OidcPayload`, one row per model and id), so sessions and
* codes survive restarts and work with more than one process. Clients aren't stored here: they're
* the linked products (oidc/clients.ts).
*/
import type { Adapter, AdapterPayload } from 'oidc-provider';
import type { Ctx } from '../context.js';
import { clientMetadataFor } from './clients.js';
/** Models whose rows are found by grant (revoked together). */
const GRANTABLE = new Set(['AccessToken', 'AuthorizationCode', 'RefreshToken', 'DeviceCode', 'BackchannelAuthenticationRequest']);
export function adapterFor(ctx: Ctx) {
return class PrismaAdapter implements Adapter {
constructor(private readonly model: string) {}
private key(id: string) {
return `${this.model}:${id}`;
}
async upsert(id: string, payload: AdapterPayload, expiresIn: number): Promise<void> {
const data = {
model: this.model,
payload: payload as never,
grantId: payload.grantId ?? null,
userCode: payload.userCode ?? null,
uid: payload.uid ?? null,
accountId: this.model === 'Session' ? (payload.accountId ?? null) : null,
expiresAt: expiresIn ? new Date(ctx.now().getTime() + expiresIn * 1000) : null,
};
await ctx.db.oidcPayload.upsert({ where: { id: this.key(id) }, create: { id: this.key(id), ...data }, update: data });
}
private live(row: { payload: unknown; expiresAt: Date | null; consumedAt: Date | null } | null): AdapterPayload | undefined {
if (!row || (row.expiresAt && row.expiresAt <= ctx.now())) return undefined;
return { ...(row.payload as AdapterPayload), ...(row.consumedAt ? { consumed: Math.floor(row.consumedAt.getTime() / 1000) } : {}) };
}
async find(id: string): Promise<AdapterPayload | undefined> {
if (this.model === 'Client') return (await clientMetadataFor(ctx, id)) as AdapterPayload | undefined;
return this.live(await ctx.db.oidcPayload.findUnique({ where: { id: this.key(id) } }));
}
async findByUserCode(userCode: string) {
return this.live(await ctx.db.oidcPayload.findFirst({ where: { model: this.model, userCode } }));
}
async findByUid(uid: string) {
return this.live(await ctx.db.oidcPayload.findFirst({ where: { model: this.model, uid } }));
}
async consume(id: string) {
await ctx.db.oidcPayload.update({ where: { id: this.key(id) }, data: { consumedAt: ctx.now() } }).catch(() => {});
}
async destroy(id: string) {
await ctx.db.oidcPayload.deleteMany({ where: { id: this.key(id) } });
}
async revokeByGrantId(grantId: string) {
if (GRANTABLE.has(this.model) || this.model === 'Grant') await ctx.db.oidcPayload.deleteMany({ where: { grantId } });
}
};
}
/** The `oidc-cleanup` job: rows past their expiry. */
export async function deleteExpired(ctx: Ctx) {
return ctx.db.oidcPayload.deleteMany({ where: { expiresAt: { lt: ctx.now() } } });
}