-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.ts
703 lines (605 loc) · 22.4 KB
/
index.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/**
* An implementation of rfc6749#section-4.1 and rfc7636.
*/
export interface Configuration {
authorizationUrl: URL;
clientId: string;
explicitlyExposedTokens?: string[];
onAccessTokenExpiry: (refreshAccessToken: () => Promise<AccessContext>) => Promise<AccessContext>;
onInvalidGrant: (refreshAuthCodeOrRefreshToken: () => Promise<void>) => void;
redirectUrl: URL;
scopes: string[];
tokenUrl: URL;
extraAuthorizationParams?: ObjStringDict;
extraRefreshParams?: ObjStringDict;
}
export interface PKCECodes {
codeChallenge: string;
codeVerifier: string;
}
export interface State {
isHTTPDecoratorActive?: boolean;
accessToken?: AccessToken;
authorizationCode?: string;
codeChallenge?: string;
codeVerifier?: string;
explicitlyExposedTokens?: ObjStringDict;
hasAuthCodeBeenExchangedForAccessToken?: boolean;
refreshToken?: RefreshToken;
stateQueryParam?: string;
scopes?: string[];
}
export interface RefreshToken {
value: string;
};
export interface AccessToken {
value: string;
expiry: string;
};
export type Scopes = string[];
export interface AccessContext {
token?: AccessToken;
explicitlyExposedTokens?: ObjStringDict;
scopes?: Scopes;
refreshToken?: RefreshToken;
};
export type ObjStringDict = { [_: string]: string };
export type HttpClient = ((...args: any[]) => Promise<any>);
export type URL = string;
/**
* A list of OAuth2AuthCodePKCE errors.
*/
// To "namespace" all errors.
export class ErrorOAuth2 { toString(): string { return 'ErrorOAuth2'; } }
// For really unknown errors.
export class ErrorUnknown extends ErrorOAuth2 { toString(): string { return 'ErrorUnknown'; }}
// Some generic, internal errors that can happen.
export class ErrorNoAuthCode extends ErrorOAuth2 { toString(): string { return 'ErrorNoAuthCode'; }}
export class ErrorInvalidReturnedStateParam extends ErrorOAuth2 { toString(): string { return 'ErrorInvalidReturnedStateParam'; }}
export class ErrorInvalidJson extends ErrorOAuth2 { toString(): string { return 'ErrorInvalidJson'; }}
// Errors that occur across many endpoints
export class ErrorInvalidScope extends ErrorOAuth2 { toString(): string { return 'ErrorInvalidScope'; }}
export class ErrorInvalidRequest extends ErrorOAuth2 { toString(): string { return 'ErrorInvalidRequest'; }}
export class ErrorInvalidToken extends ErrorOAuth2 { toString(): string { return 'ErrorInvalidToken'; }}
/**
* Possible authorization grant errors given by the redirection from the
* authorization server.
*/
export class ErrorAuthenticationGrant extends ErrorOAuth2 { toString(): string { return 'ErrorAuthenticationGrant'; }}
export class ErrorUnauthorizedClient extends ErrorAuthenticationGrant { toString(): string { return 'ErrorUnauthorizedClient'; }}
export class ErrorAccessDenied extends ErrorAuthenticationGrant { toString(): string { return 'ErrorAccessDenied'; }}
export class ErrorUnsupportedResponseType extends ErrorAuthenticationGrant { toString(): string { return 'ErrorUnsupportedResponseType'; }}
export class ErrorServerError extends ErrorAuthenticationGrant { toString(): string { return 'ErrorServerError'; }}
export class ErrorTemporarilyUnavailable extends ErrorAuthenticationGrant { toString(): string { return 'ErrorTemporarilyUnavailable'; }}
/**
* A list of possible access token response errors.
*/
export class ErrorAccessTokenResponse extends ErrorOAuth2 { toString(): string { return 'ErrorAccessTokenResponse'; }}
export class ErrorInvalidClient extends ErrorAccessTokenResponse { toString(): string { return 'ErrorInvalidClient'; }}
export class ErrorInvalidGrant extends ErrorAccessTokenResponse { toString(): string { return 'ErrorInvalidGrant'; }}
export class ErrorUnsupportedGrantType extends ErrorAccessTokenResponse { toString(): string { return 'ErrorUnsupportedGrantType'; }}
/**
* WWW-Authenticate error object structure for less error prone handling.
*/
export class ErrorWWWAuthenticate {
public realm: string = "";
public error: string = "";
}
export const RawErrorToErrorClassMap: { [_: string]: any } = {
invalid_request: ErrorInvalidRequest,
invalid_grant: ErrorInvalidGrant,
unauthorized_client: ErrorUnauthorizedClient,
access_denied: ErrorAccessDenied,
unsupported_response_type: ErrorUnsupportedResponseType,
invalid_scope: ErrorInvalidScope,
server_error: ErrorServerError,
temporarily_unavailable: ErrorTemporarilyUnavailable,
invalid_client: ErrorInvalidClient,
unsupported_grant_type: ErrorUnsupportedGrantType,
invalid_json: ErrorInvalidJson,
invalid_token: ErrorInvalidToken,
};
/**
* Translate the raw error strings returned from the server into error classes.
*/
export function toErrorClass(rawError: string): ErrorOAuth2 {
return new (RawErrorToErrorClassMap[rawError] || ErrorUnknown)();
}
/**
* A convience function to turn, for example, `Bearer realm="bity.com",
* error="invalid_client"` into `{ realm: "bity.com", error: "invalid_client"
* }`.
*/
export function fromWWWAuthenticateHeaderStringToObject(
a: string
): ErrorWWWAuthenticate {
const obj = a
.slice("Bearer ".length)
.replace(/"/g, '')
.split(', ')
.map(tokens => { const [k,v] = tokens.split('='); return {[k]:v}; })
.reduce((a, c) => ({ ...a, ...c}), {});
return { realm: obj.realm, error: obj.error };
}
/**
* HTTP headers that we need to access.
*/
const HEADER_AUTHORIZATION = "Authorization";
const HEADER_WWW_AUTHENTICATE= "WWW-Authenticate";
/**
* To store the OAuth client's data between websites due to redirection.
*/
export const LOCALSTORAGE_ID = `oauth2authcodepkce`;
export const LOCALSTORAGE_STATE = `${LOCALSTORAGE_ID}-state`;
/**
* The maximum length for a code verifier for the best security we can offer.
* Please note the NOTE section of RFC 7636 § 4.1 - the length must be >= 43,
* but <= 128, **after** base64 url encoding. This means 32 code verifier bytes
* encoded will be 43 bytes, or 96 bytes encoded will be 128 bytes. So 96 bytes
* is the highest valid value that can be used.
*/
export const RECOMMENDED_CODE_VERIFIER_LENGTH = 96;
/**
* A sensible length for the state's length, for anti-csrf.
*/
export const RECOMMENDED_STATE_LENGTH = 32;
/**
* Character set to generate code verifier defined in rfc7636.
*/
const PKCE_CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
/**
* OAuth 2.0 client that ONLY supports authorization code flow, with PKCE.
*
* Many applications structure their OAuth usage in different ways. This class
* aims to provide both flexible and easy ways to use this configuration of
* OAuth.
*
* See `example.ts` for how you'd typically use this.
*
* For others, review this class's methods.
*/
export class OAuth2AuthCodePKCE {
private config!: Configuration;
private state: State = {};
private authCodeForAccessTokenRequest?: Promise<AccessContext>;
constructor(config: Configuration) {
this.config = config;
this.recoverState();
return this;
}
/**
* Attach the OAuth logic to all fetch requests and translate errors (either
* returned as json or through the WWW-Authenticate header) into nice error
* classes.
*/
public decorateFetchHTTPClient(fetch: HttpClient): HttpClient {
return (url: string, config: any, ...rest) => {
if (!this.state.isHTTPDecoratorActive) {
return fetch(url, config, ...rest);
}
return this
.getAccessToken()
.then(({ token }: AccessContext) => {
const configNew: any = Object.assign({}, config);
if (!configNew.headers) {
configNew.headers = {};
}
configNew.headers[HEADER_AUTHORIZATION] = `Bearer ${token!.value}`;
return fetch(url, configNew, ...rest);
})
.then((res) => {
if (res.ok) {
return res;
}
if (!res.headers.has(HEADER_WWW_AUTHENTICATE.toLowerCase())) {
return res;
}
const error = toErrorClass(
fromWWWAuthenticateHeaderStringToObject(
res.headers.get(HEADER_WWW_AUTHENTICATE.toLowerCase())
).error
);
if (error instanceof ErrorInvalidToken) {
this.config
.onAccessTokenExpiry(() => this.exchangeRefreshTokenForAccessToken());
}
return Promise.reject(error);
});
};
}
/**
* If there is an error, it will be passed back as a rejected Promise.
* If there is no code, the user should be redirected via
* [fetchAuthorizationCode].
*/
public isReturningFromAuthServer(): Promise<boolean> {
const error = OAuth2AuthCodePKCE.extractParamFromUrl(location.href, 'error');
if (error) {
return Promise.reject(toErrorClass(error));
}
const code = OAuth2AuthCodePKCE.extractParamFromUrl(location.href, 'code');
if (!code) {
return Promise.resolve(false);
}
const state = JSON.parse(localStorage.getItem(LOCALSTORAGE_STATE) || '{}');
const stateQueryParam = OAuth2AuthCodePKCE.extractParamFromUrl(location.href, 'state');
if (stateQueryParam !== state.stateQueryParam) {
console.warn("state query string parameter doesn't match the one sent! Possible malicious activity somewhere.");
return Promise.reject(new ErrorInvalidReturnedStateParam());
}
state.authorizationCode = code;
state.hasAuthCodeBeenExchangedForAccessToken = false;
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(state));
this.setState(state);
return Promise.resolve(true);
}
/**
* Fetch an authorization grant via redirection. In a sense this function
* doesn't return because of the redirect behavior (uses `location.replace`).
*
* @param oneTimeParams A way to specify "one time" used query string
* parameters during the authorization code fetching process, usually for
* values which need to change at run-time.
*/
public async fetchAuthorizationCode(oneTimeParams?: ObjStringDict): Promise<void> {
this.assertStateAndConfigArePresent();
const { clientId, extraAuthorizationParams, redirectUrl, scopes } = this.config;
const { codeChallenge, codeVerifier } = await OAuth2AuthCodePKCE
.generatePKCECodes();
const stateQueryParam = OAuth2AuthCodePKCE
.generateRandomState(RECOMMENDED_STATE_LENGTH);
this.state = {
...this.state,
codeChallenge,
codeVerifier,
stateQueryParam,
isHTTPDecoratorActive: true
};
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(this.state));
let url = this.config.authorizationUrl
+ `?response_type=code&`
+ `client_id=${encodeURIComponent(clientId)}&`
+ `redirect_uri=${encodeURIComponent(redirectUrl)}&`
+ `scope=${encodeURIComponent(scopes.join(' '))}&`
+ `state=${stateQueryParam}&`
+ `code_challenge=${encodeURIComponent(codeChallenge)}&`
+ `code_challenge_method=S256`;
if (extraAuthorizationParams || oneTimeParams) {
const extraParameters: ObjStringDict = {
...extraAuthorizationParams,
...oneTimeParams
};
url = `${url}&${OAuth2AuthCodePKCE.objectToQueryString(extraParameters)}`
}
location.replace(url);
}
/**
* Tries to get the current access token. If there is none
* it will fetch another one. If it is expired, it will fire
* [onAccessTokenExpiry] but it's up to the user to call the refresh token
* function. This is because sometimes not using the refresh token facilities
* is easier.
*/
public getAccessToken(): Promise<AccessContext> {
this.assertStateAndConfigArePresent();
const { onAccessTokenExpiry } = this.config;
const {
accessToken,
authorizationCode,
explicitlyExposedTokens,
hasAuthCodeBeenExchangedForAccessToken,
refreshToken,
scopes
} = this.state;
if (!authorizationCode) {
return Promise.reject(new ErrorNoAuthCode());
}
if (this.authCodeForAccessTokenRequest) {
return this.authCodeForAccessTokenRequest;
}
if (!this.isAuthorized() || !hasAuthCodeBeenExchangedForAccessToken) {
this.authCodeForAccessTokenRequest = this.exchangeAuthCodeForAccessToken();
return this.authCodeForAccessTokenRequest;
}
// Depending on the server (and config), refreshToken may not be available.
if (refreshToken && this.isAccessTokenExpired()) {
return onAccessTokenExpiry(() => this.exchangeRefreshTokenForAccessToken());
}
return Promise.resolve({
token: accessToken,
explicitlyExposedTokens,
scopes,
refreshToken
});
}
/**
* Refresh an access token from the remote service.
*/
public exchangeRefreshTokenForAccessToken(): Promise<AccessContext> {
this.assertStateAndConfigArePresent();
const { extraRefreshParams, clientId, tokenUrl } = this.config;
const { refreshToken } = this.state;
if (!refreshToken) {
console.warn('No refresh token is present.');
}
const url = tokenUrl;
let body = `grant_type=refresh_token&`
+ `refresh_token=${refreshToken?.value}&`
+ `client_id=${clientId}`;
if (extraRefreshParams) {
body = `${url}&${OAuth2AuthCodePKCE.objectToQueryString(extraRefreshParams)}`
}
return fetch(url, {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => res.status >= 400 ? res.json().then(data => Promise.reject(data)) : res.json())
.then((json) => {
const { access_token, expires_in, refresh_token, scope } = json;
const { explicitlyExposedTokens } = this.config;
let scopes = [];
let tokensToExpose = {};
const accessToken: AccessToken = {
value: access_token,
expiry: (new Date(Date.now() + (parseInt(expires_in) * 1000))).toString()
};
this.state.accessToken = accessToken;
if (refresh_token) {
const refreshToken: RefreshToken = {
value: refresh_token
};
this.state.refreshToken = refreshToken;
}
if (explicitlyExposedTokens) {
tokensToExpose = Object.fromEntries(
explicitlyExposedTokens
.map((tokenName: string): [string, string|undefined] => [tokenName, json[tokenName]])
.filter(([_, tokenValue]: [string, string|undefined]) => tokenValue !== undefined)
);
this.state.explicitlyExposedTokens = tokensToExpose;
}
if (scope) {
// Multiple scopes are passed and delimited by spaces,
// despite using the singular name "scope".
scopes = scope.split(' ');
this.state.scopes = scopes;
}
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(this.state));
let accessContext: AccessContext = {token: accessToken, scopes};
if (explicitlyExposedTokens) {
accessContext.explicitlyExposedTokens = tokensToExpose;
}
return accessContext;
})
.catch(data => {
const { onInvalidGrant } = this.config;
const error = data.error || 'There was a network error.';
switch (error) {
case 'invalid_grant':
onInvalidGrant(() => this.fetchAuthorizationCode());
break;
default:
break;
}
return Promise.reject(toErrorClass(error));
});
}
/**
* Get the scopes that were granted by the authorization server.
*/
public getGrantedScopes(): Scopes | undefined {
return this.state.scopes;
}
/**
* Signals if OAuth HTTP decorating should be active or not.
*/
public isHTTPDecoratorActive(isActive: boolean) {
this.state.isHTTPDecoratorActive = isActive;
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(this.state));
}
/**
* Tells if the client is authorized or not. This means the client has at
* least once successfully fetched an access token. The access token could be
* expired.
*/
public isAuthorized(): boolean {
return !!this.state.accessToken;
}
/**
* Checks to see if the access token has expired.
*/
public isAccessTokenExpired(): boolean {
const { accessToken } = this.state;
return Boolean(accessToken && (new Date()) >= (new Date(accessToken.expiry)));
}
/**
* Resets the state of the client. Equivalent to "logging out" the user.
*/
public reset() {
this.setState({});
this.authCodeForAccessTokenRequest = undefined;
}
/**
* If the state or config are missing, it means the client is in a bad state.
* This should never happen, but the check is there just in case.
*/
private assertStateAndConfigArePresent() {
if (!this.state || !this.config) {
console.error('state:', this.state, 'config:', this.config);
throw new Error('state or config is not set.');
}
}
/**
* Fetch an access token from the remote service. You may pass a custom
* authorization grant code for any reason, but this is non-standard usage.
*/
private exchangeAuthCodeForAccessToken(
codeOverride?: string
): Promise<AccessContext> {
this.assertStateAndConfigArePresent();
const {
authorizationCode = codeOverride,
codeVerifier = ''
} = this.state;
const { clientId, onInvalidGrant, redirectUrl } = this.config;
if (!codeVerifier) {
console.warn('No code verifier is being sent.');
} else if (!authorizationCode) {
console.warn('No authorization grant code is being passed.');
}
const url = this.config.tokenUrl;
const body = `grant_type=authorization_code&`
+ `code=${encodeURIComponent(authorizationCode || '')}&`
+ `redirect_uri=${encodeURIComponent(redirectUrl)}&`
+ `client_id=${encodeURIComponent(clientId)}&`
+ `code_verifier=${codeVerifier}`;
return fetch(url, {
method: 'POST',
body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(res => {
const jsonPromise = res.json()
.catch(_ => ({ error: 'invalid_json' }));
if (!res.ok) {
return jsonPromise.then(({ error }: any) => {
switch (error) {
case 'invalid_grant':
onInvalidGrant(() => this.fetchAuthorizationCode());
break;
default:
break;
}
return Promise.reject(toErrorClass(error));
});
}
return jsonPromise.then((json) => {
const { access_token, expires_in, refresh_token, scope } = json;
const { explicitlyExposedTokens } = this.config;
let scopes = [];
let tokensToExpose = {};
this.state.hasAuthCodeBeenExchangedForAccessToken = true;
this.authCodeForAccessTokenRequest = undefined;
const accessToken: AccessToken = {
value: access_token,
expiry: (new Date(Date.now() + (parseInt(expires_in) * 1000))).toString()
};
this.state.accessToken = accessToken;
if (refresh_token) {
const refreshToken: RefreshToken = {
value: refresh_token
};
this.state.refreshToken = refreshToken;
}
if (explicitlyExposedTokens) {
tokensToExpose = Object.fromEntries(
explicitlyExposedTokens
.map((tokenName: string): [string, string|undefined] => [tokenName, json[tokenName]])
.filter(([_, tokenValue]: [string, string|undefined]) => tokenValue !== undefined)
);
this.state.explicitlyExposedTokens = tokensToExpose;
}
if (scope) {
// Multiple scopes are passed and delimited by spaces,
// despite using the singular name "scope".
scopes = scope.split(' ');
this.state.scopes = scopes;
}
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(this.state));
let accessContext: AccessContext = {token: accessToken, scopes};
if (explicitlyExposedTokens) {
accessContext.explicitlyExposedTokens = tokensToExpose;
}
return accessContext;
});
});
}
private recoverState(): this {
this.state = JSON.parse(localStorage.getItem(LOCALSTORAGE_STATE) || '{}');
return this;
}
private setState(state: State): this {
this.state = state;
localStorage.setItem(LOCALSTORAGE_STATE, JSON.stringify(state));
return this;
}
/**
* Implements *base64url-encode* (RFC 4648 § 5) without padding, which is NOT
* the same as regular base64 encoding.
*/
static base64urlEncode(value: string): string {
let base64 = btoa(value);
base64 = base64.replace(/\+/g, '-');
base64 = base64.replace(/\//g, '_');
base64 = base64.replace(/=/g, '');
return base64;
}
/**
* Extracts a query string parameter.
*/
static extractParamFromUrl(url: URL, param: string): string {
let queryString = url.split('?');
if (queryString.length < 2) {
return '';
}
// Account for hash URLs that SPAs usually use.
queryString = queryString[1].split('#');
const parts = queryString[0]
.split('&')
.reduce((a: string[], s: string) => a.concat(s.split('=')), []);
if (parts.length < 2) {
return '';
}
const paramIdx = parts.indexOf(param);
return decodeURIComponent(paramIdx >= 0 ? parts[paramIdx + 1] : '');
}
/**
* Converts the keys and values of an object to a url query string
*/
static objectToQueryString(dict: ObjStringDict): string {
return Object.entries(dict).map(
([key, val]: [string, string]) => `${key}=${encodeURIComponent(val)}`
).join('&');
}
/**
* Generates a code_verifier and code_challenge, as specified in rfc7636.
*/
static generatePKCECodes(): PromiseLike<PKCECodes> {
const output = new Uint32Array(RECOMMENDED_CODE_VERIFIER_LENGTH);
crypto.getRandomValues(output);
const codeVerifier = OAuth2AuthCodePKCE.base64urlEncode(Array
.from(output)
.map((num: number) => PKCE_CHARSET[num % PKCE_CHARSET.length])
.join(''));
return crypto
.subtle
.digest('SHA-256', (new TextEncoder()).encode(codeVerifier))
.then((buffer: ArrayBuffer) => {
let hash = new Uint8Array(buffer);
let binary = '';
let hashLength = hash.byteLength;
for (let i: number = 0; i < hashLength; i++) {
binary += String.fromCharCode(hash[i]);
}
return binary;
})
.then(OAuth2AuthCodePKCE.base64urlEncode)
.then((codeChallenge: string) => ({ codeChallenge, codeVerifier }));
}
/**
* Generates random state to be passed for anti-csrf.
*/
static generateRandomState(lengthOfState: number): string {
const output = new Uint32Array(lengthOfState);
crypto.getRandomValues(output);
return Array
.from(output)
.map((num: number) => PKCE_CHARSET[num % PKCE_CHARSET.length])
.join('');
}
}