This repository was archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathoidcUtil.js
More file actions
147 lines (129 loc) · 4.05 KB
/
Copy pathoidcUtil.js
File metadata and controls
147 lines (129 loc) · 4.05 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*!
* Copyright (c) 2017-Present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/
const passport = require('passport');
const OpenIdClient = require('openid-client');
const Negotiator = require('negotiator');
const os = require('os');
const pkg = require('../package.json');
const OpenIdClientStrategy = OpenIdClient.Strategy;
const Issuer = OpenIdClient.Issuer;
const custom = OpenIdClient.custom;
const oidcUtil = module.exports;
function customizeUserAgent(options) {
/**
* Parse out the default user agent for the openid-client library, which currently looks like:
*
* openid-client/1.15.0 (https://github.com/panva/node-openid-client)
*
* We strip off the github link because it's not necessary.
*/
options = options || {};
const headers = options.headers || {};
let clientUserAgent = headers['User-Agent'];
if (typeof clientUserAgent === 'string') {
clientUserAgent = ' ' + clientUserAgent.split(' ')[0]
} else {
clientUserAgent = '';
}
const userAgent = `${pkg.name}/${pkg.version}${clientUserAgent} node/${process.versions.node} ${os.platform()}/${os.release()}`;
headers['User-Agent'] = userAgent;
options.headers = headers;
return options;
}
oidcUtil.createClient = context => {
const {
issuer,
client_id,
client_secret,
loginRedirectUri: redirect_uri,
maxClockSkew,
timeout,
oidcClientOptions
} = context.options;
Issuer[custom.http_options] = function(options) {
options = customizeUserAgent(options);
options.timeout = timeout || 10000;
return options;
};
return Issuer.discover(issuer + '/.well-known/openid-configuration')
.then(iss => {
const client = new iss.Client(Object.assign({
client_id,
client_secret,
redirect_uris: [
redirect_uri
]
}, oidcClientOptions));
client[custom.http_options] = options => {
options = customizeUserAgent(options);
options.timeout = timeout || 10000;
return options;
};
client[custom.clock_tolerance] = maxClockSkew;
return client;
});
};
oidcUtil.bootstrapPassportStrategy = context => {
const oidcStrategy = new OpenIdClientStrategy({
params: {
scope: context.options.scope
},
sessionKey: context.options.sessionKey,
client: context.client
}, (tokenSet, callbackArg1, callbackArg2) => {
let done;
let userinfo;
if (typeof(callbackArg2) !== 'undefined') {
done = callbackArg2;
userinfo = callbackArg1;
} else {
done = callbackArg1;
}
if(tokenSet) {
return userinfo ?
done(null, {
userinfo,
tokens: tokenSet,
}) :
done(null, {
tokens: tokenSet
});
} else {
return done(null);
}
});
// bypass passport's serializers
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
passport.use('oidc', oidcStrategy);
};
oidcUtil.ensureAuthenticated = (context, options = {}) => {
return (req, res, next) => {
const isAuthenticated = req.isAuthenticated && req.isAuthenticated();
if (isAuthenticated) {
return next();
}
const negotiator = new Negotiator(req);
if (negotiator.mediaType() === 'text/html') {
if (!isAuthenticated) {
if (req.session) {
req.session.returnTo = req.originalUrl || req.url;
}
const url = options.redirectTo || context.options.routes.login.path;
return res.redirect(url);
}
next();
} else {
res.sendStatus(401);
}
};
};