/** * 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 = { 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 { 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'; }