@axa-fr/react-oidc 6.0.0-beta8 → 6.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +13 -4
  2. package/dist/FetchToken.d.ts.map +1 -1
  3. package/dist/FetchToken.js +10 -6
  4. package/dist/FetchToken.js.map +1 -1
  5. package/dist/OidcProvider.d.ts +1 -0
  6. package/dist/OidcProvider.d.ts.map +1 -1
  7. package/dist/OidcProvider.js +11 -4
  8. package/dist/OidcProvider.js.map +1 -1
  9. package/dist/OidcSecure.js +2 -2
  10. package/dist/OidcSecure.js.map +1 -1
  11. package/dist/OidcServiceWorker.js +62 -32
  12. package/dist/OidcTrustedDomains.js +7 -2
  13. package/dist/ReactOidc.d.ts.map +1 -1
  14. package/dist/ReactOidc.js +4 -3
  15. package/dist/ReactOidc.js.map +1 -1
  16. package/dist/core/default-component/SilentLogin.component.js +1 -1
  17. package/dist/core/default-component/SilentLogin.component.js.map +1 -1
  18. package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
  19. package/dist/core/routes/OidcRoutes.js +1 -4
  20. package/dist/core/routes/OidcRoutes.js.map +1 -1
  21. package/dist/vanilla/initSession.d.ts +2 -1
  22. package/dist/vanilla/initSession.d.ts.map +1 -1
  23. package/dist/vanilla/initSession.js +7 -7
  24. package/dist/vanilla/initSession.js.map +1 -1
  25. package/dist/vanilla/initWorker.d.ts +2 -3
  26. package/dist/vanilla/initWorker.d.ts.map +1 -1
  27. package/dist/vanilla/initWorker.js +6 -21
  28. package/dist/vanilla/initWorker.js.map +1 -1
  29. package/dist/vanilla/oidc.d.ts +10 -5
  30. package/dist/vanilla/oidc.d.ts.map +1 -1
  31. package/dist/vanilla/oidc.js +462 -469
  32. package/dist/vanilla/oidc.js.map +1 -1
  33. package/dist/vanilla/parseTokens.d.ts +5 -0
  34. package/dist/vanilla/parseTokens.d.ts.map +1 -0
  35. package/dist/vanilla/parseTokens.js +107 -0
  36. package/dist/vanilla/parseTokens.js.map +1 -0
  37. package/package.json +3 -3
  38. package/src/oidc/FetchToken.tsx +7 -4
  39. package/src/oidc/OidcProvider.tsx +9 -0
  40. package/src/oidc/OidcSecure.tsx +2 -2
  41. package/src/oidc/ReactOidc.tsx +4 -3
  42. package/src/oidc/core/default-component/SilentLogin.component.tsx +1 -1
  43. package/src/oidc/core/routes/OidcRoutes.tsx +0 -4
  44. package/src/oidc/vanilla/OidcServiceWorker.js +62 -32
  45. package/src/oidc/vanilla/OidcTrustedDomains.js +7 -2
  46. package/src/oidc/vanilla/initSession.ts +6 -7
  47. package/src/oidc/vanilla/initWorker.ts +6 -15
  48. package/src/oidc/vanilla/oidc.ts +221 -265
  49. package/src/oidc/vanilla/parseTokens.ts +107 -0
  50. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts +0 -4
  51. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts.map +0 -1
  52. package/dist/core/default-component/ServiceWorkerInstall.component.js +0 -131
  53. package/dist/core/default-component/ServiceWorkerInstall.component.js.map +0 -1
  54. package/src/oidc/core/default-component/ServiceWorkerInstall.component.tsx +0 -60
@@ -74,8 +74,9 @@ var initSession_1 = require("./initSession");
74
74
  var timer_1 = __importDefault(require("./timer"));
75
75
  var checkSessionIFrame_1 = require("./checkSessionIFrame");
76
76
  var route_utils_1 = require("./route-utils");
77
+ var parseTokens_1 = require("./parseTokens");
77
78
  var performTokenRequestAsync = function (url, details, extras) { return __awaiter(void 0, void 0, void 0, function () {
78
- var _i, _a, _b, key, value, formBody, property, encodedKey, encodedValue, formBodyString, response, tokens, currentTimeUnixSecond;
79
+ var _i, _a, _b, key, value, formBody, property, encodedKey, encodedValue, formBodyString, response, tokens;
79
80
  return __generator(this, function (_c) {
80
81
  switch (_c.label) {
81
82
  case 0:
@@ -107,20 +108,9 @@ var performTokenRequestAsync = function (url, details, extras) { return __awaite
107
108
  return [4 /*yield*/, response.json()];
108
109
  case 2:
109
110
  tokens = _c.sent();
110
- if (!tokens.issued_at) {
111
- currentTimeUnixSecond = new Date().getTime() / 1000;
112
- tokens.issued_at = currentTimeUnixSecond;
113
- }
114
- return [2 /*return*/, { success: true,
115
- data: {
116
- accessToken: tokens.access_token,
117
- expiresIn: tokens.expires_in,
118
- idToken: tokens.id_token,
119
- refreshToken: tokens.refresh_token,
120
- scope: tokens.scope,
121
- tokenType: tokens.token_type,
122
- issuedAt: tokens.issued_at
123
- }
111
+ return [2 /*return*/, {
112
+ success: true,
113
+ data: (0, parseTokens_1.parseOriginalTokens)(tokens)
124
114
  }];
125
115
  }
126
116
  });
@@ -171,33 +161,6 @@ var OidcAuthorizationServiceConfiguration = /** @class */ (function (_super) {
171
161
  return OidcAuthorizationServiceConfiguration;
172
162
  }(appauth_1.AuthorizationServiceConfiguration));
173
163
  exports.OidcAuthorizationServiceConfiguration = OidcAuthorizationServiceConfiguration;
174
- var idTokenPayload = function (token) {
175
- var base64Url = token.split('.')[1];
176
- var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
177
- var jsonPayload = decodeURIComponent(atob(base64).split('').map(function (c) {
178
- return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
179
- }).join(''));
180
- return JSON.parse(jsonPayload);
181
- };
182
- var countLetter = function (str, find) {
183
- return (str.split(find)).length - 1;
184
- };
185
- var extractAccessTokenPayload = function (tokens) {
186
- if (tokens.accessTokenPayload) {
187
- return tokens.accessTokenPayload;
188
- }
189
- var accessToken = tokens.accessToken;
190
- try {
191
- if (!accessToken || countLetter(accessToken, '.') != 2) {
192
- return null;
193
- }
194
- return JSON.parse(atob(accessToken.split('.')[1]));
195
- }
196
- catch (e) {
197
- console.warn(e);
198
- }
199
- return null;
200
- };
201
164
  var oidcDatabase = {};
202
165
  var oidcFactory = function (configuration, name) {
203
166
  if (name === void 0) { name = "default"; }
@@ -220,50 +183,35 @@ var loginCallbackWithAutoTokensRenewAsync = function (oidc) { return __awaiter(v
220
183
  });
221
184
  }); };
222
185
  var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
223
- var _a;
224
- var refreshTimeBeforeTokensExpirationInSecond = (_a = oidc.configuration.refresh_time_before_tokens_expiration_in_second) !== null && _a !== void 0 ? _a : 60;
186
+ var refreshTimeBeforeTokensExpirationInSecond = oidc.configuration.refresh_time_before_tokens_expiration_in_second;
225
187
  return timer_1.default.setTimeout(function () { return __awaiter(void 0, void 0, void 0, function () {
226
- var currentTimeUnixSecond, timeInfo, tokens, _a, tokens;
188
+ var timeLeft, timeInfo, _a, tokens, status;
227
189
  return __generator(this, function (_b) {
228
190
  switch (_b.label) {
229
191
  case 0:
230
- currentTimeUnixSecond = new Date().getTime() / 1000;
231
- timeInfo = { timeLeft: Math.round(((expiresAt - refreshTimeBeforeTokensExpirationInSecond) - currentTimeUnixSecond)) };
192
+ timeLeft = (0, parseTokens_1.computeTimeLeft)(refreshTimeBeforeTokensExpirationInSecond, expiresAt);
193
+ timeInfo = { timeLeft: timeLeft };
232
194
  oidc.publishEvent(Oidc.eventNames.token_timer, timeInfo);
233
- if (!(currentTimeUnixSecond > (expiresAt - refreshTimeBeforeTokensExpirationInSecond))) return [3 /*break*/, 5];
234
- return [4 /*yield*/, oidc.refreshTokensAsync(refreshToken)];
195
+ return [4 /*yield*/, oidc.synchroniseTokensAsync(refreshToken)];
235
196
  case 1:
236
- tokens = _b.sent();
237
- _a = oidc;
238
- return [4 /*yield*/, setTokensAsync(oidc.serviceWorker, tokens)];
239
- case 2:
240
- _a.tokens = _b.sent();
241
- if (!!oidc.serviceWorker) return [3 /*break*/, 4];
197
+ _a = _b.sent(), tokens = _a.tokens, status = _a.status;
198
+ oidc.tokens = tokens;
199
+ if (!!oidc.serviceWorker) return [3 /*break*/, 3];
242
200
  return [4 /*yield*/, oidc.session.setTokens(oidc.tokens)];
243
- case 3:
201
+ case 2:
244
202
  _b.sent();
245
- _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)];
246
207
  case 4:
247
- if (!oidc.tokens) {
248
- if (oidc.checkSessionIFrame) {
249
- oidc.checkSessionIFrame.stop();
250
- oidc.checkSessionIFrame = null;
251
- }
252
- return [2 /*return*/];
253
- }
254
- oidc.publishEvent(Oidc.eventNames.token_renewed, {});
208
+ _b.sent();
209
+ return [2 /*return*/];
210
+ case 5:
255
211
  if (oidc.timeoutId) {
256
212
  oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
257
213
  }
258
- return [3 /*break*/, 7];
259
- case 5: return [4 /*yield*/, oidc.syncTokensAsync()];
260
- case 6:
261
- tokens = _b.sent();
262
- if (tokens && oidc.timeoutId) {
263
- oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, expiresAt);
264
- }
265
- _b.label = 7;
266
- case 7: return [2 /*return*/];
214
+ return [2 /*return*/];
267
215
  }
268
216
  });
269
217
  }); }, 1000);
@@ -271,8 +219,18 @@ var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
271
219
  var getLoginSessionKey = function (configurationName, redirectUri) {
272
220
  return "oidc_login.".concat(configurationName, ":").concat(redirectUri);
273
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;
274
228
  var getLoginParams = function (configurationName, redirectUri) {
275
- 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;
276
234
  };
277
235
  var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, function () {
278
236
  var accessToken, oidcServerConfiguration, url, fetchUserInfo, userInfo;
@@ -285,15 +243,19 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
285
243
  if (!oidc.tokens) {
286
244
  return [2 /*return*/, null];
287
245
  }
288
- if (!oidc.syncTokensAsyncPromise) return [3 /*break*/, 2];
289
- return [4 /*yield*/, oidc.syncTokensAsyncPromise];
246
+ accessToken = oidc.tokens.accessToken;
247
+ if (!accessToken) {
248
+ return [2 /*return*/, null];
249
+ }
250
+ _a.label = 1;
290
251
  case 1:
291
- _a.sent();
292
- _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)];
293
254
  case 2:
294
- accessToken = oidc.tokens.accessToken;
295
- return [4 /*yield*/, oidc.initAsync(oidc.configuration.authority, oidc.configuration.authority_configuration)];
296
- 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:
297
259
  oidcServerConfiguration = _a.sent();
298
260
  url = oidcServerConfiguration.userInfoEndpoint;
299
261
  fetchUserInfo = function (accessToken) { return __awaiter(void 0, void 0, void 0, function () {
@@ -316,47 +278,18 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
316
278
  });
317
279
  }); };
318
280
  return [4 /*yield*/, fetchUserInfo(accessToken)];
319
- case 4:
281
+ case 5:
320
282
  userInfo = _a.sent();
321
283
  oidc.userInfo = userInfo;
322
284
  return [2 /*return*/, userInfo];
323
285
  }
324
286
  });
325
287
  }); };
326
- var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0, void 0, void 0, function () {
327
- var accessTokenPayload, _idTokenPayload, idTokenExipreAt, accessTokenExpiresAt, expiresAt;
328
- return __generator(this, function (_a) {
329
- switch (_a.label) {
330
- case 0:
331
- if (!(tokens == null)) return [3 /*break*/, 3];
332
- if (!serviceWorker) return [3 /*break*/, 2];
333
- return [4 /*yield*/, serviceWorker.clearAsync()];
334
- case 1:
335
- _a.sent();
336
- _a.label = 2;
337
- case 2: return [2 /*return*/, null];
338
- case 3:
339
- if (!serviceWorker) return [3 /*break*/, 5];
340
- return [4 /*yield*/, serviceWorker.getAccessTokenPayloadAsync()];
341
- case 4:
342
- accessTokenPayload = _a.sent();
343
- return [3 /*break*/, 6];
344
- case 5:
345
- accessTokenPayload = extractAccessTokenPayload(tokens);
346
- _a.label = 6;
347
- case 6:
348
- _idTokenPayload = idTokenPayload(tokens.idToken);
349
- idTokenExipreAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : Number.MAX_VALUE;
350
- accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp) ? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
351
- expiresAt = idTokenExipreAt < accessTokenExpiresAt ? idTokenExipreAt : accessTokenExpiresAt;
352
- return [2 /*return*/, __assign(__assign({}, tokens), { idTokenPayload: _idTokenPayload, accessTokenPayload: accessTokenPayload, expiresAt: expiresAt })];
353
- }
354
- });
355
- }); };
356
288
  var eventNames = {
357
289
  service_worker_not_supported_by_browser: "service_worker_not_supported_by_browser",
358
290
  token_aquired: "token_aquired",
359
291
  logout_from_another_tab: "logout_from_another_tab",
292
+ logout_from_same_tab: "logout_from_same_tab",
360
293
  token_renewed: "token_renewed",
361
294
  token_timer: "token_timer",
362
295
  loginAsync_begin: "loginAsync_begin",
@@ -443,16 +376,16 @@ var buildQueries = function (extras) {
443
376
  var Oidc = /** @class */ (function () {
444
377
  function Oidc(configuration, configurationName) {
445
378
  if (configurationName === void 0) { configurationName = "default"; }
379
+ var _a, _b, _c;
446
380
  this.tryKeepExistingSessionPromise = null;
447
381
  this.loginPromise = null;
448
382
  this.loginCallbackPromise = null;
449
- this.syncTokensAsyncPromise = null;
450
383
  this.loginCallbackWithAutoTokensRenewPromise = null;
451
384
  var silent_login_uri = configuration.silent_login_uri;
452
385
  if (configuration.silent_redirect_uri && !configuration.silent_login_uri) {
453
386
  silent_login_uri = "".concat(configuration.silent_redirect_uri.replace("-callback", "").replace("callback", ""), "-login");
454
387
  }
455
- 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 });
456
389
  this.configurationName = configurationName;
457
390
  this.tokens = null;
458
391
  this.userInfo = null;
@@ -460,7 +393,7 @@ var Oidc = /** @class */ (function () {
460
393
  this.timeoutId = null;
461
394
  this.serviceWorker = null;
462
395
  this.session = null;
463
- this.refreshTokensAsync.bind(this);
396
+ this.synchroniseTokensAsync.bind(this);
464
397
  this.loginCallbackWithAutoTokensRenewAsync.bind(this);
465
398
  this.initAsync.bind(this);
466
399
  this.loginCallbackAsync.bind(this);
@@ -539,117 +472,104 @@ var Oidc = /** @class */ (function () {
539
472
  return __awaiter(this, void 0, void 0, function () {
540
473
  var configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
541
474
  return __generator(this, function (_c) {
542
- switch (_c.label) {
543
- case 0:
544
- if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
545
- 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 = {};
546
485
  }
547
- _c.label = 1;
548
- case 1:
549
- if (!document.hidden) return [3 /*break*/, 3];
550
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
551
- case 2:
552
- _c.sent();
553
- this.publishEvent(eventNames.silentLoginAsync, { message: "wait because document is hidden" });
554
- return [3 /*break*/, 1];
555
- case 3:
556
- try {
557
- this.publishEvent(eventNames.silentLoginAsync_begin, {});
558
- configuration_1 = this.configuration;
559
- queries = "";
560
- if (state) {
561
- if (extras == null) {
562
- extras = {};
563
- }
564
- extras.state = state;
565
- }
566
- if (scope) {
567
- if (extras == null) {
568
- extras = {};
569
- }
570
- 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));
571
499
  }
572
- if (extras != null) {
573
- for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
574
- _b = _a[_i], key = _b[0], value = _b[1];
575
- if (queries === "") {
576
- queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
577
- }
578
- else {
579
- queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
580
- }
581
- }
500
+ else {
501
+ queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
582
502
  }
583
- link = configuration_1.silent_login_uri + queries;
584
- idx = link.indexOf("/", link.indexOf("//") + 2);
585
- iFrameOrigin_1 = link.substr(0, idx);
586
- iframe_1 = document.createElement('iframe');
587
- iframe_1.width = "0px";
588
- iframe_1.height = "0px";
589
- iframe_1.id = "".concat(this.configurationName, "_oidc_iframe");
590
- iframe_1.setAttribute("src", link);
591
- document.body.appendChild(iframe_1);
592
- self_1 = this;
593
- return [2 /*return*/, new Promise(function (resolve, reject) {
594
- var _a;
595
- try {
596
- var isResolved_1 = false;
597
- window.onmessage = function (e) {
598
- if (e.origin === iFrameOrigin_1 &&
599
- e.source === iframe_1.contentWindow) {
600
- var key = "".concat(self_1.configurationName, "_oidc_tokens:");
601
- var key_error = "".concat(self_1.configurationName, "_oidc_error:");
602
- var data = e.data;
603
- if (data && typeof (data) === "string") {
604
- if (!isResolved_1) {
605
- if (data.startsWith(key)) {
606
- var result = JSON.parse(e.data.replace(key, ''));
607
- self_1.publishEvent(eventNames.silentLoginAsync_end, {});
608
- iframe_1.remove();
609
- isResolved_1 = true;
610
- resolve(result);
611
- }
612
- else if (data.startsWith(key_error)) {
613
- var result = JSON.parse(e.data.replace(key_error, ''));
614
- self_1.publishEvent(eventNames.silentLoginAsync_error, result);
615
- iframe_1.remove();
616
- isResolved_1 = true;
617
- reject(new Error("oidc"));
618
- }
619
- }
620
- }
621
- }
622
- };
623
- var silentSigninTimeout = (_a = configuration_1.silent_login_timeout) !== null && _a !== void 0 ? _a : 12000;
624
- 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") {
625
525
  if (!isResolved_1) {
626
- self_1.publishEvent(eventNames.silentLoginAsync_error, { reason: "timeout" });
627
- iframe_1.remove();
628
- isResolved_1 = true;
629
- 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
+ }
630
540
  }
631
- }, silentSigninTimeout);
541
+ }
632
542
  }
633
- 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" });
634
548
  iframe_1.remove();
635
- self_1.publishEvent(eventNames.silentLoginAsync_error, e);
636
- reject(e);
549
+ isResolved_1 = true;
550
+ reject(new Error("timeout"));
637
551
  }
638
- })];
639
- }
640
- catch (e) {
641
- this.publishEvent(eventNames.silentLoginAsync_error, e);
642
- throw e;
643
- }
644
- 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
+ })];
645
560
  }
561
+ catch (e) {
562
+ this.publishEvent(eventNames.silentLoginAsync_error, e);
563
+ throw e;
564
+ }
565
+ return [2 /*return*/];
646
566
  });
647
567
  });
648
568
  };
649
569
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
650
570
  var _a;
651
571
  return __awaiter(this, void 0, void 0, function () {
652
- var serviceWorker, storage, initAsyncPromise;
572
+ var serviceWorker, storage;
653
573
  return __generator(this, function (_b) {
654
574
  switch (_b.label) {
655
575
  case 0:
@@ -668,9 +588,7 @@ var Oidc = /** @class */ (function () {
668
588
  serviceWorker = _b.sent();
669
589
  storage = serviceWorker ? window.localStorage : null;
670
590
  return [4 /*yield*/, fetchFromIssuer(authority, (_a = this.configuration.authority_time_cache_wellknowurl_in_second) !== null && _a !== void 0 ? _a : 60 * 60, storage)];
671
- case 2:
672
- initAsyncPromise = _b.sent();
673
- return [2 /*return*/, initAsyncPromise];
591
+ case 2: return [2 /*return*/, _b.sent()];
674
592
  }
675
593
  });
676
594
  });
@@ -684,83 +602,68 @@ var Oidc = /** @class */ (function () {
684
602
  return [2 /*return*/, this.tryKeepExistingSessionPromise];
685
603
  }
686
604
  funcAsync = function () { return __awaiter(_this, void 0, void 0, function () {
687
- var serviceWorker, configuration, oidcServerConfiguration, tokens, reformattedToken, _a, sessionState, session, tokens, _b, sessionState, exception_1;
688
- var _c;
689
- return __generator(this, function (_d) {
690
- 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) {
691
609
  case 0:
692
610
  if (this.tokens != null) {
693
611
  return [2 /*return*/, false];
694
612
  }
695
613
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_begin, {});
696
- _d.label = 1;
614
+ _b.label = 1;
697
615
  case 1:
698
- _d.trys.push([1, 14, , 17]);
616
+ _b.trys.push([1, 12, , 15]);
699
617
  configuration = this.configuration;
700
618
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
701
619
  case 2:
702
- oidcServerConfiguration = _d.sent();
620
+ oidcServerConfiguration = _b.sent();
703
621
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
704
622
  case 3:
705
- serviceWorker = _d.sent();
706
- if (!serviceWorker) return [3 /*break*/, 9];
623
+ serviceWorker = _b.sent();
624
+ if (!serviceWorker) return [3 /*break*/, 8];
707
625
  return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync")];
708
626
  case 4:
709
- tokens = (_d.sent()).tokens;
710
- if (!tokens) return [3 /*break*/, 8];
627
+ tokens = (_b.sent()).tokens;
628
+ if (!tokens) return [3 /*break*/, 7];
711
629
  serviceWorker.startKeepAliveServiceWorker();
712
- reformattedToken = {
713
- accessToken: tokens.access_token,
714
- refreshToken: tokens.refresh_token,
715
- expiresIn: tokens.expires_in,
716
- idToken: tokens.id_token,
717
- scope: tokens.scope,
718
- tokenType: tokens.token_type,
719
- issuedAt: tokens.issued_at
720
- };
721
- _a = this;
722
- return [4 /*yield*/, setTokensAsync(serviceWorker, reformattedToken)];
723
- case 5:
724
- _a.tokens = _d.sent();
630
+ // @ts-ignore
631
+ this.tokens = tokens;
725
632
  this.serviceWorker = serviceWorker;
726
633
  // @ts-ignore
727
634
  this.timeoutId = autoRenewTokens(this, this.tokens.refreshToken, this.tokens.expiresAt);
728
635
  return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
729
- case 6:
730
- sessionState = _d.sent();
636
+ case 5:
637
+ sessionState = _b.sent();
731
638
  // @ts-ignore
732
639
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
733
- case 7:
640
+ case 6:
734
641
  // @ts-ignore
735
- _d.sent();
642
+ _b.sent();
736
643
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
737
644
  success: true,
738
645
  message: "tokens inside ServiceWorker are valid"
739
646
  });
740
647
  return [2 /*return*/, true];
741
- case 8:
648
+ case 7:
742
649
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
743
650
  success: false,
744
651
  message: "no exiting session found"
745
652
  });
746
- return [3 /*break*/, 13];
747
- case 9:
653
+ return [3 /*break*/, 11];
654
+ case 8:
748
655
  if (configuration.service_worker_relative_url) {
749
656
  this.publishEvent(eventNames.service_worker_not_supported_by_browser, {
750
657
  message: "service worker is not supported by this browser"
751
658
  });
752
659
  }
753
- 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);
754
661
  return [4 /*yield*/, session.initAsync()];
755
- case 10:
756
- tokens = (_d.sent()).tokens;
757
- if (!tokens) return [3 /*break*/, 13];
758
- // @ts-ignore
759
- _b = this;
760
- return [4 /*yield*/, setTokensAsync(serviceWorker, tokens)];
761
- case 11:
662
+ case 9:
663
+ tokens = (_b.sent()).tokens;
664
+ if (!tokens) return [3 /*break*/, 11];
762
665
  // @ts-ignore
763
- _b.tokens = _d.sent();
666
+ this.tokens = (0, parseTokens_1.setTokens)(tokens);
764
667
  //session.setTokens(this.tokens);
765
668
  this.session = session;
766
669
  // @ts-ignore
@@ -768,32 +671,32 @@ var Oidc = /** @class */ (function () {
768
671
  sessionState = session.getSessionState();
769
672
  // @ts-ignore
770
673
  return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
771
- case 12:
674
+ case 10:
772
675
  // @ts-ignore
773
- _d.sent();
676
+ _b.sent();
774
677
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
775
678
  success: true,
776
679
  message: "tokens inside storage are valid"
777
680
  });
778
681
  return [2 /*return*/, true];
779
- case 13:
682
+ case 11:
780
683
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
781
684
  success: false,
782
685
  message: serviceWorker ? "service worker sessions not retrieved" : "session storage sessions not retrieved"
783
686
  });
784
687
  return [2 /*return*/, false];
785
- case 14:
786
- exception_1 = _d.sent();
688
+ case 12:
689
+ exception_1 = _b.sent();
787
690
  console.error(exception_1);
788
- if (!serviceWorker) return [3 /*break*/, 16];
691
+ if (!serviceWorker) return [3 /*break*/, 14];
789
692
  return [4 /*yield*/, serviceWorker.clearAsync()];
790
- case 15:
791
- _d.sent();
792
- _d.label = 16;
793
- case 16:
693
+ case 13:
694
+ _b.sent();
695
+ _b.label = 14;
696
+ case 14:
794
697
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_error, "tokens inside ServiceWorker are invalid");
795
698
  return [2 /*return*/, false];
796
- case 17: return [2 /*return*/];
699
+ case 15: return [2 /*return*/];
797
700
  }
798
701
  });
799
702
  }); };
@@ -805,10 +708,9 @@ var Oidc = /** @class */ (function () {
805
708
  });
806
709
  });
807
710
  };
808
- Oidc.prototype.loginAsync = function (callbackPath, extras, installServiceWorker, state, isSilentSignin, scope) {
711
+ Oidc.prototype.loginAsync = function (callbackPath, extras, state, isSilentSignin, scope) {
809
712
  if (callbackPath === void 0) { callbackPath = undefined; }
810
713
  if (extras === void 0) { extras = null; }
811
- if (installServiceWorker === void 0) { installServiceWorker = true; }
812
714
  if (state === void 0) { state = undefined; }
813
715
  if (isSilentSignin === void 0) { isSilentSignin = false; }
814
716
  if (scope === void 0) { scope = undefined; }
@@ -820,7 +722,7 @@ var Oidc = /** @class */ (function () {
820
722
  return [2 /*return*/, this.loginPromise];
821
723
  }
822
724
  loginLocalAsync = function () { return __awaiter(_this, void 0, void 0, function () {
823
- 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;
824
726
  var _a;
825
727
  return __generator(this, function (_b) {
826
728
  switch (_b.label) {
@@ -834,8 +736,7 @@ var Oidc = /** @class */ (function () {
834
736
  if (!scope) {
835
737
  scope = configuration.scope;
836
738
  }
837
- sessionKey = getLoginSessionKey(this.configurationName, redirectUri);
838
- sessionStorage[sessionKey] = JSON.stringify({ callbackPath: url, extras: extras, state: state });
739
+ setLoginParams(this.configurationName, redirectUri, { callbackPath: url, extras: extras, state: state });
839
740
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
840
741
  case 1:
841
742
  serviceWorker = _b.sent();
@@ -922,10 +823,31 @@ var Oidc = /** @class */ (function () {
922
823
  else {
923
824
  console.debug("SessionMonitor._callback: Different subject signed into OP:", iFrameIdTokenPayload.sub);
924
825
  }
925
- }).catch(function (e) {
926
- _this.publishEvent(eventNames.logout_from_another_tab, {});
927
- _this.destroyAsync();
928
- });
826
+ }).catch(function (e) { return __awaiter(_this, void 0, void 0, function () {
827
+ var _i, _a, _b, key, oidc;
828
+ return __generator(this, function (_c) {
829
+ switch (_c.label) {
830
+ case 0:
831
+ _i = 0, _a = Object.entries(oidcDatabase);
832
+ _c.label = 1;
833
+ case 1:
834
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
835
+ _b = _a[_i], key = _b[0], oidc = _b[1];
836
+ //if(oidc !== this) {
837
+ // @ts-ignore
838
+ return [4 /*yield*/, oidc.logoutOtherTabAsync(this.configuration.client_id, idTokenPayload.sub)];
839
+ case 2:
840
+ //if(oidc !== this) {
841
+ // @ts-ignore
842
+ _c.sent();
843
+ _c.label = 3;
844
+ case 3:
845
+ _i++;
846
+ return [3 /*break*/, 1];
847
+ case 4: return [2 /*return*/];
848
+ }
849
+ });
850
+ }); });
929
851
  };
930
852
  _this.checkSessionIFrame = new checkSessionIFrame_1.CheckSessionIFrame(checkSessionCallback, clientId, checkSessionIFrameUri);
931
853
  _this.checkSessionIFrame.load().then(function () {
@@ -959,16 +881,14 @@ var Oidc = /** @class */ (function () {
959
881
  case 1:
960
882
  response = _a.sent();
961
883
  tokens = response.tokens;
962
- return [4 /*yield*/, setTokensAsync(this.serviceWorker, tokens)];
963
- case 2:
964
- parsedTokens = _a.sent();
884
+ parsedTokens = (0, parseTokens_1.setTokens)(tokens);
965
885
  this.tokens = parsedTokens;
966
- if (!!this.serviceWorker) return [3 /*break*/, 4];
886
+ if (!!this.serviceWorker) return [3 /*break*/, 3];
967
887
  return [4 /*yield*/, this.session.setTokens(parsedTokens)];
968
- case 3:
888
+ case 2:
969
889
  _a.sent();
970
- _a.label = 4;
971
- case 4:
890
+ _a.label = 3;
891
+ case 3:
972
892
  this.publishEvent(Oidc.eventNames.token_aquired, parsedTokens);
973
893
  // @ts-ignore
974
894
  return [2 /*return*/, { parsedTokens: parsedTokens, state: response.state, callbackPath: response.callbackPath }];
@@ -987,7 +907,7 @@ var Oidc = /** @class */ (function () {
987
907
  var _a;
988
908
  if (isSilentSignin === void 0) { isSilentSignin = false; }
989
909
  return __awaiter(this, void 0, void 0, function () {
990
- var configuration_2, clientId_1, redirectUri_1, authority, tokenRequestTimeout_1, oidcServerConfiguration_1, queryParams, sessionState_2, serviceWorker, storage_1, items, dummy, session, items, exception_3;
910
+ var configuration_2, clientId_1, redirectUri_1, authority, tokenRequestTimeout_1, oidcServerConfiguration_1, queryParams, sessionState_2, serviceWorker_1, storage_1, items, dummy, session, items, exception_3;
991
911
  var _this = this;
992
912
  return __generator(this, function (_b) {
993
913
  switch (_b.label) {
@@ -1006,18 +926,18 @@ var Oidc = /** @class */ (function () {
1006
926
  sessionState_2 = queryParams.session_state;
1007
927
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration_2.service_worker_relative_url, this.configurationName)];
1008
928
  case 2:
1009
- serviceWorker = _b.sent();
929
+ serviceWorker_1 = _b.sent();
1010
930
  storage_1 = null;
1011
- if (!serviceWorker) return [3 /*break*/, 8];
1012
- serviceWorker.startKeepAliveServiceWorker();
1013
- this.serviceWorker = serviceWorker;
1014
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration_1, "loginCallbackAsync")];
931
+ if (!serviceWorker_1) return [3 /*break*/, 8];
932
+ serviceWorker_1.startKeepAliveServiceWorker();
933
+ this.serviceWorker = serviceWorker_1;
934
+ return [4 /*yield*/, serviceWorker_1.initAsync(oidcServerConfiguration_1, "loginCallbackAsync")];
1015
935
  case 3:
1016
936
  _b.sent();
1017
- return [4 /*yield*/, serviceWorker.loadItemsAsync()];
937
+ return [4 /*yield*/, serviceWorker_1.loadItemsAsync()];
1018
938
  case 4:
1019
939
  items = _b.sent();
1020
- storage_1 = new memoryStorageBackend_1.MemoryStorageBackend(serviceWorker.saveItemsAsync, items);
940
+ storage_1 = new memoryStorageBackend_1.MemoryStorageBackend(serviceWorker_1.saveItemsAsync, items);
1021
941
  return [4 /*yield*/, storage_1.getItem("dummy")];
1022
942
  case 5:
1023
943
  dummy = _b.sent();
@@ -1027,7 +947,7 @@ var Oidc = /** @class */ (function () {
1027
947
  return [4 /*yield*/, storage_1.removeItem("dummy")];
1028
948
  case 6:
1029
949
  _b.sent();
1030
- return [4 /*yield*/, serviceWorker.setSessionStateAsync(sessionState_2)];
950
+ return [4 /*yield*/, serviceWorker_1.setSessionStateAsync(sessionState_2)];
1031
951
  case 7:
1032
952
  _b.sent();
1033
953
  return [3 /*break*/, 10];
@@ -1087,22 +1007,37 @@ var Oidc = /** @class */ (function () {
1087
1007
  }, tokenRequestTimeout_1 !== null && tokenRequestTimeout_1 !== void 0 ? tokenRequestTimeout_1 : 12000);
1088
1008
  try {
1089
1009
  var tokenHandler = new appauth_1.BaseTokenRequestHandler(new appauth_1.FetchRequestor());
1090
- tokenHandler.performTokenRequest(oidcServerConfiguration_1, tokenRequest).then(function (tokenResponse) {
1091
- if (timeoutId) {
1092
- clearTimeout(timeoutId);
1093
- _this.timeoutId = null;
1094
- var loginParams_1 = getLoginParams(_this.configurationName, redirectUri_1);
1095
- // @ts-ignore
1096
- _this.startCheckSessionAsync(oidcServerConfiguration_1.check_session_iframe, clientId_1, sessionState_2, isSilentSignin).then(function () {
1097
- _this.publishEvent(eventNames.loginCallbackAsync_end, {});
1098
- resolve({
1099
- tokens: tokenResponse,
1100
- state: request.state,
1101
- callbackPath: loginParams_1.callbackPath,
1102
- });
1103
- });
1104
- }
1105
- });
1010
+ tokenHandler.performTokenRequest(oidcServerConfiguration_1, tokenRequest).then(function (tokenResponse) { return __awaiter(_this, void 0, void 0, function () {
1011
+ var loginParams_1, tokens;
1012
+ var _this = this;
1013
+ return __generator(this, function (_a) {
1014
+ switch (_a.label) {
1015
+ case 0:
1016
+ if (!timeoutId) return [3 /*break*/, 3];
1017
+ clearTimeout(timeoutId);
1018
+ this.timeoutId = null;
1019
+ loginParams_1 = getLoginParams(this.configurationName, redirectUri_1);
1020
+ if (!serviceWorker_1) return [3 /*break*/, 2];
1021
+ return [4 /*yield*/, serviceWorker_1.initAsync(oidcServerConfiguration_1, "syncTokensAsync")];
1022
+ case 1:
1023
+ tokens = (_a.sent()).tokens;
1024
+ tokenResponse = tokens;
1025
+ _a.label = 2;
1026
+ case 2:
1027
+ // @ts-ignore
1028
+ this.startCheckSessionAsync(oidcServerConfiguration_1.check_session_iframe, clientId_1, sessionState_2, isSilentSignin).then(function () {
1029
+ _this.publishEvent(eventNames.loginCallbackAsync_end, {});
1030
+ resolve({
1031
+ tokens: tokenResponse,
1032
+ state: request.state,
1033
+ callbackPath: loginParams_1.callbackPath,
1034
+ });
1035
+ });
1036
+ _a.label = 3;
1037
+ case 3: return [2 /*return*/];
1038
+ }
1039
+ });
1040
+ }); });
1106
1041
  }
1107
1042
  catch (exception) {
1108
1043
  if (timeoutId) {
@@ -1126,197 +1061,207 @@ var Oidc = /** @class */ (function () {
1126
1061
  });
1127
1062
  });
1128
1063
  };
1129
- Oidc.prototype.refreshTokensAsync = function (refreshToken) {
1064
+ Oidc.prototype.synchroniseTokensAsync = function (refreshToken, index) {
1065
+ if (index === void 0) { index = 0; }
1130
1066
  return __awaiter(this, void 0, void 0, function () {
1131
- var localsilentLoginAsync, configuration, clientId, redirectUri, authority, extras, _i, _a, _b, key, value, oidcServerConfiguration, details, index, tokenResponse, exception_4;
1067
+ var numberTryOnline, configuration, localsilentLoginAsync, _a, status_1, tokens, _b, clientId, redirectUri, authority, extras, _i, _c, _d, key, value, details, oidcServerConfiguration, tokenResponse, exception_4;
1132
1068
  var _this = this;
1133
- return __generator(this, function (_c) {
1134
- switch (_c.label) {
1069
+ return __generator(this, function (_e) {
1070
+ switch (_e.label) {
1135
1071
  case 0:
1072
+ if (!document.hidden) return [3 /*break*/, 3];
1073
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1074
+ case 1:
1075
+ _e.sent();
1076
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1077
+ return [4 /*yield*/, this.synchroniseTokensAsync(refreshToken, index)];
1078
+ case 2: return [2 /*return*/, _e.sent()];
1079
+ case 3:
1080
+ numberTryOnline = 6;
1081
+ _e.label = 4;
1082
+ case 4:
1083
+ if (!(!navigator.onLine && numberTryOnline > 0)) return [3 /*break*/, 6];
1084
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1085
+ case 5:
1086
+ _e.sent();
1087
+ numberTryOnline--;
1088
+ this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because navigator is offline try ".concat(numberTryOnline) });
1089
+ return [3 /*break*/, 4];
1090
+ case 6:
1091
+ configuration = this.configuration;
1136
1092
  localsilentLoginAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1137
- var silent_token_response, exceptionSilent_1;
1093
+ var loginParams, silent_token_response, exceptionSilent_1;
1138
1094
  return __generator(this, function (_a) {
1139
1095
  switch (_a.label) {
1140
1096
  case 0:
1141
- _a.trys.push([0, 2, , 3]);
1142
- return [4 /*yield*/, this.silentLoginAsync()];
1097
+ _a.trys.push([0, 2, , 4]);
1098
+ loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1099
+ return [4 /*yield*/, this.silentLoginAsync(__assign(__assign({}, loginParams.extras), { prompt: "none" }), loginParams.state)];
1143
1100
  case 1:
1144
1101
  silent_token_response = _a.sent();
1145
1102
  if (silent_token_response) {
1146
- return [2 /*return*/, silent_token_response.tokens];
1103
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1104
+ return [2 /*return*/, { tokens: silent_token_response.tokens, status: "LOGGED" }];
1147
1105
  }
1148
- return [3 /*break*/, 3];
1106
+ return [3 /*break*/, 4];
1149
1107
  case 2:
1150
1108
  exceptionSilent_1 = _a.sent();
1151
1109
  console.error(exceptionSilent_1);
1152
- return [3 /*break*/, 3];
1153
- case 3:
1154
- if (this.timeoutId) {
1155
- timer_1.default.clearTimeout(this.timeoutId);
1156
- this.timeoutId = null;
1110
+ this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exceptionSilent", exception: exceptionSilent_1.message });
1111
+ if (exceptionSilent_1 && exceptionSilent_1.message && exceptionSilent_1.message.startsWith("oidc")) {
1112
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token silent" });
1113
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1157
1114
  }
1158
- this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token and silent refresh failed" });
1159
- return [2 /*return*/, null];
1115
+ return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1116
+ case 3:
1117
+ _a.sent();
1118
+ throw exceptionSilent_1;
1119
+ case 4:
1120
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token silent return" });
1121
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1160
1122
  }
1161
1123
  });
1162
1124
  }); };
1163
- configuration = this.configuration;
1125
+ if (!(index <= 4)) return [3 /*break*/, 24];
1126
+ _e.label = 7;
1127
+ case 7:
1128
+ _e.trys.push([7, 23, , 24]);
1129
+ if (!!refreshToken) return [3 /*break*/, 9];
1130
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1131
+ return [4 /*yield*/, localsilentLoginAsync()];
1132
+ case 8: return [2 /*return*/, _e.sent()];
1133
+ case 9: return [4 /*yield*/, this.syncTokensInfoAsync(configuration, this.configurationName, this.tokens)];
1134
+ case 10:
1135
+ _a = _e.sent(), status_1 = _a.status, tokens = _a.tokens;
1136
+ _b = status_1;
1137
+ switch (_b) {
1138
+ case "SESSION_LOST": return [3 /*break*/, 11];
1139
+ case "NOT_CONNECTED": return [3 /*break*/, 12];
1140
+ case "TOKENS_VALID": return [3 /*break*/, 13];
1141
+ case "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID": return [3 /*break*/, 13];
1142
+ case "LOGOUT_FROM_ANOTHER_TAB": return [3 /*break*/, 14];
1143
+ case "REQUIRE_SYNC_TOKENS": return [3 /*break*/, 15];
1144
+ }
1145
+ return [3 /*break*/, 17];
1146
+ case 11:
1147
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token session lost" });
1148
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1149
+ case 12: return [2 /*return*/, { tokens: null, status: null }];
1150
+ case 13: return [2 /*return*/, { tokens: tokens, status: "LOGGED_IN" }];
1151
+ case 14:
1152
+ this.publishEvent(eventNames.logout_from_another_tab, { "status": "session syncTokensAsync" });
1153
+ return [2 /*return*/, { tokens: null, status: "LOGGED_OUT" }];
1154
+ case 15:
1155
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, status: status_1, tryNumber: index });
1156
+ return [4 /*yield*/, localsilentLoginAsync()];
1157
+ case 16: return [2 /*return*/, _e.sent()];
1158
+ case 17:
1159
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, status: status_1, tryNumber: index });
1164
1160
  clientId = configuration.client_id;
1165
1161
  redirectUri = configuration.redirect_uri;
1166
1162
  authority = configuration.authority;
1167
- if (!!refreshToken) return [3 /*break*/, 2];
1168
- return [4 /*yield*/, localsilentLoginAsync()];
1169
- case 1: return [2 /*return*/, _c.sent()];
1170
- case 2:
1171
1163
  extras = {};
1172
1164
  if (configuration.token_request_extras) {
1173
- for (_i = 0, _a = Object.entries(configuration.token_request_extras); _i < _a.length; _i++) {
1174
- _b = _a[_i], key = _b[0], value = _b[1];
1165
+ for (_i = 0, _c = Object.entries(configuration.token_request_extras); _i < _c.length; _i++) {
1166
+ _d = _c[_i], key = _d[0], value = _d[1];
1175
1167
  extras[key] = value;
1176
1168
  }
1177
1169
  }
1178
- return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1179
- case 3:
1180
- oidcServerConfiguration = _c.sent();
1181
1170
  details = {
1182
1171
  client_id: clientId,
1183
1172
  redirect_uri: redirectUri,
1184
1173
  grant_type: appauth_1.GRANT_TYPE_REFRESH_TOKEN,
1185
- refresh_token: refreshToken,
1174
+ refresh_token: tokens.refreshToken,
1186
1175
  };
1187
- index = 0;
1188
- _c.label = 4;
1189
- case 4:
1190
- if (!(index <= 4)) return [3 /*break*/, 15];
1191
- _c.label = 5;
1192
- case 5:
1193
- _c.trys.push([5, 13, , 14]);
1194
- this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken, tryNumber: index });
1195
- if (!(index > 1)) return [3 /*break*/, 8];
1196
- _c.label = 6;
1197
- case 6:
1198
- if (!document.hidden) return [3 /*break*/, 8];
1199
- return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
1200
- case 7:
1201
- _c.sent();
1202
- this.publishEvent(eventNames.refreshTokensAsync, { message: "wait because document is hidden" });
1203
- return [3 /*break*/, 6];
1204
- case 8: return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1205
- case 9:
1206
- tokenResponse = _c.sent();
1207
- if (!tokenResponse.success) return [3 /*break*/, 10];
1176
+ return [4 /*yield*/, this.initAsync(authority, configuration.authority_configuration)];
1177
+ case 18:
1178
+ oidcServerConfiguration = _e.sent();
1179
+ return [4 /*yield*/, performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)];
1180
+ case 19:
1181
+ tokenResponse = _e.sent();
1182
+ if (!tokenResponse.success) return [3 /*break*/, 20];
1208
1183
  this.publishEvent(eventNames.refreshTokensAsync_end, { success: tokenResponse.success });
1209
- return [2 /*return*/, tokenResponse.data];
1210
- case 10:
1211
- this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "bad request", tokenResponse: tokenResponse });
1212
- return [4 /*yield*/, localsilentLoginAsync()];
1213
- case 11: return [2 /*return*/, _c.sent()];
1214
- case 12: return [3 /*break*/, 14];
1215
- case 13:
1216
- exception_4 = _c.sent();
1184
+ this.publishEvent(Oidc.eventNames.token_renewed, {});
1185
+ return [2 /*return*/, { tokens: tokenResponse.data, status: "LOGGED_IN" }];
1186
+ case 20:
1187
+ this.publishEvent(eventNames.refreshTokensAsync_silent_error, {
1188
+ message: "bad request",
1189
+ tokenResponse: tokenResponse
1190
+ });
1191
+ return [4 /*yield*/, this.synchroniseTokensAsync(null, index + 1)];
1192
+ case 21: return [2 /*return*/, _e.sent()];
1193
+ case 22: return [3 /*break*/, 24];
1194
+ case 23:
1195
+ exception_4 = _e.sent();
1217
1196
  console.error(exception_4);
1218
1197
  this.publishEvent(eventNames.refreshTokensAsync_silent_error, { message: "exception", exception: exception_4.message });
1219
- return [3 /*break*/, 14];
1220
- case 14:
1221
- index++;
1222
- return [3 /*break*/, 4];
1223
- case 15: return [2 /*return*/];
1198
+ return [2 /*return*/, this.synchroniseTokensAsync(refreshToken, index + 1)];
1199
+ case 24:
1200
+ this.publishEvent(eventNames.refreshTokensAsync_error, { message: "refresh token" });
1201
+ return [2 /*return*/, { tokens: null, status: "SESSION_LOST" }];
1224
1202
  }
1225
1203
  });
1226
1204
  });
1227
1205
  };
1228
- Oidc.prototype.syncTokensAsync = function () {
1206
+ Oidc.prototype.syncTokensInfoAsync = function (configuration, configurationName, currentTokens) {
1207
+ var _a;
1229
1208
  return __awaiter(this, void 0, void 0, function () {
1230
- var localSyncTokensAsync;
1231
- var _this = this;
1232
- return __generator(this, function (_a) {
1233
- localSyncTokensAsync = function () { return __awaiter(_this, void 0, void 0, function () {
1234
- var configuration, oidcServerConfiguration, serviceWorker, isLogin, silent_token_response, _a, exceptionSilent_2, session, tokens;
1235
- var _b;
1236
- return __generator(this, function (_c) {
1237
- switch (_c.label) {
1238
- case 0:
1239
- configuration = this.configuration;
1240
- if (!this.tokens) {
1241
- return [2 /*return*/, null];
1242
- }
1243
- return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1244
- case 1:
1245
- oidcServerConfiguration = _c.sent();
1246
- return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1247
- case 2:
1248
- serviceWorker = _c.sent();
1249
- if (!serviceWorker) return [3 /*break*/, 13];
1250
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1251
- case 3:
1252
- isLogin = (_c.sent()).isLogin;
1253
- if (!(isLogin == false)) return [3 /*break*/, 5];
1254
- this.publishEvent(eventNames.logout_from_another_tab, {});
1255
- return [4 /*yield*/, this.destroyAsync()];
1256
- case 4:
1257
- _c.sent();
1258
- return [2 /*return*/, null];
1259
- case 5:
1260
- if (!(isLogin == null)) return [3 /*break*/, 12];
1261
- _c.label = 6;
1262
- case 6:
1263
- _c.trys.push([6, 11, , 12]);
1264
- this.publishEvent(eventNames.syncTokensAsync_begin, {});
1265
- return [4 /*yield*/, this.silentLoginAsync({ prompt: "none" })];
1266
- case 7:
1267
- silent_token_response = _c.sent();
1268
- if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1269
- _a = this;
1270
- return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1271
- case 8:
1272
- _a.tokens = _c.sent();
1273
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1274
- return [2 /*return*/, this.tokens];
1275
- case 9:
1276
- this.publishEvent(eventNames.syncTokensAsync_error, { message: "no token found in result" });
1277
- if (this.timeoutId) {
1278
- timer_1.default.clearTimeout(this.timeoutId);
1279
- this.timeoutId = null;
1280
- }
1281
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1282
- return [2 /*return*/, null];
1283
- case 10: return [3 /*break*/, 12];
1284
- case 11:
1285
- exceptionSilent_2 = _c.sent();
1286
- console.error(exceptionSilent_2);
1287
- this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1288
- if (this.timeoutId) {
1289
- timer_1.default.clearTimeout(this.timeoutId);
1290
- this.timeoutId = null;
1291
- }
1292
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1293
- return [2 /*return*/, null];
1294
- case 12: return [3 /*break*/, 16];
1295
- case 13:
1296
- session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_b = configuration.storage) !== null && _b !== void 0 ? _b : sessionStorage);
1297
- return [4 /*yield*/, session.initAsync()];
1298
- case 14:
1299
- tokens = (_c.sent()).tokens;
1300
- if (!!tokens) return [3 /*break*/, 16];
1301
- this.publishEvent(eventNames.logout_from_another_tab, {});
1302
- return [4 /*yield*/, this.destroyAsync()];
1303
- case 15:
1304
- _c.sent();
1305
- return [2 /*return*/, null];
1306
- case 16: return [2 /*return*/, this.tokens];
1209
+ var oidcServerConfiguration, serviceWorker, _b, status_2, tokens, timeLeft_1, status_3, session, _c, tokens, status_4, timeLeft_2, status_5, timeLeft, status;
1210
+ return __generator(this, function (_d) {
1211
+ switch (_d.label) {
1212
+ case 0:
1213
+ // 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)
1214
+ //const configuration = this.configuration;
1215
+ if (!currentTokens) {
1216
+ return [2 /*return*/, { tokens: null, status: "NOT_CONNECTED" }];
1307
1217
  }
1308
- });
1309
- }); };
1310
- if (this.syncTokensAsyncPromise) {
1311
- return [2 /*return*/, this.syncTokensAsyncPromise];
1218
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1219
+ case 1:
1220
+ oidcServerConfiguration = _d.sent();
1221
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, configurationName)];
1222
+ case 2:
1223
+ serviceWorker = _d.sent();
1224
+ if (!serviceWorker) return [3 /*break*/, 4];
1225
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1226
+ case 3:
1227
+ _b = _d.sent(), status_2 = _b.status, tokens = _b.tokens;
1228
+ if (status_2 == "LOGGED_OUT") {
1229
+ return [2 /*return*/, { tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB" }];
1230
+ }
1231
+ else if (status_2 == "SESSIONS_LOST") {
1232
+ return [2 /*return*/, { tokens: null, status: "SESSIONS_LOST" }];
1233
+ }
1234
+ else if (!status_2 || !tokens) {
1235
+ return [2 /*return*/, { tokens: null, status: "REQUIRE_SYNC_TOKENS" }];
1236
+ }
1237
+ else if (tokens.issuedAt !== currentTokens.issuedAt) {
1238
+ timeLeft_1 = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, tokens.expiresAt);
1239
+ status_3 = (timeLeft_1 > 0) ? "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID" : "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_INVALID";
1240
+ return [2 /*return*/, { tokens: tokens, status: status_3 }];
1241
+ }
1242
+ return [3 /*break*/, 6];
1243
+ case 4:
1244
+ session = (0, initSession_1.initSession)(configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
1245
+ return [4 /*yield*/, session.initAsync()];
1246
+ case 5:
1247
+ _c = _d.sent(), tokens = _c.tokens, status_4 = _c.status;
1248
+ if (!tokens) {
1249
+ return [2 /*return*/, { tokens: null, status: "LOGOUT_FROM_ANOTHER_TAB" }];
1250
+ }
1251
+ else if (status_4 == "SESSIONS_LOST") {
1252
+ return [2 /*return*/, { tokens: null, status: "SESSIONS_LOST" }];
1253
+ }
1254
+ else if (tokens.issuedAt !== currentTokens.issuedAt) {
1255
+ timeLeft_2 = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, tokens.expiresAt);
1256
+ status_5 = (timeLeft_2 > 0) ? "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_VALID" : "TOKEN_UPDATED_BY_ANOTHER_TAB_TOKENS_INVALID";
1257
+ return [2 /*return*/, { tokens: tokens, status: status_5 }];
1258
+ }
1259
+ _d.label = 6;
1260
+ case 6:
1261
+ timeLeft = (0, parseTokens_1.computeTimeLeft)(configuration.refresh_time_before_tokens_expiration_in_second, currentTokens.expiresAt);
1262
+ status = (timeLeft > 0) ? "TOKENS_VALID" : "TOKENS_INVALID";
1263
+ return [2 /*return*/, { tokens: currentTokens, status: status }];
1312
1264
  }
1313
- this.syncTokensAsyncPromise = localSyncTokensAsync().then(function (result) {
1314
- if (_this.syncTokensAsyncPromise) {
1315
- _this.syncTokensAsyncPromise = null;
1316
- }
1317
- return result;
1318
- });
1319
- return [2 /*return*/, this.syncTokensAsyncPromise];
1320
1265
  });
1321
1266
  });
1322
1267
  };
@@ -1334,7 +1279,7 @@ var Oidc = /** @class */ (function () {
1334
1279
  Oidc.prototype.userInfoAsync = function () {
1335
1280
  return userInfoAsync(this);
1336
1281
  };
1337
- Oidc.prototype.destroyAsync = function () {
1282
+ Oidc.prototype.destroyAsync = function (status) {
1338
1283
  return __awaiter(this, void 0, void 0, function () {
1339
1284
  return __generator(this, function (_a) {
1340
1285
  switch (_a.label) {
@@ -1345,37 +1290,68 @@ var Oidc = /** @class */ (function () {
1345
1290
  this.checkSessionIFrame.stop();
1346
1291
  }
1347
1292
  if (!this.serviceWorker) return [3 /*break*/, 2];
1348
- return [4 /*yield*/, this.serviceWorker.clearAsync()];
1293
+ return [4 /*yield*/, this.serviceWorker.clearAsync(status)];
1349
1294
  case 1:
1350
1295
  _a.sent();
1351
1296
  _a.label = 2;
1352
1297
  case 2:
1353
1298
  if (!this.session) return [3 /*break*/, 4];
1354
- return [4 /*yield*/, this.session.clearAsync()];
1299
+ return [4 /*yield*/, this.session.clearAsync(status)];
1355
1300
  case 3:
1356
1301
  _a.sent();
1357
1302
  _a.label = 4;
1358
1303
  case 4:
1359
1304
  this.tokens = null;
1360
1305
  this.userInfo = null;
1361
- this.events = [];
1362
1306
  return [2 /*return*/];
1363
1307
  }
1364
1308
  });
1365
1309
  });
1366
1310
  };
1311
+ Oidc.prototype.logoutSameTabAsync = function (clientId, sub) {
1312
+ return __awaiter(this, void 0, void 0, function () {
1313
+ return __generator(this, function (_a) {
1314
+ switch (_a.label) {
1315
+ case 0:
1316
+ 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];
1317
+ this.publishEvent(eventNames.logout_from_same_tab, { "message": sub });
1318
+ return [4 /*yield*/, this.destroyAsync("LOGGED_OUT")];
1319
+ case 1:
1320
+ _a.sent();
1321
+ _a.label = 2;
1322
+ case 2: return [2 /*return*/];
1323
+ }
1324
+ });
1325
+ });
1326
+ };
1327
+ Oidc.prototype.logoutOtherTabAsync = function (clientId, sub) {
1328
+ return __awaiter(this, void 0, void 0, function () {
1329
+ return __generator(this, function (_a) {
1330
+ switch (_a.label) {
1331
+ case 0:
1332
+ 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];
1333
+ return [4 /*yield*/, this.destroyAsync("LOGGED_OUT")];
1334
+ case 1:
1335
+ _a.sent();
1336
+ this.publishEvent(eventNames.logout_from_another_tab, { message: "SessionMonitor", "sub": sub });
1337
+ _a.label = 2;
1338
+ case 2: return [2 /*return*/];
1339
+ }
1340
+ });
1341
+ });
1342
+ };
1367
1343
  Oidc.prototype.logoutAsync = function (callbackPathOrUrl, extras) {
1368
1344
  if (callbackPathOrUrl === void 0) { callbackPathOrUrl = undefined; }
1369
1345
  if (extras === void 0) { extras = null; }
1370
1346
  return __awaiter(this, void 0, void 0, function () {
1371
- var configuration, oidcServerConfiguration, path, isUri, url, idToken, extraQueryString, _i, _a, _b, key, value;
1372
- return __generator(this, function (_c) {
1373
- switch (_c.label) {
1347
+ var configuration, oidcServerConfiguration, path, isUri, url, idToken, sub, _i, _a, _b, key, oidc, extraQueryString, _c, _d, _e, key, value;
1348
+ return __generator(this, function (_f) {
1349
+ switch (_f.label) {
1374
1350
  case 0:
1375
1351
  configuration = this.configuration;
1376
1352
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1377
1353
  case 1:
1378
- oidcServerConfiguration = _c.sent();
1354
+ oidcServerConfiguration = _f.sent();
1379
1355
  if (callbackPathOrUrl && (typeof callbackPathOrUrl !== 'string')) {
1380
1356
  callbackPathOrUrl = undefined;
1381
1357
  console.warn('callbackPathOrUrl path is not a string');
@@ -1387,14 +1363,31 @@ var Oidc = /** @class */ (function () {
1387
1363
  }
1388
1364
  url = isUri ? callbackPathOrUrl : window.location.origin + path;
1389
1365
  idToken = this.tokens ? this.tokens.idToken : "";
1390
- return [4 /*yield*/, this.destroyAsync()];
1366
+ sub = this.tokens && this.tokens.idTokenPayload ? this.tokens.idTokenPayload.sub : null;
1367
+ return [4 /*yield*/, this.destroyAsync("LOGGED_OUT")];
1391
1368
  case 2:
1392
- _c.sent();
1369
+ _f.sent();
1370
+ _i = 0, _a = Object.entries(oidcDatabase);
1371
+ _f.label = 3;
1372
+ case 3:
1373
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
1374
+ _b = _a[_i], key = _b[0], oidc = _b[1];
1375
+ if (!(oidc !== this)) return [3 /*break*/, 5];
1376
+ // @ts-ignore
1377
+ return [4 /*yield*/, oidc.logoutSameTabAsync(this.configuration.client_id, sub)];
1378
+ case 4:
1379
+ // @ts-ignore
1380
+ _f.sent();
1381
+ _f.label = 5;
1382
+ case 5:
1383
+ _i++;
1384
+ return [3 /*break*/, 3];
1385
+ case 6:
1393
1386
  if (oidcServerConfiguration.endSessionEndpoint) {
1394
1387
  extraQueryString = "";
1395
1388
  if (extras) {
1396
- for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
1397
- _b = _a[_i], key = _b[0], value = _b[1];
1389
+ for (_c = 0, _d = Object.entries(extras); _c < _d.length; _c++) {
1390
+ _e = _d[_c], key = _e[0], value = _e[1];
1398
1391
  extraQueryString += "&".concat(key, "=").concat(encodeURIComponent(value));
1399
1392
  }
1400
1393
  }