forked from DuendeArchive/identity-model-oidc-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseValidator.js
More file actions
306 lines (240 loc) · 10.4 KB
/
ResponseValidator.js
File metadata and controls
306 lines (240 loc) · 10.4 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
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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import Log from './Log';
import MetadataService from './MetadataService';
import UserInfoService from './UserInfoService';
import JwtUtil from './JwtUtil';
const ProtocolClaims = ["nonce", "at_hash", "iat", "nbf", "exp", "aud", "iss", "c_hash"];
export default class ResponseValidator {
constructor(settings, MetadataServiceCtor = MetadataService, UserInfoServiceCtor = UserInfoService, jwtUtil = JwtUtil) {
if (!settings) {
Log.error("No settings passed to ResponseValidator");
throw new Error("settings");
}
this._settings = settings;
this._metadataService = new MetadataServiceCtor(this._settings);
this._userInfoService = new UserInfoServiceCtor(this._settings);
this._jwtUtil = jwtUtil;
}
validateSigninResponse(state, response) {
Log.info("ResponseValidator.validateSigninResponse");
return this._processSigninParams(state, response).then(response => {
Log.info("state processed");
return this._validateTokens(state, response).then(response => {
Log.info("tokens validated");
return this._processClaims(response).then(response => {
Log.info("claims processed");
return response;
});
});
});
}
validateSignoutResponse(state, response) {
Log.info("ResponseValidator.validateSignoutResponse");
if (state.id !== response.state) {
Log.error("State does not match");
return Promise.reject(new Error("State does not match"));
}
// now that we know the state matches, take the stored data
// and set it into the response so callers can get their state
// this is important for both success & error outcomes
Log.info("state validated");
response.state = state.data;
if (response.error) {
Log.warn("Response was error", response.error);
return Promise.reject(response);
}
return Promise.resolve(response);
}
_processSigninParams(state, response) {
Log.info("ResponseValidator._processSigninParams");
if (state.id !== response.state) {
Log.error("State does not match");
return Promise.reject(new Error("State does not match"));
}
// now that we know the state matches, take the stored data
// and set it into the response so callers can get their state
// this is important for both success & error outcomes
Log.info("state validated");
response.state = state.data;
if (response.error) {
Log.warn("Response was error", response.error);
return Promise.reject(response);
}
if (state.nonce && !response.id_token) {
Log.error("Expecting id_token in response");
return Promise.reject(new Error("No id_token in response"));
}
if (!state.nonce && response.id_token) {
Log.error("Not expecting id_token in response");
return Promise.reject(new Error("Unexpected id_token in response"));
}
return Promise.resolve(response);
}
_processClaims(response) {
Log.info("ResponseValidator._processClaims");
response.profile = this._filterProtocolClaims(response.profile);
if (this._settings.loadUserInfo && response.access_token) {
Log.info("loading user info");
return this._userInfoService.getClaims(response.access_token).then(claims => {
response.profile = this._mergeClaims(response.profile, claims);
Log.info("user info claims received, updated profile:", response.profile);
return response;
});
}
return Promise.resolve(response);
}
_mergeClaims(claims1, claims2) {
var result = Object.assign({}, claims1);
for (let name in claims2) {
var values = claims2[name];
if (!Array.isArray(values)) {
values = [values];
}
for (let value of values) {
if (!result[name]) {
result[name] = value;
}
else if (Array.isArray(result[name])) {
if (result[name].indexOf(value) < 0) {
result[name].push(value);
}
}
else if (result[name] !== value) {
result[name] = [result[name], value];
}
}
}
return result;
}
_filterProtocolClaims(claims) {
Log.info("ResponseValidator._filterProtocolClaims, incoming claims:", claims);
var result = Object.assign({}, claims);
if (this._settings._filterProtocolClaims) {
ProtocolClaims.forEach(type => {
delete result[type];
});
Log.info("protocol claims filtered", result);
}
else {
Log.info("protocol claims not filtered")
}
return result;
}
_validateTokens(state, response) {
Log.info("ResponseValidator._validateTokens");
if (response.id_token) {
if (response.access_token) {
Log.info("Validating id_token and access_token");
return this.__validateIdTokenAndAccessToken(state, response);
}
Log.info("Validating id_token");
return this._validateIdToken(state, response);
}
Log.info("No id_token to validate");
return Promise.resolve(response);
}
__validateIdTokenAndAccessToken(state, response) {
Log.info("ResponseValidator.__validateIdTokenAndAccessToken");
return this._validateIdToken(state, response).then(response => {
return this._validateAccessToken(response);
});
}
_validateIdToken(state, response) {
Log.info("ResponseValidator._validateIdToken");
if (!state.nonce) {
Log.error("No nonce on state");
return Promise.reject(new Error("No nonce on state"));
}
let jwt = this._jwtUtil.parseJwt(response.id_token);
if (!jwt || !jwt.header || !jwt.payload) {
Log.error("Failed to parse id_token", jwt);
return Promise.reject(new Error("Failed to parse id_token"));
}
if (state.nonce !== jwt.payload.nonce){
Log.error("Invalid nonce in id_token");
return Promise.reject(new Error("Invalid nonce in id_token"));
}
var kid = jwt.header.kid;
if (!kid) {
Log.error("No kid found in id_token");
return Promise.reject(new Error("No kid found in id_token"));
}
let audience = this._settings.client_id;
if (!audience) {
Log.error("Invalid audience/client_id value");
return Promise.reject(new Error("Invalid audience/client_id value"));
}
return this._metadataService.getIssuer().then(issuer => {
Log.info("Received issuer");
return this._metadataService.getSigningKeys().then(keys => {
if (!keys){
Log.error("No signing keys from metadata");
return Promise.reject(new Error("No signing keys from metadata"));
}
Log.info("Received signing keys");
let key = keys.filter(key => {
return key.kid === kid;
})[0];
if (!key) {
Log.error("No key matching kid found in signing keys");
return Promise.reject(new Error("No key matching kid found in signing keys"));
}
if (!this._jwtUtil.validateJwt(response.id_token, key, issuer, audience)) {
Log.error("Signature failed to validate");
return Promise.reject(new Error("Signature failed to validate"));
}
response.profile = jwt.payload;
return response;
});
});
}
_validateAccessToken(response) {
Log.info("ResponseValidator._validateAccessToken");
if (!response.profile) {
Log.error("No profile loaded from id_token");
return Promise.reject(new Error("No profile loaded from id_token"));
}
if (!response.profile.at_hash) {
Log.error("No at_hash in id_token");
return Promise.reject(new Error("No at_hash in id_token"));
}
if (!response.id_token) {
Log.error("No id_token");
return Promise.reject(new Error("No id_token"));
}
let jwt = this._jwtUtil.parseJwt(response.id_token);
if (!jwt || !jwt.header) {
Log.error("Failed to parse id_token", jwt);
return Promise.reject(new Error("Failed to parse id_token"));
}
var hashAlg = jwt.header.alg;
if (!hashAlg || hashAlg.length !== 5) {
Log.error("Unsupported alg:", hashAlg);
return Promise.reject(new Error("Unsupported alg: " + hashAlg));
}
var hashBits = hashAlg.substr(2, 3);
if (!hashBits) {
Log.error("Unsupported alg:", hashAlg, hashBits);
return Promise.reject(new Error("Unsupported alg: " + hashAlg));
}
hashBits = parseInt(hashBits);
if (hashBits !== 256 && hashBits !== 384 && hashBits !== 512) {
Log.error("Unsupported alg:", hashAlg, hashBits);
return Promise.reject(new Error("Unsupported alg: " + hashAlg));
}
let sha = "sha" + hashBits;
var hash = this._jwtUtil.hashString(response.access_token, sha);
if (!hash) {
Log.error("access_token hash failed:", sha);
return Promise.reject(new Error("Failed to validate at_hash"));
}
var left = hash.substr(0, hash.length / 2);
var left_b64u = this._jwtUtil.hexToBase64Url(left);
if (left_b64u !== response.profile.at_hash) {
Log.error("Failed to validate at_hash", left_b64u, response.profile.at_hash);
return Promise.reject(new Error("Failed to validate at_hash"));
}
return Promise.resolve(response);
}
}