@axa-fr/react-oidc 6.0.0-beta13 → 6.0.0-beta16

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
+ return [2 /*return*/, {
112
+ 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,35 @@ 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;
188
+ var timeLeft, timeInfo, _a, tokens, status;
231
189
  return __generator(this, function (_b) {
232
190
  switch (_b.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
+ _a = _b.sent(), tokens = _a.tokens, status = _a.status;
198
+ oidc.tokens = tokens;
199
+ if (!!oidc.serviceWorker) return [3 /*break*/, 3];
246
200
  return [4 /*yield*/, oidc.session.setTokens(oidc.tokens)];
247
- case 3:
201
+ case 2:
248
202
  _b.sent();
249
- _b.label = 4;
203
+ _b.label = 3;
204
+ case 3:
205
+ if (!!oidc.tokens) return [3 /*break*/, 5];
206
+ return [4 /*yield*/, oidc.destroyAsync(status)];
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
+ _b.sent();
209
+ return [2 /*return*/];
210
+ case 5:
259
211
  if (oidc.timeoutId) {
260
212
  oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
261
213
  }
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*/];
214
+ return [2 /*return*/];
271
215
  }
272
216
  });
273
217
  }); }, 1000);
@@ -275,8 +219,18 @@ var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
275
219
  var getLoginSessionKey = function (configurationName, redirectUri) {
276
220
  return "oidc_login.".concat(configurationName, ":").concat(redirectUri);
277
221
  };
222
+ var setLoginParams = function (configurationName, redirectUri, data) {
223
+ var sessionKey = getLoginSessionKey(configurationName, redirectUri);
224
+ getLoginParamsCache = data;
225
+ sessionStorage[sessionKey] = JSON.stringify(data);
226
+ };
227
+ var getLoginParamsCache = null;
278
228
  var getLoginParams = function (configurationName, redirectUri) {
279
- return JSON.parse(sessionStorage[getLoginSessionKey(configurationName, redirectUri)]);
229
+ var dataString = sessionStorage[getLoginSessionKey(configurationName, redirectUri)];
230
+ if (!getLoginParamsCache) {
231
+ getLoginParamsCache = JSON.parse(dataString);
232
+ }
233
+ return getLoginParamsCache;
280
234
  };
281
235
  var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, function () {
282
236
  var accessToken, oidcServerConfiguration, url, fetchUserInfo, userInfo;
@@ -289,15 +243,19 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
289
243
  if (!oidc.tokens) {
290
244
  return [2 /*return*/, null];
291
245
  }
292
- if (!oidc.syncTokensAsyncPromise) return [3 /*break*/, 2];
293
- return [4 /*yield*/, oidc.syncTokensAsyncPromise];
246
+ accessToken = oidc.tokens.accessToken;
247
+ if (!accessToken) {
248
+ return [2 /*return*/, null];
249
+ }
250
+ _a.label = 1;
294
251
  case 1:
295
- _a.sent();
296
- _a.label = 2;
252
+ if (!(oidc.tokens && !(0, parseTokens_1.isTokensValid)(oidc.tokens))) return [3 /*break*/, 3];
253
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(200)];
297
254
  case 2:
298
- accessToken = oidc.tokens.accessToken;
299
- return [4 /*yield*/, oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration)];
300
- case 3:
255
+ _a.sent();
256
+ return [3 /*break*/, 1];
257
+ case 3: return [4 /*yield*/, oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration)];
258
+ case 4:
301
259
  oidcServerConfiguration = _a.sent();
302
260
  url = oidcServerConfiguration.userInfoEndpoint;
303
261
  fetchUserInfo = function (accessToken) { return __awaiter(void 0, void 0, void 0, function () {
@@ -320,35 +278,13 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
320
278
  });
321
279
  }); };
322
280
  return [4 /*yield*/, fetchUserInfo(accessToken)];
323
- case 4:
281
+ case 5:
324
282
  userInfo = _a.sent();
325
283
  oidc.userInfo = userInfo;
326
284
  return [2 /*return*/, userInfo];
327
285
  }
328
286
  });
329
287
  }); };
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
288
  var eventNames = {
353
289
  service_worker_not_supported_by_browser: "service_worker_not_supported_by_browser",
354
290
  token_aquired: "token_aquired",
@@ -440,16 +376,16 @@ var buildQueries = function (extras) {
440
376
  var Oidc = /** @class */ (function () {
441
377
  function Oidc(configuration, configurationName) {
442
378
  if (configurationName === void 0) { configurationName = "default"; }
379
+ var _a, _b, _c;
443
380
  this.tryKeepExistingSessionPromise = null;
444
381
  this.loginPromise = null;
445
382
  this.loginCallbackPromise = null;
446
- this.syncTokensAsyncPromise = null;
447
383
  this.loginCallbackWithAutoTokensRenewPromise = null;
448
384
  var silent_login_uri = configuration.silent_login_uri;
449
385
  if (configuration.silent_redirect_uri && !configuration.silent_login_uri) {
450
386
  silent_login_uri = "".concat(configuration.silent_redirect_uri.replace("-callback", "").replace("callback", ""), "-login");
451
387
  }
452
- this.configuration = __assign(__assign({}, configuration), { silent_login_uri: silent_login_uri });
388
+ 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
389
  this.configurationName = configurationName;
454
390
  this.tokens = null;
455
391
  this.userInfo = null;
@@ -457,7 +393,7 @@ var Oidc = /** @class */ (function () {
457
393
  this.timeoutId = null;
458
394
  this.serviceWorker = null;
459
395
  this.session = null;
460
- this.refreshTokensAsync.bind(this);
396
+ this.synchroniseTokensAsync.bind(this);
461
397
  this.loginCallbackWithAutoTokensRenewAsync.bind(this);
462
398
  this.initAsync.bind(this);
463
399
  this.loginCallbackAsync.bind(this);
@@ -534,130 +470,106 @@ var Oidc = /** @class */ (function () {
534
470
  if (state === void 0) { state = null; }
535
471
  if (scope === void 0) { scope = null; }
536
472
  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;
473
+ var configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
538
474
  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)];
475
+ if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
476
+ return [2 /*return*/, Promise.resolve(null)];
477
+ }
478
+ try {
479
+ this.publishEvent(eventNames.silentLoginAsync_begin, {});
480
+ configuration_1 = this.configuration;
481
+ queries = "";
482
+ if (state) {
483
+ if (extras == null) {
484
+ extras = {};
543
485
  }
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;
486
+ extras.state = state;
487
+ }
488
+ if (scope) {
489
+ if (extras == null) {
490
+ extras = {};
491
+ }
492
+ extras.scope = scope;
493
+ }
494
+ if (extras != null) {
495
+ for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
496
+ _b = _a[_i], key = _b[0], value = _b[1];
497
+ if (queries === "") {
498
+ queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
579
499
  }
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
- }
500
+ else {
501
+ queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
590
502
  }
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 () {
503
+ }
504
+ }
505
+ link = configuration_1.silent_login_uri + queries;
506
+ idx = link.indexOf("/", link.indexOf("//") + 2);
507
+ iFrameOrigin_1 = link.substr(0, idx);
508
+ iframe_1 = document.createElement('iframe');
509
+ iframe_1.width = "0px";
510
+ iframe_1.height = "0px";
511
+ iframe_1.id = "".concat(this.configurationName, "_oidc_iframe");
512
+ iframe_1.setAttribute("src", link);
513
+ document.body.appendChild(iframe_1);
514
+ self_1 = this;
515
+ return [2 /*return*/, new Promise(function (resolve, reject) {
516
+ try {
517
+ var isResolved_1 = false;
518
+ window.onmessage = function (e) {
519
+ if (e.origin === iFrameOrigin_1 &&
520
+ e.source === iframe_1.contentWindow) {
521
+ var key = "".concat(self_1.configurationName, "_oidc_tokens:");
522
+ var key_error = "".concat(self_1.configurationName, "_oidc_error:");
523
+ var data = e.data;
524
+ if (data && typeof (data) === "string") {
633
525
  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"));
526
+ if (data.startsWith(key)) {
527
+ var result = JSON.parse(e.data.replace(key, ''));
528
+ self_1.publishEvent(eventNames.silentLoginAsync_end, {});
529
+ iframe_1.remove();
530
+ isResolved_1 = true;
531
+ resolve(result);
532
+ }
533
+ else if (data.startsWith(key_error)) {
534
+ var result = JSON.parse(e.data.replace(key_error, ''));
535
+ self_1.publishEvent(eventNames.silentLoginAsync_error, result);
536
+ iframe_1.remove();
537
+ isResolved_1 = true;
538
+ reject(new Error("oidc_" + result.error));
539
+ }
638
540
  }
639
- }, silentSigninTimeout);
541
+ }
640
542
  }
641
- catch (e) {
543
+ };
544
+ var silentSigninTimeout = configuration_1.silent_login_timeout;
545
+ setTimeout(function () {
546
+ if (!isResolved_1) {
547
+ self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
642
548
  iframe_1.remove();
643
- self_1.publishEvent(eventNames.silentLoginAsync_error, e);
644
- reject(e);
549
+ isResolved_1 = true;
550
+ reject(new Error("timeout"));
645
551
  }
646
- })];
647
- }
648
- catch (e) {
649
- this.publishEvent(eventNames.silentLoginAsync_error, e);
650
- throw e;
651
- }
652
- return [2 /*return*/];
552
+ }, silentSigninTimeout);
553
+ }
554
+ catch (e) {
555
+ iframe_1.remove();
556
+ self_1.publishEvent(eventNames.silentLoginAsync_error, e);
557
+ reject(e);
558
+ }
559
+ })];
560
+ }
561
+ catch (e) {
562
+ this.publishEvent(eventNames.silentLoginAsync_error, e);
563
+ throw e;
653
564
  }
565
+ return [2 /*return*/];
654
566
  });
655
567
  });
656
568
  };
657
569
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
658
570
  var _a;
659
571
  return __awaiter(this, void 0, void 0, function () {
660
- var serviceWorker, storage, initAsyncPromise;
572
+ var serviceWorker, storage;
661
573
  return __generator(this, function (_b) {
662
574
  switch (_b.label) {
663
575
  case 0:
@@ -676,9 +588,7 @@ var Oidc = /** @class */ (function () {
676
588
  serviceWorker = _b.sent();
677
589
  storage = serviceWorker ? window.localStorage : null;
678
590
  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];
591
+ case 2: return [2 /*return*/, _b.sent()];
682
592
  }
683
593
  });
684
594
  });
@@ -692,83 +602,68 @@ var Oidc = /** @class */ (function () {
692
602
  return [2 /*return*/, this.tryKeepExistingSessionPromise];
693
603
  }
694
604
  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) {
605
+ var serviceWorker, configuration, oidcServerConfiguration, tokens, sessionState, session, tokens, sessionState, exception_1;
606
+ var _a;
607
+ return __generator(this, function (_b) {
608
+ switch (_b.label) {
699
609
  case 0:
700
610
  if (this.tokens != null) {
701
611
  return [2 /*return*/, false];
702
612
  }
703
613
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_begin, {});
704
- _d.label = 1;
614
+ _b.label = 1;
705
615
  case 1:
706
- _d.trys.push([1, 14, , 17]);
616
+ _b.trys.push([1, 12, , 15]);
707
617
  configuration = this.configuration;
708
618
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
709
619
  case 2:
710
- oidcServerConfiguration = _d.sent();
620
+ oidcServerConfiguration = _b.sent();
711
621
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
712
622
  case 3:
713
- serviceWorker = _d.sent();
714
- if (!serviceWorker) return [3 /*break*/, 9];
623
+ serviceWorker = _b.sent();
624
+ if (!serviceWorker) return [3 /*break*/, 8];
715
625
  return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync")];
716
626
  case 4:
717
- tokens = (_d.sent()).tokens;
718
- if (!tokens) return [3 /*break*/, 8];
627
+ tokens = (_b.sent()).tokens;
628
+ if (!tokens) return [3 /*break*/, 7];
719
629
  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();
630
+ // @ts-ignore
631
+ this.tokens = tokens;
733
632
  this.serviceWorker = serviceWorker;
734
633
  // @ts-ignore
735
634
  this.timeoutId = autoRenewTokens(this, this.tokens.refreshToken, this.tokens.expiresAt);
736
635
  return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
737
- case 6:
738
- sessionState = _d.sent();
636
+ case 5:
637
+ sessionState = _b.sent();
739
638
  // @ts-ignore
740
639
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
741
- case 7:
640
+ case 6:
742
641
  // @ts-ignore
743
- _d.sent();
642
+ _b.sent();
744
643
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
745
644
  success: true,
746
645
  message: "tokens inside ServiceWorker are valid"
747
646
  });
748
647
  return [2 /*return*/, true];
749
- case 8:
648
+ case 7:
750
649
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
751
650
  success: false,
752
651
  message: "no exiting session found"
753
652
  });
754
- return [3 /*break*/, 13];
755
- case 9:
653
+ return [3 /*break*/, 11];
654
+ case 8:
756
655
  if (configuration.service_worker_relative_url) {
757
656
  this.publishEvent(eventNames.service_worker_not_supported_by_browser, {
758
657
  message: "service worker is not supported by this browser"
759
658
  });
760
659
  }
761
- session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_c = configuration.storage) !== null && _c !== void 0 ? _c : sessionStorage);
660
+ session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
762
661
  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:
662
+ case 9:
663
+ tokens = (_b.sent()).tokens;
664
+ if (!tokens) return [3 /*break*/, 11];
770
665
  // @ts-ignore
771
- _b.tokens = _d.sent();
666
+ this.tokens = (0, parseTokens_1.setTokens)(tokens);
772
667
  //session.setTokens(this.tokens);
773
668
  this.session = session;
774
669
  // @ts-ignore
@@ -776,32 +671,32 @@ var Oidc = /** @class */ (function () {
776
671
  sessionState = session.getSessionState();
777
672
  // @ts-ignore
778
673
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
779
- case 12:
674
+ case 10:
780
675
  // @ts-ignore
781
- _d.sent();
676
+ _b.sent();
782
677
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
783
678
  success: true,
784
679
  message: "tokens inside storage are valid"
785
680
  });
786
681
  return [2 /*return*/, true];
787
- case 13:
682
+ case 11:
788
683
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
789
684
  success: false,
790
685
  message: serviceWorker ? "service worker sessions not retrieved" : "session storage sessions not retrieved"
791
686
  });
792
687
  return [2 /*return*/, false];
793
- case 14:
794
- exception_1 = _d.sent();
688
+ case 12:
689
+ exception_1 = _b.sent();
795
690
  console.error(exception_1);
796
- if (!serviceWorker) return [3 /*break*/, 16];
691
+ if (!serviceWorker) return [3 /*break*/, 14];
797
692
  return [4 /*yield*/, serviceWorker.clearAsync()];
798
- case 15:
799
- _d.sent();
800
- _d.label = 16;
801
- case 16:
693
+ case 13:
694
+ _b.sent();
695
+ _b.label = 14;
696
+ case 14:
802
697
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_error, "tokens inside ServiceWorker are invalid");
803
698
  return [2 /*return*/, false];
804
- case 17: return [2 /*return*/];
699
+ case 15: return [2 /*return*/];
805
700
  }
806
701
  });
807
702
  }); };
@@ -813,10 +708,9 @@ var Oidc = /** @class */ (function () {
813
708
  });
814
709
  });
815
710
  };
816
- Oidc.prototype.loginAsync = function (callbackPath, extras, installServiceWorker, state, isSilentSignin, scope) {
711
+ Oidc.prototype.loginAsync = function (callbackPath, extras, state, isSilentSignin, scope) {
817
712
  if (callbackPath === void 0) { callbackPath = undefined; }
818
713
  if (extras === void 0) { extras = null; }
819
- if (installServiceWorker === void 0) { installServiceWorker = true; }
820
714
  if (state === void 0) { state = undefined; }
821
715
  if (isSilentSignin === void 0) { isSilentSignin = false; }
822
716
  if (scope === void 0) { scope = undefined; }
@@ -828,7 +722,7 @@ var Oidc = /** @class */ (function () {
828
722
  return [2 /*return*/, this.loginPromise];
829
723
  }
830
724
  loginLocalAsync = function () { return __awaiter(_this, void 0, void 0, function () {
831
- var location_1, url, configuration, redirectUri, sessionKey, serviceWorker, oidcServerConfiguration, storage, session, extraFinal, queryStringUtil, authorizationHandler, authRequest, exception_2;
725
+ var location_1, url, configuration, redirectUri, serviceWorker, oidcServerConfiguration, storage, session, extraFinal, queryStringUtil, authorizationHandler, authRequest, exception_2;
832
726
  var _a;
833
727
  return __generator(this, function (_b) {
834
728
  switch (_b.label) {
@@ -842,8 +736,7 @@ var Oidc = /** @class */ (function () {
842
736
  if (!scope) {
843
737
  scope = configuration.scope;
844
738
  }
845
- sessionKey = getLoginSessionKey(this.configurationName, redirectUri);
846
- sessionStorage[sessionKey] = JSON.stringify({ callbackPath: url, extras: extras, state: state });
739
+ setLoginParams(this.configurationName, redirectUri, { callbackPath: url, extras: extras, state: state });
847
740
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
848
741
  case 1:
849
742
  serviceWorker = _b.sent();
@@ -942,7 +835,7 @@ var Oidc = /** @class */ (function () {
942
835
  _b = _a[_i], key = _b[0], oidc = _b[1];
943
836
  //if(oidc !== this) {
944
837
  // @ts-ignore
945
- return [4 /*yield*/, oidc.logoutOtherTabAsync(idTokenPayload.sub)];
838
+ return [4 /*yield*/, oidc.logoutOtherTabAsync(this.configuration.client_id, idTokenPayload.sub)];
946
839
  case 2:
947
840
  //if(oidc !== this) {
948
841
  // @ts-ignore
@@ -988,16 +881,14 @@ var Oidc = /** @class */ (function () {
988
881
  case 1:
989
882
  response = _a.sent();
990
883
  tokens = response.tokens;
991
- return [4 /*yield*/, setTokensAsync(this.serviceWorker, tokens)];
992
- case 2:
993
- parsedTokens = _a.sent();
884
+ parsedTokens = (0, parseTokens_1.setTokens)(tokens);
994
885
  this.tokens = parsedTokens;
995
- if (!!this.serviceWorker) return [3 /*break*/, 4];
886
+ if (!!this.serviceWorker) return [3 /*break*/, 3];
996
887
  return [4 /*yield*/, this.session.setTokens(parsedTokens)];
997
- case 3:
888
+ case 2:
998
889
  _a.sent();
999
- _a.label = 4;
1000
- case 4:
890
+ _a.label = 3;
891
+ case 3:
1001
892
  this.publishEvent(Oidc.eventNames.token_aquired, parsedTokens);
1002
893
  // @ts-ignore
1003
894
  return [2 /*return*/, { parsedTokens: parsedTokens, state: response.state, callbackPath: response.callbackPath }];
@@ -1155,210 +1046,207 @@ var Oidc = /** @class */ (function () {
1155
1046
  });
1156
1047
  });
1157
1048
  };
1158
- Oidc.prototype.refreshTokensAsync = function (refreshToken) {
1049
+ Oidc.prototype.synchroniseTokensAsync = function (refreshToken, index) {
1050
+ if (index === void 0) { index = 0; }
1159
1051
  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;
1052
+ var numberTryOnline, configuration, localsilentLoginAsync, _a, status_1, tokens, _b, clientId, redirectUri, authority, extras, _i, _c, _d, key, value, details, oidcServerConfiguration, tokenResponse, exception_4;
1161
1053
  var _this = this;
1162
- return __generator(this, function (_c) {
1163
- switch (_c.label) {
1054
+ return __generator(this, function (_e) {
1055
+ switch (_e.label) {
1164
1056
  case 0:
1057
+ if (!document.hidden) return [3 /*break*/, 3];
1058
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1059
+ case 1:
1060
+ _e.sent();
1061
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1062
+ return [4 /*yield*/, this.synchroniseTokensAsync(refreshToken, index)];
1063
+ case 2: return [2 /*return*/, _e.sent()];
1064
+ case 3:
1065
+ numberTryOnline = 6;
1066
+ _e.label = 4;
1067
+ case 4:
1068
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 6];
1069
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1070
+ case 5:
1071
+ _e.sent();
1072
+ numberTryOnline--;
1073
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1074
+ return [3 /*break*/, 4];
1075
+ case 6:
1076
+ configuration = this.configuration;
1165
1077
  localsilentLoginAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1166
1078
  var loginParams, silent_token_response, exceptionSilent_1;
1167
1079
  return __generator(this, function (_a) {
1168
1080
  switch (_a.label) {
1169
1081
  case 0:
1170
- _a.trys.push([0, 2, , 3]);
1082
+ _a.trys.push([0, 2, , 4]);
1171
1083
  loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1172
- return [4 /*yield*/, this.silentLoginAsync(loginParams.extras, loginParams.state)];
1084
+ return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1173
1085
  case 1:
1174
1086
  silent_token_response = _a.sent();
1175
1087
  if (silent_token_response) {
1176
- return [2 /*return*/, silent_token_response.tokens];
1088
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1089
+ return [2 /*return*/, { tokens: silent_token_response.tokens, status: "LOGGED" }];
1177
1090
  }
1178
- return [3 /*break*/, 3];
1091
+ return [3 /*break*/, 4];
1179
1092
  case 2:
1180
1093
  exceptionSilent_1 = _a.sent();
1181
1094
  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;
1095
+ this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exceptionSilent", exception: exceptionSilent_1.message });
1096
+ if (exceptionSilent_1 && exceptionSilent_1.message && exceptionSilent_1.message.startsWith("oidc")) {
1097
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token silent" });
1098
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1187
1099
  }
1188
- this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token and silent refresh failed" });
1189
- return [2 /*return*/, null];
1100
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1101
+ case 3:
1102
+ _a.sent();
1103
+ throw exceptionSilent_1;
1104
+ case 4:
1105
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token silent return" });
1106
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1190
1107
  }
1191
1108
  });
1192
1109
  }); };
1193
- configuration = this.configuration;
1110
+ if (!(index <= 4)) return [3 /*break*/, 24];
1111
+ _e.label = 7;
1112
+ case 7:
1113
+ _e.trys.push([7, 23, , 24]);
1114
+ if (!!refreshToken) return [3 /*break*/, 9];
1115
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1116
+ return [4 /*yield*/, localsilentLoginAsync()];
1117
+ case 8: return [2 /*return*/, _e.sent()];
1118
+ case 9: return [4 /*yield*/, this.syncTokensInfoAsync(configuration, this.configurationName, this.tokens)];
1119
+ case 10:
1120
+ _a = _e.sent(), status_1 = _a.status, tokens = _a.tokens;
1121
+ _b = status_1;
1122
+ switch (_b) {
1123
+ case "SESSION_LOST": return [3 /*break*/, 11];
1124
+ case "NOT_CONNECTED": return [3 /*break*/, 12];
1125
+ case "TOKENS_VALID": return [3 /*break*/, 13];
1126
+ case "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID": return [3 /*break*/, 13];
1127
+ case "LOGOUT_FROM_ANOTHER_TAB": return [3 /*break*/, 14];
1128
+ case "REQUIRE_SYNC_TOKENS": return [3 /*break*/, 15];
1129
+ }
1130
+ return [3 /*break*/, 17];
1131
+ case 11:
1132
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token session lost" });
1133
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1134
+ case 12: return [2 /*return*/, { tokens: null, status: null }];
1135
+ case 13: return [2 /*return*/, { tokens: tokens, status: "LOGGED_IN" }];
1136
+ case 14:
1137
+ this.publishEvent(eventNames.logout_from_another_tab, { "status": "session syncTokensAsync" });
1138
+ return [2 /*return*/, { tokens: null, status: "LOGGED_OUT" }];
1139
+ case 15:
1140
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, status: status_1, tryNumber: index });
1141
+ return [4 /*yield*/, localsilentLoginAsync()];
1142
+ case 16: return [2 /*return*/, _e.sent()];
1143
+ case 17:
1144
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, status: status_1, tryNumber: index });
1194
1145
  clientId = configuration.client_id;
1195
1146
  redirectUri = configuration.redirect_uri;
1196
1147
  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
1148
  extras = {};
1202
1149
  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];
1150
+ for (_i = 0, _c = Object.entries(configuration.token_request_extras); _i < _c.length; _i++) {
1151
+ _d = _c[_i], key = _d[0], value = _d[1];
1205
1152
  extras[key] = value;
1206
1153
  }
1207
1154
  }
1208
- return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1209
- case 3:
1210
- oidcServerConfiguration = _c.sent();
1211
1155
  details = {
1212
1156
  client_id: clientId,
1213
1157
  redirect_uri: redirectUri,
1214
1158
  grant_type: appauth_1.GRANT_TYPE_REFRESH_TOKEN,
1215
- refresh_token: refreshToken,
1159
+ refresh_token: tokens.refreshToken,
1216
1160
  };
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];
1161
+ return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1162
+ case 18:
1163
+ oidcServerConfiguration = _e.sent();
1164
+ return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1165
+ case 19:
1166
+ tokenResponse = _e.sent();
1167
+ if (!tokenResponse.success) return [3 /*break*/, 20];
1249
1168
  this.publishEvent(eventNames.refreshTokensAsync_end, { success: tokenResponse.success });
1250
- 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();
1169
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1170
+ return [2 /*return*/, { tokens: tokenResponse.data, status: "LOGGED_IN" }];
1171
+ case 20:
1172
+ this.publishEvent(eventNames.refreshTokensAsync_silent_error, {
1173
+ message: "bad request",
1174
+ tokenResponse: tokenResponse
1175
+ });
1176
+ return [4 /*yield*/, this.synchroniseTokensAsync(null, index + 1)];
1177
+ case 21: return [2 /*return*/, _e.sent()];
1178
+ case 22: return [3 /*break*/, 24];
1179
+ case 23:
1180
+ exception_4 = _e.sent();
1258
1181
  console.error(exception_4);
1259
1182
  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*/];
1183
+ return [2 /*return*/, this.synchroniseTokensAsync(refreshToken, index + 1)];
1184
+ case 24:
1185
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token" });
1186
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1265
1187
  }
1266
1188
  });
1267
1189
  });
1268
1190
  };
1269
- Oidc.prototype.syncTokensAsync = function () {
1191
+ Oidc.prototype.syncTokensInfoAsync = function (configuration, configurationName, currentTokens) {
1192
+ var _a;
1270
1193
  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];
1194
+ var oidcServerConfiguration, serviceWorker, _b, status_2, tokens, timeLeft_1, status_3, session, _c, tokens, status_4, timeLeft_2, status_5, timeLeft, status;
1195
+ return __generator(this, function (_d) {
1196
+ switch (_d.label) {
1197
+ case 0:
1198
+ // 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)
1199
+ //const configuration = this.configuration;
1200
+ if (!currentTokens) {
1201
+ return [2 /*return*/, { tokens: null, status: "NOT_CONNECTED" }];
1349
1202
  }
1350
- });
1351
- }); };
1352
- if (this.syncTokensAsyncPromise) {
1353
- return [2 /*return*/, this.syncTokensAsyncPromise];
1203
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1204
+ case 1:
1205
+ oidcServerConfiguration = _d.sent();
1206
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, configurationName)];
1207
+ case 2:
1208
+ serviceWorker = _d.sent();
1209
+ if (!serviceWorker) return [3 /*break*/, 4];
1210
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1211
+ case 3:
1212
+ _b = _d.sent(), status_2 = _b.status, tokens = _b.tokens;
1213
+ if (status_2 == "LOGGED_OUT") {
1214
+ return [2 /*return*/, { tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB" }];
1215
+ }
1216
+ else if (status_2 == "SESSIONS_LOST") {
1217
+ return [2 /*return*/, { tokens: null, status: "SESSIONS_LOST" }];
1218
+ }
1219
+ else if (!status_2 || !tokens) {
1220
+ return [2 /*return*/, { tokens: null, status: "REQUIRE_SYNC_TOKENS" }];
1221
+ }
1222
+ else if (tokens.issuedAt !== currentTokens.issuedAt) {
1223
+ timeLeft_1 = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, tokens.expiresAt);
1224
+ status_3 = (timeLeft_1 > 0) ? "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID" : "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_INVALID";
1225
+ return [2 /*return*/, { tokens: tokens, status: status_3 }];
1226
+ }
1227
+ return [3 /*break*/, 6];
1228
+ case 4:
1229
+ session = (0, initSession_1.initSession)(configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
1230
+ return [4 /*yield*/, session.initAsync()];
1231
+ case 5:
1232
+ _c = _d.sent(), tokens = _c.tokens, status_4 = _c.status;
1233
+ if (!tokens) {
1234
+ return [2 /*return*/, { tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB" }];
1235
+ }
1236
+ else if (status_4 == "SESSIONS_LOST") {
1237
+ return [2 /*return*/, { tokens: null, status: "SESSIONS_LOST" }];
1238
+ }
1239
+ else if (tokens.issuedAt !== currentTokens.issuedAt) {
1240
+ timeLeft_2 = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, tokens.expiresAt);
1241
+ status_5 = (timeLeft_2 > 0) ? "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID" : "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_INVALID";
1242
+ return [2 /*return*/, { tokens: tokens, status: status_5 }];
1243
+ }
1244
+ _d.label = 6;
1245
+ case 6:
1246
+ timeLeft = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, currentTokens.expiresAt);
1247
+ status = (timeLeft > 0) ? "TOKENS_VALID" : "TOKENS_INVALID";
1248
+ return [2 /*return*/, { tokens: currentTokens, status: status }];
1354
1249
  }
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
1250
  });
1363
1251
  });
1364
1252
  };
@@ -1376,7 +1264,7 @@ var Oidc = /** @class */ (function () {
1376
1264
  Oidc.prototype.userInfoAsync = function () {
1377
1265
  return userInfoAsync(this);
1378
1266
  };
1379
- Oidc.prototype.destroyAsync = function () {
1267
+ Oidc.prototype.destroyAsync = function (status) {
1380
1268
  return __awaiter(this, void 0, void 0, function () {
1381
1269
  return __generator(this, function (_a) {
1382
1270
  switch (_a.label) {
@@ -1387,13 +1275,13 @@ var Oidc = /** @class */ (function () {
1387
1275
  this.checkSessionIFrame.stop();
1388
1276
  }
1389
1277
  if (!this.serviceWorker) return [3 /*break*/, 2];
1390
- return [4 /*yield*/, this.serviceWorker.clearAsync()];
1278
+ return [4 /*yield*/, this.serviceWorker.clearAsync(status)];
1391
1279
  case 1:
1392
1280
  _a.sent();
1393
1281
  _a.label = 2;
1394
1282
  case 2:
1395
1283
  if (!this.session) return [3 /*break*/, 4];
1396
- return [4 /*yield*/, this.session.clearAsync()];
1284
+ return [4 /*yield*/, this.session.clearAsync(status)];
1397
1285
  case 3:
1398
1286
  _a.sent();
1399
1287
  _a.label = 4;
@@ -1405,14 +1293,14 @@ var Oidc = /** @class */ (function () {
1405
1293
  });
1406
1294
  });
1407
1295
  };
1408
- Oidc.prototype.logoutSameTabAsync = function (sub) {
1296
+ Oidc.prototype.logoutSameTabAsync = function (clientId, sub) {
1409
1297
  return __awaiter(this, void 0, void 0, function () {
1410
1298
  return __generator(this, function (_a) {
1411
1299
  switch (_a.label) {
1412
1300
  case 0:
1413
- if (!(this.configuration.monitor_session && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1301
+ if (!(this.configuration.monitor_session && this.configuration.client_id === clientId && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1414
1302
  this.publishEvent(eventNames.logout_from_same_tab, { "message": sub });
1415
- return [4 /*yield*/, this.destroyAsync()];
1303
+ return [4 /*yield*/, this.destroyAsync("LOGGED_OUT")];
1416
1304
  case 1:
1417
1305
  _a.sent();
1418
1306
  _a.label = 2;
@@ -1421,16 +1309,15 @@ var Oidc = /** @class */ (function () {
1421
1309
  });
1422
1310
  });
1423
1311
  };
1424
- Oidc.prototype.logoutOtherTabAsync = function (sub) {
1312
+ Oidc.prototype.logoutOtherTabAsync = function (clientId, sub) {
1425
1313
  return __awaiter(this, void 0, void 0, function () {
1426
1314
  return __generator(this, function (_a) {
1427
1315
  switch (_a.label) {
1428
1316
  case 0:
1429
- if (!(this.configuration.monitor_session && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1430
- return [4 /*yield*/, this.destroyAsync()];
1317
+ if (!(this.configuration.monitor_session && this.configuration.client_id === clientId && sub && this.tokens && this.tokens.idTokenPayload && this.tokens.idTokenPayload.sub === sub)) return [3 /*break*/, 2];
1318
+ return [4 /*yield*/, this.destroyAsync("LOGGED_OUT")];
1431
1319
  case 1:
1432
1320
  _a.sent();
1433
- console.log("logoutOtherTabAsync(sub)" + this.configurationName);
1434
1321
  this.publishEvent(eventNames.logout_from_another_tab, { message: "SessionMonitor", "sub": sub });
1435
1322
  _a.label = 2;
1436
1323
  case 2: return [2 /*return*/];
@@ -1462,7 +1349,7 @@ var Oidc = /** @class */ (function () {
1462
1349
  url = isUri ? callbackPathOrUrl : window.location.origin + path;
1463
1350
  idToken = this.tokens ? this.tokens.idToken : "";
1464
1351
  sub = this.tokens && this.tokens.idTokenPayload ? this.tokens.idTokenPayload.sub : null;
1465
- return [4 /*yield*/, this.destroyAsync()];
1352
+ return [4 /*yield*/, this.destroyAsync("LOGGED_OUT")];
1466
1353
  case 2:
1467
1354
  _f.sent();
1468
1355
  _i = 0, _a = Object.entries(oidcDatabase);
@@ -1472,7 +1359,7 @@ var Oidc = /** @class */ (function () {
1472
1359
  _b = _a[_i], key = _b[0], oidc = _b[1];
1473
1360
  if (!(oidc !== this)) return [3 /*break*/, 5];
1474
1361
  // @ts-ignore
1475
- return [4 /*yield*/, oidc.logoutSameTabAsync(sub)];
1362
+ return [4 /*yield*/, oidc.logoutSameTabAsync(this.configuration.client_id, sub)];
1476
1363
  case 4:
1477
1364
  // @ts-ignore
1478
1365
  _f.sent();