-
-
Notifications
You must be signed in to change notification settings - Fork 773
/
Copy pathoidc_context.js
395 lines (320 loc) · 11.4 KB
/
oidc_context.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const events = require('events');
const url = require('url');
const { deprecate } = require('util');
const isPlainObject = require('lodash/isPlainObject');
const cloneDeep = require('lodash/cloneDeep');
const omitBy = require('lodash/omitBy');
const get = require('lodash/get');
const isUndefined = require('lodash/isUndefined');
const debug = require('debug')('oidc-provider:bearer');
const { JWT, JWK, errors } = require('jose');
const ctxRef = require('../models/ctx_ref');
const Cookies = require('../cookies');
const nanoid = require('./nanoid');
const { InvalidRequest } = require('./errors');
const instance = require('./weak_cache');
const resolveResponseMode = require('./resolve_response_mode');
const COOKIES = Symbol('context#cookies');
module.exports = function getContext(provider) {
const { clockTolerance, features: { dPoP: dPoPConfig } } = instance(provider).configuration();
const { app } = provider;
class OIDCContext extends events.EventEmitter {
constructor(ctx) {
super();
this.ctx = ctx;
this.route = ctx._matchedRouteName;
this.authorization = {};
this.redirectUriCheckPerformed = false;
this.webMessageUriCheckPerformed = false;
this.uid = (ctx.params && ctx.params.uid) || nanoid();
this.entities = {};
this.claims = {};
}
get cookies() {
if (!this[COOKIES]) {
this[COOKIES] = new Cookies(this.ctx.req, this.ctx.res, {
keys: app.keys,
secure: this.ctx.secure,
});
}
return this[COOKIES];
}
get issuer() { // eslint-disable-line class-methods-use-this
return provider.issuer;
}
get provider() { // eslint-disable-line class-methods-use-this
return provider;
}
entity(key, value) {
if (value instanceof provider.BaseToken) {
ctxRef.set(value, this.ctx);
}
this.entities[key] = value;
this.emit(`assign.${key.toLowerCase()}`, this.ctx, value);
}
urlFor(name, opt) {
const mountPath = (this.ctx.req.originalUrl && this.ctx.req.originalUrl.substring(
0,
this.ctx.req.originalUrl.indexOf(this.ctx.request.url),
))
|| this.ctx.mountPath // koa-mount
|| this.ctx.req.baseUrl // expressApp.use('/op', provider.callback);
|| ''; // no mount
return url.resolve(this.ctx.href, provider.pathFor(name, { mountPath, ...opt }));
}
promptPending(name) {
if (this.ctx.oidc.route.endsWith('resume')) {
const should = new Set([...this.prompts]);
Object.keys(this.result || {}).forEach(Set.prototype.delete.bind(should));
return should.has(name);
}
// first pass
return this.prompts.has(name);
}
get dPoP() {
if (!dPoPConfig.enabled) {
return undefined;
}
if ('dpop' in instance(this)) {
return instance(this).dpop;
}
const token = this.ctx.get('DPoP');
if (!token) {
return undefined;
}
try {
const { header, payload } = JWT.decode(token, { complete: true });
let key;
if (header.typ !== 'dpop+jwt') {
throw new Error('typ must be dpop+jwt');
}
if (typeof header.alg !== 'string' || !header.alg || header.alg === 'none' || header.alg.startsWith('HS')) {
throw new Error('invalid alg');
}
if (!instance(provider).configuration('dPoPSigningAlgValues').includes(header.alg)) {
throw new Error('unsupported alg');
}
if (typeof header.jwk !== 'object' || !header.jwk) {
throw new Error('header must have a jwk');
}
try {
key = JWK.asKey(header.jwk);
} catch (err) {
throw new Error('failed to import jwk');
}
if (key.type !== 'public') {
throw new Error('jwk must be a public key');
}
if (typeof payload.jti !== 'string' || !payload.jti) {
throw new Error('must have a jti string property');
}
if (typeof payload.iat !== 'number' || !payload.iat) {
throw new Error('must have a iat number property');
}
if (payload.http_method !== this.ctx.method) {
throw new Error('http_method mismatch');
}
if (payload.http_uri !== `${this.ctx.origin}${this.ctx.path}`) {
throw new Error('http_uri mismatch');
}
try {
JWT.verify(token, key, { maxTokenAge: `${dPoPConfig.iatTolerance} seconds`, clockTolerance: `${clockTolerance} seconds` });
} catch (err) {
if (err instanceof errors.JWTClaimInvalid) {
throw new Error(`failed claim check (${err.message})`);
}
throw err;
}
const result = { jwk: key, jti: payload.jti, iat: payload.iat };
instance(this).dpop = result;
return result;
} catch (err) {
throw new InvalidRequest(`invalid DPoP Proof JWT (${err.message})`);
}
}
get requestParamClaims() {
if ('requestParamClaims' in instance(this)) {
return instance(this).requestParamClaims;
}
const requestParamClaims = new Set();
if (this.params.claims) {
const {
userinfo, id_token: idToken,
} = JSON.parse(this.params.claims);
const claims = instance(provider).configuration('claimsSupported');
if (userinfo) {
Object.entries(userinfo).forEach(([claim, value]) => {
if (claims.has(claim) && (value === null || isPlainObject(value))) {
requestParamClaims.add(claim);
}
});
}
if (idToken) {
Object.entries(idToken).forEach(([claim, value]) => {
if (claims.has(claim) && (value === null || isPlainObject(value))) {
requestParamClaims.add(claim);
}
});
}
}
instance(this).requestParamClaims = requestParamClaims;
return requestParamClaims;
}
get requestParamScopes() {
if ('requestParamScopes' in instance(this)) {
return instance(this).requestParamScopes;
}
const requestParamScopes = new Set();
if (this.params.scope) {
const scopes = this.params.scope.split(' ');
const { scopes: statics, dynamicScopes: dynamics } = instance(provider).configuration();
scopes.forEach((scope) => {
if (statics.has(scope)) {
requestParamScopes.add(scope);
return;
}
for (const dynamic of dynamics) { // eslint-disable-line no-restricted-syntax
if (dynamic.test(scope)) {
requestParamScopes.add(scope);
return;
}
}
});
}
instance(this).requestParamScopes = requestParamScopes;
return requestParamScopes;
}
acceptedScope() {
// acceptedScopesFor already has the rejected filtered out
const accepted = this.session.acceptedScopesFor(this.params.client_id);
return [...this.requestParamScopes].filter((scope) => accepted.has(scope)).join(' ') || undefined;
}
resolvedClaims() {
const rejected = this.session.rejectedClaimsFor(this.params.client_id);
const claims = cloneDeep(this.claims);
claims.rejected = [...rejected];
return claims;
}
get responseMode() {
if (typeof this.params.response_mode === 'string') {
return this.params.response_mode;
}
if (this.params.response_type !== undefined) {
return resolveResponseMode(this.params.response_type);
}
return undefined;
}
get acr() {
return this.session.acr;
}
get amr() {
return this.session.amr;
}
get prompts() {
return new Set(this.params.prompt ? this.params.prompt.split(' ') : []);
}
get registrationAccessToken() {
return this.entities.RegistrationAccessToken;
}
get deviceCode() {
return this.entities.DeviceCode;
}
get accessToken() {
return this.entities.AccessToken;
}
get account() {
return this.entities.Account;
}
get client() {
return this.entities.Client;
}
getAccessToken({ acceptDPoP = false, acceptQueryParam = true } = {}) {
if ('accessToken' in instance(this)) {
return instance(this).accessToken;
}
const { ctx } = this;
const mechanisms = omitBy({
body: get(ctx.oidc, 'body.access_token'),
header: ctx.headers.authorization,
query: acceptQueryParam ? ctx.query.access_token : undefined,
}, (value) => typeof value !== 'string' || !value);
debug('uid=%s received access token via %o', this.uid, mechanisms);
let mechanism;
let length;
let token;
try {
({ 0: [mechanism, token], length } = Object.entries(mechanisms));
} catch (err) {}
if (!length) {
throw new InvalidRequest('no access token provided');
}
if (length > 1) {
throw new InvalidRequest('access token must only be provided using one mechanism');
}
let dPoP;
if (acceptDPoP && dPoPConfig.enabled) {
({ dPoP } = this);
}
if (mechanism === 'header') {
const header = token;
const { 0: scheme, 1: value, length: parts } = header.split(' ');
if (parts !== 2) {
throw new InvalidRequest('invalid authorization header value format');
}
if (dPoP && scheme.toLowerCase() !== 'dpop') {
throw new InvalidRequest('authorization header scheme must be `DPoP` when DPoP is used');
} else if (dPoPConfig.enabled && scheme.toLowerCase() === 'dpop' && !dPoP) {
throw new InvalidRequest('`DPoP` header not provided');
} else if (!dPoP && scheme.toLowerCase() !== 'bearer') {
throw new InvalidRequest('authorization header scheme must be `Bearer`');
}
token = value;
}
if (dPoP && mechanism !== 'header') {
throw new InvalidRequest('`DPoP` tokens must be provided via an authorization header');
}
instance(this).accessToken = token;
return token;
}
}
Object.defineProperty(OIDCContext.prototype, 'bearer', {
get: deprecate(/* istanbul ignore next */ function getBearer() {
if ('bearer' in instance(this)) {
return instance(this).bearer;
}
const { ctx } = this;
const mechanisms = omitBy({
body: get(ctx.oidc, 'body.access_token'),
header: ctx.headers.authorization,
query: ctx.query.access_token,
}, isUndefined);
debug('uid=%s received bearer via %o', this.uid, mechanisms);
let mechanism;
let length;
let bearer;
try {
({ 0: [mechanism, bearer], length } = Object.entries(mechanisms));
} catch (err) {}
if (!length) {
throw new InvalidRequest('no bearer auth mechanism provided');
}
if (length > 1) {
throw new InvalidRequest('bearer token must only be provided using one mechanism');
}
if (mechanism === 'header') {
const header = bearer;
const { 0: scheme, 1: value, length: parts } = header.split(' ');
if (parts !== 2 || scheme.toLowerCase() !== 'bearer') {
throw new InvalidRequest('invalid authorization header value format');
}
bearer = value;
}
if (!bearer) {
throw new InvalidRequest('no bearer token provided');
}
instance(this).bearer = bearer;
return bearer;
}, 'ctx.oidc.bearer is deprecated, use ctx.oidc.getAccessToken() instead'),
});
return OIDCContext;
};