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

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 (33) hide show
  1. package/README.md +11 -8
  2. package/dist/OidcProvider.d.ts +2 -1
  3. package/dist/OidcProvider.d.ts.map +1 -1
  4. package/dist/OidcProvider.js +15 -7
  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/ReactOidc.d.ts.map +1 -1
  9. package/dist/ReactOidc.js +3 -2
  10. package/dist/ReactOidc.js.map +1 -1
  11. package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
  12. package/dist/core/default-component/SilentCallback.component.js +5 -19
  13. package/dist/core/default-component/SilentCallback.component.js.map +1 -1
  14. package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
  15. package/dist/core/routes/OidcRoutes.js +1 -4
  16. package/dist/core/routes/OidcRoutes.js.map +1 -1
  17. package/dist/vanilla/oidc.d.ts +8 -5
  18. package/dist/vanilla/oidc.d.ts.map +1 -1
  19. package/dist/vanilla/oidc.js +301 -144
  20. package/dist/vanilla/oidc.js.map +1 -1
  21. package/package.json +1 -1
  22. package/src/oidc/OidcProvider.tsx +17 -8
  23. package/src/oidc/ReactOidc.tsx +3 -2
  24. package/src/oidc/core/default-component/SilentCallback.component.tsx +1 -6
  25. package/src/oidc/core/routes/OidcRoutes.tsx +0 -4
  26. package/src/oidc/vanilla/OidcServiceWorker.js +21 -4
  27. package/src/oidc/vanilla/OidcTrustedDomains.js +7 -2
  28. package/src/oidc/vanilla/oidc.ts +192 -110
  29. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts +0 -4
  30. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts.map +0 -1
  31. package/dist/core/default-component/ServiceWorkerInstall.component.js +0 -131
  32. package/dist/core/default-component/ServiceWorkerInstall.component.js.map +0 -1
  33. 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
  });
@@ -350,6 +357,7 @@ var eventNames = {
350
357
  service_worker_not_supported_by_browser: "service_worker_not_supported_by_browser",
351
358
  token_aquired: "token_aquired",
352
359
  logout_from_another_tab: "logout_from_another_tab",
360
+ logout_from_same_tab: "logout_from_same_tab",
353
361
  token_renewed: "token_renewed",
354
362
  token_timer: "token_timer",
355
363
  loginAsync_begin: "loginAsync_begin",
@@ -376,23 +384,28 @@ var eventNames = {
376
384
  var getRandomInt = function (max) {
377
385
  return Math.floor(Math.random() * max);
378
386
  };
379
- var WELL_KNOWN_PATH = '.well-known';
380
- var OPENID_CONFIGURATION = 'openid-configuration';
381
387
  var oneHourSecond = 60 * 60;
382
- var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond) {
388
+ var fetchFromIssuerCache = null;
389
+ var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond, storage) {
383
390
  if (timeCacheSecond === void 0) { timeCacheSecond = oneHourSecond; }
391
+ if (storage === void 0) { storage = window.sessionStorage; }
384
392
  return __awaiter(void 0, void 0, void 0, function () {
385
- var fullUrl, localStorageKey, cacheJson, oneHourMinisecond, response, result;
393
+ var fullUrl, localStorageKey, cacheJson, oneHourMinisecond, response, result, timestamp;
386
394
  return __generator(this, function (_a) {
387
395
  switch (_a.label) {
388
396
  case 0:
389
- fullUrl = "".concat(openIdIssuerUrl, "/").concat(WELL_KNOWN_PATH, "/").concat(OPENID_CONFIGURATION);
397
+ fullUrl = "".concat(openIdIssuerUrl, "/.well-known/openid-configuration");
390
398
  localStorageKey = "oidc.server:".concat(openIdIssuerUrl);
391
- cacheJson = window.sessionStorage.getItem(localStorageKey);
399
+ if (!fetchFromIssuerCache && storage) {
400
+ cacheJson = storage.getItem(localStorageKey);
401
+ if (cacheJson) {
402
+ fetchFromIssuerCache = JSON.parse(cacheJson);
403
+ }
404
+ }
392
405
  oneHourMinisecond = 1000 * timeCacheSecond;
393
406
  // @ts-ignore
394
- if (cacheJson && (cacheJson.timestamp + oneHourMinisecond) > Date.now()) {
395
- return [2 /*return*/, new OidcAuthorizationServiceConfiguration(JSON.parse(cacheJson))];
407
+ if (fetchFromIssuerCache && (fetchFromIssuerCache.timestamp + oneHourMinisecond) > Date.now()) {
408
+ return [2 /*return*/, new OidcAuthorizationServiceConfiguration(fetchFromIssuerCache.result)];
396
409
  }
397
410
  return [4 /*yield*/, fetch(fullUrl)];
398
411
  case 1:
@@ -403,7 +416,11 @@ var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond) {
403
416
  return [4 /*yield*/, response.json()];
404
417
  case 2:
405
418
  result = _a.sent();
406
- window.sessionStorage.setItem(localStorageKey, JSON.stringify({ result: result, timestamp: Date.now() }));
419
+ timestamp = Date.now();
420
+ fetchFromIssuerCache = { result: result, timestamp: timestamp };
421
+ if (storage) {
422
+ storage.setItem(localStorageKey, JSON.stringify({ result: result, timestamp: timestamp }));
423
+ }
407
424
  return [2 /*return*/, new OidcAuthorizationServiceConfiguration(result)];
408
425
  }
409
426
  });
@@ -427,13 +444,16 @@ var buildQueries = function (extras) {
427
444
  var Oidc = /** @class */ (function () {
428
445
  function Oidc(configuration, configurationName) {
429
446
  if (configurationName === void 0) { configurationName = "default"; }
430
- this.initAsyncPromise = null;
431
447
  this.tryKeepExistingSessionPromise = null;
432
448
  this.loginPromise = null;
433
449
  this.loginCallbackPromise = null;
434
450
  this.syncTokensAsyncPromise = null;
435
451
  this.loginCallbackWithAutoTokensRenewPromise = null;
436
- this.configuration = configuration;
452
+ var silent_login_uri = configuration.silent_login_uri;
453
+ if (configuration.silent_redirect_uri && !configuration.silent_login_uri) {
454
+ silent_login_uri = "".concat(configuration.silent_redirect_uri.replace("-callback", "").replace("callback", ""), "-login");
455
+ }
456
+ this.configuration = __assign(__assign({}, configuration), { silent_login_uri: silent_login_uri });
437
457
  this.configurationName = configurationName;
438
458
  this.tokens = null;
439
459
  this.userInfo = null;
@@ -451,6 +471,7 @@ var Oidc = /** @class */ (function () {
451
471
  this.publishEvent.bind(this);
452
472
  this.destroyAsync.bind(this);
453
473
  this.logoutAsync.bind(this);
474
+ this.initAsync(this.configuration.authority, this.configuration.authority_configuration);
454
475
  }
455
476
  Oidc.prototype.subscriveEvents = function (func) {
456
477
  var id = getRandomInt(9999999999999).toString();
@@ -478,24 +499,46 @@ var Oidc = /** @class */ (function () {
478
499
  }
479
500
  return oidcDatabase[name];
480
501
  };
481
- Oidc.prototype.silentLoginCallbackFromIFrame = function () {
502
+ Oidc.prototype._silentLoginCallbackFromIFrame = function () {
482
503
  if (this.configuration.silent_redirect_uri && this.configuration.silent_login_uri) {
483
504
  var queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
484
505
  window.top.postMessage("".concat(this.configurationName, "_oidc_tokens:").concat(JSON.stringify({ tokens: this.tokens, sessionState: queryParams.session_state })), window.location.origin);
485
506
  }
486
507
  };
487
- Oidc.prototype.silentLoginErrorCallbackFromIFrame = function () {
508
+ Oidc.prototype._silentLoginErrorCallbackFromIFrame = function () {
488
509
  if (this.configuration.silent_redirect_uri && this.configuration.silent_login_uri) {
489
510
  var queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
490
511
  window.top.postMessage("".concat(this.configurationName, "_oidc_error:").concat(JSON.stringify({ error: queryParams.error })), window.location.origin);
491
512
  }
492
513
  };
514
+ Oidc.prototype.silentLoginCallBackAsync = function () {
515
+ return __awaiter(this, void 0, void 0, function () {
516
+ var error_1;
517
+ return __generator(this, function (_a) {
518
+ switch (_a.label) {
519
+ case 0:
520
+ _a.trys.push([0, 2, , 3]);
521
+ return [4 /*yield*/, this.loginCallbackAsync(true)];
522
+ case 1:
523
+ _a.sent();
524
+ this._silentLoginCallbackFromIFrame();
525
+ return [3 /*break*/, 3];
526
+ case 2:
527
+ error_1 = _a.sent();
528
+ console.error(error_1);
529
+ this._silentLoginErrorCallbackFromIFrame();
530
+ return [3 /*break*/, 3];
531
+ case 3: return [2 /*return*/];
532
+ }
533
+ });
534
+ });
535
+ };
493
536
  Oidc.prototype.silentLoginAsync = function (extras, state, scope) {
494
537
  if (extras === void 0) { extras = null; }
495
538
  if (state === void 0) { state = null; }
496
539
  if (scope === void 0) { scope = null; }
497
540
  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;
541
+ var numberTryOnline, configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
499
542
  return __generator(this, function (_c) {
500
543
  switch (_c.label) {
501
544
  case 0:
@@ -511,6 +554,17 @@ var Oidc = /** @class */ (function () {
511
554
  this.publishEvent(eventNames.silentLoginAsync, { message: "wait because document is hidden" });
512
555
  return [3 /*break*/, 1];
513
556
  case 3:
557
+ numberTryOnline = 6;
558
+ _c.label = 4;
559
+ case 4:
560
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 6];
561
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
562
+ case 5:
563
+ _c.sent();
564
+ numberTryOnline--;
565
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
566
+ return [3 /*break*/, 4];
567
+ case 6:
514
568
  try {
515
569
  this.publishEvent(eventNames.silentLoginAsync_begin, {});
516
570
  configuration_1 = this.configuration;
@@ -572,7 +626,7 @@ var Oidc = /** @class */ (function () {
572
626
  self_1.publishEvent(eventNames.silentLoginAsync_error, result);
573
627
  iframe_1.remove();
574
628
  isResolved_1 = true;
575
- reject(result);
629
+ reject(new Error("oidc_" + result.error));
576
630
  }
577
631
  }
578
632
  }
@@ -581,10 +635,10 @@ var Oidc = /** @class */ (function () {
581
635
  var silentSigninTimeout = (_a = configuration_1.silent_login_timeout) !== null && _a !== void 0 ? _a : 12000;
582
636
  setTimeout(function () {
583
637
  if (!isResolved_1) {
584
- self_1.publishEvent(eventNames.silentLoginAsync_error, "timeout");
638
+ self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
585
639
  iframe_1.remove();
586
640
  isResolved_1 = true;
587
- reject("timeout");
641
+ reject(new Error("timeout"));
588
642
  }
589
643
  }, silentSigninTimeout);
590
644
  }
@@ -607,9 +661,9 @@ var Oidc = /** @class */ (function () {
607
661
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
608
662
  var _a;
609
663
  return __awaiter(this, void 0, void 0, function () {
610
- var _b;
611
- return __generator(this, function (_c) {
612
- switch (_c.label) {
664
+ var serviceWorker, storage, initAsyncPromise;
665
+ return __generator(this, function (_b) {
666
+ switch (_b.label) {
613
667
  case 0:
614
668
  if (authorityConfiguration != null) {
615
669
  return [2 /*return*/, new OidcAuthorizationServiceConfiguration({
@@ -621,14 +675,14 @@ var Oidc = /** @class */ (function () {
621
675
  check_session_iframe: authorityConfiguration.check_session_iframe,
622
676
  })];
623
677
  }
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)];
678
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(this.configuration.service_worker_relative_url, this.configurationName)];
629
679
  case 1:
630
- _b.initAsyncPromise = _c.sent();
631
- return [2 /*return*/, this.initAsyncPromise];
680
+ serviceWorker = _b.sent();
681
+ storage = serviceWorker ? window.localStorage : null;
682
+ return [4 /*yield*/, fetchFromIssuer(authority, (_a = this.configuration.authority_time_cache_wellknowurl_in_second) !== null && _a !== void 0 ? _a : 60 * 60, storage)];
683
+ case 2:
684
+ initAsyncPromise = _b.sent();
685
+ return [2 /*return*/, initAsyncPromise];
632
686
  }
633
687
  });
634
688
  });
@@ -673,7 +727,8 @@ var Oidc = /** @class */ (function () {
673
727
  expiresIn: tokens.expires_in,
674
728
  idToken: tokens.id_token,
675
729
  scope: tokens.scope,
676
- tokenType: tokens.token_type
730
+ tokenType: tokens.token_type,
731
+ issuedAt: tokens.issued_at
677
732
  };
678
733
  _a = this;
679
734
  return [4 /*yield*/, setTokensAsync(serviceWorker, reformattedToken)];
@@ -685,8 +740,10 @@ var Oidc = /** @class */ (function () {
685
740
  return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
686
741
  case 6:
687
742
  sessionState = _d.sent();
743
+ // @ts-ignore
688
744
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
689
745
  case 7:
746
+ // @ts-ignore
690
747
  _d.sent();
691
748
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
692
749
  success: true,
@@ -721,8 +778,10 @@ var Oidc = /** @class */ (function () {
721
778
  // @ts-ignore
722
779
  this.timeoutId = autoRenewTokens(this, tokens.refreshToken, this.tokens.expiresAt);
723
780
  sessionState = session.getSessionState();
781
+ // @ts-ignore
724
782
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
725
783
  case 12:
784
+ // @ts-ignore
726
785
  _d.sent();
727
786
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
728
787
  success: true,
@@ -875,10 +934,31 @@ var Oidc = /** @class */ (function () {
875
934
  else {
876
935
  console.debug("SessionMonitor._callback: Different subject signed into OP:", iFrameIdTokenPayload.sub);
877
936
  }
878
- }).catch(function (e) {
879
- _this.publishEvent(eventNames.logout_from_another_tab, {});
880
- _this.destroyAsync();
881
- });
937
+ }).catch(function (e) { return __awaiter(_this, void 0, void 0, function () {
938
+ var _i, _a, _b, key, oidc;
939
+ return __generator(this, function (_c) {
940
+ switch (_c.label) {
941
+ case 0:
942
+ _i = 0, _a = Object.entries(oidcDatabase);
943
+ _c.label = 1;
944
+ case 1:
945
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
946
+ _b = _a[_i], key = _b[0], oidc = _b[1];
947
+ //if(oidc !== this) {
948
+ // @ts-ignore
949
+ return [4 /*yield*/, oidc.logoutOtherTabAsync(idTokenPayload.sub)];
950
+ case 2:
951
+ //if(oidc !== this) {
952
+ // @ts-ignore
953
+ _c.sent();
954
+ _c.label = 3;
955
+ case 3:
956
+ _i++;
957
+ return [3 /*break*/, 1];
958
+ case 4: return [2 /*return*/];
959
+ }
960
+ });
961
+ }); });
882
962
  };
883
963
  _this.checkSessionIFrame = new checkSessionIFrame_1.CheckSessionIFrame(checkSessionCallback, clientId, checkSessionIFrameUri);
884
964
  _this.checkSessionIFrame.load().then(function () {
@@ -1045,6 +1125,7 @@ var Oidc = /** @class */ (function () {
1045
1125
  clearTimeout(timeoutId);
1046
1126
  _this.timeoutId = null;
1047
1127
  var loginParams_1 = getLoginParams(_this.configurationName, redirectUri_1);
1128
+ // @ts-ignore
1048
1129
  _this.startCheckSessionAsync(oidcServerConfiguration_1.check_session_iframe, clientId_1, sessionState_2, isSilentSignin).then(function () {
1049
1130
  _this.publishEvent(eventNames.loginCallbackAsync_end, {});
1050
1131
  resolve({
@@ -1080,18 +1161,19 @@ var Oidc = /** @class */ (function () {
1080
1161
  };
1081
1162
  Oidc.prototype.refreshTokensAsync = function (refreshToken) {
1082
1163
  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;
1164
+ var localsilentLoginAsync, configuration, clientId, redirectUri, authority, extras, _i, _a, _b, key, value, oidcServerConfiguration, details, index, numberTryOnline, tokenResponse, exception_4;
1084
1165
  var _this = this;
1085
1166
  return __generator(this, function (_c) {
1086
1167
  switch (_c.label) {
1087
1168
  case 0:
1088
1169
  localsilentLoginAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1089
- var silent_token_response, exceptionSilent_1;
1170
+ var loginParams, silent_token_response, exceptionSilent_1;
1090
1171
  return __generator(this, function (_a) {
1091
1172
  switch (_a.label) {
1092
1173
  case 0:
1093
1174
  _a.trys.push([0, 2, , 3]);
1094
- return [4 /*yield*/, this.silentLoginAsync()];
1175
+ loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1176
+ return [4 /*yield*/, this.silentLoginAsync(loginParams.extras, loginParams.state)];
1095
1177
  case 1:
1096
1178
  silent_token_response = _a.sent();
1097
1179
  if (silent_token_response) {
@@ -1139,12 +1221,12 @@ var Oidc = /** @class */ (function () {
1139
1221
  index = 0;
1140
1222
  _c.label = 4;
1141
1223
  case 4:
1142
- if (!(index <= 2)) return [3 /*break*/, 15];
1224
+ if (!(index <= 4)) return [3 /*break*/, 18];
1143
1225
  _c.label = 5;
1144
1226
  case 5:
1145
- _c.trys.push([5, 13, , 14]);
1227
+ _c.trys.push([5, 16, , 17]);
1146
1228
  this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1147
- if (!(index > 1)) return [3 /*break*/, 8];
1229
+ if (!(index > 1)) return [3 /*break*/, 11];
1148
1230
  _c.label = 6;
1149
1231
  case 6:
1150
1232
  if (!document.hidden) return [3 /*break*/, 8];
@@ -1153,108 +1235,134 @@ var Oidc = /** @class */ (function () {
1153
1235
  _c.sent();
1154
1236
  this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1155
1237
  return [3 /*break*/, 6];
1156
- case 8: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1238
+ case 8:
1239
+ numberTryOnline = 6;
1240
+ _c.label = 9;
1157
1241
  case 9:
1242
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 11];
1243
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1244
+ case 10:
1245
+ _c.sent();
1246
+ numberTryOnline--;
1247
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1248
+ return [3 /*break*/, 9];
1249
+ case 11: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1250
+ case 12:
1158
1251
  tokenResponse = _c.sent();
1159
- if (!tokenResponse.success) return [3 /*break*/, 10];
1252
+ if (!tokenResponse.success) return [3 /*break*/, 13];
1160
1253
  this.publishEvent(eventNames.refreshTokensAsync_end, { success: tokenResponse.success });
1161
1254
  return [2 /*return*/, tokenResponse.data];
1162
- case 10:
1255
+ case 13:
1163
1256
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "bad request", tokenResponse: tokenResponse });
1164
1257
  return [4 /*yield*/, localsilentLoginAsync()];
1165
- case 11: return [2 /*return*/, _c.sent()];
1166
- case 12: return [3 /*break*/, 14];
1167
- case 13:
1258
+ case 14: return [2 /*return*/, _c.sent()];
1259
+ case 15: return [3 /*break*/, 17];
1260
+ case 16:
1168
1261
  exception_4 = _c.sent();
1169
1262
  console.error(exception_4);
1170
1263
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exception", exception: exception_4.message });
1171
- return [3 /*break*/, 14];
1172
- case 14:
1264
+ return [3 /*break*/, 17];
1265
+ case 17:
1173
1266
  index++;
1174
1267
  return [3 /*break*/, 4];
1175
- case 15: return [2 /*return*/];
1268
+ case 18: return [2 /*return*/];
1176
1269
  }
1177
1270
  });
1178
1271
  });
1179
1272
  };
1180
1273
  Oidc.prototype.syncTokensAsync = function () {
1181
- var _a;
1182
1274
  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;
1275
+ var localSyncTokensAsync;
1276
+ var _this = this;
1277
+ return __generator(this, function (_a) {
1278
+ localSyncTokensAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1279
+ var configuration, oidcServerConfiguration, serviceWorker, isLogin, loginParams, silent_token_response, _a, exceptionSilent_2, session, tokens;
1280
+ var _b;
1281
+ return __generator(this, function (_c) {
1282
+ switch (_c.label) {
1283
+ case 0:
1284
+ configuration = this.configuration;
1285
+ if (!this.tokens) {
1286
+ return [2 /*return*/, null];
1287
+ }
1288
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1289
+ case 1:
1290
+ oidcServerConfiguration = _c.sent();
1291
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1292
+ case 2:
1293
+ serviceWorker = _c.sent();
1294
+ if (!serviceWorker) return [3 /*break*/, 13];
1295
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1296
+ case 3:
1297
+ isLogin = (_c.sent()).isLogin;
1298
+ if (!(isLogin == false)) return [3 /*break*/, 5];
1299
+ this.publishEvent(eventNames.logout_from_another_tab, { "message": "service worker syncTokensAsync" });
1300
+ return [4 /*yield*/, this.destroyAsync()];
1301
+ case 4:
1302
+ _c.sent();
1303
+ return [2 /*return*/, null];
1304
+ case 5:
1305
+ if (!(isLogin == null)) return [3 /*break*/, 12];
1306
+ _c.label = 6;
1307
+ case 6:
1308
+ _c.trys.push([6, 11, , 12]);
1309
+ this.publishEvent(eventNames.syncTokensAsync_begin, {});
1310
+ loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1311
+ return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1312
+ case 7:
1313
+ silent_token_response = _c.sent();
1314
+ if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1315
+ _a = this;
1316
+ return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1317
+ case 8:
1318
+ _a.tokens = _c.sent();
1319
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1320
+ return [2 /*return*/, this.tokens];
1321
+ case 9:
1322
+ this.publishEvent(eventNames.syncTokensAsync_error, { message: "no token found in result" });
1323
+ if (this.timeoutId) {
1324
+ timer_1.default.clearTimeout(this.timeoutId);
1325
+ this.timeoutId = null;
1326
+ }
1327
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1328
+ return [2 /*return*/, null];
1329
+ case 10: return [3 /*break*/, 12];
1330
+ case 11:
1331
+ exceptionSilent_2 = _c.sent();
1332
+ console.error(exceptionSilent_2);
1333
+ this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1334
+ if (this.timeoutId) {
1335
+ timer_1.default.clearTimeout(this.timeoutId);
1336
+ this.timeoutId = null;
1337
+ }
1338
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1339
+ return [2 /*return*/, null];
1340
+ case 12: return [3 /*break*/, 16];
1341
+ case 13:
1342
+ session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_b = configuration.storage) !== null && _b !== void 0 ? _b : sessionStorage);
1343
+ return [4 /*yield*/, session.initAsync()];
1344
+ case 14:
1345
+ tokens = (_c.sent()).tokens;
1346
+ if (!!tokens) return [3 /*break*/, 16];
1347
+ this.publishEvent(eventNames.logout_from_another_tab, { "message": "session syncTokensAsync" });
1348
+ return [4 /*yield*/, this.destroyAsync()];
1349
+ case 15:
1350
+ _c.sent();
1351
+ return [2 /*return*/, null];
1352
+ case 16: return [2 /*return*/, this.tokens];
1238
1353
  }
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*/];
1354
+ });
1355
+ }); };
1356
+ if (this.syncTokensAsyncPromise) {
1357
+ return [2 /*return*/, this.syncTokensAsyncPromise];
1257
1358
  }
1359
+ this.syncTokensAsyncPromise = localSyncTokensAsync().then(function (result) {
1360
+ if (_this.syncTokensAsyncPromise) {
1361
+ _this.syncTokensAsyncPromise = null;
1362
+ }
1363
+ return result;
1364
+ });
1365
+ return [2 /*return*/, this.syncTokensAsyncPromise];
1258
1366
  });
1259
1367
  });
1260
1368
  };
@@ -1296,24 +1404,56 @@ var Oidc = /** @class */ (function () {
1296
1404
  case 4:
1297
1405
  this.tokens = null;
1298
1406
  this.userInfo = null;
1299
- this.events = [];
1300
1407
  return [2 /*return*/];
1301
1408
  }
1302
1409
  });
1303
1410
  });
1304
1411
  };
1412
+ Oidc.prototype.logoutSameTabAsync = function (sub) {
1413
+ return __awaiter(this, void 0, void 0, function () {
1414
+ return __generator(this, function (_a) {
1415
+ switch (_a.label) {
1416
+ case 0:
1417
+ if (!(this.configuration.monitor_session && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1418
+ this.publishEvent(eventNames.logout_from_same_tab, { "message": sub });
1419
+ return [4 /*yield*/, this.destroyAsync()];
1420
+ case 1:
1421
+ _a.sent();
1422
+ _a.label = 2;
1423
+ case 2: return [2 /*return*/];
1424
+ }
1425
+ });
1426
+ });
1427
+ };
1428
+ Oidc.prototype.logoutOtherTabAsync = function (sub) {
1429
+ return __awaiter(this, void 0, void 0, function () {
1430
+ return __generator(this, function (_a) {
1431
+ switch (_a.label) {
1432
+ case 0:
1433
+ if (!(this.configuration.monitor_session && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1434
+ return [4 /*yield*/, this.destroyAsync()];
1435
+ case 1:
1436
+ _a.sent();
1437
+ console.log("logoutOtherTabAsync(sub)" + this.configurationName);
1438
+ this.publishEvent(eventNames.logout_from_another_tab, { message: "SessionMonitor", "sub": sub });
1439
+ _a.label = 2;
1440
+ case 2: return [2 /*return*/];
1441
+ }
1442
+ });
1443
+ });
1444
+ };
1305
1445
  Oidc.prototype.logoutAsync = function (callbackPathOrUrl, extras) {
1306
1446
  if (callbackPathOrUrl === void 0) { callbackPathOrUrl = undefined; }
1307
1447
  if (extras === void 0) { extras = null; }
1308
1448
  return __awaiter(this, void 0, void 0, function () {
1309
- var configuration, oidcServerConfiguration, path, isUri, url, idToken, extraQueryString, _i, _a, _b, key, value;
1310
- return __generator(this, function (_c) {
1311
- switch (_c.label) {
1449
+ var configuration, oidcServerConfiguration, path, isUri, url, idToken, sub, _i, _a, _b, key, oidc, extraQueryString, _c, _d, _e, key, value;
1450
+ return __generator(this, function (_f) {
1451
+ switch (_f.label) {
1312
1452
  case 0:
1313
1453
  configuration = this.configuration;
1314
1454
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1315
1455
  case 1:
1316
- oidcServerConfiguration = _c.sent();
1456
+ oidcServerConfiguration = _f.sent();
1317
1457
  if (callbackPathOrUrl && (typeof callbackPathOrUrl !== 'string')) {
1318
1458
  callbackPathOrUrl = undefined;
1319
1459
  console.warn('callbackPathOrUrl path is not a string');
@@ -1325,14 +1465,31 @@ var Oidc = /** @class */ (function () {
1325
1465
  }
1326
1466
  url = isUri ? callbackPathOrUrl : window.location.origin + path;
1327
1467
  idToken = this.tokens ? this.tokens.idToken : "";
1468
+ sub = this.tokens && this.tokens.idTokenPayload ? this.tokens.idTokenPayload.sub : null;
1328
1469
  return [4 /*yield*/, this.destroyAsync()];
1329
1470
  case 2:
1330
- _c.sent();
1471
+ _f.sent();
1472
+ _i = 0, _a = Object.entries(oidcDatabase);
1473
+ _f.label = 3;
1474
+ case 3:
1475
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
1476
+ _b = _a[_i], key = _b[0], oidc = _b[1];
1477
+ if (!(oidc !== this)) return [3 /*break*/, 5];
1478
+ // @ts-ignore
1479
+ return [4 /*yield*/, oidc.logoutSameTabAsync(sub)];
1480
+ case 4:
1481
+ // @ts-ignore
1482
+ _f.sent();
1483
+ _f.label = 5;
1484
+ case 5:
1485
+ _i++;
1486
+ return [3 /*break*/, 3];
1487
+ case 6:
1331
1488
  if (oidcServerConfiguration.endSessionEndpoint) {
1332
1489
  extraQueryString = "";
1333
1490
  if (extras) {
1334
- for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
1335
- _b = _a[_i], key = _b[0], value = _b[1];
1491
+ for (_c = 0, _d = Object.entries(extras); _c < _d.length; _c++) {
1492
+ _e = _d[_c], key = _e[0], value = _e[1];
1336
1493
  extraQueryString += "&".concat(key, "=").concat(encodeURIComponent(value));
1337
1494
  }
1338
1495
  }