@axa-fr/react-oidc 6.0.0-beta1 → 6.0.0-beta10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/README.md +10 -8
  2. package/dist/OidcProvider.d.ts +1 -1
  3. package/dist/OidcProvider.d.ts.map +1 -1
  4. package/dist/OidcProvider.js +5 -4
  5. package/dist/OidcProvider.js.map +1 -1
  6. package/dist/OidcServiceWorker.js +21 -4
  7. package/dist/OidcTrustedDomains.js +7 -2
  8. package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
  9. package/dist/core/default-component/SilentCallback.component.js +5 -19
  10. package/dist/core/default-component/SilentCallback.component.js.map +1 -1
  11. package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
  12. package/dist/core/routes/OidcRoutes.js +1 -4
  13. package/dist/core/routes/OidcRoutes.js.map +1 -1
  14. package/dist/vanilla/oidc.d.ts +5 -5
  15. package/dist/vanilla/oidc.d.ts.map +1 -1
  16. package/dist/vanilla/oidc.js +218 -132
  17. package/dist/vanilla/oidc.js.map +1 -1
  18. package/package.json +1 -1
  19. package/src/oidc/OidcProvider.tsx +8 -8
  20. package/src/oidc/core/default-component/SilentCallback.component.tsx +1 -6
  21. package/src/oidc/core/routes/OidcRoutes.tsx +0 -4
  22. package/src/oidc/vanilla/OidcServiceWorker.js +21 -4
  23. package/src/oidc/vanilla/OidcTrustedDomains.js +7 -2
  24. package/src/oidc/vanilla/oidc.ts +153 -104
  25. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts +0 -4
  26. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts.map +0 -1
  27. package/dist/core/default-component/ServiceWorkerInstall.component.js +0 -131
  28. package/dist/core/default-component/ServiceWorkerInstall.component.js.map +0 -1
  29. package/src/oidc/core/default-component/ServiceWorkerInstall.component.tsx +0 -60
@@ -75,7 +75,7 @@ var timer_1 = __importDefault(require("./timer"));
75
75
  var checkSessionIFrame_1 = require("./checkSessionIFrame");
76
76
  var route_utils_1 = require("./route-utils");
77
77
  var performTokenRequestAsync = function (url, details, extras) { return __awaiter(void 0, void 0, void 0, function () {
78
- var _i, _a, _b, key, value, formBody, property, encodedKey, encodedValue, formBodyString, response, result;
78
+ var _i, _a, _b, key, value, formBody, property, encodedKey, encodedValue, formBodyString, response, tokens, currentTimeUnixSecond;
79
79
  return __generator(this, function (_c) {
80
80
  switch (_c.label) {
81
81
  case 0:
@@ -106,15 +106,20 @@ var performTokenRequestAsync = function (url, details, extras) { return __awaite
106
106
  }
107
107
  return [4 /*yield*/, response.json()];
108
108
  case 2:
109
- result = _c.sent();
109
+ tokens = _c.sent();
110
+ if (!tokens.issued_at) {
111
+ currentTimeUnixSecond = new Date().getTime() / 1000;
112
+ tokens.issued_at = currentTimeUnixSecond;
113
+ }
110
114
  return [2 /*return*/, { success: true,
111
115
  data: {
112
- accessToken: result.access_token,
113
- expiresIn: result.expires_in,
114
- idToken: result.id_token,
115
- refreshToken: result.refresh_token,
116
- scope: result.scope,
117
- tokenType: result.token_type,
116
+ accessToken: tokens.access_token,
117
+ expiresIn: tokens.expires_in,
118
+ idToken: tokens.id_token,
119
+ refreshToken: tokens.refresh_token,
120
+ scope: tokens.scope,
121
+ tokenType: tokens.token_type,
122
+ issuedAt: tokens.issued_at
118
123
  }
119
124
  }];
120
125
  }
@@ -218,7 +223,7 @@ var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
218
223
  var _a;
219
224
  var refreshTimeBeforeTokensExpirationInSecond = (_a = oidc.configuration.refresh_time_before_tokens_expiration_in_second) !== null && _a !== void 0 ? _a : 60;
220
225
  return timer_1.default.setTimeout(function () { return __awaiter(void 0, void 0, void 0, function () {
221
- var currentTimeUnixSecond, timeInfo, tokens, _a;
226
+ var currentTimeUnixSecond, timeInfo, tokens, _a, tokens;
222
227
  return __generator(this, function (_b) {
223
228
  switch (_b.label) {
224
229
  case 0:
@@ -253,9 +258,9 @@ var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
253
258
  return [3 /*break*/, 7];
254
259
  case 5: return [4 /*yield*/, oidc.syncTokensAsync()];
255
260
  case 6:
256
- _b.sent();
257
- if (oidc.timeoutId) {
258
- oidc.timeoutId = autoRenewTokens(oidc, refreshToken, expiresAt);
261
+ tokens = _b.sent();
262
+ if (tokens && oidc.timeoutId) {
263
+ oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, tokens.expiresAt);
259
264
  }
260
265
  _b.label = 7;
261
266
  case 7: return [2 /*return*/];
@@ -298,7 +303,7 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
298
303
  case 0: return [4 /*yield*/, fetch(url, {
299
304
  headers: {
300
305
  authorization: "Bearer ".concat(accessToken),
301
- credentials: 'same-origin'
306
+ credentials: 'include'
302
307
  }
303
308
  })];
304
309
  case 1:
@@ -319,7 +324,7 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
319
324
  });
320
325
  }); };
321
326
  var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0, void 0, void 0, function () {
322
- var accessTokenPayload, _idTokenPayload, expiresAt;
327
+ var accessTokenPayload, _idTokenPayload, idTokenExipreAt, accessTokenExpiresAt, expiresAt;
323
328
  return __generator(this, function (_a) {
324
329
  switch (_a.label) {
325
330
  case 0:
@@ -341,7 +346,9 @@ var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0,
341
346
  _a.label = 6;
342
347
  case 6:
343
348
  _idTokenPayload = idTokenPayload(tokens.idToken);
344
- expiresAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
349
+ idTokenExipreAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : Number.MAX_VALUE;
350
+ accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp) ? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
351
+ expiresAt = idTokenExipreAt < accessTokenExpiresAt ? idTokenExipreAt : accessTokenExpiresAt;
345
352
  return [2 /*return*/, __assign(__assign({}, tokens), { idTokenPayload: _idTokenPayload, accessTokenPayload: accessTokenPayload, expiresAt: expiresAt })];
346
353
  }
347
354
  });
@@ -376,23 +383,28 @@ var eventNames = {
376
383
  var getRandomInt = function (max) {
377
384
  return Math.floor(Math.random() * max);
378
385
  };
379
- var WELL_KNOWN_PATH = '.well-known';
380
- var OPENID_CONFIGURATION = 'openid-configuration';
381
386
  var oneHourSecond = 60 * 60;
382
- var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond) {
387
+ var fetchFromIssuerCache = null;
388
+ var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond, storage) {
383
389
  if (timeCacheSecond === void 0) { timeCacheSecond = oneHourSecond; }
390
+ if (storage === void 0) { storage = window.sessionStorage; }
384
391
  return __awaiter(void 0, void 0, void 0, function () {
385
- var fullUrl, localStorageKey, cacheJson, oneHourMinisecond, response, result;
392
+ var fullUrl, localStorageKey, cacheJson, oneHourMinisecond, response, result, timestamp;
386
393
  return __generator(this, function (_a) {
387
394
  switch (_a.label) {
388
395
  case 0:
389
- fullUrl = "".concat(openIdIssuerUrl, "/").concat(WELL_KNOWN_PATH, "/").concat(OPENID_CONFIGURATION);
396
+ fullUrl = "".concat(openIdIssuerUrl, "/.well-known/openid-configuration");
390
397
  localStorageKey = "oidc.server:".concat(openIdIssuerUrl);
391
- cacheJson = window.sessionStorage.getItem(localStorageKey);
398
+ if (!fetchFromIssuerCache && storage) {
399
+ cacheJson = storage.getItem(localStorageKey);
400
+ if (cacheJson) {
401
+ fetchFromIssuerCache = JSON.parse(cacheJson);
402
+ }
403
+ }
392
404
  oneHourMinisecond = 1000 * timeCacheSecond;
393
405
  // @ts-ignore
394
- if (cacheJson && (cacheJson.timestamp + oneHourMinisecond) > Date.now()) {
395
- return [2 /*return*/, new OidcAuthorizationServiceConfiguration(JSON.parse(cacheJson))];
406
+ if (fetchFromIssuerCache && (fetchFromIssuerCache.timestamp + oneHourMinisecond) > Date.now()) {
407
+ return [2 /*return*/, new OidcAuthorizationServiceConfiguration(fetchFromIssuerCache.result)];
396
408
  }
397
409
  return [4 /*yield*/, fetch(fullUrl)];
398
410
  case 1:
@@ -403,7 +415,11 @@ var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond) {
403
415
  return [4 /*yield*/, response.json()];
404
416
  case 2:
405
417
  result = _a.sent();
406
- window.sessionStorage.setItem(localStorageKey, JSON.stringify({ result: result, timestamp: Date.now() }));
418
+ timestamp = Date.now();
419
+ fetchFromIssuerCache = { result: result, timestamp: timestamp };
420
+ if (storage) {
421
+ storage.setItem(localStorageKey, JSON.stringify({ result: result, timestamp: timestamp }));
422
+ }
407
423
  return [2 /*return*/, new OidcAuthorizationServiceConfiguration(result)];
408
424
  }
409
425
  });
@@ -427,13 +443,16 @@ var buildQueries = function (extras) {
427
443
  var Oidc = /** @class */ (function () {
428
444
  function Oidc(configuration, configurationName) {
429
445
  if (configurationName === void 0) { configurationName = "default"; }
430
- this.initAsyncPromise = null;
431
446
  this.tryKeepExistingSessionPromise = null;
432
447
  this.loginPromise = null;
433
448
  this.loginCallbackPromise = null;
434
449
  this.syncTokensAsyncPromise = null;
435
450
  this.loginCallbackWithAutoTokensRenewPromise = null;
436
- this.configuration = configuration;
451
+ var silent_login_uri = configuration.silent_login_uri;
452
+ if (configuration.silent_redirect_uri && !configuration.silent_login_uri) {
453
+ silent_login_uri = "".concat(configuration.silent_redirect_uri.replace("-callback", "").replace("callback", ""), "-login");
454
+ }
455
+ this.configuration = __assign(__assign({}, configuration), { silent_login_uri: silent_login_uri });
437
456
  this.configurationName = configurationName;
438
457
  this.tokens = null;
439
458
  this.userInfo = null;
@@ -451,6 +470,7 @@ var Oidc = /** @class */ (function () {
451
470
  this.publishEvent.bind(this);
452
471
  this.destroyAsync.bind(this);
453
472
  this.logoutAsync.bind(this);
473
+ this.initAsync(this.configuration.authority, this.configuration.authority_configuration);
454
474
  }
455
475
  Oidc.prototype.subscriveEvents = function (func) {
456
476
  var id = getRandomInt(9999999999999).toString();
@@ -478,24 +498,46 @@ var Oidc = /** @class */ (function () {
478
498
  }
479
499
  return oidcDatabase[name];
480
500
  };
481
- Oidc.prototype.silentLoginCallbackFromIFrame = function () {
501
+ Oidc.prototype._silentLoginCallbackFromIFrame = function () {
482
502
  if (this.configuration.silent_redirect_uri && this.configuration.silent_login_uri) {
483
503
  var queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
484
504
  window.top.postMessage("".concat(this.configurationName, "_oidc_tokens:").concat(JSON.stringify({ tokens: this.tokens, sessionState: queryParams.session_state })), window.location.origin);
485
505
  }
486
506
  };
487
- Oidc.prototype.silentLoginErrorCallbackFromIFrame = function () {
507
+ Oidc.prototype._silentLoginErrorCallbackFromIFrame = function () {
488
508
  if (this.configuration.silent_redirect_uri && this.configuration.silent_login_uri) {
489
509
  var queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
490
510
  window.top.postMessage("".concat(this.configurationName, "_oidc_error:").concat(JSON.stringify({ error: queryParams.error })), window.location.origin);
491
511
  }
492
512
  };
513
+ Oidc.prototype.silentLoginCallBackAsync = function () {
514
+ return __awaiter(this, void 0, void 0, function () {
515
+ var error_1;
516
+ return __generator(this, function (_a) {
517
+ switch (_a.label) {
518
+ case 0:
519
+ _a.trys.push([0, 2, , 3]);
520
+ return [4 /*yield*/, this.loginCallbackAsync(true)];
521
+ case 1:
522
+ _a.sent();
523
+ this._silentLoginCallbackFromIFrame();
524
+ return [3 /*break*/, 3];
525
+ case 2:
526
+ error_1 = _a.sent();
527
+ console.error(error_1);
528
+ this._silentLoginErrorCallbackFromIFrame();
529
+ return [3 /*break*/, 3];
530
+ case 3: return [2 /*return*/];
531
+ }
532
+ });
533
+ });
534
+ };
493
535
  Oidc.prototype.silentLoginAsync = function (extras, state, scope) {
494
536
  if (extras === void 0) { extras = null; }
495
537
  if (state === void 0) { state = null; }
496
538
  if (scope === void 0) { scope = null; }
497
539
  return __awaiter(this, void 0, void 0, function () {
498
- var configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
540
+ var numberTryOnline, configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
499
541
  return __generator(this, function (_c) {
500
542
  switch (_c.label) {
501
543
  case 0:
@@ -511,6 +553,17 @@ var Oidc = /** @class */ (function () {
511
553
  this.publishEvent(eventNames.silentLoginAsync, { message: "wait because document is hidden" });
512
554
  return [3 /*break*/, 1];
513
555
  case 3:
556
+ numberTryOnline = 6;
557
+ _c.label = 4;
558
+ case 4:
559
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 6];
560
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
561
+ case 5:
562
+ _c.sent();
563
+ numberTryOnline--;
564
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
565
+ return [3 /*break*/, 4];
566
+ case 6:
514
567
  try {
515
568
  this.publishEvent(eventNames.silentLoginAsync_begin, {});
516
569
  configuration_1 = this.configuration;
@@ -572,7 +625,7 @@ var Oidc = /** @class */ (function () {
572
625
  self_1.publishEvent(eventNames.silentLoginAsync_error, result);
573
626
  iframe_1.remove();
574
627
  isResolved_1 = true;
575
- reject(result);
628
+ reject(new Error("oidc_" + result.error));
576
629
  }
577
630
  }
578
631
  }
@@ -581,10 +634,10 @@ var Oidc = /** @class */ (function () {
581
634
  var silentSigninTimeout = (_a = configuration_1.silent_login_timeout) !== null && _a !== void 0 ? _a : 12000;
582
635
  setTimeout(function () {
583
636
  if (!isResolved_1) {
584
- self_1.publishEvent(eventNames.silentLoginAsync_error, "timeout");
637
+ self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
585
638
  iframe_1.remove();
586
639
  isResolved_1 = true;
587
- reject("timeout");
640
+ reject(new Error("timeout"));
588
641
  }
589
642
  }, silentSigninTimeout);
590
643
  }
@@ -607,9 +660,9 @@ var Oidc = /** @class */ (function () {
607
660
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
608
661
  var _a;
609
662
  return __awaiter(this, void 0, void 0, function () {
610
- var _b;
611
- return __generator(this, function (_c) {
612
- switch (_c.label) {
663
+ var serviceWorker, storage, initAsyncPromise;
664
+ return __generator(this, function (_b) {
665
+ switch (_b.label) {
613
666
  case 0:
614
667
  if (authorityConfiguration != null) {
615
668
  return [2 /*return*/, new OidcAuthorizationServiceConfiguration({
@@ -621,14 +674,14 @@ var Oidc = /** @class */ (function () {
621
674
  check_session_iframe: authorityConfiguration.check_session_iframe,
622
675
  })];
623
676
  }
624
- if (this.initAsyncPromise) {
625
- return [2 /*return*/, this.initAsyncPromise];
626
- }
627
- _b = this;
628
- return [4 /*yield*/, fetchFromIssuer(authority, (_a = this.configuration.authority_time_cache_wellknowurl_in_second) !== null && _a !== void 0 ? _a : 60 * 60)];
677
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(this.configuration.service_worker_relative_url, this.configurationName)];
629
678
  case 1:
630
- _b.initAsyncPromise = _c.sent();
631
- return [2 /*return*/, this.initAsyncPromise];
679
+ serviceWorker = _b.sent();
680
+ storage = serviceWorker ? window.localStorage : null;
681
+ return [4 /*yield*/, fetchFromIssuer(authority, (_a = this.configuration.authority_time_cache_wellknowurl_in_second) !== null && _a !== void 0 ? _a : 60 * 60, storage)];
682
+ case 2:
683
+ initAsyncPromise = _b.sent();
684
+ return [2 /*return*/, initAsyncPromise];
632
685
  }
633
686
  });
634
687
  });
@@ -673,7 +726,8 @@ var Oidc = /** @class */ (function () {
673
726
  expiresIn: tokens.expires_in,
674
727
  idToken: tokens.id_token,
675
728
  scope: tokens.scope,
676
- tokenType: tokens.token_type
729
+ tokenType: tokens.token_type,
730
+ issuedAt: tokens.issued_at
677
731
  };
678
732
  _a = this;
679
733
  return [4 /*yield*/, setTokensAsync(serviceWorker, reformattedToken)];
@@ -685,8 +739,10 @@ var Oidc = /** @class */ (function () {
685
739
  return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
686
740
  case 6:
687
741
  sessionState = _d.sent();
742
+ // @ts-ignore
688
743
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
689
744
  case 7:
745
+ // @ts-ignore
690
746
  _d.sent();
691
747
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
692
748
  success: true,
@@ -721,8 +777,10 @@ var Oidc = /** @class */ (function () {
721
777
  // @ts-ignore
722
778
  this.timeoutId = autoRenewTokens(this, tokens.refreshToken, this.tokens.expiresAt);
723
779
  sessionState = session.getSessionState();
780
+ // @ts-ignore
724
781
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
725
782
  case 12:
783
+ // @ts-ignore
726
784
  _d.sent();
727
785
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
728
786
  success: true,
@@ -1045,6 +1103,7 @@ var Oidc = /** @class */ (function () {
1045
1103
  clearTimeout(timeoutId);
1046
1104
  _this.timeoutId = null;
1047
1105
  var loginParams_1 = getLoginParams(_this.configurationName, redirectUri_1);
1106
+ // @ts-ignore
1048
1107
  _this.startCheckSessionAsync(oidcServerConfiguration_1.check_session_iframe, clientId_1, sessionState_2, isSilentSignin).then(function () {
1049
1108
  _this.publishEvent(eventNames.loginCallbackAsync_end, {});
1050
1109
  resolve({
@@ -1080,18 +1139,19 @@ var Oidc = /** @class */ (function () {
1080
1139
  };
1081
1140
  Oidc.prototype.refreshTokensAsync = function (refreshToken) {
1082
1141
  return __awaiter(this, void 0, void 0, function () {
1083
- var localsilentLoginAsync, configuration, clientId, redirectUri, authority, extras, _i, _a, _b, key, value, oidcServerConfiguration, details, index, tokenResponse, exception_4;
1142
+ var localsilentLoginAsync, configuration, clientId, redirectUri, authority, extras, _i, _a, _b, key, value, oidcServerConfiguration, details, index, numberTryOnline, tokenResponse, exception_4;
1084
1143
  var _this = this;
1085
1144
  return __generator(this, function (_c) {
1086
1145
  switch (_c.label) {
1087
1146
  case 0:
1088
1147
  localsilentLoginAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1089
- var silent_token_response, exceptionSilent_1;
1148
+ var loginParams, silent_token_response, exceptionSilent_1;
1090
1149
  return __generator(this, function (_a) {
1091
1150
  switch (_a.label) {
1092
1151
  case 0:
1093
1152
  _a.trys.push([0, 2, , 3]);
1094
- return [4 /*yield*/, this.silentLoginAsync()];
1153
+ loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1154
+ return [4 /*yield*/, this.silentLoginAsync(loginParams.extras, loginParams.state)];
1095
1155
  case 1:
1096
1156
  silent_token_response = _a.sent();
1097
1157
  if (silent_token_response) {
@@ -1139,12 +1199,12 @@ var Oidc = /** @class */ (function () {
1139
1199
  index = 0;
1140
1200
  _c.label = 4;
1141
1201
  case 4:
1142
- if (!(index <= 2)) return [3 /*break*/, 15];
1202
+ if (!(index <= 4)) return [3 /*break*/, 18];
1143
1203
  _c.label = 5;
1144
1204
  case 5:
1145
- _c.trys.push([5, 13, , 14]);
1205
+ _c.trys.push([5, 16, , 17]);
1146
1206
  this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1147
- if (!(index > 1)) return [3 /*break*/, 8];
1207
+ if (!(index > 1)) return [3 /*break*/, 11];
1148
1208
  _c.label = 6;
1149
1209
  case 6:
1150
1210
  if (!document.hidden) return [3 /*break*/, 8];
@@ -1153,108 +1213,134 @@ var Oidc = /** @class */ (function () {
1153
1213
  _c.sent();
1154
1214
  this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1155
1215
  return [3 /*break*/, 6];
1156
- case 8: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1216
+ case 8:
1217
+ numberTryOnline = 6;
1218
+ _c.label = 9;
1157
1219
  case 9:
1220
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 11];
1221
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1222
+ case 10:
1223
+ _c.sent();
1224
+ numberTryOnline--;
1225
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1226
+ return [3 /*break*/, 9];
1227
+ case 11: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1228
+ case 12:
1158
1229
  tokenResponse = _c.sent();
1159
- if (!tokenResponse.success) return [3 /*break*/, 10];
1230
+ if (!tokenResponse.success) return [3 /*break*/, 13];
1160
1231
  this.publishEvent(eventNames.refreshTokensAsync_end, { success: tokenResponse.success });
1161
1232
  return [2 /*return*/, tokenResponse.data];
1162
- case 10:
1233
+ case 13:
1163
1234
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "bad request", tokenResponse: tokenResponse });
1164
1235
  return [4 /*yield*/, localsilentLoginAsync()];
1165
- case 11: return [2 /*return*/, _c.sent()];
1166
- case 12: return [3 /*break*/, 14];
1167
- case 13:
1236
+ case 14: return [2 /*return*/, _c.sent()];
1237
+ case 15: return [3 /*break*/, 17];
1238
+ case 16:
1168
1239
  exception_4 = _c.sent();
1169
1240
  console.error(exception_4);
1170
1241
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exception", exception: exception_4.message });
1171
- return [3 /*break*/, 14];
1172
- case 14:
1242
+ return [3 /*break*/, 17];
1243
+ case 17:
1173
1244
  index++;
1174
1245
  return [3 /*break*/, 4];
1175
- case 15: return [2 /*return*/];
1246
+ case 18: return [2 /*return*/];
1176
1247
  }
1177
1248
  });
1178
1249
  });
1179
1250
  };
1180
1251
  Oidc.prototype.syncTokensAsync = function () {
1181
- var _a;
1182
1252
  return __awaiter(this, void 0, void 0, function () {
1183
- var configuration, oidcServerConfiguration, serviceWorker, isLogin, silent_token_response, _b, exceptionSilent_2, session, tokens;
1184
- return __generator(this, function (_c) {
1185
- switch (_c.label) {
1186
- case 0:
1187
- configuration = this.configuration;
1188
- if (!this.tokens) {
1189
- return [2 /*return*/];
1190
- }
1191
- return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1192
- case 1:
1193
- oidcServerConfiguration = _c.sent();
1194
- return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1195
- case 2:
1196
- serviceWorker = _c.sent();
1197
- if (!serviceWorker) return [3 /*break*/, 14];
1198
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1199
- case 3:
1200
- isLogin = (_c.sent()).isLogin;
1201
- if (!(isLogin == false)) return [3 /*break*/, 5];
1202
- this.publishEvent(eventNames.logout_from_another_tab, {});
1203
- return [4 /*yield*/, this.destroyAsync()];
1204
- case 4:
1205
- _c.sent();
1206
- return [3 /*break*/, 13];
1207
- case 5:
1208
- if (!(isLogin == null)) return [3 /*break*/, 13];
1209
- _c.label = 6;
1210
- case 6:
1211
- _c.trys.push([6, 11, , 12]);
1212
- this.publishEvent(eventNames.syncTokensAsync_begin, {});
1213
- this.syncTokensAsyncPromise = this.silentLoginAsync({ prompt: "none" });
1214
- return [4 /*yield*/, this.syncTokensAsyncPromise];
1215
- case 7:
1216
- silent_token_response = _c.sent();
1217
- if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1218
- _b = this;
1219
- return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1220
- case 8:
1221
- _b.tokens = _c.sent();
1222
- return [3 /*break*/, 10];
1223
- case 9:
1224
- this.publishEvent(eventNames.syncTokensAsync_error, { message: "no token found in result" });
1225
- if (this.timeoutId) {
1226
- timer_1.default.clearTimeout(this.timeoutId);
1227
- this.timeoutId = null;
1228
- }
1229
- return [2 /*return*/];
1230
- case 10: return [3 /*break*/, 12];
1231
- case 11:
1232
- exceptionSilent_2 = _c.sent();
1233
- console.error(exceptionSilent_2);
1234
- this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1235
- if (this.timeoutId) {
1236
- timer_1.default.clearTimeout(this.timeoutId);
1237
- this.timeoutId = null;
1253
+ var localSyncTokensAsync;
1254
+ var _this = this;
1255
+ return __generator(this, function (_a) {
1256
+ localSyncTokensAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1257
+ var configuration, oidcServerConfiguration, serviceWorker, isLogin, loginParams, silent_token_response, _a, exceptionSilent_2, session, tokens;
1258
+ var _b;
1259
+ return __generator(this, function (_c) {
1260
+ switch (_c.label) {
1261
+ case 0:
1262
+ configuration = this.configuration;
1263
+ if (!this.tokens) {
1264
+ return [2 /*return*/, null];
1265
+ }
1266
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1267
+ case 1:
1268
+ oidcServerConfiguration = _c.sent();
1269
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1270
+ case 2:
1271
+ serviceWorker = _c.sent();
1272
+ if (!serviceWorker) return [3 /*break*/, 13];
1273
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1274
+ case 3:
1275
+ isLogin = (_c.sent()).isLogin;
1276
+ if (!(isLogin == false)) return [3 /*break*/, 5];
1277
+ this.publishEvent(eventNames.logout_from_another_tab, {});
1278
+ return [4 /*yield*/, this.destroyAsync()];
1279
+ case 4:
1280
+ _c.sent();
1281
+ return [2 /*return*/, null];
1282
+ case 5:
1283
+ if (!(isLogin == null)) return [3 /*break*/, 12];
1284
+ _c.label = 6;
1285
+ case 6:
1286
+ _c.trys.push([6, 11, , 12]);
1287
+ this.publishEvent(eventNames.syncTokensAsync_begin, {});
1288
+ loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1289
+ return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1290
+ case 7:
1291
+ silent_token_response = _c.sent();
1292
+ if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1293
+ _a = this;
1294
+ return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1295
+ case 8:
1296
+ _a.tokens = _c.sent();
1297
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1298
+ return [2 /*return*/, this.tokens];
1299
+ case 9:
1300
+ this.publishEvent(eventNames.syncTokensAsync_error, { message: "no token found in result" });
1301
+ if (this.timeoutId) {
1302
+ timer_1.default.clearTimeout(this.timeoutId);
1303
+ this.timeoutId = null;
1304
+ }
1305
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1306
+ return [2 /*return*/, null];
1307
+ case 10: return [3 /*break*/, 12];
1308
+ case 11:
1309
+ exceptionSilent_2 = _c.sent();
1310
+ console.error(exceptionSilent_2);
1311
+ this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1312
+ if (this.timeoutId) {
1313
+ timer_1.default.clearTimeout(this.timeoutId);
1314
+ this.timeoutId = null;
1315
+ }
1316
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1317
+ return [2 /*return*/, null];
1318
+ case 12: return [3 /*break*/, 16];
1319
+ case 13:
1320
+ session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_b = configuration.storage) !== null && _b !== void 0 ? _b : sessionStorage);
1321
+ return [4 /*yield*/, session.initAsync()];
1322
+ case 14:
1323
+ tokens = (_c.sent()).tokens;
1324
+ if (!!tokens) return [3 /*break*/, 16];
1325
+ this.publishEvent(eventNames.logout_from_another_tab, {});
1326
+ return [4 /*yield*/, this.destroyAsync()];
1327
+ case 15:
1328
+ _c.sent();
1329
+ return [2 /*return*/, null];
1330
+ case 16: return [2 /*return*/, this.tokens];
1238
1331
  }
1239
- return [2 /*return*/];
1240
- case 12:
1241
- this.syncTokensAsyncPromise = null;
1242
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1243
- _c.label = 13;
1244
- case 13: return [3 /*break*/, 17];
1245
- case 14:
1246
- session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
1247
- return [4 /*yield*/, session.initAsync()];
1248
- case 15:
1249
- tokens = (_c.sent()).tokens;
1250
- if (!!tokens) return [3 /*break*/, 17];
1251
- this.publishEvent(eventNames.logout_from_another_tab, {});
1252
- return [4 /*yield*/, this.destroyAsync()];
1253
- case 16:
1254
- _c.sent();
1255
- _c.label = 17;
1256
- case 17: return [2 /*return*/];
1332
+ });
1333
+ }); };
1334
+ if (this.syncTokensAsyncPromise) {
1335
+ return [2 /*return*/, this.syncTokensAsyncPromise];
1257
1336
  }
1337
+ this.syncTokensAsyncPromise = localSyncTokensAsync().then(function (result) {
1338
+ if (_this.syncTokensAsyncPromise) {
1339
+ _this.syncTokensAsyncPromise = null;
1340
+ }
1341
+ return result;
1342
+ });
1343
+ return [2 /*return*/, this.syncTokensAsyncPromise];
1258
1344
  });
1259
1345
  });
1260
1346
  };