@axa-fr/react-oidc 6.0.0-beta11 → 6.0.0-beta14

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.
@@ -74,8 +74,9 @@ var initSession_1 = require("./initSession");
74
74
  var timer_1 = __importDefault(require("./timer"));
75
75
  var checkSessionIFrame_1 = require("./checkSessionIFrame");
76
76
  var route_utils_1 = require("./route-utils");
77
+ var parseTokens_1 = require("./parseTokens");
77
78
  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, tokens, currentTimeUnixSecond;
79
+ var _i, _a, _b, key, value, formBody, property, encodedKey, encodedValue, formBodyString, response, tokens;
79
80
  return __generator(this, function (_c) {
80
81
  switch (_c.label) {
81
82
  case 0:
@@ -107,20 +108,9 @@ var performTokenRequestAsync = function (url, details, extras) { return __awaite
107
108
  return [4 /*yield*/, response.json()];
108
109
  case 2:
109
110
  tokens = _c.sent();
110
- if (!tokens.issued_at) {
111
- currentTimeUnixSecond = new Date().getTime() / 1000;
112
- tokens.issued_at = currentTimeUnixSecond;
113
- }
111
+ console.log(tokens);
114
112
  return [2 /*return*/, { success: true,
115
- data: {
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
123
- }
113
+ data: (0, parseTokens_1.parseOriginalTokens)(tokens)
124
114
  }];
125
115
  }
126
116
  });
@@ -171,33 +161,6 @@ var OidcAuthorizationServiceConfiguration = /** @class */ (function (_super) {
171
161
  return OidcAuthorizationServiceConfiguration;
172
162
  }(appauth_1.AuthorizationServiceConfiguration));
173
163
  exports.OidcAuthorizationServiceConfiguration = OidcAuthorizationServiceConfiguration;
174
- var idTokenPayload = function (token) {
175
- var base64Url = token.split('.')[1];
176
- var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
177
- var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
178
- return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
179
- }).join(''));
180
- return JSON.parse(jsonPayload);
181
- };
182
- var countLetter = function (str, find) {
183
- return (str.split(find)).length - 1;
184
- };
185
- var extractAccessTokenPayload = function (tokens) {
186
- if (tokens.accessTokenPayload) {
187
- return tokens.accessTokenPayload;
188
- }
189
- var accessToken = tokens.accessToken;
190
- try {
191
- if (!accessToken || countLetter(accessToken, '.') != 2) {
192
- return null;
193
- }
194
- return JSON.parse(atob(accessToken.split('.')[1]));
195
- }
196
- catch (e) {
197
- console.warn(e);
198
- }
199
- return null;
200
- };
201
164
  var oidcDatabase = {};
202
165
  var oidcFactory = function (configuration, name) {
203
166
  if (name === void 0) { name = "default"; }
@@ -220,50 +183,36 @@ var loginCallbackWithAutoTokensRenewAsync = function (oidc) { return __awaiter(v
220
183
  });
221
184
  }); };
222
185
  var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
223
- var _a;
224
- var refreshTimeBeforeTokensExpirationInSecond = (_a = oidc.configuration.refresh_time_before_tokens_expiration_in_second) !== null && _a !== void 0 ? _a : 60;
186
+ var refreshTimeBeforeTokensExpirationInSecond = oidc.configuration.refresh_time_before_tokens_expiration_in_second;
225
187
  return timer_1.default.setTimeout(function () { return __awaiter(void 0, void 0, void 0, function () {
226
- var currentTimeUnixSecond, timeInfo, tokens, _a, tokens;
227
- return __generator(this, function (_b) {
228
- switch (_b.label) {
188
+ var timeLeft, timeInfo, tokens;
189
+ return __generator(this, function (_a) {
190
+ switch (_a.label) {
229
191
  case 0:
230
- currentTimeUnixSecond = new Date().getTime() / 1000;
231
- timeInfo = { timeLeft: Math.round(((expiresAt - refreshTimeBeforeTokensExpirationInSecond) - currentTimeUnixSecond)) };
192
+ timeLeft = (0, parseTokens_1.computeTimeLeft)(refreshTimeBeforeTokensExpirationInSecond, expiresAt);
193
+ timeInfo = { timeLeft: timeLeft };
232
194
  oidc.publishEvent(Oidc.eventNames.token_timer, timeInfo);
233
- if (!(currentTimeUnixSecond > (expiresAt - refreshTimeBeforeTokensExpirationInSecond))) return [3 /*break*/, 5];
234
- return [4 /*yield*/, oidc.refreshTokensAsync(refreshToken)];
195
+ return [4 /*yield*/, oidc.synchroniseTokensAsync(refreshToken)];
235
196
  case 1:
236
- tokens = _b.sent();
237
- _a = oidc;
238
- return [4 /*yield*/, setTokensAsync(oidc.serviceWorker, tokens)];
239
- case 2:
240
- _a.tokens = _b.sent();
241
- if (!!oidc.serviceWorker) return [3 /*break*/, 4];
197
+ tokens = _a.sent();
198
+ oidc.tokens = tokens;
199
+ if (!!oidc.serviceWorker) return [3 /*break*/, 3];
242
200
  return [4 /*yield*/, oidc.session.setTokens(oidc.tokens)];
201
+ case 2:
202
+ _a.sent();
203
+ _a.label = 3;
243
204
  case 3:
244
- _b.sent();
245
- _b.label = 4;
205
+ if (!!oidc.tokens) return [3 /*break*/, 5];
206
+ return [4 /*yield*/, oidc.destroyAsync()];
246
207
  case 4:
247
- if (!oidc.tokens) {
248
- if (oidc.checkSessionIFrame) {
249
- oidc.checkSessionIFrame.stop();
250
- oidc.checkSessionIFrame = null;
251
- }
252
- return [2 /*return*/];
253
- }
254
- oidc.publishEvent(Oidc.eventNames.token_renewed, {});
208
+ _a.sent();
209
+ oidc.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token" });
210
+ return [2 /*return*/];
211
+ case 5:
255
212
  if (oidc.timeoutId) {
256
213
  oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
257
214
  }
258
- return [3 /*break*/, 7];
259
- case 5: return [4 /*yield*/, oidc.syncTokensAsync()];
260
- case 6:
261
- tokens = _b.sent();
262
- if (tokens && oidc.timeoutId) {
263
- oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, tokens.expiresAt);
264
- }
265
- _b.label = 7;
266
- case 7: return [2 /*return*/];
215
+ return [2 /*return*/];
267
216
  }
268
217
  });
269
218
  }); }, 1000);
@@ -285,15 +234,19 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
285
234
  if (!oidc.tokens) {
286
235
  return [2 /*return*/, null];
287
236
  }
288
- if (!oidc.syncTokensAsyncPromise) return [3 /*break*/, 2];
289
- return [4 /*yield*/, oidc.syncTokensAsyncPromise];
237
+ accessToken = oidc.tokens.accessToken;
238
+ if (!accessToken) {
239
+ return [2 /*return*/, null];
240
+ }
241
+ _a.label = 1;
290
242
  case 1:
291
- _a.sent();
292
- _a.label = 2;
243
+ if (!(oidc.tokens && !(0, parseTokens_1.isTokensValid)(oidc.tokens))) return [3 /*break*/, 3];
244
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(200)];
293
245
  case 2:
294
- accessToken = oidc.tokens.accessToken;
295
- return [4 /*yield*/, oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration)];
296
- case 3:
246
+ _a.sent();
247
+ return [3 /*break*/, 1];
248
+ case 3: return [4 /*yield*/, oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration)];
249
+ case 4:
297
250
  oidcServerConfiguration = _a.sent();
298
251
  url = oidcServerConfiguration.userInfoEndpoint;
299
252
  fetchUserInfo = function (accessToken) { return __awaiter(void 0, void 0, void 0, function () {
@@ -316,43 +269,13 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
316
269
  });
317
270
  }); };
318
271
  return [4 /*yield*/, fetchUserInfo(accessToken)];
319
- case 4:
272
+ case 5:
320
273
  userInfo = _a.sent();
321
274
  oidc.userInfo = userInfo;
322
275
  return [2 /*return*/, userInfo];
323
276
  }
324
277
  });
325
278
  }); };
326
- var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0, void 0, void 0, function () {
327
- var accessTokenPayload, _idTokenPayload, idTokenExipreAt, accessTokenExpiresAt, expiresAt;
328
- return __generator(this, function (_a) {
329
- switch (_a.label) {
330
- case 0:
331
- if (!(tokens == null)) return [3 /*break*/, 3];
332
- if (!serviceWorker) return [3 /*break*/, 2];
333
- return [4 /*yield*/, serviceWorker.clearAsync()];
334
- case 1:
335
- _a.sent();
336
- _a.label = 2;
337
- case 2: return [2 /*return*/, null];
338
- case 3:
339
- if (!serviceWorker) return [3 /*break*/, 5];
340
- return [4 /*yield*/, serviceWorker.getAccessTokenPayloadAsync()];
341
- case 4:
342
- accessTokenPayload = _a.sent();
343
- return [3 /*break*/, 6];
344
- case 5:
345
- accessTokenPayload = extractAccessTokenPayload(tokens);
346
- _a.label = 6;
347
- case 6:
348
- _idTokenPayload = idTokenPayload(tokens.idToken);
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;
352
- return [2 /*return*/, __assign(__assign({}, tokens), { idTokenPayload: _idTokenPayload, accessTokenPayload: accessTokenPayload, expiresAt: expiresAt })];
353
- }
354
- });
355
- }); };
356
279
  var eventNames = {
357
280
  service_worker_not_supported_by_browser: "service_worker_not_supported_by_browser",
358
281
  token_aquired: "token_aquired",
@@ -444,16 +367,16 @@ var buildQueries = function (extras) {
444
367
  var Oidc = /** @class */ (function () {
445
368
  function Oidc(configuration, configurationName) {
446
369
  if (configurationName === void 0) { configurationName = "default"; }
370
+ var _a, _b, _c;
447
371
  this.tryKeepExistingSessionPromise = null;
448
372
  this.loginPromise = null;
449
373
  this.loginCallbackPromise = null;
450
- this.syncTokensAsyncPromise = null;
451
374
  this.loginCallbackWithAutoTokensRenewPromise = null;
452
375
  var silent_login_uri = configuration.silent_login_uri;
453
376
  if (configuration.silent_redirect_uri && !configuration.silent_login_uri) {
454
377
  silent_login_uri = "".concat(configuration.silent_redirect_uri.replace("-callback", "").replace("callback", ""), "-login");
455
378
  }
456
- this.configuration = __assign(__assign({}, configuration), { silent_login_uri: silent_login_uri });
379
+ this.configuration = __assign(__assign({}, configuration), { silent_login_uri: silent_login_uri, monitor_session: (_a = configuration.monitor_session) !== null && _a !== void 0 ? _a : true, refresh_time_before_tokens_expiration_in_second: (_b = configuration.refresh_time_before_tokens_expiration_in_second) !== null && _b !== void 0 ? _b : 60, silent_login_timeout: (_c = configuration.silent_login_timeout) !== null && _c !== void 0 ? _c : 12000 });
457
380
  this.configurationName = configurationName;
458
381
  this.tokens = null;
459
382
  this.userInfo = null;
@@ -461,7 +384,7 @@ var Oidc = /** @class */ (function () {
461
384
  this.timeoutId = null;
462
385
  this.serviceWorker = null;
463
386
  this.session = null;
464
- this.refreshTokensAsync.bind(this);
387
+ this.synchroniseTokensAsync.bind(this);
465
388
  this.loginCallbackWithAutoTokensRenewAsync.bind(this);
466
389
  this.initAsync.bind(this);
467
390
  this.loginCallbackAsync.bind(this);
@@ -538,130 +461,106 @@ var Oidc = /** @class */ (function () {
538
461
  if (state === void 0) { state = null; }
539
462
  if (scope === void 0) { scope = null; }
540
463
  return __awaiter(this, void 0, void 0, function () {
541
- var numberTryOnline, configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
464
+ var configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
542
465
  return __generator(this, function (_c) {
543
- switch (_c.label) {
544
- case 0:
545
- if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
546
- return [2 /*return*/, Promise.resolve(null)];
466
+ if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
467
+ return [2 /*return*/, Promise.resolve(null)];
468
+ }
469
+ try {
470
+ this.publishEvent(eventNames.silentLoginAsync_begin, {});
471
+ configuration_1 = this.configuration;
472
+ queries = "";
473
+ if (state) {
474
+ if (extras == null) {
475
+ extras = {};
547
476
  }
548
- _c.label = 1;
549
- case 1:
550
- if (!document.hidden) return [3 /*break*/, 3];
551
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
552
- case 2:
553
- _c.sent();
554
- this.publishEvent(eventNames.silentLoginAsync, { message: "wait because document is hidden" });
555
- return [3 /*break*/, 1];
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:
568
- try {
569
- this.publishEvent(eventNames.silentLoginAsync_begin, {});
570
- configuration_1 = this.configuration;
571
- queries = "";
572
- if (state) {
573
- if (extras == null) {
574
- extras = {};
575
- }
576
- extras.state = state;
577
- }
578
- if (scope) {
579
- if (extras == null) {
580
- extras = {};
581
- }
582
- extras.scope = scope;
477
+ extras.state = state;
478
+ }
479
+ if (scope) {
480
+ if (extras == null) {
481
+ extras = {};
482
+ }
483
+ extras.scope = scope;
484
+ }
485
+ if (extras != null) {
486
+ for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
487
+ _b = _a[_i], key = _b[0], value = _b[1];
488
+ if (queries === "") {
489
+ queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
583
490
  }
584
- if (extras != null) {
585
- for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
586
- _b = _a[_i], key = _b[0], value = _b[1];
587
- if (queries === "") {
588
- queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
589
- }
590
- else {
591
- queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
592
- }
593
- }
491
+ else {
492
+ queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
594
493
  }
595
- link = configuration_1.silent_login_uri + queries;
596
- idx = link.indexOf("/", link.indexOf("//") + 2);
597
- iFrameOrigin_1 = link.substr(0, idx);
598
- iframe_1 = document.createElement('iframe');
599
- iframe_1.width = "0px";
600
- iframe_1.height = "0px";
601
- iframe_1.id = "".concat(this.configurationName, "_oidc_iframe");
602
- iframe_1.setAttribute("src", link);
603
- document.body.appendChild(iframe_1);
604
- self_1 = this;
605
- return [2 /*return*/, new Promise(function (resolve, reject) {
606
- var _a;
607
- try {
608
- var isResolved_1 = false;
609
- window.onmessage = function (e) {
610
- if (e.origin === iFrameOrigin_1 &&
611
- e.source === iframe_1.contentWindow) {
612
- var key = "".concat(self_1.configurationName, "_oidc_tokens:");
613
- var key_error = "".concat(self_1.configurationName, "_oidc_error:");
614
- var data = e.data;
615
- if (data && typeof (data) === "string") {
616
- if (!isResolved_1) {
617
- if (data.startsWith(key)) {
618
- var result = JSON.parse(e.data.replace(key, ''));
619
- self_1.publishEvent(eventNames.silentLoginAsync_end, {});
620
- iframe_1.remove();
621
- isResolved_1 = true;
622
- resolve(result);
623
- }
624
- else if (data.startsWith(key_error)) {
625
- var result = JSON.parse(e.data.replace(key_error, ''));
626
- self_1.publishEvent(eventNames.silentLoginAsync_error, result);
627
- iframe_1.remove();
628
- isResolved_1 = true;
629
- reject(new Error("oidc_" + result.error));
630
- }
631
- }
632
- }
633
- }
634
- };
635
- var silentSigninTimeout = (_a = configuration_1.silent_login_timeout) !== null && _a !== void 0 ? _a : 12000;
636
- setTimeout(function () {
494
+ }
495
+ }
496
+ link = configuration_1.silent_login_uri + queries;
497
+ idx = link.indexOf("/", link.indexOf("//") + 2);
498
+ iFrameOrigin_1 = link.substr(0, idx);
499
+ iframe_1 = document.createElement('iframe');
500
+ iframe_1.width = "0px";
501
+ iframe_1.height = "0px";
502
+ iframe_1.id = "".concat(this.configurationName, "_oidc_iframe");
503
+ iframe_1.setAttribute("src", link);
504
+ document.body.appendChild(iframe_1);
505
+ self_1 = this;
506
+ return [2 /*return*/, new Promise(function (resolve, reject) {
507
+ try {
508
+ var isResolved_1 = false;
509
+ window.onmessage = function (e) {
510
+ if (e.origin === iFrameOrigin_1 &&
511
+ e.source === iframe_1.contentWindow) {
512
+ var key = "".concat(self_1.configurationName, "_oidc_tokens:");
513
+ var key_error = "".concat(self_1.configurationName, "_oidc_error:");
514
+ var data = e.data;
515
+ if (data && typeof (data) === "string") {
637
516
  if (!isResolved_1) {
638
- self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
639
- iframe_1.remove();
640
- isResolved_1 = true;
641
- reject(new Error("timeout"));
517
+ if (data.startsWith(key)) {
518
+ var result = JSON.parse(e.data.replace(key, ''));
519
+ self_1.publishEvent(eventNames.silentLoginAsync_end, {});
520
+ iframe_1.remove();
521
+ isResolved_1 = true;
522
+ resolve(result);
523
+ }
524
+ else if (data.startsWith(key_error)) {
525
+ var result = JSON.parse(e.data.replace(key_error, ''));
526
+ self_1.publishEvent(eventNames.silentLoginAsync_error, result);
527
+ iframe_1.remove();
528
+ isResolved_1 = true;
529
+ reject(new Error("oidc_" + result.error));
530
+ }
642
531
  }
643
- }, silentSigninTimeout);
532
+ }
644
533
  }
645
- catch (e) {
534
+ };
535
+ var silentSigninTimeout = configuration_1.silent_login_timeout;
536
+ setTimeout(function () {
537
+ if (!isResolved_1) {
538
+ self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
646
539
  iframe_1.remove();
647
- self_1.publishEvent(eventNames.silentLoginAsync_error, e);
648
- reject(e);
540
+ isResolved_1 = true;
541
+ reject(new Error("timeout"));
649
542
  }
650
- })];
651
- }
652
- catch (e) {
653
- this.publishEvent(eventNames.silentLoginAsync_error, e);
654
- throw e;
655
- }
656
- return [2 /*return*/];
543
+ }, silentSigninTimeout);
544
+ }
545
+ catch (e) {
546
+ iframe_1.remove();
547
+ self_1.publishEvent(eventNames.silentLoginAsync_error, e);
548
+ reject(e);
549
+ }
550
+ })];
551
+ }
552
+ catch (e) {
553
+ this.publishEvent(eventNames.silentLoginAsync_error, e);
554
+ throw e;
657
555
  }
556
+ return [2 /*return*/];
658
557
  });
659
558
  });
660
559
  };
661
560
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
662
561
  var _a;
663
562
  return __awaiter(this, void 0, void 0, function () {
664
- var serviceWorker, storage, initAsyncPromise;
563
+ var serviceWorker, storage;
665
564
  return __generator(this, function (_b) {
666
565
  switch (_b.label) {
667
566
  case 0:
@@ -680,9 +579,7 @@ var Oidc = /** @class */ (function () {
680
579
  serviceWorker = _b.sent();
681
580
  storage = serviceWorker ? window.localStorage : null;
682
581
  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];
582
+ case 2: return [2 /*return*/, _b.sent()];
686
583
  }
687
584
  });
688
585
  });
@@ -696,83 +593,68 @@ var Oidc = /** @class */ (function () {
696
593
  return [2 /*return*/, this.tryKeepExistingSessionPromise];
697
594
  }
698
595
  funcAsync = function () { return __awaiter(_this, void 0, void 0, function () {
699
- var serviceWorker, configuration, oidcServerConfiguration, tokens, reformattedToken, _a, sessionState, session, tokens, _b, sessionState, exception_1;
700
- var _c;
701
- return __generator(this, function (_d) {
702
- switch (_d.label) {
596
+ var serviceWorker, configuration, oidcServerConfiguration, tokens, sessionState, session, tokens, sessionState, exception_1;
597
+ var _a;
598
+ return __generator(this, function (_b) {
599
+ switch (_b.label) {
703
600
  case 0:
704
601
  if (this.tokens != null) {
705
602
  return [2 /*return*/, false];
706
603
  }
707
604
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_begin, {});
708
- _d.label = 1;
605
+ _b.label = 1;
709
606
  case 1:
710
- _d.trys.push([1, 14, , 17]);
607
+ _b.trys.push([1, 12, , 15]);
711
608
  configuration = this.configuration;
712
609
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
713
610
  case 2:
714
- oidcServerConfiguration = _d.sent();
611
+ oidcServerConfiguration = _b.sent();
715
612
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
716
613
  case 3:
717
- serviceWorker = _d.sent();
718
- if (!serviceWorker) return [3 /*break*/, 9];
614
+ serviceWorker = _b.sent();
615
+ if (!serviceWorker) return [3 /*break*/, 8];
719
616
  return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync")];
720
617
  case 4:
721
- tokens = (_d.sent()).tokens;
722
- if (!tokens) return [3 /*break*/, 8];
618
+ tokens = (_b.sent()).tokens;
619
+ if (!tokens) return [3 /*break*/, 7];
723
620
  serviceWorker.startKeepAliveServiceWorker();
724
- reformattedToken = {
725
- accessToken: tokens.access_token,
726
- refreshToken: tokens.refresh_token,
727
- expiresIn: tokens.expires_in,
728
- idToken: tokens.id_token,
729
- scope: tokens.scope,
730
- tokenType: tokens.token_type,
731
- issuedAt: tokens.issued_at
732
- };
733
- _a = this;
734
- return [4 /*yield*/, setTokensAsync(serviceWorker, reformattedToken)];
735
- case 5:
736
- _a.tokens = _d.sent();
621
+ // @ts-ignore
622
+ this.tokens = tokens;
737
623
  this.serviceWorker = serviceWorker;
738
624
  // @ts-ignore
739
625
  this.timeoutId = autoRenewTokens(this, this.tokens.refreshToken, this.tokens.expiresAt);
740
626
  return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
741
- case 6:
742
- sessionState = _d.sent();
627
+ case 5:
628
+ sessionState = _b.sent();
743
629
  // @ts-ignore
744
630
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
745
- case 7:
631
+ case 6:
746
632
  // @ts-ignore
747
- _d.sent();
633
+ _b.sent();
748
634
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
749
635
  success: true,
750
636
  message: "tokens inside ServiceWorker are valid"
751
637
  });
752
638
  return [2 /*return*/, true];
753
- case 8:
639
+ case 7:
754
640
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
755
641
  success: false,
756
642
  message: "no exiting session found"
757
643
  });
758
- return [3 /*break*/, 13];
759
- case 9:
644
+ return [3 /*break*/, 11];
645
+ case 8:
760
646
  if (configuration.service_worker_relative_url) {
761
647
  this.publishEvent(eventNames.service_worker_not_supported_by_browser, {
762
648
  message: "service worker is not supported by this browser"
763
649
  });
764
650
  }
765
- session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_c = configuration.storage) !== null && _c !== void 0 ? _c : sessionStorage);
651
+ session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
766
652
  return [4 /*yield*/, session.initAsync()];
767
- case 10:
768
- tokens = (_d.sent()).tokens;
769
- if (!tokens) return [3 /*break*/, 13];
770
- // @ts-ignore
771
- _b = this;
772
- return [4 /*yield*/, setTokensAsync(serviceWorker, tokens)];
773
- case 11:
653
+ case 9:
654
+ tokens = (_b.sent()).tokens;
655
+ if (!tokens) return [3 /*break*/, 11];
774
656
  // @ts-ignore
775
- _b.tokens = _d.sent();
657
+ this.tokens = (0, parseTokens_1.setTokens)(tokens);
776
658
  //session.setTokens(this.tokens);
777
659
  this.session = session;
778
660
  // @ts-ignore
@@ -780,32 +662,32 @@ var Oidc = /** @class */ (function () {
780
662
  sessionState = session.getSessionState();
781
663
  // @ts-ignore
782
664
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
783
- case 12:
665
+ case 10:
784
666
  // @ts-ignore
785
- _d.sent();
667
+ _b.sent();
786
668
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
787
669
  success: true,
788
670
  message: "tokens inside storage are valid"
789
671
  });
790
672
  return [2 /*return*/, true];
791
- case 13:
673
+ case 11:
792
674
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
793
675
  success: false,
794
676
  message: serviceWorker ? "service worker sessions not retrieved" : "session storage sessions not retrieved"
795
677
  });
796
678
  return [2 /*return*/, false];
797
- case 14:
798
- exception_1 = _d.sent();
679
+ case 12:
680
+ exception_1 = _b.sent();
799
681
  console.error(exception_1);
800
- if (!serviceWorker) return [3 /*break*/, 16];
682
+ if (!serviceWorker) return [3 /*break*/, 14];
801
683
  return [4 /*yield*/, serviceWorker.clearAsync()];
802
- case 15:
803
- _d.sent();
804
- _d.label = 16;
805
- case 16:
684
+ case 13:
685
+ _b.sent();
686
+ _b.label = 14;
687
+ case 14:
806
688
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_error, "tokens inside ServiceWorker are invalid");
807
689
  return [2 /*return*/, false];
808
- case 17: return [2 /*return*/];
690
+ case 15: return [2 /*return*/];
809
691
  }
810
692
  });
811
693
  }); };
@@ -817,10 +699,9 @@ var Oidc = /** @class */ (function () {
817
699
  });
818
700
  });
819
701
  };
820
- Oidc.prototype.loginAsync = function (callbackPath, extras, installServiceWorker, state, isSilentSignin, scope) {
702
+ Oidc.prototype.loginAsync = function (callbackPath, extras, state, isSilentSignin, scope) {
821
703
  if (callbackPath === void 0) { callbackPath = undefined; }
822
704
  if (extras === void 0) { extras = null; }
823
- if (installServiceWorker === void 0) { installServiceWorker = true; }
824
705
  if (state === void 0) { state = undefined; }
825
706
  if (isSilentSignin === void 0) { isSilentSignin = false; }
826
707
  if (scope === void 0) { scope = undefined; }
@@ -934,10 +815,31 @@ var Oidc = /** @class */ (function () {
934
815
  else {
935
816
  console.debug("SessionMonitor._callback: Different subject signed into OP:", iFrameIdTokenPayload.sub);
936
817
  }
937
- }).catch(function (e) {
938
- _this.publishEvent(eventNames.logout_from_another_tab, { message: "SessionMonitor" });
939
- _this.destroyAsync();
940
- });
818
+ }).catch(function (e) { return __awaiter(_this, void 0, void 0, function () {
819
+ var _i, _a, _b, key, oidc;
820
+ return __generator(this, function (_c) {
821
+ switch (_c.label) {
822
+ case 0:
823
+ _i = 0, _a = Object.entries(oidcDatabase);
824
+ _c.label = 1;
825
+ case 1:
826
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
827
+ _b = _a[_i], key = _b[0], oidc = _b[1];
828
+ //if(oidc !== this) {
829
+ // @ts-ignore
830
+ return [4 /*yield*/, oidc.logoutOtherTabAsync(idTokenPayload.sub)];
831
+ case 2:
832
+ //if(oidc !== this) {
833
+ // @ts-ignore
834
+ _c.sent();
835
+ _c.label = 3;
836
+ case 3:
837
+ _i++;
838
+ return [3 /*break*/, 1];
839
+ case 4: return [2 /*return*/];
840
+ }
841
+ });
842
+ }); });
941
843
  };
942
844
  _this.checkSessionIFrame = new checkSessionIFrame_1.CheckSessionIFrame(checkSessionCallback, clientId, checkSessionIFrameUri);
943
845
  _this.checkSessionIFrame.load().then(function () {
@@ -971,16 +873,14 @@ var Oidc = /** @class */ (function () {
971
873
  case 1:
972
874
  response = _a.sent();
973
875
  tokens = response.tokens;
974
- return [4 /*yield*/, setTokensAsync(this.serviceWorker, tokens)];
975
- case 2:
976
- parsedTokens = _a.sent();
876
+ parsedTokens = (0, parseTokens_1.setTokens)(tokens);
977
877
  this.tokens = parsedTokens;
978
- if (!!this.serviceWorker) return [3 /*break*/, 4];
878
+ if (!!this.serviceWorker) return [3 /*break*/, 3];
979
879
  return [4 /*yield*/, this.session.setTokens(parsedTokens)];
980
- case 3:
880
+ case 2:
981
881
  _a.sent();
982
- _a.label = 4;
983
- case 4:
882
+ _a.label = 3;
883
+ case 3:
984
884
  this.publishEvent(Oidc.eventNames.token_aquired, parsedTokens);
985
885
  // @ts-ignore
986
886
  return [2 /*return*/, { parsedTokens: parsedTokens, state: response.state, callbackPath: response.callbackPath }];
@@ -1138,13 +1038,34 @@ var Oidc = /** @class */ (function () {
1138
1038
  });
1139
1039
  });
1140
1040
  };
1141
- Oidc.prototype.refreshTokensAsync = function (refreshToken) {
1041
+ Oidc.prototype.synchroniseTokensAsync = function (refreshToken, index) {
1042
+ if (index === void 0) { index = 0; }
1142
1043
  return __awaiter(this, void 0, void 0, function () {
1143
- var localsilentLoginAsync, configuration, clientId, redirectUri, authority, extras, _i, _a, _b, key, value, oidcServerConfiguration, details, index, numberTryOnline, tokenResponse, exception_4;
1044
+ var numberTryOnline, configuration, localsilentLoginAsync, _a, status_1, tokens, _b, clientId, redirectUri, authority, extras, _i, _c, _d, key, value, details, oidcServerConfiguration, tokenResponse, exception_4;
1144
1045
  var _this = this;
1145
- return __generator(this, function (_c) {
1146
- switch (_c.label) {
1046
+ return __generator(this, function (_e) {
1047
+ switch (_e.label) {
1147
1048
  case 0:
1049
+ if (!document.hidden) return [3 /*break*/, 3];
1050
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1051
+ case 1:
1052
+ _e.sent();
1053
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1054
+ return [4 /*yield*/, this.synchroniseTokensAsync(refreshToken, index)];
1055
+ case 2: return [2 /*return*/, _e.sent()];
1056
+ case 3:
1057
+ numberTryOnline = 6;
1058
+ _e.label = 4;
1059
+ case 4:
1060
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 6];
1061
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1062
+ case 5:
1063
+ _e.sent();
1064
+ numberTryOnline--;
1065
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1066
+ return [3 /*break*/, 4];
1067
+ case 6:
1068
+ configuration = this.configuration;
1148
1069
  localsilentLoginAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1149
1070
  var loginParams, silent_token_response, exceptionSilent_1;
1150
1071
  return __generator(this, function (_a) {
@@ -1152,196 +1073,152 @@ var Oidc = /** @class */ (function () {
1152
1073
  case 0:
1153
1074
  _a.trys.push([0, 2, , 3]);
1154
1075
  loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1155
- return [4 /*yield*/, this.silentLoginAsync(loginParams.extras, loginParams.state)];
1076
+ return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1156
1077
  case 1:
1157
1078
  silent_token_response = _a.sent();
1158
1079
  if (silent_token_response) {
1080
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1159
1081
  return [2 /*return*/, silent_token_response.tokens];
1160
1082
  }
1161
1083
  return [3 /*break*/, 3];
1162
1084
  case 2:
1163
1085
  exceptionSilent_1 = _a.sent();
1164
- console.error(exceptionSilent_1);
1165
- return [3 /*break*/, 3];
1166
- case 3:
1167
- if (this.timeoutId) {
1168
- timer_1.default.clearTimeout(this.timeoutId);
1169
- this.timeoutId = null;
1086
+ if (exceptionSilent_1 && exceptionSilent_1.message && exceptionSilent_1.message.startsWith("oidc")) {
1087
+ return [2 /*return*/, null];
1170
1088
  }
1171
- this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token and silent refresh failed" });
1172
- return [2 /*return*/, null];
1089
+ throw exceptionSilent_1;
1090
+ case 3: return [2 /*return*/, null];
1173
1091
  }
1174
1092
  });
1175
1093
  }); };
1176
- configuration = this.configuration;
1094
+ if (!(index <= 4)) return [3 /*break*/, 23];
1095
+ _e.label = 7;
1096
+ case 7:
1097
+ _e.trys.push([7, 22, , 23]);
1098
+ if (!!refreshToken) return [3 /*break*/, 9];
1099
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1100
+ return [4 /*yield*/, localsilentLoginAsync()];
1101
+ case 8: return [2 /*return*/, _e.sent()];
1102
+ case 9: return [4 /*yield*/, this.syncTokensInfoAsync(configuration, this.configurationName, this.tokens)];
1103
+ case 10:
1104
+ _a = _e.sent(), status_1 = _a.status, tokens = _a.tokens;
1105
+ _b = status_1;
1106
+ switch (_b) {
1107
+ case "NOT_CONNECTED": return [3 /*break*/, 11];
1108
+ case "TOKENS_VALID": return [3 /*break*/, 12];
1109
+ case "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID": return [3 /*break*/, 12];
1110
+ case "LOGOUT_FROM_ANOTHER_TAB": return [3 /*break*/, 13];
1111
+ case "REQUIRE_SYNC_TOKENS": return [3 /*break*/, 14];
1112
+ }
1113
+ return [3 /*break*/, 16];
1114
+ case 11: return [2 /*return*/, null];
1115
+ case 12: return [2 /*return*/, tokens];
1116
+ case 13:
1117
+ this.publishEvent(eventNames.logout_from_another_tab, { "status": "session syncTokensAsync" });
1118
+ return [2 /*return*/, null];
1119
+ case 14:
1120
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, status: status_1, tryNumber: index });
1121
+ return [4 /*yield*/, localsilentLoginAsync()];
1122
+ case 15: return [2 /*return*/, _e.sent()];
1123
+ case 16:
1124
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, status: status_1, tryNumber: index });
1177
1125
  clientId = configuration.client_id;
1178
1126
  redirectUri = configuration.redirect_uri;
1179
1127
  authority = configuration.authority;
1180
- if (!!refreshToken) return [3 /*break*/, 2];
1181
- return [4 /*yield*/, localsilentLoginAsync()];
1182
- case 1: return [2 /*return*/, _c.sent()];
1183
- case 2:
1184
1128
  extras = {};
1185
1129
  if (configuration.token_request_extras) {
1186
- for (_i = 0, _a = Object.entries(configuration.token_request_extras); _i < _a.length; _i++) {
1187
- _b = _a[_i], key = _b[0], value = _b[1];
1130
+ for (_i = 0, _c = Object.entries(configuration.token_request_extras); _i < _c.length; _i++) {
1131
+ _d = _c[_i], key = _d[0], value = _d[1];
1188
1132
  extras[key] = value;
1189
1133
  }
1190
1134
  }
1191
- return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1192
- case 3:
1193
- oidcServerConfiguration = _c.sent();
1194
1135
  details = {
1195
1136
  client_id: clientId,
1196
1137
  redirect_uri: redirectUri,
1197
1138
  grant_type: appauth_1.GRANT_TYPE_REFRESH_TOKEN,
1198
- refresh_token: refreshToken,
1139
+ refresh_token: tokens.refreshToken,
1199
1140
  };
1200
- index = 0;
1201
- _c.label = 4;
1202
- case 4:
1203
- if (!(index <= 4)) return [3 /*break*/, 18];
1204
- _c.label = 5;
1205
- case 5:
1206
- _c.trys.push([5, 16, , 17]);
1207
- this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1208
- if (!(index > 1)) return [3 /*break*/, 11];
1209
- _c.label = 6;
1210
- case 6:
1211
- if (!document.hidden) return [3 /*break*/, 8];
1212
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1213
- case 7:
1214
- _c.sent();
1215
- this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1216
- return [3 /*break*/, 6];
1217
- case 8:
1218
- numberTryOnline = 6;
1219
- _c.label = 9;
1220
- case 9:
1221
- if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 11];
1222
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1223
- case 10:
1224
- _c.sent();
1225
- numberTryOnline--;
1226
- this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1227
- return [3 /*break*/, 9];
1228
- case 11: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1229
- case 12:
1230
- tokenResponse = _c.sent();
1231
- if (!tokenResponse.success) return [3 /*break*/, 13];
1141
+ return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1142
+ case 17:
1143
+ oidcServerConfiguration = _e.sent();
1144
+ return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1145
+ case 18:
1146
+ tokenResponse = _e.sent();
1147
+ if (!tokenResponse.success) return [3 /*break*/, 19];
1232
1148
  this.publishEvent(eventNames.refreshTokensAsync_end, { success: tokenResponse.success });
1149
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1233
1150
  return [2 /*return*/, tokenResponse.data];
1234
- case 13:
1235
- this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "bad request", tokenResponse: tokenResponse });
1236
- return [4 /*yield*/, localsilentLoginAsync()];
1237
- case 14: return [2 /*return*/, _c.sent()];
1238
- case 15: return [3 /*break*/, 17];
1239
- case 16:
1240
- exception_4 = _c.sent();
1151
+ case 19:
1152
+ this.publishEvent(eventNames.refreshTokensAsync_silent_error, {
1153
+ message: "bad request",
1154
+ tokenResponse: tokenResponse
1155
+ });
1156
+ return [4 /*yield*/, this.synchroniseTokensAsync(null, index + 1)];
1157
+ case 20: return [2 /*return*/, _e.sent()];
1158
+ case 21: return [3 /*break*/, 23];
1159
+ case 22:
1160
+ exception_4 = _e.sent();
1241
1161
  console.error(exception_4);
1242
1162
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exception", exception: exception_4.message });
1243
- return [3 /*break*/, 17];
1244
- case 17:
1245
- index++;
1246
- return [3 /*break*/, 4];
1247
- case 18: return [2 /*return*/];
1163
+ return [2 /*return*/, this.synchroniseTokensAsync(refreshToken, index + 1)];
1164
+ case 23: return [2 /*return*/, null];
1248
1165
  }
1249
1166
  });
1250
1167
  });
1251
1168
  };
1252
- Oidc.prototype.syncTokensAsync = function () {
1169
+ Oidc.prototype.syncTokensInfoAsync = function (configuration, configurationName, currentTokens) {
1170
+ var _a;
1253
1171
  return __awaiter(this, void 0, void 0, function () {
1254
- var localSyncTokensAsync;
1255
- var _this = this;
1256
- return __generator(this, function (_a) {
1257
- localSyncTokensAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1258
- var configuration, oidcServerConfiguration, serviceWorker, isLogin, loginParams, silent_token_response, _a, exceptionSilent_2, session, tokens;
1259
- var _b;
1260
- return __generator(this, function (_c) {
1261
- switch (_c.label) {
1262
- case 0:
1263
- configuration = this.configuration;
1264
- if (!this.tokens) {
1265
- return [2 /*return*/, null];
1266
- }
1267
- return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1268
- case 1:
1269
- oidcServerConfiguration = _c.sent();
1270
- return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1271
- case 2:
1272
- serviceWorker = _c.sent();
1273
- if (!serviceWorker) return [3 /*break*/, 13];
1274
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1275
- case 3:
1276
- isLogin = (_c.sent()).isLogin;
1277
- if (!(isLogin == false)) return [3 /*break*/, 5];
1278
- this.publishEvent(eventNames.logout_from_another_tab, { "message": "service worker syncTokensAsync" });
1279
- return [4 /*yield*/, this.destroyAsync()];
1280
- case 4:
1281
- _c.sent();
1282
- return [2 /*return*/, null];
1283
- case 5:
1284
- if (!(isLogin == null)) return [3 /*break*/, 12];
1285
- _c.label = 6;
1286
- case 6:
1287
- _c.trys.push([6, 11, , 12]);
1288
- this.publishEvent(eventNames.syncTokensAsync_begin, {});
1289
- loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1290
- return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1291
- case 7:
1292
- silent_token_response = _c.sent();
1293
- if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1294
- _a = this;
1295
- return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1296
- case 8:
1297
- _a.tokens = _c.sent();
1298
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1299
- return [2 /*return*/, this.tokens];
1300
- case 9:
1301
- this.publishEvent(eventNames.syncTokensAsync_error, { message: "no token found in result" });
1302
- if (this.timeoutId) {
1303
- timer_1.default.clearTimeout(this.timeoutId);
1304
- this.timeoutId = null;
1305
- }
1306
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1307
- return [2 /*return*/, null];
1308
- case 10: return [3 /*break*/, 12];
1309
- case 11:
1310
- exceptionSilent_2 = _c.sent();
1311
- console.error(exceptionSilent_2);
1312
- this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1313
- if (this.timeoutId) {
1314
- timer_1.default.clearTimeout(this.timeoutId);
1315
- this.timeoutId = null;
1316
- }
1317
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1318
- return [2 /*return*/, null];
1319
- case 12: return [3 /*break*/, 16];
1320
- case 13:
1321
- session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_b = configuration.storage) !== null && _b !== void 0 ? _b : sessionStorage);
1322
- return [4 /*yield*/, session.initAsync()];
1323
- case 14:
1324
- tokens = (_c.sent()).tokens;
1325
- if (!!tokens) return [3 /*break*/, 16];
1326
- this.publishEvent(eventNames.logout_from_another_tab, { "message": "session syncTokensAsync" });
1327
- return [4 /*yield*/, this.destroyAsync()];
1328
- case 15:
1329
- _c.sent();
1330
- return [2 /*return*/, null];
1331
- case 16: return [2 /*return*/, this.tokens];
1172
+ var oidcServerConfiguration, serviceWorker, _b, isLogin, tokens, timeLeft_1, status_2, session, tokens, timeLeft_2, status_3, timeLeft, status;
1173
+ return __generator(this, function (_c) {
1174
+ switch (_c.label) {
1175
+ case 0:
1176
+ // Service Worker can be killed by the browser (when it wants,for example after 10 seconds of inactivity, so we retreieve the session if it happen)
1177
+ //const configuration = this.configuration;
1178
+ if (!currentTokens) {
1179
+ return [2 /*return*/, { tokens: null, status: "NOT_CONNECTED" }];
1332
1180
  }
1333
- });
1334
- }); };
1335
- if (this.syncTokensAsyncPromise) {
1336
- return [2 /*return*/, this.syncTokensAsyncPromise];
1181
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1182
+ case 1:
1183
+ oidcServerConfiguration = _c.sent();
1184
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, configurationName)];
1185
+ case 2:
1186
+ serviceWorker = _c.sent();
1187
+ if (!serviceWorker) return [3 /*break*/, 4];
1188
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1189
+ case 3:
1190
+ _b = _c.sent(), isLogin = _b.isLogin, tokens = _b.tokens;
1191
+ if (isLogin == false) {
1192
+ return [2 /*return*/, { tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB" }];
1193
+ }
1194
+ else if (isLogin == null) {
1195
+ return [2 /*return*/, { tokens: null, status: "REQUIRE_SYNC_TOKENS" }];
1196
+ }
1197
+ else if (tokens.issuedAt !== currentTokens.issuedAt) {
1198
+ timeLeft_1 = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, tokens.expiresAt);
1199
+ status_2 = (timeLeft_1 > 0) ? "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID" : "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_INVALID";
1200
+ return [2 /*return*/, { tokens: tokens, status: status_2 }];
1201
+ }
1202
+ return [3 /*break*/, 6];
1203
+ case 4:
1204
+ session = (0, initSession_1.initSession)(configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
1205
+ return [4 /*yield*/, session.initAsync()];
1206
+ case 5:
1207
+ tokens = (_c.sent()).tokens;
1208
+ if (!tokens) {
1209
+ return [2 /*return*/, { tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB" }];
1210
+ }
1211
+ else if (tokens.issuedAt !== currentTokens.issuedAt) {
1212
+ timeLeft_2 = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, tokens.expiresAt);
1213
+ status_3 = (timeLeft_2 > 0) ? "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID" : "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_INVALID";
1214
+ return [2 /*return*/, { tokens: tokens, status: status_3 }];
1215
+ }
1216
+ _c.label = 6;
1217
+ case 6:
1218
+ timeLeft = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, currentTokens.expiresAt);
1219
+ status = (timeLeft > 0) ? "TOKENS_VALID" : "TOKENS_INVALID";
1220
+ return [2 /*return*/, { tokens: currentTokens, status: status }];
1337
1221
  }
1338
- this.syncTokensAsyncPromise = localSyncTokensAsync().then(function (result) {
1339
- if (_this.syncTokensAsyncPromise) {
1340
- _this.syncTokensAsyncPromise = null;
1341
- }
1342
- return result;
1343
- });
1344
- return [2 /*return*/, this.syncTokensAsyncPromise];
1345
1222
  });
1346
1223
  });
1347
1224
  };
@@ -1383,7 +1260,6 @@ var Oidc = /** @class */ (function () {
1383
1260
  case 4:
1384
1261
  this.tokens = null;
1385
1262
  this.userInfo = null;
1386
- this.events = [];
1387
1263
  return [2 /*return*/];
1388
1264
  }
1389
1265
  });
@@ -1405,6 +1281,23 @@ var Oidc = /** @class */ (function () {
1405
1281
  });
1406
1282
  });
1407
1283
  };
1284
+ Oidc.prototype.logoutOtherTabAsync = function (sub) {
1285
+ return __awaiter(this, void 0, void 0, function () {
1286
+ return __generator(this, function (_a) {
1287
+ switch (_a.label) {
1288
+ case 0:
1289
+ if (!(this.configuration.monitor_session && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1290
+ return [4 /*yield*/, this.destroyAsync()];
1291
+ case 1:
1292
+ _a.sent();
1293
+ console.log("logoutOtherTabAsync(sub)" + this.configurationName);
1294
+ this.publishEvent(eventNames.logout_from_another_tab, { message: "SessionMonitor", "sub": sub });
1295
+ _a.label = 2;
1296
+ case 2: return [2 /*return*/];
1297
+ }
1298
+ });
1299
+ });
1300
+ };
1408
1301
  Oidc.prototype.logoutAsync = function (callbackPathOrUrl, extras) {
1409
1302
  if (callbackPathOrUrl === void 0) { callbackPathOrUrl = undefined; }
1410
1303
  if (extras === void 0) { extras = null; }