-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.ts
435 lines (400 loc) · 13.7 KB
/
session.ts
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import { decodeJwt } from 'jose';
import { default as dpopFn } from 'dpop';
import { base64 } from '@opentdf/sdk/encodings';
import { AuthProvider, HttpRequest, withHeaders } from '@opentdf/sdk';
export type OpenidConfiguration = {
issuer: string;
authorization_endpoint: string;
token_endpoint: string;
userinfo_endpoint: string;
jwks_uri: string;
scopes_supported: string[];
};
export type AuthorizationError =
// the request is missing a parameter, contains an invalid parameter, includes a parameter more than once, or is otherwise invalid.
| 'invalid_request'
// the user or authorization server denied the request
| 'access_denied'
// the client is not allowed to request an authorization code using this method, for example if a confidential client attempts to use the implicit grant type.
| 'unauthorized_client'
// the server does not support obtaining an authorization code using this method, for example if the authorization server never implemented the implicit grant type.
| 'unsupported_response_type'
// the requested scope is invalid or unknown.
| 'invalid_scope'
// instead of displaying a 500 Internal Server Error page to the user, the server can redirect with this error code.
| 'server_error'
// if the server is undergoing maintenance, or is otherwise unavailable, this error code can be returned instead of responding with a 503 Service Unavailable status code.
| 'temporarily_unavailable';
type AuthorizationResponseSuccess = {
_: 'OK';
code: string;
state: string;
};
type AuthorizationResponseError = {
_: 'FAIL';
error: AuthorizationError;
error_description: string;
};
type AuthorizationResponse = AuthorizationResponseError | AuthorizationResponseSuccess;
/**
* Response returned from a successful token endpoint request
*/
export type TokenResponse = {
access_token: string;
expires_in: number;
id_token: string;
refresh_token: string;
};
/**
* Authenticated user details
*/
export type User = {
userId: string;
accessToken: string;
expiresAt: number;
idToken: string;
refreshToken: string;
};
/**
* start -> redirecting -> { error | loggedin }
*/
export type SessionState = 'start' | 'loggedin' | 'redirecting' | 'error';
export type SessionInformation = {
/** Current state diagram position */
sessionState: SessionState;
/** User info, if possible */
user?: User;
};
type AuthRequestState = SessionInformation & {
redirectUri: string;
/** Random state parameter. Unique per redirect request; can identify them. Proves the redirect is for the correct request. */
state: string;
/** Random code verifier parameter for PKCE. Unique per redirect request. Semi-secret. Proves the code exchange is for the same client session. */
codeVerifier: string;
/** When this started */
start: number;
/** Auth Codes */
usedCodes: string[];
};
export type Sessions = {
config: OpenidConfiguration;
/** state -> auth request */
requests: Record<string, AuthRequestState>;
/** state for most recent request */
lastRequest?: string;
/** DPoP key */
k?: string[];
};
function getTimestampInSeconds() {
return Math.floor(Date.now() / 1000);
}
function rsaPkcs1Sha256(): RsaHashedKeyGenParams {
return {
name: 'RSASSA-PKCS1-v1_5',
hash: {
name: 'SHA-256',
},
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), // 24 bit representation of 65537
};
}
const extractAuthorizationResponse = (url: string): AuthorizationResponse | null => {
const queryParams = new URLSearchParams(url);
console.log(`response: ${JSON.stringify(queryParams.toString())}`);
// state=ApkI9hDcVp8nIOZq4rqxM65mv_mEQDUEHTulLFt9jRA
// &session_state=96a991a3-e94f-456d-8c85-75904e1fbdbb
// &code=c0a1fc73-f2c5-4182-a9de-2569c55cc878.96a991a3-e94f-456d-8c85-75904e1fbdbb.b4877ed3-6dc1-41f1-b6ca-d9efdb7d8d7f
const error = queryParams.get('error');
if (error) {
const s = queryParams.get('state');
if (s) {
console.log(`state: {s}`);
}
return {
_: 'FAIL',
error: error as AuthorizationError,
error_description: queryParams.get('error_description') || '',
};
}
const code = queryParams.get('code');
const state = queryParams.get('state');
if (code || state) {
if (!code || !state) {
throw new Error('Invalid response state');
}
return {
_: 'OK',
code,
state,
};
}
return null;
};
function createCodeVerifier(): string {
const r = new Uint8Array(32);
crypto.getRandomValues(r);
const verifier = base64.encodeArrayBuffer(r, true);
return verifier;
}
async function createCodeChallenge(code: string): Promise<string> {
const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(code));
return base64.encodeArrayBuffer(hash, true);
}
async function fetchConfig(server: string): Promise<unknown> {
const response = await fetch(`${server}/.well-known/openid-configuration`, {
headers: {
accept: 'application/json',
},
});
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
}
export class OidcClient implements AuthProvider {
clientId: string;
host: string;
scope: string;
sessionIdentifier: string;
_sessions?: Sessions;
signingKey?: CryptoKeyPair;
constructor(host: string, clientId: string, sessionIdentifier: string) {
this.clientId = clientId;
this.host = host;
this.sessionIdentifier = sessionIdentifier;
this.scope = 'openid profile email offline_access';
}
ssk(key: string): string {
return `${this.host}-${this.sessionIdentifier}-${key}`;
}
async loadSessions(): Promise<Sessions> {
if (this._sessions) {
return this._sessions;
}
const key = this.ssk('sessions');
const rawData = sessionStorage.getItem(key);
if (rawData) {
return (this._sessions = JSON.parse(rawData));
}
const config = (await fetchConfig(this.host)) as OpenidConfiguration;
console.log(config);
this._sessions = {
config,
requests: {},
};
sessionStorage.setItem(key, JSON.stringify(this._sessions));
return this._sessions;
}
storeSessions() {
sessionStorage.setItem(this.ssk('sessions'), JSON.stringify(this._sessions));
}
/**
* Initiate a login flow with PKCE and code. By default, this will result in
* a page reaload of the current page (or a load of the redirect page) with the
* oidc response in the URL parameters (either state and code for success or
* optionally error and error_description for failures).
*
* So whenever loading the page, check for the redirect parameters
* and do something good with them as appropriate.
*
* @param redirectUri Where to return the user to after a successful login. Defaults to current location
*/
async authViaRedirect(redirectUri?: string) {
const codeVerifier = createCodeVerifier();
const sessions = await this.loadSessions();
const newSession: AuthRequestState = {
sessionState: 'redirecting',
// just use 32 random bytes for state as well. This should be safe
state: createCodeVerifier(),
redirectUri: redirectUri || window.location.href.split('?')[0],
codeVerifier,
start: getTimestampInSeconds(),
usedCodes: [],
};
sessions.lastRequest = newSession.state;
sessions.requests[newSession.state] = newSession;
this.storeSessions();
const args = new URLSearchParams({
client_id: this.clientId,
code_challenge: await createCodeChallenge(newSession.codeVerifier),
code_challenge_method: 'S256',
redirect_uri: newSession.redirectUri,
response_type: 'code',
scope: this.scope,
state: newSession.state,
});
const whereto = `${sessions.config.authorization_endpoint}/?${args}`;
console.log('Navigating to ', whereto);
window.location.href = whereto;
}
_cs?: Promise<SessionInformation>;
async currentSession(): Promise<SessionInformation> {
if (!this._cs) {
this._cs = (async (): Promise<SessionInformation> => {
const s = await this.handleRedirect();
if (s) {
console.log('redirected');
return s;
}
const sessions = await this.loadSessions();
if (!sessions?.lastRequest) {
return { sessionState: 'start' };
}
const thisSession = sessions.requests[sessions.lastRequest];
return thisSession;
})();
}
return this._cs;
}
async currentUser(): Promise<User | undefined> {
return (await this.currentSession()).user;
}
async handleRedirect(): Promise<AuthRequestState | undefined> {
const response = extractAuthorizationResponse(window.location.search);
if (!response) {
return;
}
const sessions = await this.loadSessions();
if (response._ === 'FAIL') {
throw new Error(`OIDC auth ${response.error || 'error'}: [${response.error_description}]`);
}
if (
response.state === '__proto__' ||
response.state === 'constructor' ||
response.state === 'prototype'
) {
throw new Error('Invalid state value');
}
const currentSession = sessions.requests[response.state];
if (!currentSession) {
throw new Error(`OIDC auth error: session storage missing state for ${response}`);
}
const usedRedirectCodes = currentSession.usedCodes;
console.log('redirect response:', response, usedRedirectCodes);
if (usedRedirectCodes.includes(response.code)) {
console.log('Ignoring repeated redirect code');
return;
}
currentSession.usedCodes.push(response.code);
this.storeSessions();
try {
currentSession.user = await this._makeAccessTokenRequest({
grantType: 'authorization_code',
codeOrRefreshToken: response.code,
codeVerifier: currentSession.codeVerifier,
redirectUri: currentSession.redirectUri,
});
currentSession.sessionState = 'loggedin';
this.storeSessions();
return currentSession;
} finally {
// no catch needed as we want error to be thrown
// history state should still be cleaned up regardless
window.history.replaceState({}, '', window.location.pathname);
}
}
async getSigningKey(): Promise<CryptoKeyPair> {
if (this.signingKey) {
return this.signingKey;
}
if (this._sessions?.k) {
const k = this._sessions?.k.map((e) => base64.decodeArrayBuffer(e));
const algorithm = rsaPkcs1Sha256();
const [publicKey, privateKey] = await Promise.all([
crypto.subtle.importKey('spki', k[0], algorithm, true, ['verify']),
crypto.subtle.importKey('pkcs8', k[1], algorithm, true, ['sign']),
]);
this.signingKey = { privateKey, publicKey };
} else {
this.signingKey = await crypto.subtle.generateKey(rsaPkcs1Sha256(), true, ['sign']);
}
return this.signingKey;
}
private async _makeAccessTokenRequest(options: {
grantType: 'authorization_code' | 'refresh_token';
codeOrRefreshToken: string;
codeVerifier: string;
redirectUri: string;
}): Promise<User> {
const params = new URLSearchParams();
params.set('client_id', this.clientId);
params.set('redirect_uri', options.redirectUri);
params.set('grant_type', options.grantType);
if (options.grantType === 'authorization_code') {
params.set('code', options.codeOrRefreshToken);
} else {
params.set('refresh_token', options.codeOrRefreshToken);
}
params.set('code_verifier', options.codeVerifier);
params.set('scope', this.scope);
const config = this._sessions?.config;
if (!config) {
throw new Error('Unable to autoconfigure OIDC');
}
const headers: Record<string, string> = {
'Content-Type': 'application/x-www-form-urlencoded',
};
const signingKey = await this.getSigningKey();
if (this._sessions && this.signingKey) {
const k = await Promise.all([
crypto.subtle.exportKey('spki', this.signingKey.publicKey),
crypto.subtle.exportKey('pkcs8', this.signingKey.privateKey),
]);
this._sessions.k = k.map((e) => base64.encodeArrayBuffer(e));
}
console.info(
`signing token request with DPoP key ${JSON.stringify(
await crypto.subtle.exportKey('jwk', signingKey.publicKey)
)}`
);
headers.DPoP = await dpopFn(signingKey, config.token_endpoint, 'POST');
const response = await fetch(config.token_endpoint, {
method: 'POST',
headers,
body: params,
credentials: 'include',
});
if (!response.ok) {
throw new Error(response.statusText);
}
const { access_token, expires_in, id_token, refresh_token } = await response.json();
const { virtru_user_id } = decodeJwt(access_token);
return {
userId: virtru_user_id as string,
accessToken: access_token,
expiresAt: new Date().getTime() + expires_in * 1000,
idToken: id_token,
refreshToken: refresh_token,
};
}
async updateClientPublicKey(signingKey: CryptoKeyPair): Promise<void> {
this.signingKey = signingKey;
}
async withCreds(httpReq: HttpRequest): Promise<HttpRequest> {
const user = await this.currentUser();
if (!user) {
console.error('Not logged in');
return httpReq;
}
const { accessToken } = user;
const { signingKey } = this;
if (!signingKey || !signingKey.publicKey) {
console.error('missing DPoP key');
return httpReq;
}
console.info(
`signing request for ${httpReq.url} with DPoP key ${JSON.stringify(
await crypto.subtle.exportKey('jwk', signingKey.publicKey)
)}`
);
const dpopToken = await dpopFn(
signingKey,
httpReq.url,
httpReq.method,
/* nonce */ undefined,
accessToken
);
// TODO: Consider: only set DPoP if cnf.jkt is present in access token?
return withHeaders(httpReq, { Authorization: `Bearer ${accessToken}`, DPoP: dpopToken });
}
}