This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
express.js
72 lines (59 loc) · 2.11 KB
/
express.js
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
/* eslint-disable no-console */
const path = require('path');
const url = require('url');
const { set } = require('lodash');
const express = require('express'); // eslint-disable-line import/no-unresolved
const helmet = require('helmet');
const Provider = require('../lib'); // require('oidc-provider');
const Account = require('./support/account');
const { provider: providerConfiguration, clients, keys } = require('./support/configuration');
const routes = require('./routes/express');
const { PORT = 3000, ISSUER = `http://localhost:${PORT}`, TIMEOUT } = process.env;
providerConfiguration.findById = Account.findById;
const app = express();
app.use(helmet());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
const provider = new Provider(ISSUER, providerConfiguration);
if (TIMEOUT) {
provider.defaultHttpOptions = { timeout: parseInt(TIMEOUT, 10) };
}
let server;
(async () => {
await provider.initialize({
adapter: process.env.MONGODB_URI ? require('./support/heroku_mongo_adapter') : undefined, // eslint-disable-line global-require
clients,
keystore: { keys },
});
if (process.env.NODE_ENV === 'production') {
app.enable('trust proxy');
provider.proxy = true;
set(providerConfiguration, 'cookies.short.secure', true);
set(providerConfiguration, 'cookies.long.secure', 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.get('host'),
pathname: req.originalUrl,
}));
} else {
res.status(400).json({
error: 'invalid_request',
error_description: 'do yourself a favor and only use https',
});
}
});
}
routes(app, provider);
app.use(provider.callback);
server = app.listen(PORT, () => {
console.log(`application is listening on port ${PORT}, check it's /.well-known/openid-configuration`);
});
})().catch((err) => {
if (server && server.listening) server.close();
console.error(err);
process.exitCode = 1;
});