forked from auth0/auth0-spa-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth0Client.ts
925 lines (813 loc) · 24.6 KB
/
Auth0Client.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
import Lock from 'browser-tabs-lock';
import {
createQueryParams,
runPopup,
parseQueryResult,
encode,
createRandomString,
runIframe,
sha256,
bufferToBase64UrlEncoded,
oauthToken,
validateCrypto
} from './utils';
import { getUniqueScopes } from './scope';
import { InMemoryCache, ICache, LocalStorageCache } from './cache';
import TransactionManager from './transaction-manager';
import { verify as verifyIdToken } from './jwt';
import { AuthenticationError } from './errors';
import {
ClientStorage,
CookieStorage,
CookieStorageWithLegacySameSite,
SessionStorage
} from './storage';
import {
CACHE_LOCATION_MEMORY,
DEFAULT_POPUP_CONFIG_OPTIONS,
DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS,
MISSING_REFRESH_TOKEN_ERROR_MESSAGE,
DEFAULT_SCOPE,
RECOVERABLE_ERRORS,
DEFAULT_SESSION_CHECK_EXPIRY_DAYS
} from './constants';
import version from './version';
import {
Auth0ClientOptions,
BaseLoginOptions,
AuthorizeOptions,
RedirectLoginOptions,
PopupLoginOptions,
PopupConfigOptions,
GetUserOptions,
GetIdTokenClaimsOptions,
RedirectLoginResult,
GetTokenSilentlyOptions,
GetTokenWithPopupOptions,
LogoutOptions,
RefreshTokenOptions,
OAuthTokenOptions,
CacheLocation,
LogoutUrlOptions,
User
} from './global';
// @ts-ignore
import TokenWorker from './token.worker.ts';
import { isIE11, isSafari10, isSafari11, isSafari12_0 } from './user-agent';
/**
* @ignore
*/
const lock = new Lock();
/**
* @ignore
*/
const GET_TOKEN_SILENTLY_LOCK_KEY = 'auth0.lock.getTokenSilently';
/**
* @ignore
*/
const cacheLocationBuilders = {
memory: () => new InMemoryCache().enclosedCache,
localstorage: () => new LocalStorageCache()
};
/**
* @ignore
*/
const cacheFactory = (location: string) => {
return cacheLocationBuilders[location];
};
/**
* @ignore
*/
const supportWebWorker = () =>
!isIE11() && !isSafari10() && !isSafari11() && !isSafari12_0();
/**
* @ignore
*/
const getTokenIssuer = (issuer, domainUrl) => {
if (issuer) {
return issuer.startsWith('https://') ? issuer : `https://${issuer}/`;
}
return `${domainUrl}/`;
};
/**
* @ignore
*/
const getCustomInitialOptions = (
options: Auth0ClientOptions
): BaseLoginOptions => {
const {
advancedOptions,
audience,
auth0Client,
authorizeTimeoutInSeconds,
cacheLocation,
client_id,
domain,
issuer,
leeway,
max_age,
redirect_uri,
scope,
useRefreshTokens,
...customParams
} = options;
return customParams;
};
/**
* Auth0 SDK for Single Page Applications using [Authorization Code Grant Flow with PKCE](https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce).
*/
export default class Auth0Client {
private cache: ICache;
private transactionManager: TransactionManager;
private customOptions: BaseLoginOptions;
private domainUrl: string;
private tokenIssuer: string;
private defaultScope: string;
private scope: string;
private cookieStorage: ClientStorage;
private sessionCheckExpiryDays: number;
cacheLocation: CacheLocation;
private worker: Worker;
constructor(private options: Auth0ClientOptions) {
typeof window !== 'undefined' && validateCrypto();
this.cacheLocation = options.cacheLocation || CACHE_LOCATION_MEMORY;
this.cookieStorage =
options.legacySameSiteCookie === false
? CookieStorage
: CookieStorageWithLegacySameSite;
this.sessionCheckExpiryDays =
options.sessionCheckExpiryDays || DEFAULT_SESSION_CHECK_EXPIRY_DAYS;
if (!cacheFactory(this.cacheLocation)) {
throw new Error(`Invalid cache location "${this.cacheLocation}"`);
}
const transactionStorage = options.useCookiesForTransactions
? this.cookieStorage
: SessionStorage;
this.cache = cacheFactory(this.cacheLocation)();
this.scope = this.options.scope;
this.transactionManager = new TransactionManager(transactionStorage);
this.domainUrl = `https://${this.options.domain}`;
this.tokenIssuer = getTokenIssuer(this.options.issuer, this.domainUrl);
this.defaultScope = getUniqueScopes(
'openid',
this.options?.advancedOptions?.defaultScope !== undefined
? this.options.advancedOptions.defaultScope
: DEFAULT_SCOPE
);
// If using refresh tokens, automatically specify the `offline_access` scope.
// Note we cannot add this to 'defaultScope' above as the scopes are used in the
// cache keys - changing the order could invalidate the keys
if (this.options.useRefreshTokens) {
this.scope = getUniqueScopes(this.scope, 'offline_access');
}
// Don't use web workers unless using refresh tokens in memory and not IE11
if (
typeof window !== 'undefined' &&
window.Worker &&
this.options.useRefreshTokens &&
this.cacheLocation === CACHE_LOCATION_MEMORY &&
supportWebWorker()
) {
this.worker = new TokenWorker();
}
this.customOptions = getCustomInitialOptions(options);
}
private _url(path) {
const auth0Client = encodeURIComponent(
btoa(
JSON.stringify(
this.options.auth0Client || {
name: 'auth0-spa-js',
version: version
}
)
)
);
return `${this.domainUrl}${path}&auth0Client=${auth0Client}`;
}
private _getParams(
authorizeOptions: BaseLoginOptions,
state: string,
nonce: string,
code_challenge: string,
redirect_uri: string
): AuthorizeOptions {
const {
domain,
leeway,
useRefreshTokens,
auth0Client,
cacheLocation,
advancedOptions,
...withoutDomain
} = this.options;
return {
...withoutDomain,
...authorizeOptions,
scope: getUniqueScopes(
this.defaultScope,
this.scope,
authorizeOptions.scope
),
response_type: 'code',
response_mode: 'query',
state,
nonce,
redirect_uri: redirect_uri || this.options.redirect_uri,
code_challenge,
code_challenge_method: 'S256'
};
}
private _authorizeUrl(authorizeOptions: AuthorizeOptions) {
return this._url(`/authorize?${createQueryParams(authorizeOptions)}`);
}
private _verifyIdToken(
id_token: string,
nonce?: string,
organizationId?: string
) {
return verifyIdToken({
iss: this.tokenIssuer,
aud: this.options.client_id,
id_token,
nonce,
organizationId,
leeway: this.options.leeway,
max_age: this._parseNumber(this.options.max_age)
});
}
private _parseNumber(value: any): number {
if (typeof value !== 'string') {
return value;
}
return parseInt(value, 10) || undefined;
}
/**
* ```js
* await auth0.buildAuthorizeUrl(options);
* ```
*
* Builds an `/authorize` URL for loginWithRedirect using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated.
*
* @param options
*/
public async buildAuthorizeUrl(
options: RedirectLoginOptions = {}
): Promise<string> {
const { redirect_uri, appState, ...authorizeOptions } = options;
const stateIn = encode(createRandomString());
const nonceIn = encode(createRandomString());
const code_verifier = createRandomString();
const code_challengeBuffer = await sha256(code_verifier);
const code_challenge = bufferToBase64UrlEncoded(code_challengeBuffer);
const fragment = options.fragment ? `#${options.fragment}` : '';
const params = this._getParams(
authorizeOptions,
stateIn,
nonceIn,
code_challenge,
redirect_uri
);
const url = this._authorizeUrl(params);
const organizationId = options.organization || this.options.organization;
this.transactionManager.create({
nonce: nonceIn,
code_verifier,
appState,
scope: params.scope,
audience: params.audience || 'default',
redirect_uri: params.redirect_uri,
...(organizationId && { organizationId })
});
return url + fragment;
}
/**
* ```js
* await auth0.loginWithPopup(options);
* ```
*
* Opens a popup with the `/authorize` URL using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated. If the response is successful,
* results will be valid according to their expiration times.
*
* IMPORTANT: This method has to be called from an event handler
* that was started by the user like a button click, for example,
* otherwise the popup will be blocked in most browsers.
*
* @param options
* @param config
*/
public async loginWithPopup(
options: PopupLoginOptions = {},
config: PopupConfigOptions = {}
) {
const { ...authorizeOptions } = options;
const stateIn = encode(createRandomString());
const nonceIn = encode(createRandomString());
const code_verifier = createRandomString();
const code_challengeBuffer = await sha256(code_verifier);
const code_challenge = bufferToBase64UrlEncoded(code_challengeBuffer);
const params = this._getParams(
authorizeOptions,
stateIn,
nonceIn,
code_challenge,
this.options.redirect_uri || window.location.origin
);
const url = this._authorizeUrl({
...params,
response_mode: 'web_message'
});
const codeResult = await runPopup(url, {
...config,
timeoutInSeconds:
config.timeoutInSeconds ||
this.options.authorizeTimeoutInSeconds ||
DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS
});
if (stateIn !== codeResult.state) {
throw new Error('Invalid state');
}
const authResult = await oauthToken(
{
audience: params.audience,
scope: params.scope,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
code_verifier,
code: codeResult.code,
grant_type: 'authorization_code',
redirect_uri: params.redirect_uri
} as OAuthTokenOptions,
this.worker
);
const organizationId = options.organization || this.options.organization;
const decodedToken = this._verifyIdToken(
authResult.id_token,
nonceIn,
organizationId
);
const cacheEntry = {
...authResult,
decodedToken,
scope: params.scope,
audience: params.audience || 'default',
client_id: this.options.client_id
};
this.cache.save(cacheEntry);
this.cookieStorage.save('auth0.is.authenticated', true, {
daysUntilExpire: this.sessionCheckExpiryDays
});
}
/**
* ```js
* const user = await auth0.getUser();
* ```
*
* Returns the user information if available (decoded
* from the `id_token`).
*
* @typeparam TUser The type to return, has to extend {@link User}. Defaults to {@link User} when omitted.
* @param options
*/
public async getUser<TUser extends User = User>(
options: GetUserOptions = {}
): Promise<TUser> {
const audience = options.audience || this.options.audience || 'default';
const scope = getUniqueScopes(this.defaultScope, this.scope, options.scope);
const cache = this.cache.get({
client_id: this.options.client_id,
audience,
scope
});
return cache && cache.decodedToken && (cache.decodedToken.user as TUser);
}
/**
* ```js
* const claims = await auth0.getIdTokenClaims();
* ```
*
* Returns all claims from the id_token if available.
*
* @param options
*/
public async getIdTokenClaims(options: GetIdTokenClaimsOptions = {}) {
const audience = options.audience || this.options.audience || 'default';
const scope = getUniqueScopes(this.defaultScope, this.scope, options.scope);
const cache = this.cache.get({
client_id: this.options.client_id,
audience,
scope
});
return cache && cache.decodedToken && cache.decodedToken.claims;
}
/**
* ```js
* await auth0.loginWithRedirect(options);
* ```
*
* Performs a redirect to `/authorize` using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated.
*
* @param options
*/
public async loginWithRedirect(options: RedirectLoginOptions = {}) {
const url = await this.buildAuthorizeUrl(options);
window.location.assign(url);
}
/**
* After the browser redirects back to the callback page,
* call `handleRedirectCallback` to handle success and error
* responses from Auth0. If the response is successful, results
* will be valid according to their expiration times.
*/
public async handleRedirectCallback(
url: string = window.location.href
): Promise<RedirectLoginResult> {
const queryStringFragments = url.split('?').slice(1);
if (queryStringFragments.length === 0) {
throw new Error('There are no query params available for parsing.');
}
const { state, code, error, error_description } = parseQueryResult(
queryStringFragments.join('')
);
const transaction = this.transactionManager.get();
// Transaction should have a `code_verifier` to do PKCE and a `nonce` for CSRF protection
if (!transaction || !transaction.code_verifier || !transaction.nonce) {
throw new Error('Invalid state');
}
this.transactionManager.remove();
if (error) {
throw new AuthenticationError(
error,
error_description,
state,
transaction.appState
);
}
const tokenOptions = {
audience: transaction.audience,
scope: transaction.scope,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
code_verifier: transaction.code_verifier,
grant_type: 'authorization_code',
code
} as OAuthTokenOptions;
// some old versions of the SDK might not have added redirect_uri to the
// transaction, we dont want the key to be set to undefined.
if (undefined !== transaction.redirect_uri) {
tokenOptions.redirect_uri = transaction.redirect_uri;
}
const authResult = await oauthToken(tokenOptions, this.worker);
const decodedToken = this._verifyIdToken(
authResult.id_token,
transaction.nonce,
transaction.organizationId
);
const cacheEntry = {
...authResult,
decodedToken,
audience: transaction.audience,
scope: transaction.scope,
client_id: this.options.client_id
};
this.cache.save(cacheEntry);
this.cookieStorage.save('auth0.is.authenticated', true, {
daysUntilExpire: this.sessionCheckExpiryDays
});
return {
appState: transaction.appState
};
}
/**
* ```js
* await auth0.checkSession();
* ```
*
* Check if the user is logged in using `getTokenSilently`. The difference
* with `getTokenSilently` is that this doesn't return a token, but it will
* pre-fill the token cache.
*
* It should be used for silently logging in the user when you instantiate the
* `Auth0Client` constructor. You should not need this if you are using the
* `createAuth0Client` factory.
*
* @param options
*/
public async checkSession(options?: GetTokenSilentlyOptions) {
if (!this.cookieStorage.get('auth0.is.authenticated')) {
return;
}
try {
await this.getTokenSilently(options);
} catch (error) {
if (!RECOVERABLE_ERRORS.includes(error.error)) {
throw error;
}
}
}
/**
* ```js
* const token = await auth0.getTokenSilently(options);
* ```
*
* If there's a valid token stored, return it. Otherwise, opens an
* iframe with the `/authorize` URL using the parameters provided
* as arguments. Random and secure `state` and `nonce` parameters
* will be auto-generated. If the response is successful, results
* will be valid according to their expiration times.
*
* If refresh tokens are used, the token endpoint is called directly with the
* 'refresh_token' grant. If no refresh token is available to make this call,
* the SDK falls back to using an iframe to the '/authorize' URL.
*
* This method may use a web worker to perform the token call if the in-memory
* cache is used.
*
* If an `audience` value is given to this function, the SDK always falls
* back to using an iframe to make the token exchange.
*
* Note that in all cases, falling back to an iframe requires access to
* the `auth0` cookie.
*
* @param options
*/
public async getTokenSilently(options: GetTokenSilentlyOptions = {}) {
const { ignoreCache, ...getTokenOptions } = {
audience: this.options.audience,
ignoreCache: false,
...options,
scope: getUniqueScopes(this.defaultScope, this.scope, options.scope)
};
const getAccessTokenFromCache = () => {
const cache = this.cache.get(
{
scope: getTokenOptions.scope,
audience: getTokenOptions.audience || 'default',
client_id: this.options.client_id
},
60 // get a new token if within 60 seconds of expiring
);
return cache && cache.access_token;
};
// Check the cache before acquiring the lock to avoid the latency of
// `lock.acquireLock` when the cache is populated.
if (!ignoreCache) {
let accessToken = getAccessTokenFromCache();
if (accessToken) {
return accessToken;
}
}
while (true) {
if (await lock.acquireLock(GET_TOKEN_SILENTLY_LOCK_KEY, 5000)) {
try {
// Check the cache a second time, because it may have been populated
// by a previous call while this call was waiting to acquire the lock.
if (!ignoreCache) {
let accessToken = getAccessTokenFromCache();
if (accessToken) {
return accessToken;
}
}
const authResult = this.options.useRefreshTokens
? await this._getTokenUsingRefreshToken(getTokenOptions)
: await this._getTokenFromIFrame(getTokenOptions);
this.cache.save({ client_id: this.options.client_id, ...authResult });
this.cookieStorage.save('auth0.is.authenticated', true, {
daysUntilExpire: this.sessionCheckExpiryDays
});
return authResult.access_token;
} finally {
await lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY);
}
}
}
}
/**
* ```js
* const token = await auth0.getTokenWithPopup(options);
* ```
* Opens a popup with the `/authorize` URL using the parameters
* provided as arguments. Random and secure `state` and `nonce`
* parameters will be auto-generated. If the response is successful,
* results will be valid according to their expiration times.
*
* @param options
* @param config
*/
public async getTokenWithPopup(
options: GetTokenWithPopupOptions = {},
config: PopupConfigOptions = {}
) {
options.audience = options.audience || this.options.audience;
options.scope = getUniqueScopes(
this.defaultScope,
this.scope,
options.scope
);
config = {
...DEFAULT_POPUP_CONFIG_OPTIONS,
...config
};
await this.loginWithPopup(options, config);
const cache = this.cache.get({
scope: options.scope,
audience: options.audience || 'default',
client_id: this.options.client_id
});
return cache.access_token;
}
/**
* ```js
* const isAuthenticated = await auth0.isAuthenticated();
* ```
*
* Returns `true` if there's valid information stored,
* otherwise returns `false`.
*
*/
public async isAuthenticated() {
const user = await this.getUser();
return !!user;
}
/**
* ```js
* await auth0.buildLogoutUrl(options);
* ```
*
* Builds a URL to the logout endpoint using the parameters provided as arguments.
* @param options
*/
public buildLogoutUrl(options: LogoutUrlOptions = {}): string {
if (options.client_id !== null) {
options.client_id = options.client_id || this.options.client_id;
} else {
delete options.client_id;
}
const { federated, ...logoutOptions } = options;
const federatedQuery = federated ? `&federated` : '';
const url = this._url(`/v2/logout?${createQueryParams(logoutOptions)}`);
return url + federatedQuery;
}
/**
* ```js
* auth0.logout();
* ```
*
* Clears the application session and performs a redirect to `/v2/logout`, using
* the parameters provided as arguments, to clear the Auth0 session.
* If the `federated` option is specified it also clears the Identity Provider session.
* If the `localOnly` option is specified, it only clears the application session.
* It is invalid to set both the `federated` and `localOnly` options to `true`,
* and an error will be thrown if you do.
* [Read more about how Logout works at Auth0](https://auth0.com/docs/logout).
*
* @param options
*/
public logout(options: LogoutOptions = {}) {
const { localOnly, ...logoutOptions } = options;
if (localOnly && logoutOptions.federated) {
throw new Error(
'It is invalid to set both the `federated` and `localOnly` options to `true`'
);
}
this.cache.clear();
this.cookieStorage.remove('auth0.is.authenticated');
if (localOnly) {
return;
}
const url = this.buildLogoutUrl(logoutOptions);
window.location.assign(url);
}
private async _getTokenFromIFrame(
options: GetTokenSilentlyOptions
): Promise<any> {
const stateIn = encode(createRandomString());
const nonceIn = encode(createRandomString());
const code_verifier = createRandomString();
const code_challengeBuffer = await sha256(code_verifier);
const code_challenge = bufferToBase64UrlEncoded(code_challengeBuffer);
const params = this._getParams(
options,
stateIn,
nonceIn,
code_challenge,
options.redirect_uri ||
this.options.redirect_uri ||
window.location.origin
);
const url = this._authorizeUrl({
...params,
prompt: 'none',
response_mode: 'web_message'
});
const timeout =
options.timeoutInSeconds || this.options.authorizeTimeoutInSeconds;
const codeResult = await runIframe(url, this.domainUrl, timeout);
if (stateIn !== codeResult.state) {
throw new Error('Invalid state');
}
const {
scope,
audience,
redirect_uri,
ignoreCache,
timeoutInSeconds,
...customOptions
} = options;
const tokenResult = await oauthToken(
{
...this.customOptions,
...customOptions,
scope,
audience,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
code_verifier,
code: codeResult.code,
grant_type: 'authorization_code',
redirect_uri: params.redirect_uri
} as OAuthTokenOptions,
this.worker
);
const decodedToken = this._verifyIdToken(tokenResult.id_token, nonceIn);
return {
...tokenResult,
decodedToken,
scope: params.scope,
audience: params.audience || 'default'
};
}
private async _getTokenUsingRefreshToken(
options: GetTokenSilentlyOptions
): Promise<any> {
options.scope = getUniqueScopes(
this.defaultScope,
this.options.scope,
options.scope
);
const cache = this.cache.get({
scope: options.scope,
audience: options.audience || 'default',
client_id: this.options.client_id
});
// If you don't have a refresh token in memory
// and you don't have a refresh token in web worker memory
// fallback to an iframe.
if ((!cache || !cache.refresh_token) && !this.worker) {
return await this._getTokenFromIFrame(options);
}
const redirect_uri =
options.redirect_uri ||
this.options.redirect_uri ||
window.location.origin;
let tokenResult;
const {
scope,
audience,
ignoreCache,
timeoutInSeconds,
...customOptions
} = options;
const timeout =
typeof options.timeoutInSeconds === 'number'
? options.timeoutInSeconds * 1000
: null;
try {
tokenResult = await oauthToken(
{
...this.customOptions,
...customOptions,
audience,
scope,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
grant_type: 'refresh_token',
refresh_token: cache && cache.refresh_token,
redirect_uri,
...(timeout && { timeout })
} as RefreshTokenOptions,
this.worker
);
} catch (e) {
// The web worker didn't have a refresh token in memory so
// fallback to an iframe.
if (e.message === MISSING_REFRESH_TOKEN_ERROR_MESSAGE) {
return await this._getTokenFromIFrame(options);
}
throw e;
}
const decodedToken = this._verifyIdToken(tokenResult.id_token);
return {
...tokenResult,
decodedToken,
scope: options.scope,
audience: options.audience || 'default'
};
}
}