-
Notifications
You must be signed in to change notification settings - Fork 166
/
oauth.js
321 lines (262 loc) · 7.74 KB
/
oauth.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const crypto = require('crypto');
const saml = require('../saml/saml.js');
const codeVerifier = require('./oauth/code-verifier.js');
const { indexNames, extractAuthToken } = require('./utils.js');
const dbutils = require('../db/utils.js');
const redirect = require('./oauth/redirect.js');
const allowed = require('./oauth/allowed.js');
let configStore;
let sessionStore;
let codeStore;
let tokenStore;
let options;
const relayStatePrefix = 'boxyhq_jackson_';
function getEncodedClientId(client_id) {
try {
const sp = new URLSearchParams(client_id);
const tenant = sp.get('tenant');
const product = sp.get('product');
if (tenant && product) {
return {
tenant: sp.get('tenant'),
product: sp.get('product'),
};
}
return null;
} catch (err) {
return null;
}
}
const authorize = async (req, res) => {
const {
response_type = 'code',
client_id,
redirect_uri,
state,
tenant,
product,
code_challenge,
code_challenge_method = '',
// eslint-disable-next-line no-unused-vars
provider = 'saml',
} = req.query;
if (!redirect_uri) {
return res.status(400).send('Please specify a redirect URL.');
}
if (!state) {
return res
.status(400)
.send('Please specify a state to safeguard against XSRF attacks.');
}
let samlConfig;
if (
client_id &&
client_id !== '' &&
client_id !== 'undefined' &&
client_id !== 'null'
) {
// if tenant and product are encoded in the client_id then we parse it and check for the relevant config(s)
const sp = getEncodedClientId(client_id);
if (sp) {
const samlConfigs = await configStore.getByIndex({
name: indexNames.tenantProduct,
value: dbutils.keyFromParts(sp.tenant, sp.product),
});
if (!samlConfigs || samlConfigs.length === 0) {
return res.status(403).send('SAML configuration not found.');
}
// TODO: Support multiple matches
samlConfig = samlConfigs[0];
} else {
samlConfig = await configStore.get(client_id);
}
} else {
const samlConfigs = await configStore.getByIndex({
name: indexNames.tenantProduct,
value: dbutils.keyFromParts(tenant, product),
});
if (!samlConfigs || samlConfigs.length === 0) {
return res.status(403).send('SAML configuration not found.');
}
// TODO: Support multiple matches
samlConfig = samlConfigs[0];
}
if (!samlConfig) {
return res.status(403).send('SAML configuration not found.');
}
if (!allowed.redirect(redirect_uri, samlConfig.redirectUrl)) {
return res.status(403).send('Redirect URL is not allowed.');
}
const samlReq = saml.request({
entityID: options.samlAudience,
callbackUrl: options.externalUrl + options.samlPath,
signingKey: samlConfig.certs.privateKey,
});
const sessionId = crypto.randomBytes(16).toString('hex');
await sessionStore.put(sessionId, {
id: samlReq.id,
redirect_uri,
response_type,
state,
code_challenge,
code_challenge_method,
});
return redirect.success(res, samlConfig.idpMetadata.sso.redirectUrl, {
RelayState: relayStatePrefix + sessionId,
SAMLRequest: Buffer.from(samlReq.request).toString('base64'),
});
};
const samlResponse = async (req, res) => {
const { SAMLResponse } = req.body; // RelayState will contain the sessionId from earlier quasi-oauth flow
let RelayState = req.body.RelayState || '';
if (!options.idpEnabled && !RelayState.startsWith(relayStatePrefix)) {
// IDP is disabled so block the request
return res
.status(403)
.send(
'IdP (Identity Provider) flow has been disabled. Please head to your Service Provider to login.'
);
}
if (!RelayState.startsWith(relayStatePrefix)) {
RelayState = '';
}
RelayState = RelayState.replace(relayStatePrefix, '');
const rawResponse = Buffer.from(SAMLResponse, 'base64').toString();
const parsedResp = await saml.parseAsync(rawResponse);
const samlConfigs = await configStore.getByIndex({
name: indexNames.entityID,
value: parsedResp.issuer,
});
if (!samlConfigs || samlConfigs.length === 0) {
return res.status(403).send('SAML configuration not found.');
}
// TODO: Support multiple matches
const samlConfig = samlConfigs[0];
let session;
if (RelayState !== '') {
session = await sessionStore.get(RelayState);
if (!session) {
return redirect.error(
res,
samlConfig.defaultRedirectUrl,
'Unable to validate state from the origin request.'
);
}
}
let validateOpts = {
thumbprint: samlConfig.idpMetadata.thumbprint,
audience: options.samlAudience,
};
if (session && session.id) {
validateOpts.inResponseTo = session.id;
}
const profile = await saml.validateAsync(rawResponse, validateOpts);
// some providers don't return the id in the assertion, we set it to a sha256 hash of the email
if (profile && profile.claims && !profile.claims.id) {
profile.claims.id = crypto.createHash('sha256').update(profile.claims.email).digest('hex');
}
// store details against a code
const code = crypto.randomBytes(20).toString('hex');
let codeVal = {
profile,
clientID: samlConfig.clientID,
clientSecret: samlConfig.clientSecret,
};
if (session) {
codeVal.session = session;
}
await codeStore.put(code, codeVal);
if (
session &&
session.redirect_uri &&
!allowed.redirect(session.redirect_uri, samlConfig.redirectUrl)
) {
return res.status(403).send('Redirect URL is not allowed.');
}
let params = {
code,
};
if (session && session.state) {
params.state = session.state;
}
return redirect.success(
res,
(session && session.redirect_uri) || samlConfig.defaultRedirectUrl,
params
);
};
const token = async (req, res) => {
const {
client_id,
client_secret,
code_verifier,
code,
grant_type = 'authorization_code',
} = req.body;
if (grant_type !== 'authorization_code') {
return res.status(400).send('Unsupported grant_type');
}
if (!code) {
return res.status(400).send('Please specify code');
}
const codeVal = await codeStore.get(code);
if (!codeVal || !codeVal.profile) {
return res.status(403).send('Invalid code');
}
if (client_id && client_secret) {
// check if we have an encoded client_id
const sp = getEncodedClientId(client_id);
if (!sp) {
// OAuth flow
if (
client_id !== codeVal.clientID ||
client_secret !== codeVal.clientSecret
) {
return res.status(401).send('Invalid client_id or client_secret');
}
}
} else if (code_verifier) {
// PKCE flow
let cv = code_verifier;
if (codeVal.session.code_challenge_method.toLowerCase() === 's256') {
cv = codeVerifier.encode(code_verifier);
}
if (codeVal.session.code_challenge !== cv) {
return res.status(401).send('Invalid code_verifier');
}
} else if (codeVal && codeVal.session) {
return res
.status(401)
.send('Please specify client_secret or code_verifier');
}
// store details against a token
const token = crypto.randomBytes(20).toString('hex');
await tokenStore.put(token, codeVal.profile);
res.json({
access_token: token,
token_type: 'bearer',
expires_in: options.db.ttl,
});
};
const userInfo = async (req, res) => {
let token = extractAuthToken(req);
// check for query param
if (!token) {
token = req.query.access_token;
}
const profile = await tokenStore.get(token);
res.json(profile.claims);
};
module.exports = (opts) => {
configStore = opts.configStore;
sessionStore = opts.sessionStore;
codeStore = opts.codeStore;
tokenStore = opts.tokenStore;
options = opts.opts;
return {
authorize,
samlResponse,
token,
userInfo,
};
};