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