|
| 1 | +import * as path from 'node:path'; |
| 2 | +import * as url from 'node:url'; |
| 3 | + |
| 4 | +import { dirname } from 'desm'; |
| 5 | +import express from 'express'; |
| 6 | +import helmet from 'helmet'; |
| 7 | +import Provider from 'oidc-provider'; |
| 8 | + |
| 9 | +import AccountService from './util/accountService.js'; |
| 10 | +import configuration from './util/configuration.js'; |
| 11 | +import routes from './util/routes.js'; |
| 12 | +import { addUserInfo } from "./util/verifyJWT.js"; |
| 13 | +import PersistentAdapter from "./util/persistentAdapter.js"; |
| 14 | +import { KeyManager } from "./util/generateKeys.js"; |
| 15 | +import { CookieSecretManager } from "./util/generateCookieKeys.js"; |
| 16 | +import { MAIN_CONFIG } from "./config/main.js"; |
| 17 | + |
| 18 | +const __dirname = dirname(import.meta.url); |
| 19 | + |
| 20 | +/** |
| 21 | + * Initializes and configures the Express application and the OIDC Provider. |
| 22 | + * Allows overriding configurations and dependencies for unit/integration testing. |
| 23 | + */ |
| 24 | +export async function initializeApp(options = {}) { |
| 25 | + const config = options.config || MAIN_CONFIG; |
| 26 | + const oidcConfig = { ...configuration }; |
| 27 | + |
| 28 | + const app = express(); |
| 29 | + |
| 30 | + if (config.env_production) { |
| 31 | + const directives = helmet.contentSecurityPolicy.getDefaultDirectives(); |
| 32 | + delete directives['form-action']; |
| 33 | + app.use(helmet({ |
| 34 | + contentSecurityPolicy: { |
| 35 | + useDefaults: false, |
| 36 | + directives, |
| 37 | + }, |
| 38 | + })); |
| 39 | + } |
| 40 | + |
| 41 | + app.set('views', options.viewsPath || path.join(__dirname, 'views')); |
| 42 | + app.set('view engine', 'ejs'); |
| 43 | + |
| 44 | + oidcConfig.findAccount = options.findAccount || AccountService.findAccount; |
| 45 | + |
| 46 | + if (options.jwks) { |
| 47 | + oidcConfig.jwks = options.jwks; |
| 48 | + } else { |
| 49 | + const keyManager = new KeyManager(); |
| 50 | + oidcConfig.jwks = await keyManager.loadKeysOrGenerateAndSave(options.keysPath || path.join('data', 'keys.json')); |
| 51 | + } |
| 52 | + |
| 53 | + if (options.cookies) { |
| 54 | + oidcConfig.cookies = options.cookies; |
| 55 | + } else { |
| 56 | + oidcConfig.cookies = new CookieSecretManager(options.cookieSecretsPath || path.join('data', 'cookie_secrets.json')).getCookies(); |
| 57 | + } |
| 58 | + |
| 59 | + const provider = new Provider(config.issuer, { |
| 60 | + adapter: options.adapter || PersistentAdapter, |
| 61 | + ...oidcConfig |
| 62 | + }); |
| 63 | + |
| 64 | + if (config.env_production) { |
| 65 | + app.enable('trust proxy'); |
| 66 | + provider.proxy = true; |
| 67 | + |
| 68 | + app.use((req, res, next) => { |
| 69 | + if (req.secure) { |
| 70 | + next(); |
| 71 | + } else if (req.method === 'GET' || req.method === 'HEAD') { |
| 72 | + res.redirect(url.format({ |
| 73 | + protocol: 'https', |
| 74 | + host: req.hostname, |
| 75 | + pathname: req.originalUrl, |
| 76 | + })); |
| 77 | + } else { |
| 78 | + res.status(400).json({ |
| 79 | + error: 'invalid_request', |
| 80 | + error_description: 'do yourself a favor and only use https', |
| 81 | + }); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + app.use(options.addUserInfo || addUserInfo); |
| 87 | + routes(app, provider); |
| 88 | + app.use(provider.callback()); |
| 89 | + |
| 90 | + return { app, provider }; |
| 91 | +} |
0 commit comments