Files
crosscode-platform/apps/accounts/backend/src/oidc/clients.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

52 lines
2.2 KiB
TypeScript

/**
* OIDC clients (docs/ACCOUNTS-1.0-PLAN.md §4): every linked product is one, with no registration
* screen. `client_id` is its Hub install id, the secret is derived from its link's secret, and its
* addresses are the fixed paths on the address the Hub listed for it (exact matches only).
*
* Plus Accounts' own pages (`accounts-self`): the account page signs in through the same flow.
*/
import { oidcClientAddresses } from '@crosscode/contracts';
import { oidcClientSecret } from '@crosscode/contracts/signing';
import type { ClientMetadata } from 'oidc-provider';
import type { Ctx } from '../context.js';
export const SELF_CLIENT_ID = 'accounts-self';
export const SELF_CALLBACK = '/api/self/callback';
const PRODUCT_NAMES: Record<string, string> = { events: 'Cross Events', website: 'the church website', app: 'the church app' };
export function selfClient(ctx: Ctx): ClientMetadata {
return {
client_id: SELF_CLIENT_ID,
client_name: 'Your account',
token_endpoint_auth_method: 'none',
grant_types: ['authorization_code'],
response_types: ['code'],
redirect_uris: [`${ctx.config.publicUrl}${SELF_CALLBACK}`],
post_logout_redirect_uris: [`${ctx.config.publicUrl}/signed-out`],
};
}
export async function clientMetadataFor(ctx: Ctx, clientId: string): Promise<ClientMetadata | undefined> {
const link = await ctx.db.productLink.findUnique({ where: { installId: clientId } }).catch(() => null);
if (!link) return undefined;
const a = oidcClientAddresses(link.url);
return {
client_id: link.installId,
client_secret: oidcClientSecret(ctx.secrets.decrypt(link.secret)),
client_name: PRODUCT_NAMES[link.product] ?? link.product,
token_endpoint_auth_method: 'client_secret_basic',
grant_types: ['authorization_code'],
response_types: ['code'],
redirect_uris: [a.redirectUri],
post_logout_redirect_uris: [a.postLogoutRedirectUri],
backchannel_logout_uri: a.backchannelLogoutUri,
backchannel_logout_session_required: true,
'crosscode.product': link.product,
} as ClientMetadata;
}
export function productName(product: string | undefined): string {
return (product && PRODUCT_NAMES[product]) || 'this site';
}