forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (92 loc) · 3.01 KB
/
Copy pathindex.js
File metadata and controls
114 lines (92 loc) · 3.01 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* eslint-disable no-console */
'use strict';
const path = require('path');
const _ = require('lodash');
const koa = require('koa');
const body = require('koa-body');
const mount = require('koa-mount');
const querystring = require('querystring');
const rewrite = require('koa-rewrite');
const Router = require('koa-router');
const render = require('koa-ejs');
const port = process.env.PORT || 3000;
const app = koa();
render(app, {
cache: false,
layout: false,
root: path.join(__dirname, 'views'),
});
app.keys = ['some secret key', 'and also the old one'];
const Account = require('./account');
const settings = require('./settings');
const Provider = require('../lib').Provider;
const issuer = process.env.ISSUER || 'http://localhost:3000/op';
if (process.env.HEROKU) {
settings.config.timeouts = {
request_uri: 15000,
sector_identifier_uri: 15000,
backchannel_logout_uri: 15000,
jwks_uri: 15000,
};
app.proxy = true;
_.set(settings.config, 'cookies.short.secure', true);
_.set(settings.config, 'cookies.long.secure', true);
app.use(function * ensureSecure(next) {
if (this.secure) {
yield next;
} else if (this.method === 'GET' || this.method === 'HEAD') {
this.redirect(this.href.replace(/^http:\/\//i, 'https://'));
} else {
this.body = {
error: 'invalid_request',
error_description: 'do yourself a favor and only use https',
};
this.status = 400;
}
});
}
if (process.env.MONGODB_URI) {
const MongoAdapter = require('./adapters/mongodb'); // eslint-disable-line global-require
settings.config.adapter = MongoAdapter;
}
settings.config.findById = Account.findById;
const provider = new Provider(issuer, settings.config);
app.use(rewrite(/^\/\.well-known\/(.*)/, '/op/.well-known/$1'));
app.use(mount('/op', provider.app));
const router = new Router();
router.get('/interaction/:grant', function * renderInteraction(next) {
const cookie = JSON.parse(this.cookies.get('_grant', { signed: true }));
const client = yield provider.get('Client').find(cookie.params.client_id);
yield this.render('login', {
client,
cookie,
action: '/login',
debug: querystring.stringify(cookie.params, ',<br/>', ' = ', {
encodeURIComponent: (value) => value,
}),
interaction: querystring.stringify(cookie.interaction, ',<br/>', ' = ', {
encodeURIComponent: (value) => value,
}),
});
yield next;
});
router.post('/login', body(), function * submitLoginForm() {
const account = yield Account.findByLogin(this.request.body.login);
const result = {
login: {
account: account.accountId,
acr: '1',
remember: !!this.request.body.remember,
ts: Date.now() / 1000 | 0,
},
consent: {},
};
provider.resume(this, this.request.body.uuid, result);
});
app.use(router.routes());
Promise.all(settings.certificates.map(cert => provider.addKey(cert)))
.then(() => Promise.all(
settings.clients.map(client => provider.addClient(client))
).catch(console.error))
.then(
() => app.listen(port));