@axa-fr/react-oidc 6.0.0-beta13 → 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, data;
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,24 +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
- }
114
- data = {
115
- accessToken: tokens.access_token,
116
- expiresIn: tokens.expires_in,
117
- idToken: tokens.id_token,
118
- refreshToken: tokens.refresh_token,
119
- scope: tokens.scope,
120
- tokenType: tokens.token_type,
121
- issuedAt: tokens.issued_at
122
- };
123
- if (tokens.accessTokenPayload !== undefined) {
124
- // @ts-ignore
125
- data.accessTokenPayload = tokens.accessTokenPayload;
126
- }
127
- return [2 /*return*/, { success: true, data: data
111
+ console.log(tokens);
112
+ return [2 /*return*/, { success: true,
113
+ data: (0, parseTokens_1.parseOriginalTokens)(tokens)
128
114
  }];
129
115
  }
130
116
  });
@@ -175,33 +161,6 @@ var OidcAuthorizationServiceConfiguration = /** @class */ (function (_super) {
175
161
  return OidcAuthorizationServiceConfiguration;
176
162
  }(appauth_1.AuthorizationServiceConfiguration));
177
163
  exports.OidcAuthorizationServiceConfiguration = OidcAuthorizationServiceConfiguration;
178
- var idTokenPayload = function (token) {
179
- var base64Url = token.split('.')[1];
180
- var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
181
- var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
182
- return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
183
- }).join(''));
184
- return JSON.parse(jsonPayload);
185
- };
186
- var countLetter = function (str, find) {
187
- return (str.split(find)).length - 1;
188
- };
189
- var extractAccessTokenPayload = function (tokens) {
190
- if (tokens.accessTokenPayload) {
191
- return tokens.accessTokenPayload;
192
- }
193
- var accessToken = tokens.accessToken;
194
- try {
195
- if (!accessToken || countLetter(accessToken, '.') != 2) {
196
- return null;
197
- }
198
- return JSON.parse(atob(accessToken.split('.')[1]));
199
- }
200
- catch (e) {
201
- console.warn(e);
202
- }
203
- return null;
204
- };
205
164
  var oidcDatabase = {};
206
165
  var oidcFactory = function (configuration, name) {
207
166
  if (name === void 0) { name = "default"; }
@@ -224,50 +183,36 @@ var loginCallbackWithAutoTokensRenewAsync = function (oidc) { return __awaiter(v
224
183
  });
225
184
  }); };
226
185
  var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
227
- var _a;
228
- 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;
229
187
  return timer_1.default.setTimeout(function () { return __awaiter(void 0, void 0, void 0, function () {
230
- var currentTimeUnixSecond, timeInfo, tokens, _a, tokens;
231
- return __generator(this, function (_b) {
232
- switch (_b.label) {
188
+ var timeLeft, timeInfo, tokens;
189
+ return __generator(this, function (_a) {
190
+ switch (_a.label) {
233
191
  case 0:
234
- currentTimeUnixSecond = new Date().getTime() / 1000;
235
- timeInfo = { timeLeft: Math.round(((expiresAt - refreshTimeBeforeTokensExpirationInSecond) - currentTimeUnixSecond)) };
192
+ timeLeft = (0, parseTokens_1.computeTimeLeft)(refreshTimeBeforeTokensExpirationInSecond, expiresAt);
193
+ timeInfo = { timeLeft: timeLeft };
236
194
  oidc.publishEvent(Oidc.eventNames.token_timer, timeInfo);
237
- if (!(currentTimeUnixSecond > (expiresAt - refreshTimeBeforeTokensExpirationInSecond))) return [3 /*break*/, 5];
238
- return [4 /*yield*/, oidc.refreshTokensAsync(refreshToken)];
195
+ return [4 /*yield*/, oidc.synchroniseTokensAsync(refreshToken)];
239
196
  case 1:
240
- tokens = _b.sent();
241
- _a = oidc;
242
- return [4 /*yield*/, setTokensAsync(oidc.serviceWorker, tokens)];
243
- case 2:
244
- _a.tokens = _b.sent();
245
- if (!!oidc.serviceWorker) return [3 /*break*/, 4];
197
+ tokens = _a.sent();
198
+ oidc.tokens = tokens;
199
+ if (!!oidc.serviceWorker) return [3 /*break*/, 3];
246
200
  return [4 /*yield*/, oidc.session.setTokens(oidc.tokens)];
201
+ case 2:
202
+ _a.sent();
203
+ _a.label = 3;
247
204
  case 3:
248
- _b.sent();
249
- _b.label = 4;
205
+ if (!!oidc.tokens) return [3 /*break*/, 5];
206
+ return [4 /*yield*/, oidc.destroyAsync()];
250
207
  case 4:
251
- if (!oidc.tokens) {
252
- if (oidc.checkSessionIFrame) {
253
- oidc.checkSessionIFrame.stop();
254
- oidc.checkSessionIFrame = null;
255
- }
256
- return [2 /*return*/];
257
- }
258
- 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:
259
212
  if (oidc.timeoutId) {
260
213
  oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
261
214
  }
262
- return [3 /*break*/, 7];
263
- case 5: return [4 /*yield*/, oidc.syncTokensAsync()];
264
- case 6:
265
- tokens = _b.sent();
266
- if (tokens && oidc.timeoutId) {
267
- oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, tokens.expiresAt);
268
- }
269
- _b.label = 7;
270
- case 7: return [2 /*return*/];
215
+ return [2 /*return*/];
271
216
  }
272
217
  });
273
218
  }); }, 1000);
@@ -289,15 +234,19 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
289
234
  if (!oidc.tokens) {
290
235
  return [2 /*return*/, null];
291
236
  }
292
- if (!oidc.syncTokensAsyncPromise) return [3 /*break*/, 2];
293
- return [4 /*yield*/, oidc.syncTokensAsyncPromise];
237
+ accessToken = oidc.tokens.accessToken;
238
+ if (!accessToken) {
239
+ return [2 /*return*/, null];
240
+ }
241
+ _a.label = 1;
294
242
  case 1:
295
- _a.sent();
296
- _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)];
297
245
  case 2:
298
- accessToken = oidc.tokens.accessToken;
299
- return [4 /*yield*/, oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration)];
300
- 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:
301
250
  oidcServerConfiguration = _a.sent();
302
251
  url = oidcServerConfiguration.userInfoEndpoint;
303
252
  fetchUserInfo = function (accessToken) { return __awaiter(void 0, void 0, void 0, function () {
@@ -320,35 +269,13 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
320
269
  });
321
270
  }); };
322
271
  return [4 /*yield*/, fetchUserInfo(accessToken)];
323
- case 4:
272
+ case 5:
324
273
  userInfo = _a.sent();
325
274
  oidc.userInfo = userInfo;
326
275
  return [2 /*return*/, userInfo];
327
276
  }
328
277
  });
329
278
  }); };
330
- var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0, void 0, void 0, function () {
331
- var accessTokenPayload, _idTokenPayload, idTokenExipreAt, accessTokenExpiresAt, expiresAt;
332
- return __generator(this, function (_a) {
333
- /*if(tokens == null){
334
- if(serviceWorker){
335
- await serviceWorker.clearAsync();
336
- }
337
- return null;
338
- }*/
339
- if (tokens.accessTokenPayload !== undefined) {
340
- accessTokenPayload = tokens.accessTokenPayload; //await serviceWorker.getAccessTokenPayloadAsync();
341
- }
342
- else {
343
- accessTokenPayload = extractAccessTokenPayload(tokens);
344
- }
345
- _idTokenPayload = idTokenPayload(tokens.idToken);
346
- idTokenExipreAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : Number.MAX_VALUE;
347
- accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp) ? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
348
- expiresAt = idTokenExipreAt < accessTokenExpiresAt ? idTokenExipreAt : accessTokenExpiresAt;
349
- return [2 /*return*/, __assign(__assign({}, tokens), { idTokenPayload: _idTokenPayload, accessTokenPayload: accessTokenPayload, expiresAt: expiresAt })];
350
- });
351
- }); };
352
279
  var eventNames = {
353
280
  service_worker_not_supported_by_browser: "service_worker_not_supported_by_browser",
354
281
  token_aquired: "token_aquired",
@@ -440,16 +367,16 @@ var buildQueries = function (extras) {
440
367
  var Oidc = /** @class */ (function () {
441
368
  function Oidc(configuration, configurationName) {
442
369
  if (configurationName === void 0) { configurationName = "default"; }
370
+ var _a, _b, _c;
443
371
  this.tryKeepExistingSessionPromise = null;
444
372
  this.loginPromise = null;
445
373
  this.loginCallbackPromise = null;
446
- this.syncTokensAsyncPromise = null;
447
374
  this.loginCallbackWithAutoTokensRenewPromise = null;
448
375
  var silent_login_uri = configuration.silent_login_uri;
449
376
  if (configuration.silent_redirect_uri && !configuration.silent_login_uri) {
450
377
  silent_login_uri = "".concat(configuration.silent_redirect_uri.replace("-callback", "").replace("callback", ""), "-login");
451
378
  }
452
- 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 });
453
380
  this.configurationName = configurationName;
454
381
  this.tokens = null;
455
382
  this.userInfo = null;
@@ -457,7 +384,7 @@ var Oidc = /** @class */ (function () {
457
384
  this.timeoutId = null;
458
385
  this.serviceWorker = null;
459
386
  this.session = null;
460
- this.refreshTokensAsync.bind(this);
387
+ this.synchroniseTokensAsync.bind(this);
461
388
  this.loginCallbackWithAutoTokensRenewAsync.bind(this);
462
389
  this.initAsync.bind(this);
463
390
  this.loginCallbackAsync.bind(this);
@@ -534,130 +461,106 @@ var Oidc = /** @class */ (function () {
534
461
  if (state === void 0) { state = null; }
535
462
  if (scope === void 0) { scope = null; }
536
463
  return __awaiter(this, void 0, void 0, function () {
537
- 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;
538
465
  return __generator(this, function (_c) {
539
- switch (_c.label) {
540
- case 0:
541
- if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
542
- 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 = {};
543
476
  }
544
- _c.label = 1;
545
- case 1:
546
- if (!document.hidden) return [3 /*break*/, 3];
547
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
548
- case 2:
549
- _c.sent();
550
- this.publishEvent(eventNames.silentLoginAsync, { message: "wait because document is hidden" });
551
- return [3 /*break*/, 1];
552
- case 3:
553
- numberTryOnline = 6;
554
- _c.label = 4;
555
- case 4:
556
- if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 6];
557
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
558
- case 5:
559
- _c.sent();
560
- numberTryOnline--;
561
- this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
562
- return [3 /*break*/, 4];
563
- case 6:
564
- try {
565
- this.publishEvent(eventNames.silentLoginAsync_begin, {});
566
- configuration_1 = this.configuration;
567
- queries = "";
568
- if (state) {
569
- if (extras == null) {
570
- extras = {};
571
- }
572
- extras.state = state;
573
- }
574
- if (scope) {
575
- if (extras == null) {
576
- extras = {};
577
- }
578
- 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));
579
490
  }
580
- if (extras != null) {
581
- for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
582
- _b = _a[_i], key = _b[0], value = _b[1];
583
- if (queries === "") {
584
- queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
585
- }
586
- else {
587
- queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
588
- }
589
- }
491
+ else {
492
+ queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
590
493
  }
591
- link = configuration_1.silent_login_uri + queries;
592
- idx = link.indexOf("/", link.indexOf("//") + 2);
593
- iFrameOrigin_1 = link.substr(0, idx);
594
- iframe_1 = document.createElement('iframe');
595
- iframe_1.width = "0px";
596
- iframe_1.height = "0px";
597
- iframe_1.id = "".concat(this.configurationName, "_oidc_iframe");
598
- iframe_1.setAttribute("src", link);
599
- document.body.appendChild(iframe_1);
600
- self_1 = this;
601
- return [2 /*return*/, new Promise(function (resolve, reject) {
602
- var _a;
603
- try {
604
- var isResolved_1 = false;
605
- window.onmessage = function (e) {
606
- if (e.origin === iFrameOrigin_1 &&
607
- e.source === iframe_1.contentWindow) {
608
- var key = "".concat(self_1.configurationName, "_oidc_tokens:");
609
- var key_error = "".concat(self_1.configurationName, "_oidc_error:");
610
- var data = e.data;
611
- if (data && typeof (data) === "string") {
612
- if (!isResolved_1) {
613
- if (data.startsWith(key)) {
614
- var result = JSON.parse(e.data.replace(key, ''));
615
- self_1.publishEvent(eventNames.silentLoginAsync_end, {});
616
- iframe_1.remove();
617
- isResolved_1 = true;
618
- resolve(result);
619
- }
620
- else if (data.startsWith(key_error)) {
621
- var result = JSON.parse(e.data.replace(key_error, ''));
622
- self_1.publishEvent(eventNames.silentLoginAsync_error, result);
623
- iframe_1.remove();
624
- isResolved_1 = true;
625
- reject(new Error("oidc_" + result.error));
626
- }
627
- }
628
- }
629
- }
630
- };
631
- var silentSigninTimeout = (_a = configuration_1.silent_login_timeout) !== null && _a !== void 0 ? _a : 12000;
632
- 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") {
633
516
  if (!isResolved_1) {
634
- self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
635
- iframe_1.remove();
636
- isResolved_1 = true;
637
- 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
+ }
638
531
  }
639
- }, silentSigninTimeout);
532
+ }
640
533
  }
641
- 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" });
642
539
  iframe_1.remove();
643
- self_1.publishEvent(eventNames.silentLoginAsync_error, e);
644
- reject(e);
540
+ isResolved_1 = true;
541
+ reject(new Error("timeout"));
645
542
  }
646
- })];
647
- }
648
- catch (e) {
649
- this.publishEvent(eventNames.silentLoginAsync_error, e);
650
- throw e;
651
- }
652
- 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;
653
555
  }
556
+ return [2 /*return*/];
654
557
  });
655
558
  });
656
559
  };
657
560
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
658
561
  var _a;
659
562
  return __awaiter(this, void 0, void 0, function () {
660
- var serviceWorker, storage, initAsyncPromise;
563
+ var serviceWorker, storage;
661
564
  return __generator(this, function (_b) {
662
565
  switch (_b.label) {
663
566
  case 0:
@@ -676,9 +579,7 @@ var Oidc = /** @class */ (function () {
676
579
  serviceWorker = _b.sent();
677
580
  storage = serviceWorker ? window.localStorage : null;
678
581
  return [4 /*yield*/, fetchFromIssuer(authority, (_a = this.configuration.authority_time_cache_wellknowurl_in_second) !== null && _a !== void 0 ? _a : 60 * 60, storage)];
679
- case 2:
680
- initAsyncPromise = _b.sent();
681
- return [2 /*return*/, initAsyncPromise];
582
+ case 2: return [2 /*return*/, _b.sent()];
682
583
  }
683
584
  });
684
585
  });
@@ -692,83 +593,68 @@ var Oidc = /** @class */ (function () {
692
593
  return [2 /*return*/, this.tryKeepExistingSessionPromise];
693
594
  }
694
595
  funcAsync = function () { return __awaiter(_this, void 0, void 0, function () {
695
- var serviceWorker, configuration, oidcServerConfiguration, tokens, reformattedToken, _a, sessionState, session, tokens, _b, sessionState, exception_1;
696
- var _c;
697
- return __generator(this, function (_d) {
698
- 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) {
699
600
  case 0:
700
601
  if (this.tokens != null) {
701
602
  return [2 /*return*/, false];
702
603
  }
703
604
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_begin, {});
704
- _d.label = 1;
605
+ _b.label = 1;
705
606
  case 1:
706
- _d.trys.push([1, 14, , 17]);
607
+ _b.trys.push([1, 12, , 15]);
707
608
  configuration = this.configuration;
708
609
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
709
610
  case 2:
710
- oidcServerConfiguration = _d.sent();
611
+ oidcServerConfiguration = _b.sent();
711
612
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
712
613
  case 3:
713
- serviceWorker = _d.sent();
714
- if (!serviceWorker) return [3 /*break*/, 9];
614
+ serviceWorker = _b.sent();
615
+ if (!serviceWorker) return [3 /*break*/, 8];
715
616
  return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync")];
716
617
  case 4:
717
- tokens = (_d.sent()).tokens;
718
- if (!tokens) return [3 /*break*/, 8];
618
+ tokens = (_b.sent()).tokens;
619
+ if (!tokens) return [3 /*break*/, 7];
719
620
  serviceWorker.startKeepAliveServiceWorker();
720
- reformattedToken = {
721
- accessToken: tokens.access_token,
722
- refreshToken: tokens.refresh_token,
723
- expiresIn: tokens.expires_in,
724
- idToken: tokens.id_token,
725
- scope: tokens.scope,
726
- tokenType: tokens.token_type,
727
- issuedAt: tokens.issued_at
728
- };
729
- _a = this;
730
- return [4 /*yield*/, setTokensAsync(serviceWorker, reformattedToken)];
731
- case 5:
732
- _a.tokens = _d.sent();
621
+ // @ts-ignore
622
+ this.tokens = tokens;
733
623
  this.serviceWorker = serviceWorker;
734
624
  // @ts-ignore
735
625
  this.timeoutId = autoRenewTokens(this, this.tokens.refreshToken, this.tokens.expiresAt);
736
626
  return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
737
- case 6:
738
- sessionState = _d.sent();
627
+ case 5:
628
+ sessionState = _b.sent();
739
629
  // @ts-ignore
740
630
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
741
- case 7:
631
+ case 6:
742
632
  // @ts-ignore
743
- _d.sent();
633
+ _b.sent();
744
634
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
745
635
  success: true,
746
636
  message: "tokens inside ServiceWorker are valid"
747
637
  });
748
638
  return [2 /*return*/, true];
749
- case 8:
639
+ case 7:
750
640
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
751
641
  success: false,
752
642
  message: "no exiting session found"
753
643
  });
754
- return [3 /*break*/, 13];
755
- case 9:
644
+ return [3 /*break*/, 11];
645
+ case 8:
756
646
  if (configuration.service_worker_relative_url) {
757
647
  this.publishEvent(eventNames.service_worker_not_supported_by_browser, {
758
648
  message: "service worker is not supported by this browser"
759
649
  });
760
650
  }
761
- 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);
762
652
  return [4 /*yield*/, session.initAsync()];
763
- case 10:
764
- tokens = (_d.sent()).tokens;
765
- if (!tokens) return [3 /*break*/, 13];
766
- // @ts-ignore
767
- _b = this;
768
- return [4 /*yield*/, setTokensAsync(serviceWorker, tokens)];
769
- case 11:
653
+ case 9:
654
+ tokens = (_b.sent()).tokens;
655
+ if (!tokens) return [3 /*break*/, 11];
770
656
  // @ts-ignore
771
- _b.tokens = _d.sent();
657
+ this.tokens = (0, parseTokens_1.setTokens)(tokens);
772
658
  //session.setTokens(this.tokens);
773
659
  this.session = session;
774
660
  // @ts-ignore
@@ -776,32 +662,32 @@ var Oidc = /** @class */ (function () {
776
662
  sessionState = session.getSessionState();
777
663
  // @ts-ignore
778
664
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
779
- case 12:
665
+ case 10:
780
666
  // @ts-ignore
781
- _d.sent();
667
+ _b.sent();
782
668
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
783
669
  success: true,
784
670
  message: "tokens inside storage are valid"
785
671
  });
786
672
  return [2 /*return*/, true];
787
- case 13:
673
+ case 11:
788
674
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
789
675
  success: false,
790
676
  message: serviceWorker ? "service worker sessions not retrieved" : "session storage sessions not retrieved"
791
677
  });
792
678
  return [2 /*return*/, false];
793
- case 14:
794
- exception_1 = _d.sent();
679
+ case 12:
680
+ exception_1 = _b.sent();
795
681
  console.error(exception_1);
796
- if (!serviceWorker) return [3 /*break*/, 16];
682
+ if (!serviceWorker) return [3 /*break*/, 14];
797
683
  return [4 /*yield*/, serviceWorker.clearAsync()];
798
- case 15:
799
- _d.sent();
800
- _d.label = 16;
801
- case 16:
684
+ case 13:
685
+ _b.sent();
686
+ _b.label = 14;
687
+ case 14:
802
688
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_error, "tokens inside ServiceWorker are invalid");
803
689
  return [2 /*return*/, false];
804
- case 17: return [2 /*return*/];
690
+ case 15: return [2 /*return*/];
805
691
  }
806
692
  });
807
693
  }); };
@@ -813,10 +699,9 @@ var Oidc = /** @class */ (function () {
813
699
  });
814
700
  });
815
701
  };
816
- Oidc.prototype.loginAsync = function (callbackPath, extras, installServiceWorker, state, isSilentSignin, scope) {
702
+ Oidc.prototype.loginAsync = function (callbackPath, extras, state, isSilentSignin, scope) {
817
703
  if (callbackPath === void 0) { callbackPath = undefined; }
818
704
  if (extras === void 0) { extras = null; }
819
- if (installServiceWorker === void 0) { installServiceWorker = true; }
820
705
  if (state === void 0) { state = undefined; }
821
706
  if (isSilentSignin === void 0) { isSilentSignin = false; }
822
707
  if (scope === void 0) { scope = undefined; }
@@ -988,16 +873,14 @@ var Oidc = /** @class */ (function () {
988
873
  case 1:
989
874
  response = _a.sent();
990
875
  tokens = response.tokens;
991
- return [4 /*yield*/, setTokensAsync(this.serviceWorker, tokens)];
992
- case 2:
993
- parsedTokens = _a.sent();
876
+ parsedTokens = (0, parseTokens_1.setTokens)(tokens);
994
877
  this.tokens = parsedTokens;
995
- if (!!this.serviceWorker) return [3 /*break*/, 4];
878
+ if (!!this.serviceWorker) return [3 /*break*/, 3];
996
879
  return [4 /*yield*/, this.session.setTokens(parsedTokens)];
997
- case 3:
880
+ case 2:
998
881
  _a.sent();
999
- _a.label = 4;
1000
- case 4:
882
+ _a.label = 3;
883
+ case 3:
1001
884
  this.publishEvent(Oidc.eventNames.token_aquired, parsedTokens);
1002
885
  // @ts-ignore
1003
886
  return [2 /*return*/, { parsedTokens: parsedTokens, state: response.state, callbackPath: response.callbackPath }];
@@ -1155,13 +1038,34 @@ var Oidc = /** @class */ (function () {
1155
1038
  });
1156
1039
  });
1157
1040
  };
1158
- Oidc.prototype.refreshTokensAsync = function (refreshToken) {
1041
+ Oidc.prototype.synchroniseTokensAsync = function (refreshToken, index) {
1042
+ if (index === void 0) { index = 0; }
1159
1043
  return __awaiter(this, void 0, void 0, function () {
1160
- 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;
1161
1045
  var _this = this;
1162
- return __generator(this, function (_c) {
1163
- switch (_c.label) {
1046
+ return __generator(this, function (_e) {
1047
+ switch (_e.label) {
1164
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;
1165
1069
  localsilentLoginAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1166
1070
  var loginParams, silent_token_response, exceptionSilent_1;
1167
1071
  return __generator(this, function (_a) {
@@ -1169,196 +1073,152 @@ var Oidc = /** @class */ (function () {
1169
1073
  case 0:
1170
1074
  _a.trys.push([0, 2, , 3]);
1171
1075
  loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1172
- return [4 /*yield*/, this.silentLoginAsync(loginParams.extras, loginParams.state)];
1076
+ return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1173
1077
  case 1:
1174
1078
  silent_token_response = _a.sent();
1175
1079
  if (silent_token_response) {
1080
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1176
1081
  return [2 /*return*/, silent_token_response.tokens];
1177
1082
  }
1178
1083
  return [3 /*break*/, 3];
1179
1084
  case 2:
1180
1085
  exceptionSilent_1 = _a.sent();
1181
- console.error(exceptionSilent_1);
1182
- return [3 /*break*/, 3];
1183
- case 3:
1184
- if (this.timeoutId) {
1185
- timer_1.default.clearTimeout(this.timeoutId);
1186
- this.timeoutId = null;
1086
+ if (exceptionSilent_1 && exceptionSilent_1.message && exceptionSilent_1.message.startsWith("oidc")) {
1087
+ return [2 /*return*/, null];
1187
1088
  }
1188
- this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token and silent refresh failed" });
1189
- return [2 /*return*/, null];
1089
+ throw exceptionSilent_1;
1090
+ case 3: return [2 /*return*/, null];
1190
1091
  }
1191
1092
  });
1192
1093
  }); };
1193
- 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 });
1194
1125
  clientId = configuration.client_id;
1195
1126
  redirectUri = configuration.redirect_uri;
1196
1127
  authority = configuration.authority;
1197
- if (!!refreshToken) return [3 /*break*/, 2];
1198
- return [4 /*yield*/, localsilentLoginAsync()];
1199
- case 1: return [2 /*return*/, _c.sent()];
1200
- case 2:
1201
1128
  extras = {};
1202
1129
  if (configuration.token_request_extras) {
1203
- for (_i = 0, _a = Object.entries(configuration.token_request_extras); _i < _a.length; _i++) {
1204
- _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];
1205
1132
  extras[key] = value;
1206
1133
  }
1207
1134
  }
1208
- return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1209
- case 3:
1210
- oidcServerConfiguration = _c.sent();
1211
1135
  details = {
1212
1136
  client_id: clientId,
1213
1137
  redirect_uri: redirectUri,
1214
1138
  grant_type: appauth_1.GRANT_TYPE_REFRESH_TOKEN,
1215
- refresh_token: refreshToken,
1139
+ refresh_token: tokens.refreshToken,
1216
1140
  };
1217
- index = 0;
1218
- _c.label = 4;
1219
- case 4:
1220
- if (!(index <= 4)) return [3 /*break*/, 18];
1221
- _c.label = 5;
1222
- case 5:
1223
- _c.trys.push([5, 16, , 17]);
1224
- this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1225
- if (!(index > 1)) return [3 /*break*/, 11];
1226
- _c.label = 6;
1227
- case 6:
1228
- if (!document.hidden) return [3 /*break*/, 8];
1229
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1230
- case 7:
1231
- _c.sent();
1232
- this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1233
- return [3 /*break*/, 6];
1234
- case 8:
1235
- numberTryOnline = 6;
1236
- _c.label = 9;
1237
- case 9:
1238
- if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 11];
1239
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1240
- case 10:
1241
- _c.sent();
1242
- numberTryOnline--;
1243
- this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1244
- return [3 /*break*/, 9];
1245
- case 11: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1246
- case 12:
1247
- tokenResponse = _c.sent();
1248
- 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];
1249
1148
  this.publishEvent(eventNames.refreshTokensAsync_end, { success: tokenResponse.success });
1149
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1250
1150
  return [2 /*return*/, tokenResponse.data];
1251
- case 13:
1252
- this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "bad request", tokenResponse: tokenResponse });
1253
- return [4 /*yield*/, localsilentLoginAsync()];
1254
- case 14: return [2 /*return*/, _c.sent()];
1255
- case 15: return [3 /*break*/, 17];
1256
- case 16:
1257
- 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();
1258
1161
  console.error(exception_4);
1259
1162
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exception", exception: exception_4.message });
1260
- return [3 /*break*/, 17];
1261
- case 17:
1262
- index++;
1263
- return [3 /*break*/, 4];
1264
- case 18: return [2 /*return*/];
1163
+ return [2 /*return*/, this.synchroniseTokensAsync(refreshToken, index + 1)];
1164
+ case 23: return [2 /*return*/, null];
1265
1165
  }
1266
1166
  });
1267
1167
  });
1268
1168
  };
1269
- Oidc.prototype.syncTokensAsync = function () {
1169
+ Oidc.prototype.syncTokensInfoAsync = function (configuration, configurationName, currentTokens) {
1170
+ var _a;
1270
1171
  return __awaiter(this, void 0, void 0, function () {
1271
- var localSyncTokensAsync;
1272
- var _this = this;
1273
- return __generator(this, function (_a) {
1274
- localSyncTokensAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1275
- var configuration, oidcServerConfiguration, serviceWorker, isLogin, loginParams, silent_token_response, _a, exceptionSilent_2, session, tokens;
1276
- var _b;
1277
- return __generator(this, function (_c) {
1278
- switch (_c.label) {
1279
- case 0:
1280
- configuration = this.configuration;
1281
- if (!this.tokens) {
1282
- return [2 /*return*/, null];
1283
- }
1284
- return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1285
- case 1:
1286
- oidcServerConfiguration = _c.sent();
1287
- return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1288
- case 2:
1289
- serviceWorker = _c.sent();
1290
- if (!serviceWorker) return [3 /*break*/, 13];
1291
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1292
- case 3:
1293
- isLogin = (_c.sent()).isLogin;
1294
- if (!(isLogin == false)) return [3 /*break*/, 5];
1295
- this.publishEvent(eventNames.logout_from_another_tab, { "message": "service worker syncTokensAsync" });
1296
- return [4 /*yield*/, this.destroyAsync()];
1297
- case 4:
1298
- _c.sent();
1299
- return [2 /*return*/, null];
1300
- case 5:
1301
- if (!(isLogin == null)) return [3 /*break*/, 12];
1302
- _c.label = 6;
1303
- case 6:
1304
- _c.trys.push([6, 11, , 12]);
1305
- this.publishEvent(eventNames.syncTokensAsync_begin, {});
1306
- loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1307
- return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1308
- case 7:
1309
- silent_token_response = _c.sent();
1310
- if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1311
- _a = this;
1312
- return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1313
- case 8:
1314
- _a.tokens = _c.sent();
1315
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1316
- return [2 /*return*/, this.tokens];
1317
- case 9:
1318
- this.publishEvent(eventNames.syncTokensAsync_error, { message: "no token found in result" });
1319
- if (this.timeoutId) {
1320
- timer_1.default.clearTimeout(this.timeoutId);
1321
- this.timeoutId = null;
1322
- }
1323
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1324
- return [2 /*return*/, null];
1325
- case 10: return [3 /*break*/, 12];
1326
- case 11:
1327
- exceptionSilent_2 = _c.sent();
1328
- console.error(exceptionSilent_2);
1329
- this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1330
- if (this.timeoutId) {
1331
- timer_1.default.clearTimeout(this.timeoutId);
1332
- this.timeoutId = null;
1333
- }
1334
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1335
- return [2 /*return*/, null];
1336
- case 12: return [3 /*break*/, 16];
1337
- case 13:
1338
- session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_b = configuration.storage) !== null && _b !== void 0 ? _b : sessionStorage);
1339
- return [4 /*yield*/, session.initAsync()];
1340
- case 14:
1341
- tokens = (_c.sent()).tokens;
1342
- if (!!tokens) return [3 /*break*/, 16];
1343
- this.publishEvent(eventNames.logout_from_another_tab, { "message": "session syncTokensAsync" });
1344
- return [4 /*yield*/, this.destroyAsync()];
1345
- case 15:
1346
- _c.sent();
1347
- return [2 /*return*/, null];
1348
- 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" }];
1349
1180
  }
1350
- });
1351
- }); };
1352
- if (this.syncTokensAsyncPromise) {
1353
- 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 }];
1354
1221
  }
1355
- this.syncTokensAsyncPromise = localSyncTokensAsync().then(function (result) {
1356
- if (_this.syncTokensAsyncPromise) {
1357
- _this.syncTokensAsyncPromise = null;
1358
- }
1359
- return result;
1360
- });
1361
- return [2 /*return*/, this.syncTokensAsyncPromise];
1362
1222
  });
1363
1223
  });
1364
1224
  };