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:
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* The Express app (docs/ACCOUNTS-1.0-PLAN.md §1). Everything is under /api, so the site and its API
|
||||
* share one address (as Events 2.2): nginx sends /api to this backend and the rest to Next.js.
|
||||
*
|
||||
* /api/health version and database, for monitoring and the Hub
|
||||
* /api/oidc/… the OpenID Connect provider (discovery, auth, token, …)
|
||||
* /api/self/… the account page's own sign-in
|
||||
* /api/church/v1, /api/links/v1/… for the church's other products
|
||||
* /api/v2/… the pages' API (sign-in steps, account, admin, setup)
|
||||
*/
|
||||
import express from 'express';
|
||||
import type Provider from 'oidc-provider';
|
||||
import type { Ctx } from './context.js';
|
||||
import { sameOrigin, sendError } from './http.js';
|
||||
import { adminRouter } from './admin/routes.js';
|
||||
import { interactionRouter, selfRouter } from './interaction/routes.js';
|
||||
import { meRouter } from './me/routes.js';
|
||||
import { LOGOUT_JS } from './oidc/pages.js';
|
||||
import { publicRouter } from './public/routes.js';
|
||||
import { setupRouter } from './setup/routes.js';
|
||||
import { health } from './hub/client.js';
|
||||
|
||||
export function createApp(ctx: Ctx, provider: Provider) {
|
||||
const app = express();
|
||||
app.disable('x-powered-by');
|
||||
if (ctx.config.trustProxy) app.set('trust proxy', 1);
|
||||
|
||||
app.use((_req, res, next) => {
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
res.setHeader('Referrer-Policy', 'same-origin');
|
||||
res.setHeader('X-Frame-Options', 'DENY');
|
||||
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
|
||||
next();
|
||||
});
|
||||
|
||||
app.get('/api/health', async (_req, res) => {
|
||||
const h = await health(ctx);
|
||||
res.status(h.db ? 200 : 503).json({ status: h.db ? 'ok' : 'error', product: 'accounts', version: process.env.npm_package_version || '1.0.0', db: h.db });
|
||||
});
|
||||
|
||||
app.get('/api/static/logout.js', (_req, res) => {
|
||||
res.type('application/javascript').set('Cache-Control', 'public, max-age=3600').send(LOGOUT_JS);
|
||||
});
|
||||
|
||||
// The provider parses its own bodies: mounted before express.json.
|
||||
app.use('/api/oidc', provider.callback());
|
||||
app.use('/api/self', selfRouter(ctx));
|
||||
app.use('/api', publicRouter(ctx));
|
||||
|
||||
const v2 = express.Router();
|
||||
v2.use(express.json({ limit: '64kb' }));
|
||||
v2.use(sameOrigin(ctx));
|
||||
v2.use(interactionRouter(ctx, provider));
|
||||
v2.use(setupRouter(ctx));
|
||||
v2.use(meRouter(ctx, provider));
|
||||
v2.use('/admin', adminRouter(ctx, provider));
|
||||
v2.use((_req, res) => res.status(404).json({ error: { code: 'NOT_FOUND', message: 'There is no such API endpoint.' } }));
|
||||
app.use('/api/v2', v2);
|
||||
|
||||
app.use((err: unknown, _req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
if (res.headersSent) return next(err);
|
||||
const e = err as { type?: string };
|
||||
if (e?.type === 'entity.parse.failed') return res.status(400).json({ error: { code: 'VALIDATION', message: 'The body must be JSON.' } });
|
||||
return sendError(res, err);
|
||||
});
|
||||
return app;
|
||||
}
|
||||
Reference in New Issue
Block a user