-
-
Notifications
You must be signed in to change notification settings - Fork 767
/
oidc_context.js
291 lines (226 loc) · 7.53 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
import * as events from 'node:events';
import ctxRef from '../models/ctx_ref.js';
import get from './_/get.js';
import isPlainObject from './_/is_plain_object.js';
import omitBy from './_/omit_by.js';
import { InvalidRequest } from './errors.js';
import instance from './weak_cache.js';
import resolveResponseMode from './resolve_response_mode.js';
const COOKIES = Symbol();
export default function getContext(provider) {
const {
acceptQueryParamAccessTokens,
features: {
dPoP: dPoPConfig,
fapi,
},
scopes: oidcScopes,
} = instance(provider).configuration();
const { app } = provider;
class OIDCContext extends events.EventEmitter {
#requestParamClaims = null;
#accessToken = null;
#fapiProfile = null;
constructor(ctx) {
super();
this.ctx = ctx;
this.route = ctx._matchedRouteName;
this.authorization = {};
this.redirectUriCheckPerformed = false;
this.webMessageUriCheckPerformed = false;
this.entities = {};
this.claims = {};
this.resourceServers = {};
}
get cookies() {
if (!this[COOKIES]) {
this[COOKIES] = app.createContext(this.ctx.req, this.ctx.res).cookies;
this[COOKIES].secure = !this[COOKIES].secure && this.ctx.secure
? true : this[COOKIES].secure;
}
return this[COOKIES];
}
get fapiProfile() {
if (this.#fapiProfile === null) {
this.#fapiProfile = fapi.profile(this.ctx, this.client);
}
return this.#fapiProfile;
}
isFapi(...oneOf) {
const i = oneOf.indexOf(this.fapiProfile);
return i !== -1 ? oneOf[i] : undefined;
}
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;
if (key === 'Client') {
this.emit('assign.client', 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 new URL(provider.pathFor(name, { mountPath, ...opt }), this.ctx.href).href;
}
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 requestParamClaims() {
if (this.#requestParamClaims) {
return 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);
}
});
}
}
this.#requestParamClaims = requestParamClaims;
return requestParamClaims;
}
clientJwtAuthExpectedAudience() {
return new Set([this.issuer, this.urlFor('token'), this.urlFor(this.route)]);
}
get requestParamScopes() {
return new Set(this.params.scope ? this.params.scope.split(' ') : undefined);
}
get requestParamOIDCScopes() {
if (!this.params.scope) {
return new Set();
}
return new Set(this.params.scope.split(' ').filter(Set.prototype.has.bind(oidcScopes)));
}
resolvedClaims() {
const rejected = this.session.rejectedClaimsFor(this.params.client_id);
const claims = JSON.parse(JSON.stringify(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 authorizationCode() {
return this.entities.AuthorizationCode;
}
get refreshToken() {
return this.entities.RefreshToken;
}
get accessToken() {
return this.entities.AccessToken;
}
get account() {
return this.entities.Account;
}
get client() {
return this.entities.Client;
}
get grant() {
return this.entities.Grant;
}
getAccessToken({
acceptDPoP = false, acceptQueryParam = acceptQueryParamAccessTokens && !fapi.enabled,
} = {}) {
if (this.#accessToken) {
return this.#accessToken;
}
const { ctx } = this;
const mechanisms = omitBy({
body: ctx.is('application/x-www-form-urlencoded') && get(ctx.oidc, 'body.access_token'),
header: ctx.headers.authorization,
query: ctx.query.access_token,
}, (value) => typeof value !== 'string' || !value);
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');
}
if (!acceptQueryParam && mechanism === 'query') {
throw new InvalidRequest('access tokens must not be provided via query parameter');
}
const dpop = acceptDPoP && dPoPConfig.enabled && ctx.get('DPoP');
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 (!dpop && scheme.toLowerCase() === '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');
}
this.#accessToken = token;
return token;
}
}
return OIDCContext;
}