Skip to content

Commit 0772881

Browse files
committed
feat: unit tests
1 parent 5af9a67 commit 0772881

13 files changed

Lines changed: 1759 additions & 3071 deletions

app.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

index.js

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,16 @@
44
import 'dotenv/config';
55
import https from 'https';
66
import fs from 'fs';
7-
import * as path from 'node:path';
8-
import * as url from 'node:url';
97

10-
import { dirname } from 'desm';
11-
import express from 'express';
12-
import helmet from 'helmet';
13-
14-
import Provider from 'oidc-provider';
15-
16-
import AccountService from './util/accountService.js';
17-
import configuration from './util/configuration.js';
18-
import routes from './util/routes.js';
19-
import { addUserInfo } from "./util/verifyJWT.js";
20-
import PersistentAdapter from "./util/persistentAdapter.js";
21-
import { KeyManager } from "./util/generateKeys.js";
22-
import { CookieSecretManager } from "./util/generateCookieKeys.js";
23-
import { MAIN_CONFIG } from "./config/main.js";
24-
25-
const __dirname = dirname(import.meta.url);
26-
27-
const app = express();
28-
29-
if (MAIN_CONFIG.env_production) {
30-
const directives = helmet.contentSecurityPolicy.getDefaultDirectives();
31-
delete directives['form-action'];
32-
app.use(helmet({
33-
contentSecurityPolicy: {
34-
useDefaults: false,
35-
directives,
36-
},
37-
}));
38-
}
39-
40-
app.set('views', path.join(__dirname, 'views'));
41-
app.set('view engine', 'ejs');
8+
import { initializeApp } from './app.js';
9+
import { MAIN_CONFIG } from './config/main.js';
4210

4311
async function main() {
4412
let server;
4513
try {
46-
configuration.findAccount = AccountService.findAccount;
47-
configuration.jwks = await (new KeyManager()).loadKeysOrGenerateAndSave(path.join('data', 'keys.json'));
48-
configuration.cookies = new CookieSecretManager(path.join('data', 'cookie_secrets.json')).getCookies();
49-
const provider = new Provider(MAIN_CONFIG.issuer, {adapter: PersistentAdapter, ...configuration });
14+
const { app } = await initializeApp();
5015
const port = MAIN_CONFIG.port;
5116

52-
if (MAIN_CONFIG.env_production) {
53-
app.enable('trust proxy');
54-
provider.proxy = true;
55-
56-
app.use((req, res, next) => {
57-
if (req.secure) {
58-
next();
59-
} else if (req.method === 'GET' || req.method === 'HEAD') {
60-
res.redirect(url.format({
61-
protocol: 'https',
62-
host: req.hostname,
63-
pathname: req.originalUrl,
64-
}));
65-
} else {
66-
res.status(400).json({
67-
error: 'invalid_request',
68-
error_description: 'do yourself a favor and only use https',
69-
});
70-
}
71-
});
72-
}
73-
74-
// noinspection JSCheckFunctionSignatures
75-
app.use(addUserInfo)
76-
routes(app, provider);
77-
app.use(provider.callback());
78-
7917
if (MAIN_CONFIG.use_ssl) {
8018
server = https.createServer({
8119
key: fs.readFileSync('key.pem'),

0 commit comments

Comments
 (0)