/** * The few pages oidc-provider renders itself (an error, the sign-out confirmation, "signed out"), * in the church's colours. No inline scripts (the CSP forbids them): the sign-out page submits * itself with /api/static/logout.js. */ import type { KoaContextWithOIDC } from 'oidc-provider'; import type { Ctx } from '../context.js'; import { branding } from '../church/profile.js'; const esc = (s: unknown) => String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]!); async function page(ctx: Ctx, title: string, body: string): Promise { const b = await branding(ctx).catch(() => ({ name: 'Sign in', colours: { primary: '#1f4e79' }, logoUrl: null })); return ` ${esc(title)} · ${esc(b.name)}
${b.logoUrl ? `` : ''}

${esc(b.name)}

${body}
`; } export function renderError(ctx: Ctx) { return async (k: KoaContextWithOIDC, out: { error: string; error_description?: string }, err?: Error) => { console.warn(`[oidc] ${out.error}: ${out.error_description ?? ''}${err && !(err as { expose?: boolean }).expose ? ` (${err.stack})` : ''}`); k.type = 'html'; const message = out.error === 'invalid_redirect_uri' || out.error === 'invalid_client' ? 'This sign-in link isn\'t from one of the church\'s sites, so it was stopped.' : out.error_description || 'Something went wrong with signing in.'; k.body = await page(ctx, 'Sign-in problem', `

We couldn't sign you in

${esc(message)}

Go to your account`); }; } export function renderLogout(ctx: Ctx) { return async (k: KoaContextWithOIDC, form: string) => { k.type = 'html'; k.body = await page(ctx, 'Signing out', `${form}

Signing you out…

You'll be signed out of all the church's sites.

`); }; } export function renderLoggedOut(ctx: Ctx) { return async (k: KoaContextWithOIDC) => { k.type = 'html'; k.body = await page(ctx, 'Signed out', '

You\'re signed out

You\'ve been signed out of all the church\'s sites.

Sign in again'); }; } /** Submits the sign-out form straight away (the person already chose to sign out on the product). */ export const LOGOUT_JS = 'document.addEventListener("DOMContentLoaded",function(){var f=document.getElementById("op.logoutForm");if(f){var i=document.createElement("input");i.type="hidden";i.name="logout";i.value="yes";f.appendChild(i);f.submit();}});';