@axa-fr/react-oidc 5.14.0 → 6.0.0-alpha2

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 (66) hide show
  1. package/dist/OidcProvider.d.ts +1 -0
  2. package/dist/OidcProvider.d.ts.map +1 -1
  3. package/dist/OidcProvider.js +13 -5
  4. package/dist/OidcProvider.js.map +1 -1
  5. package/dist/OidcServiceWorker.js +29 -1
  6. package/dist/OidcTrustedDomains.js +7 -4
  7. package/dist/ReactOidc.d.ts.map +1 -1
  8. package/dist/ReactOidc.js +29 -7
  9. package/dist/ReactOidc.js.map +1 -1
  10. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts.map +1 -1
  11. package/dist/core/default-component/ServiceWorkerInstall.component.js +21 -9
  12. package/dist/core/default-component/ServiceWorkerInstall.component.js.map +1 -1
  13. package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
  14. package/dist/core/default-component/SilentCallback.component.js +23 -15
  15. package/dist/core/default-component/SilentCallback.component.js.map +1 -1
  16. package/dist/core/default-component/SilentSignin.component.d.ts +4 -0
  17. package/dist/core/default-component/SilentSignin.component.d.ts.map +1 -0
  18. package/dist/core/default-component/SilentSignin.component.js +58 -0
  19. package/dist/core/default-component/SilentSignin.component.js.map +1 -0
  20. package/dist/core/routes/OidcRoutes.d.ts +1 -0
  21. package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
  22. package/dist/core/routes/OidcRoutes.js +8 -2
  23. package/dist/core/routes/OidcRoutes.js.map +1 -1
  24. package/dist/vanilla/checkSessionIFrame.d.ts +17 -0
  25. package/dist/vanilla/checkSessionIFrame.d.ts.map +1 -0
  26. package/dist/vanilla/checkSessionIFrame.js +78 -0
  27. package/dist/vanilla/checkSessionIFrame.js.map +1 -0
  28. package/dist/vanilla/initSession.d.ts +3 -1
  29. package/dist/vanilla/initSession.d.ts.map +1 -1
  30. package/dist/vanilla/initSession.js +20 -11
  31. package/dist/vanilla/initSession.js.map +1 -1
  32. package/dist/vanilla/initWorker.d.ts +4 -0
  33. package/dist/vanilla/initWorker.d.ts.map +1 -1
  34. package/dist/vanilla/initWorker.js +29 -3
  35. package/dist/vanilla/initWorker.js.map +1 -1
  36. package/dist/vanilla/oidc.d.ts +24 -7
  37. package/dist/vanilla/oidc.d.ts.map +1 -1
  38. package/dist/vanilla/oidc.js +526 -241
  39. package/dist/vanilla/oidc.js.map +1 -1
  40. package/dist/vanilla/route-utils.d.ts +13 -0
  41. package/dist/vanilla/route-utils.d.ts.map +1 -0
  42. package/dist/vanilla/route-utils.js +65 -0
  43. package/dist/vanilla/route-utils.js.map +1 -0
  44. package/package.json +1 -1
  45. package/src/App.tsx +1 -1
  46. package/src/MultiAuth.tsx +2 -2
  47. package/src/configurations.ts +6 -2
  48. package/src/oidc/OidcProvider.tsx +11 -0
  49. package/src/oidc/ReactOidc.tsx +32 -8
  50. package/src/oidc/core/default-component/ServiceWorkerInstall.component.tsx +15 -3
  51. package/src/oidc/core/default-component/SilentCallback.component.tsx +10 -15
  52. package/src/oidc/core/default-component/SilentSignin.component.tsx +35 -0
  53. package/src/oidc/core/routes/OidcRoutes.tsx +10 -1
  54. package/src/oidc/vanilla/OidcServiceWorker.js +29 -1
  55. package/src/oidc/vanilla/OidcTrustedDomains.js +7 -4
  56. package/src/oidc/vanilla/checkSessionIFrame.ts +82 -0
  57. package/src/oidc/vanilla/initSession.ts +23 -11
  58. package/src/oidc/vanilla/initWorker.ts +19 -2
  59. package/src/oidc/vanilla/oidc.ts +410 -152
  60. package/src/oidc/{core/routes → vanilla}/route-utils.spec.ts +0 -0
  61. package/src/oidc/vanilla/route-utils.ts +76 -0
  62. package/dist/core/routes/route-utils.d.ts +0 -2
  63. package/dist/core/routes/route-utils.d.ts.map +0 -1
  64. package/dist/core/routes/route-utils.js +0 -32
  65. package/dist/core/routes/route-utils.js.map +0 -1
  66. package/src/oidc/core/routes/route-utils.ts +0 -34
@@ -1,4 +1,19 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
2
17
  var __assign = (this && this.__assign) || function () {
3
18
  __assign = Object.assign || function(t) {
4
19
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -50,21 +65,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
65
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
66
  };
52
67
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.Oidc = exports.getLoginParams = void 0;
68
+ exports.Oidc = exports.OidcAuthorizationServiceConfiguration = void 0;
54
69
  var appauth_1 = require("@openid/appauth");
55
70
  var noHashQueryStringUtils_1 = require("./noHashQueryStringUtils");
56
71
  var initWorker_1 = require("./initWorker");
57
72
  var memoryStorageBackend_1 = require("./memoryStorageBackend");
58
73
  var initSession_1 = require("./initSession");
59
74
  var timer_1 = __importDefault(require("./timer"));
60
- var isInIframe = function () {
61
- try {
62
- return window.self !== window.top;
63
- }
64
- catch (e) {
65
- return true;
75
+ var checkSessionIFrame_1 = require("./checkSessionIFrame");
76
+ var route_utils_1 = require("./route-utils");
77
+ var OidcAuthorizationServiceConfiguration = /** @class */ (function (_super) {
78
+ __extends(OidcAuthorizationServiceConfiguration, _super);
79
+ function OidcAuthorizationServiceConfiguration(request) {
80
+ var _this = _super.call(this, request) || this;
81
+ _this.authorizationEndpoint = request.authorization_endpoint;
82
+ _this.tokenEndpoint = request.token_endpoint;
83
+ _this.revocationEndpoint = request.revocation_endpoint;
84
+ _this.userInfoEndpoint = request.userinfo_endpoint;
85
+ _this.check_session_iframe = request.check_session_iframe;
86
+ return _this;
66
87
  }
67
- };
88
+ return OidcAuthorizationServiceConfiguration;
89
+ }(appauth_1.AuthorizationServiceConfiguration));
90
+ exports.OidcAuthorizationServiceConfiguration = OidcAuthorizationServiceConfiguration;
68
91
  var idTokenPayload = function (token) {
69
92
  var base64Url = token.split('.')[1];
70
93
  var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
@@ -102,26 +125,14 @@ var oidcFactory = function (configuration, name) {
102
125
  return oidcDatabase[name];
103
126
  };
104
127
  var loginCallbackWithAutoTokensRenewAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, function () {
105
- var response, tokens, _a;
128
+ var _a, parsedTokens, state, callbackPath;
106
129
  return __generator(this, function (_b) {
107
130
  switch (_b.label) {
108
131
  case 0: return [4 /*yield*/, oidc.loginCallbackAsync()];
109
132
  case 1:
110
- response = _b.sent();
111
- tokens = response.tokens;
112
- _a = oidc;
113
- return [4 /*yield*/, setTokensAsync(oidc.serviceWorker, tokens)];
114
- case 2:
115
- _a.tokens = _b.sent();
116
- if (!!oidc.serviceWorker) return [3 /*break*/, 4];
117
- return [4 /*yield*/, oidc.session.setTokens(oidc.tokens)];
118
- case 3:
119
- _b.sent();
120
- _b.label = 4;
121
- case 4:
122
- oidc.publishEvent(Oidc.eventNames.token_aquired, oidc.tokens);
123
- oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
124
- return [2 /*return*/, { state: response.state, callbackPath: response.callbackPath }];
133
+ _a = _b.sent(), parsedTokens = _a.parsedTokens, state = _a.state, callbackPath = _a.callbackPath;
134
+ oidc.timeoutId = autoRenewTokens(oidc, parsedTokens.refreshToken, parsedTokens.expiresAt);
135
+ return [2 /*return*/, { state: state, callbackPath: callbackPath }];
125
136
  }
126
137
  });
127
138
  }); };
@@ -151,9 +162,13 @@ var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
151
162
  _b.label = 4;
152
163
  case 4:
153
164
  if (!oidc.tokens) {
165
+ if (oidc.checkSessionIFrame) {
166
+ oidc.checkSessionIFrame.stop();
167
+ oidc.checkSessionIFrame = null;
168
+ }
154
169
  return [2 /*return*/];
155
170
  }
156
- oidc.publishEvent(Oidc.eventNames.token_renewed, oidc.tokens);
171
+ oidc.publishEvent(Oidc.eventNames.token_renewed, {});
157
172
  if (oidc.timeoutId) {
158
173
  oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
159
174
  }
@@ -170,10 +185,12 @@ var autoRenewTokens = function (oidc, refreshToken, expiresAt) {
170
185
  });
171
186
  }); }, 1000);
172
187
  };
173
- var getLoginParams = function (configurationName) {
174
- return JSON.parse(sessionStorage["oidc_login.".concat(configurationName)]);
188
+ var getLoginSessionKey = function (configurationName, redirectUri) {
189
+ return "oidc_login.".concat(configurationName, ":").concat(redirectUri);
190
+ };
191
+ var getLoginParams = function (configurationName, redirectUri) {
192
+ return JSON.parse(sessionStorage[getLoginSessionKey(configurationName, redirectUri)]);
175
193
  };
176
- exports.getLoginParams = getLoginParams;
177
194
  var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, function () {
178
195
  var accessToken, oidcServerConfiguration, url, fetchUserInfo, userInfo;
179
196
  return __generator(this, function (_a) {
@@ -223,7 +240,7 @@ var userInfoAsync = function (oidc) { return __awaiter(void 0, void 0, void 0, f
223
240
  });
224
241
  }); };
225
242
  var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0, void 0, void 0, function () {
226
- var accessTokenPayload, expiresAt;
243
+ var accessTokenPayload, _idTokenPayload, expiresAt;
227
244
  return __generator(this, function (_a) {
228
245
  switch (_a.label) {
229
246
  case 0:
@@ -244,14 +261,16 @@ var setTokensAsync = function (serviceWorker, tokens) { return __awaiter(void 0,
244
261
  accessTokenPayload = extractAccessTokenPayload(tokens);
245
262
  _a.label = 6;
246
263
  case 6:
247
- expiresAt = tokens.issuedAt + tokens.expiresIn;
248
- return [2 /*return*/, __assign(__assign({}, tokens), { idTokenPayload: idTokenPayload(tokens.idToken), accessTokenPayload: accessTokenPayload, expiresAt: expiresAt })];
264
+ _idTokenPayload = idTokenPayload(tokens.idToken);
265
+ expiresAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
266
+ return [2 /*return*/, __assign(__assign({}, tokens), { idTokenPayload: _idTokenPayload, accessTokenPayload: accessTokenPayload, expiresAt: expiresAt })];
249
267
  }
250
268
  });
251
269
  }); };
252
270
  var eventNames = {
253
271
  service_worker_not_supported_by_browser: "service_worker_not_supported_by_browser",
254
272
  token_aquired: "token_aquired",
273
+ logout_from_another_tab: "logout_from_another_tab",
255
274
  token_renewed: "token_renewed",
256
275
  token_timer: "token_timer",
257
276
  loginAsync_begin: "loginAsync_begin",
@@ -262,8 +281,6 @@ var eventNames = {
262
281
  refreshTokensAsync_begin: "refreshTokensAsync_begin",
263
282
  refreshTokensAsync_end: "refreshTokensAsync_end",
264
283
  refreshTokensAsync_error: "refreshTokensAsync_error",
265
- refreshTokensAsync_silent_begin: "refreshTokensAsync_silent_begin",
266
- refreshTokensAsync_silent_end: "refreshTokensAsync_silent_end",
267
284
  refreshTokensAsync_silent_error: "refreshTokensAsync_silent_error",
268
285
  tryKeepExistingSessionAsync_begin: "tryKeepExistingSessionAsync_begin",
269
286
  tryKeepExistingSessionAsync_end: "tryKeepExistingSessionAsync_end",
@@ -279,11 +296,61 @@ var eventNames = {
279
296
  var getRandomInt = function (max) {
280
297
  return Math.floor(Math.random() * max);
281
298
  };
299
+ var WELL_KNOWN_PATH = '.well-known';
300
+ var OPENID_CONFIGURATION = 'openid-configuration';
301
+ var oneHourSecond = 60 * 60;
302
+ var fetchFromIssuer = function (openIdIssuerUrl, timeCacheSecond) {
303
+ if (timeCacheSecond === void 0) { timeCacheSecond = oneHourSecond; }
304
+ return __awaiter(void 0, void 0, void 0, function () {
305
+ var fullUrl, localStorageKey, cacheJson, oneHourMinisecond, res, result;
306
+ return __generator(this, function (_a) {
307
+ switch (_a.label) {
308
+ case 0:
309
+ fullUrl = "".concat(openIdIssuerUrl, "/").concat(WELL_KNOWN_PATH, "/").concat(OPENID_CONFIGURATION);
310
+ localStorageKey = "oidc.server:".concat(openIdIssuerUrl);
311
+ cacheJson = window.localStorage.getItem(localStorageKey);
312
+ oneHourMinisecond = 1000 * timeCacheSecond;
313
+ // @ts-ignore
314
+ if (cacheJson && (cacheJson.timestamp + oneHourMinisecond) > Date.now()) {
315
+ return [2 /*return*/, new OidcAuthorizationServiceConfiguration(JSON.parse(cacheJson))];
316
+ }
317
+ return [4 /*yield*/, fetch(fullUrl)];
318
+ case 1:
319
+ res = _a.sent();
320
+ if (res.status != 200) {
321
+ return [2 /*return*/, null];
322
+ }
323
+ return [4 /*yield*/, res.json()];
324
+ case 2:
325
+ result = _a.sent();
326
+ window.localStorage.setItem(localStorageKey, JSON.stringify({ result: result, timestamp: Date.now() }));
327
+ return [2 /*return*/, new OidcAuthorizationServiceConfiguration(result)];
328
+ }
329
+ });
330
+ });
331
+ };
332
+ var buildQueries = function (extras) {
333
+ var queries = '';
334
+ if (extras != null) {
335
+ for (var _i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
336
+ var _b = _a[_i], key = _b[0], value = _b[1];
337
+ if (queries === "") {
338
+ queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
339
+ }
340
+ else {
341
+ queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
342
+ }
343
+ }
344
+ }
345
+ return queries;
346
+ };
282
347
  var Oidc = /** @class */ (function () {
283
348
  function Oidc(configuration, configurationName) {
284
349
  if (configurationName === void 0) { configurationName = "default"; }
285
350
  this.initAsyncPromise = null;
286
351
  this.tryKeepExistingSessionPromise = null;
352
+ this.loginPromise = null;
353
+ this.loginCallbackPromise = null;
287
354
  this.syncTokensAsyncPromise = null;
288
355
  this.loginCallbackWithAutoTokensRenewPromise = null;
289
356
  this.configuration = configuration;
@@ -298,6 +365,7 @@ var Oidc = /** @class */ (function () {
298
365
  this.loginCallbackWithAutoTokensRenewAsync.bind(this);
299
366
  this.initAsync.bind(this);
300
367
  this.loginCallbackAsync.bind(this);
368
+ this._loginCallbackAsync.bind(this);
301
369
  this.subscriveEvents.bind(this);
302
370
  this.removeEventSubscription.bind(this);
303
371
  this.publishEvent.bind(this);
@@ -331,31 +399,67 @@ var Oidc = /** @class */ (function () {
331
399
  };
332
400
  Oidc.prototype.silentSigninCallbackFromIFrame = function () {
333
401
  if (this.configuration.silent_redirect_uri) {
334
- window.top.postMessage("".concat(this.configurationName, "_oidc_tokens:").concat(JSON.stringify(this.tokens)), window.location.origin);
402
+ var queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
403
+ window.top.postMessage("".concat(this.configurationName, "_oidc_tokens:").concat(JSON.stringify({ tokens: this.tokens, sessionState: queryParams.session_state })), window.location.origin);
404
+ }
405
+ };
406
+ Oidc.prototype.silentSigninErrorCallbackFromIFrame = function () {
407
+ if (this.configuration.silent_redirect_uri) {
408
+ var queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
409
+ window.top.postMessage("".concat(this.configurationName, "_oidc_error:").concat(JSON.stringify({ error: queryParams.error })), window.location.origin);
335
410
  }
336
411
  };
337
- Oidc.prototype.silentSigninAsync = function () {
412
+ Oidc.prototype.silentSigninAsync = function (extras, state, scope) {
413
+ if (extras === void 0) { extras = null; }
414
+ if (state === void 0) { state = null; }
415
+ if (scope === void 0) { scope = null; }
338
416
  return __awaiter(this, void 0, void 0, function () {
339
- var configuration_1, link, iframe_1, self_1;
340
- return __generator(this, function (_a) {
341
- switch (_a.label) {
417
+ var configuration_1, queries, _i, _a, _b, key, value, link, idx, iFrameOrigin_1, iframe_1, self_1;
418
+ return __generator(this, function (_c) {
419
+ switch (_c.label) {
342
420
  case 0:
343
- if (!this.configuration.silent_redirect_uri) {
421
+ if (!this.configuration.silent_redirect_uri || !this.configuration.silent_signin_uri) {
344
422
  return [2 /*return*/, Promise.resolve(null)];
345
423
  }
346
- _a.label = 1;
424
+ _c.label = 1;
347
425
  case 1:
348
426
  if (!document.hidden) return [3 /*break*/, 3];
349
427
  return [4 /*yield*/, (0, initWorker_1.sleepAsync)(1000)];
350
428
  case 2:
351
- _a.sent();
429
+ _c.sent();
352
430
  this.publishEvent(eventNames.silentSigninAsync, { message: "wait because document is hidden" });
353
431
  return [3 /*break*/, 1];
354
432
  case 3:
355
433
  try {
356
434
  this.publishEvent(eventNames.silentSigninAsync_begin, {});
357
435
  configuration_1 = this.configuration;
358
- link = configuration_1.silent_redirect_uri;
436
+ queries = "";
437
+ if (state) {
438
+ if (extras == null) {
439
+ extras = {};
440
+ }
441
+ extras.state = state;
442
+ }
443
+ if (scope) {
444
+ if (extras == null) {
445
+ extras = {};
446
+ }
447
+ extras.scope = scope;
448
+ }
449
+ if (extras != null) {
450
+ for (_i = 0, _a = Object.entries(extras); _i < _a.length; _i++) {
451
+ _b = _a[_i], key = _b[0], value = _b[1];
452
+ if (queries === "") {
453
+ queries = "?".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
454
+ }
455
+ else {
456
+ queries += "&".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
457
+ }
458
+ }
459
+ }
460
+ link = configuration_1.silent_signin_uri + queries;
461
+ idx = link.indexOf("/", link.indexOf("//") + 2);
462
+ iFrameOrigin_1 = link.substr(0, idx);
359
463
  iframe_1 = document.createElement('iframe');
360
464
  iframe_1.width = "0px";
361
465
  iframe_1.height = "0px";
@@ -364,21 +468,36 @@ var Oidc = /** @class */ (function () {
364
468
  document.body.appendChild(iframe_1);
365
469
  self_1 = this;
366
470
  return [2 /*return*/, new Promise(function (resolve, reject) {
471
+ var _a;
367
472
  try {
368
473
  var isResolved_1 = false;
369
474
  window.onmessage = function (e) {
370
- var key = "".concat(self_1.configurationName, "_oidc_tokens:");
371
- if (e.data && typeof (e.data) === "string" && e.data.startsWith(key)) {
372
- if (!isResolved_1) {
373
- var result = JSON.parse(e.data.replace(key, ''));
374
- self_1.publishEvent(eventNames.silentSigninAsync_end, result);
375
- iframe_1.remove();
376
- isResolved_1 = true;
377
- resolve(result);
475
+ if (e.origin === iFrameOrigin_1 &&
476
+ e.source === iframe_1.contentWindow) {
477
+ var key = "".concat(self_1.configurationName, "_oidc_tokens:");
478
+ var key_error = "".concat(self_1.configurationName, "_oidc_error:");
479
+ var data = e.data;
480
+ if (data && typeof (data) === "string") {
481
+ if (!isResolved_1) {
482
+ if (data.startsWith(key)) {
483
+ var result = JSON.parse(e.data.replace(key, ''));
484
+ self_1.publishEvent(eventNames.silentSigninAsync_end, {});
485
+ iframe_1.remove();
486
+ isResolved_1 = true;
487
+ resolve(result);
488
+ }
489
+ else if (data.startsWith(key_error)) {
490
+ var result = JSON.parse(e.data.replace(key_error, ''));
491
+ self_1.publishEvent(eventNames.silentSigninAsync_error, result);
492
+ iframe_1.remove();
493
+ isResolved_1 = true;
494
+ reject(result);
495
+ }
496
+ }
378
497
  }
379
498
  }
380
499
  };
381
- var silentSigninTimeout = configuration_1.silent_signin_timeout ? configuration_1.silent_signin_timeout : 12000;
500
+ var silentSigninTimeout = (_a = configuration_1.silent_signin_timeout) !== null && _a !== void 0 ? _a : 12000;
382
501
  setTimeout(function () {
383
502
  if (!isResolved_1) {
384
503
  self_1.publishEvent(eventNames.silentSigninAsync_error, "timeout");
@@ -405,27 +524,29 @@ var Oidc = /** @class */ (function () {
405
524
  });
406
525
  };
407
526
  Oidc.prototype.initAsync = function (authority, authorityConfiguration) {
527
+ var _a;
408
528
  return __awaiter(this, void 0, void 0, function () {
409
- var _a;
410
- return __generator(this, function (_b) {
411
- switch (_b.label) {
529
+ var _b;
530
+ return __generator(this, function (_c) {
531
+ switch (_c.label) {
412
532
  case 0:
413
533
  if (authorityConfiguration != null) {
414
- return [2 /*return*/, new appauth_1.AuthorizationServiceConfiguration({
534
+ return [2 /*return*/, new OidcAuthorizationServiceConfiguration({
415
535
  authorization_endpoint: authorityConfiguration.authorization_endpoint,
416
536
  end_session_endpoint: authorityConfiguration.end_session_endpoint,
417
537
  revocation_endpoint: authorityConfiguration.revocation_endpoint,
418
538
  token_endpoint: authorityConfiguration.token_endpoint,
419
- userinfo_endpoint: authorityConfiguration.userinfo_endpoint
539
+ userinfo_endpoint: authorityConfiguration.userinfo_endpoint,
540
+ check_session_iframe: authorityConfiguration.check_session_iframe,
420
541
  })];
421
542
  }
422
543
  if (this.initAsyncPromise) {
423
544
  return [2 /*return*/, this.initAsyncPromise];
424
545
  }
425
- _a = this;
426
- return [4 /*yield*/, appauth_1.AuthorizationServiceConfiguration.fetchFromIssuer(authority, new appauth_1.FetchRequestor())];
546
+ _b = this;
547
+ return [4 /*yield*/, fetchFromIssuer(authority, (_a = this.configuration.authority_time_cache_wellknowurl_in_second) !== null && _a !== void 0 ? _a : 60 * 60)];
427
548
  case 1:
428
- _a.initAsyncPromise = _b.sent();
549
+ _b.initAsyncPromise = _c.sent();
429
550
  return [2 /*return*/, this.initAsyncPromise];
430
551
  }
431
552
  });
@@ -440,7 +561,7 @@ var Oidc = /** @class */ (function () {
440
561
  return [2 /*return*/, this.tryKeepExistingSessionPromise];
441
562
  }
442
563
  funcAsync = function () { return __awaiter(_this, void 0, void 0, function () {
443
- var serviceWorker, configuration, oidcServerConfiguration, tokens, updatedTokens, _a, session, tokens, updatedTokens, _b, exception_1;
564
+ var serviceWorker, configuration, oidcServerConfiguration, tokens, reformattedToken, _a, sessionState, session, tokens, _b, sessionState, exception_1;
444
565
  var _c;
445
566
  return __generator(this, function (_d) {
446
567
  switch (_d.label) {
@@ -451,7 +572,7 @@ var Oidc = /** @class */ (function () {
451
572
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_begin, {});
452
573
  _d.label = 1;
453
574
  case 1:
454
- _d.trys.push([1, 13, , 16]);
575
+ _d.trys.push([1, 14, , 17]);
455
576
  configuration = this.configuration;
456
577
  return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
457
578
  case 2:
@@ -459,81 +580,94 @@ var Oidc = /** @class */ (function () {
459
580
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
460
581
  case 3:
461
582
  serviceWorker = _d.sent();
462
- if (!serviceWorker) return [3 /*break*/, 8];
583
+ if (!serviceWorker) return [3 /*break*/, 9];
463
584
  return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "tryKeepExistingSessionAsync")];
464
585
  case 4:
465
586
  tokens = (_d.sent()).tokens;
466
- if (!tokens) return [3 /*break*/, 7];
587
+ if (!tokens) return [3 /*break*/, 8];
467
588
  serviceWorker.startKeepAliveServiceWorker();
468
- return [4 /*yield*/, this.refreshTokensAsync(tokens.refresh_token, true)];
469
- case 5:
470
- updatedTokens = _d.sent();
471
- // @ts-ignore
589
+ reformattedToken = {
590
+ accessToken: tokens.access_token,
591
+ refreshToken: tokens.refresh_token,
592
+ expiresIn: tokens.expires_in,
593
+ idToken: tokens.id_token,
594
+ scope: tokens.scope,
595
+ tokenType: tokens.token_type
596
+ };
472
597
  _a = this;
473
- return [4 /*yield*/, setTokensAsync(serviceWorker, updatedTokens)];
474
- case 6:
475
- // @ts-ignore
598
+ return [4 /*yield*/, setTokensAsync(serviceWorker, reformattedToken)];
599
+ case 5:
476
600
  _a.tokens = _d.sent();
477
601
  this.serviceWorker = serviceWorker;
478
602
  // @ts-ignore
479
- this.timeoutId = autoRenewTokens(this, updatedTokens.refreshToken, this.tokens.expiresAt);
603
+ this.timeoutId = autoRenewTokens(this, this.tokens.refreshToken, this.tokens.expiresAt);
604
+ return [4 /*yield*/, serviceWorker.getSessionStateAsync()];
605
+ case 6:
606
+ sessionState = _d.sent();
607
+ return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
608
+ case 7:
609
+ _d.sent();
480
610
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
481
611
  success: true,
482
612
  message: "tokens inside ServiceWorker are valid"
483
613
  });
484
614
  return [2 /*return*/, true];
485
- case 7:
615
+ case 8:
486
616
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
487
617
  success: false,
488
618
  message: "no exiting session found"
489
619
  });
490
- return [3 /*break*/, 12];
491
- case 8:
620
+ return [3 /*break*/, 13];
621
+ case 9:
492
622
  if (configuration.service_worker_relative_url) {
493
623
  this.publishEvent(eventNames.service_worker_not_supported_by_browser, {
494
624
  message: "service worker is not supported by this browser"
495
625
  });
496
626
  }
497
- session = (0, initSession_1.initSession)(this.configurationName, (_c = configuration.storage) !== null && _c !== void 0 ? _c : sessionStorage);
627
+ session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_c = configuration.storage) !== null && _c !== void 0 ? _c : sessionStorage);
498
628
  return [4 /*yield*/, session.initAsync()];
499
- case 9:
500
- tokens = (_d.sent()).tokens;
501
- if (!tokens) return [3 /*break*/, 12];
502
- return [4 /*yield*/, this.refreshTokensAsync(tokens.refreshToken, true)];
503
629
  case 10:
504
- updatedTokens = _d.sent();
630
+ tokens = (_d.sent()).tokens;
631
+ console.log("const {tokens} = await session.initAsync();");
632
+ console.log(tokens);
633
+ if (!tokens) return [3 /*break*/, 13];
505
634
  // @ts-ignore
506
635
  _b = this;
507
- return [4 /*yield*/, setTokensAsync(serviceWorker, updatedTokens)];
636
+ return [4 /*yield*/, setTokensAsync(serviceWorker, tokens)];
508
637
  case 11:
509
638
  // @ts-ignore
510
639
  _b.tokens = _d.sent();
511
- session.setTokens(this.tokens);
640
+ //session.setTokens(this.tokens);
512
641
  this.session = session;
513
642
  // @ts-ignore
514
- this.timeoutId = autoRenewTokens(this, updatedTokens.refreshToken, this.tokens.expiresAt);
643
+ this.timeoutId = autoRenewTokens(this, tokens.refreshToken, this.tokens.expiresAt);
644
+ sessionState = session.getSessionState();
645
+ return [4 /*yield*/, this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState)];
646
+ case 12:
647
+ _d.sent();
515
648
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
516
649
  success: true,
517
650
  message: "tokens inside storage are valid"
518
651
  });
519
652
  return [2 /*return*/, true];
520
- case 12:
653
+ case 13:
521
654
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
522
655
  success: false,
523
656
  message: serviceWorker ? "service worker sessions not retrieved" : "session storage sessions not retrieved"
524
657
  });
525
658
  return [2 /*return*/, false];
526
- case 13:
659
+ case 14:
527
660
  exception_1 = _d.sent();
528
- if (!serviceWorker) return [3 /*break*/, 15];
661
+ console.error(exception_1);
662
+ if (!serviceWorker) return [3 /*break*/, 16];
529
663
  return [4 /*yield*/, serviceWorker.clearAsync()];
530
- case 14:
531
- _d.sent();
532
- _d.label = 15;
533
664
  case 15:
665
+ _d.sent();
666
+ _d.label = 16;
667
+ case 16:
534
668
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_error, "tokens inside ServiceWorker are invalid");
535
669
  return [2 /*return*/, false];
536
- case 16: return [2 /*return*/];
670
+ case 17: return [2 /*return*/];
537
671
  }
538
672
  });
539
673
  }); };
@@ -545,165 +679,226 @@ var Oidc = /** @class */ (function () {
545
679
  });
546
680
  });
547
681
  };
548
- Oidc.prototype.loginAsync = function (callbackPath, extras, installServiceWorker, state) {
682
+ Oidc.prototype.loginAsync = function (callbackPath, extras, installServiceWorker, state, isSilentSignin, scope) {
549
683
  if (callbackPath === void 0) { callbackPath = undefined; }
550
684
  if (extras === void 0) { extras = null; }
551
685
  if (installServiceWorker === void 0) { installServiceWorker = true; }
552
686
  if (state === void 0) { state = undefined; }
687
+ if (isSilentSignin === void 0) { isSilentSignin = false; }
688
+ if (scope === void 0) { scope = undefined; }
553
689
  return __awaiter(this, void 0, void 0, function () {
554
- var location_1, url, configuration, serviceWorker, oidcServerConfiguration, isServiceWorkerProxyActive, storage, session, queryStringUtil, authorizationHandler, authRequest, exception_2;
690
+ var loginLocalAsync;
691
+ var _this = this;
555
692
  return __generator(this, function (_a) {
556
- switch (_a.label) {
557
- case 0:
558
- _a.trys.push([0, 9, , 10]);
559
- location_1 = window.location;
560
- url = callbackPath || location_1.pathname + (location_1.search || '') + (location_1.hash || '');
561
- this.publishEvent(eventNames.loginAsync_begin, {});
562
- configuration = this.configuration;
563
- // Security we cannot loggin from Iframe
564
- if (!configuration.silent_redirect_uri && isInIframe()) {
565
- throw new Error("Login from iframe is forbidden");
566
- }
567
- sessionStorage["oidc_login.".concat(this.configurationName)] = JSON.stringify({ callbackPath: url, extras: extras, state: state });
568
- return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
569
- case 1:
570
- serviceWorker = _a.sent();
571
- return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
572
- case 2:
573
- oidcServerConfiguration = _a.sent();
574
- if (!(serviceWorker && installServiceWorker)) return [3 /*break*/, 4];
575
- return [4 /*yield*/, serviceWorker.isServiceWorkerProxyActiveAsync()];
576
- case 3:
577
- isServiceWorkerProxyActive = _a.sent();
578
- if (!isServiceWorkerProxyActive) {
579
- window.location.href = "".concat(configuration.redirect_uri, "/service-worker-install");
580
- return [2 /*return*/];
581
- }
582
- _a.label = 4;
583
- case 4:
584
- storage = void 0;
585
- if (!serviceWorker) return [3 /*break*/, 7];
586
- serviceWorker.startKeepAliveServiceWorker();
587
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "loginAsync")];
588
- case 5:
589
- _a.sent();
590
- storage = new memoryStorageBackend_1.MemoryStorageBackend(serviceWorker.saveItemsAsync, {});
591
- return [4 /*yield*/, storage.setItem("dummy", {})];
592
- case 6:
593
- _a.sent();
594
- return [3 /*break*/, 8];
595
- case 7:
596
- session = (0, initSession_1.initSession)(this.configurationName);
597
- storage = new memoryStorageBackend_1.MemoryStorageBackend(session.saveItemsAsync, {});
598
- _a.label = 8;
599
- case 8:
600
- queryStringUtil = configuration.redirect_uri.includes("#") ? new noHashQueryStringUtils_1.HashQueryStringUtils() : new noHashQueryStringUtils_1.NoHashQueryStringUtils();
601
- authorizationHandler = new appauth_1.RedirectRequestHandler(storage, queryStringUtil, window.location, new appauth_1.DefaultCrypto());
602
- authRequest = new appauth_1.AuthorizationRequest({
603
- client_id: configuration.client_id,
604
- redirect_uri: configuration.redirect_uri,
605
- scope: configuration.scope,
606
- response_type: appauth_1.AuthorizationRequest.RESPONSE_TYPE_CODE,
607
- state: state,
608
- extras: extras !== null && extras !== void 0 ? extras : configuration.extras
609
- });
610
- authorizationHandler.performAuthorizationRequest(oidcServerConfiguration, authRequest);
611
- return [3 /*break*/, 10];
612
- case 9:
613
- exception_2 = _a.sent();
614
- this.publishEvent(eventNames.loginAsync_error, exception_2);
615
- throw exception_2;
616
- case 10: return [2 /*return*/];
693
+ if (this.loginPromise !== null) {
694
+ return [2 /*return*/, this.loginPromise];
617
695
  }
696
+ loginLocalAsync = function () { return __awaiter(_this, void 0, void 0, function () {
697
+ var location_1, url, configuration, redirectUri, sessionKey, serviceWorker, oidcServerConfiguration, isServiceWorkerProxyActive, isUnregistered, storage, session, extraFinal, queryStringUtil, authorizationHandler, authRequest, exception_2;
698
+ var _a;
699
+ return __generator(this, function (_b) {
700
+ switch (_b.label) {
701
+ case 0:
702
+ _b.trys.push([0, 11, , 12]);
703
+ location_1 = window.location;
704
+ url = callbackPath || location_1.pathname + (location_1.search || '') + (location_1.hash || '');
705
+ this.publishEvent(eventNames.loginAsync_begin, {});
706
+ configuration = this.configuration;
707
+ redirectUri = isSilentSignin ? configuration.silent_redirect_uri : configuration.redirect_uri;
708
+ if (!scope) {
709
+ scope = configuration.scope;
710
+ }
711
+ sessionKey = getLoginSessionKey(this.configurationName, redirectUri);
712
+ sessionStorage[sessionKey] = JSON.stringify({ callbackPath: url, extras: extras, state: state });
713
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
714
+ case 1:
715
+ serviceWorker = _b.sent();
716
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
717
+ case 2:
718
+ oidcServerConfiguration = _b.sent();
719
+ if (!(serviceWorker && installServiceWorker)) return [3 /*break*/, 6];
720
+ return [4 /*yield*/, serviceWorker.isServiceWorkerProxyActiveAsync()];
721
+ case 3:
722
+ isServiceWorkerProxyActive = _b.sent();
723
+ if (!!isServiceWorkerProxyActive) return [3 /*break*/, 6];
724
+ return [4 /*yield*/, serviceWorker.unregisterAsync()];
725
+ case 4:
726
+ isUnregistered = _b.sent();
727
+ console.log("isUnregistered");
728
+ console.log(isUnregistered);
729
+ if (!isUnregistered) return [3 /*break*/, 6];
730
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
731
+ case 5:
732
+ serviceWorker = _b.sent();
733
+ _b.label = 6;
734
+ case 6:
735
+ storage = void 0;
736
+ if (!serviceWorker) return [3 /*break*/, 9];
737
+ serviceWorker.startKeepAliveServiceWorker();
738
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "loginAsync")];
739
+ case 7:
740
+ _b.sent();
741
+ storage = new memoryStorageBackend_1.MemoryStorageBackend(serviceWorker.saveItemsAsync, {});
742
+ return [4 /*yield*/, storage.setItem("dummy", {})];
743
+ case 8:
744
+ _b.sent();
745
+ return [3 /*break*/, 10];
746
+ case 9:
747
+ session = (0, initSession_1.initSession)(this.configurationName, redirectUri);
748
+ storage = new memoryStorageBackend_1.MemoryStorageBackend(session.saveItemsAsync, {});
749
+ _b.label = 10;
750
+ case 10:
751
+ extraFinal = (_a = extras !== null && extras !== void 0 ? extras : configuration.extras) !== null && _a !== void 0 ? _a : {};
752
+ queryStringUtil = redirectUri.includes("#") ? new noHashQueryStringUtils_1.HashQueryStringUtils() : new noHashQueryStringUtils_1.NoHashQueryStringUtils();
753
+ authorizationHandler = new appauth_1.RedirectRequestHandler(storage, queryStringUtil, window.location, new appauth_1.DefaultCrypto());
754
+ authRequest = new appauth_1.AuthorizationRequest({
755
+ client_id: configuration.client_id,
756
+ redirect_uri: redirectUri,
757
+ scope: scope,
758
+ response_type: appauth_1.AuthorizationRequest.RESPONSE_TYPE_CODE,
759
+ state: state,
760
+ extras: extraFinal
761
+ });
762
+ authorizationHandler.performAuthorizationRequest(oidcServerConfiguration, authRequest);
763
+ return [3 /*break*/, 12];
764
+ case 11:
765
+ exception_2 = _b.sent();
766
+ this.publishEvent(eventNames.loginAsync_error, exception_2);
767
+ throw exception_2;
768
+ case 12: return [2 /*return*/];
769
+ }
770
+ });
771
+ }); };
772
+ this.loginPromise = loginLocalAsync();
773
+ return [2 /*return*/, this.loginPromise.then(function (result) {
774
+ _this.loginPromise = null;
775
+ return result;
776
+ })];
618
777
  });
619
778
  });
620
779
  };
621
- Oidc.prototype.syncTokensAsync = function () {
780
+ Oidc.prototype.startCheckSessionAsync = function (checkSessionIFrameUri, clientId, sessionState, isSilentSignin) {
781
+ if (isSilentSignin === void 0) { isSilentSignin = false; }
622
782
  return __awaiter(this, void 0, void 0, function () {
623
- var configuration, oidcServerConfiguration, serviceWorker, tokens, silent_token_response, _a, exceptionSilent_1;
624
- return __generator(this, function (_b) {
625
- switch (_b.label) {
626
- case 0:
627
- configuration = this.configuration;
628
- if (!this.tokens) {
629
- return [2 /*return*/];
630
- }
631
- return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
632
- case 1:
633
- oidcServerConfiguration = _b.sent();
634
- return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
635
- case 2:
636
- serviceWorker = _b.sent();
637
- if (!serviceWorker) return [3 /*break*/, 11];
638
- return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
639
- case 3:
640
- tokens = (_b.sent()).tokens;
641
- if (!!tokens) return [3 /*break*/, 11];
642
- _b.label = 4;
643
- case 4:
644
- _b.trys.push([4, 9, , 10]);
645
- this.publishEvent(eventNames.syncTokensAsync_begin, {});
646
- this.syncTokensAsyncPromise = this.silentSigninAsync();
647
- return [4 /*yield*/, this.syncTokensAsyncPromise];
648
- case 5:
649
- silent_token_response = _b.sent();
650
- console.log("silent_token_response");
651
- console.log(silent_token_response);
652
- if (!silent_token_response) return [3 /*break*/, 7];
653
- _a = this;
654
- return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response)];
655
- case 6:
656
- _a.tokens = _b.sent();
657
- return [3 /*break*/, 8];
658
- case 7:
659
- this.publishEvent(eventNames.syncTokensAsync_error, null);
660
- if (this.timeoutId) {
661
- timer_1.default.clearTimeout(this.timeoutId);
662
- this.timeoutId = null;
783
+ var _this = this;
784
+ return __generator(this, function (_a) {
785
+ return [2 /*return*/, new Promise(function (resolve, reject) {
786
+ if (_this.configuration.silent_signin_uri && _this.configuration.silent_redirect_uri && _this.configuration.monitor_session && checkSessionIFrameUri && sessionState && !isSilentSignin) {
787
+ var checkSessionCallback = function () {
788
+ _this.checkSessionIFrame.stop();
789
+ if (_this.tokens === null) {
790
+ return;
791
+ }
792
+ // @ts-ignore
793
+ var idToken = _this.tokens.idToken;
794
+ // @ts-ignore
795
+ var idTokenPayload = _this.tokens.idTokenPayload;
796
+ _this.silentSigninAsync({
797
+ prompt: "none",
798
+ id_token_hint: idToken,
799
+ scope: "openid"
800
+ }).then(function (silentSigninResponse) {
801
+ var iFrameIdTokenPayload = silentSigninResponse.tokens.idTokenPayload;
802
+ if (idTokenPayload.sub === iFrameIdTokenPayload.sub) {
803
+ var sessionState_1 = silentSigninResponse.sessionState;
804
+ _this.checkSessionIFrame.start(silentSigninResponse.sessionState);
805
+ if (idTokenPayload.sid === iFrameIdTokenPayload.sid) {
806
+ console.debug("SessionMonitor._callback: Same sub still logged in at OP, restarting check session iframe; session_state:", sessionState_1);
807
+ }
808
+ else {
809
+ console.debug("SessionMonitor._callback: Same sub still logged in at OP, session state has changed, restarting check session iframe; session_state:", sessionState_1);
810
+ }
811
+ }
812
+ else {
813
+ console.debug("SessionMonitor._callback: Different subject signed into OP:", iFrameIdTokenPayload.sub);
814
+ }
815
+ }).catch(function (e) {
816
+ _this.publishEvent(eventNames.logout_from_another_tab, {});
817
+ _this.destroyAsync();
818
+ });
819
+ };
820
+ _this.checkSessionIFrame = new checkSessionIFrame_1.CheckSessionIFrame(checkSessionCallback, clientId, checkSessionIFrameUri);
821
+ _this.checkSessionIFrame.load().then(function () {
822
+ _this.checkSessionIFrame.start(sessionState);
823
+ resolve();
824
+ }).catch(function (e) {
825
+ reject(e);
826
+ });
663
827
  }
664
- return [2 /*return*/];
665
- case 8: return [3 /*break*/, 10];
666
- case 9:
667
- exceptionSilent_1 = _b.sent();
668
- console.error(exceptionSilent_1);
669
- this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_1);
670
- if (this.timeoutId) {
671
- timer_1.default.clearTimeout(this.timeoutId);
672
- this.timeoutId = null;
828
+ else {
829
+ resolve();
673
830
  }
674
- return [2 /*return*/];
675
- case 10:
676
- this.syncTokensAsyncPromise = null;
677
- this.publishEvent(eventNames.syncTokensAsync_end, {});
678
- _b.label = 11;
679
- case 11: return [2 /*return*/];
831
+ })];
832
+ });
833
+ });
834
+ };
835
+ Oidc.prototype.loginCallbackAsync = function (isSilenSignin) {
836
+ if (isSilenSignin === void 0) { isSilenSignin = false; }
837
+ return __awaiter(this, void 0, void 0, function () {
838
+ var loginCallbackLocalAsync;
839
+ var _this = this;
840
+ return __generator(this, function (_a) {
841
+ if (this.loginCallbackPromise !== null) {
842
+ return [2 /*return*/, this.loginCallbackPromise];
680
843
  }
844
+ loginCallbackLocalAsync = function () { return __awaiter(_this, void 0, void 0, function () {
845
+ var response, tokens, parsedTokens;
846
+ return __generator(this, function (_a) {
847
+ switch (_a.label) {
848
+ case 0: return [4 /*yield*/, this._loginCallbackAsync(isSilenSignin)];
849
+ case 1:
850
+ response = _a.sent();
851
+ tokens = response.tokens;
852
+ return [4 /*yield*/, setTokensAsync(this.serviceWorker, tokens)];
853
+ case 2:
854
+ parsedTokens = _a.sent();
855
+ this.tokens = parsedTokens;
856
+ if (!!this.serviceWorker) return [3 /*break*/, 4];
857
+ return [4 /*yield*/, this.session.setTokens(parsedTokens)];
858
+ case 3:
859
+ _a.sent();
860
+ _a.label = 4;
861
+ case 4:
862
+ this.publishEvent(Oidc.eventNames.token_aquired, parsedTokens);
863
+ // @ts-ignore
864
+ return [2 /*return*/, { parsedTokens: parsedTokens, state: response.state, callbackPath: response.callbackPath }];
865
+ }
866
+ });
867
+ }); };
868
+ this.loginCallbackPromise = loginCallbackLocalAsync();
869
+ return [2 /*return*/, this.loginCallbackPromise.then(function (result) {
870
+ _this.loginCallbackPromise = null;
871
+ return result;
872
+ })];
681
873
  });
682
874
  });
683
875
  };
684
- Oidc.prototype.loginCallbackAsync = function () {
876
+ Oidc.prototype._loginCallbackAsync = function (isSilentSignin) {
685
877
  var _a;
878
+ if (isSilentSignin === void 0) { isSilentSignin = false; }
686
879
  return __awaiter(this, void 0, void 0, function () {
687
- var configuration_2, clientId_1, redirectURL_1, authority, tokenRequestTimeout_1, oidcServerConfiguration_1, serviceWorker, storage_1, items, dummy, session, items, exception_3;
880
+ var configuration_2, clientId_1, redirectUri_1, authority, tokenRequestTimeout_1, oidcServerConfiguration_1, queryParams, sessionState_2, serviceWorker, storage_1, items, dummy, session, items, exception_3;
688
881
  var _this = this;
689
882
  return __generator(this, function (_b) {
690
883
  switch (_b.label) {
691
884
  case 0:
692
- _b.trys.push([0, 10, , 11]);
885
+ _b.trys.push([0, 11, , 12]);
693
886
  this.publishEvent(eventNames.loginCallbackAsync_begin, {});
694
887
  configuration_2 = this.configuration;
695
888
  clientId_1 = configuration_2.client_id;
696
- redirectURL_1 = configuration_2.redirect_uri;
889
+ redirectUri_1 = isSilentSignin ? configuration_2.silent_redirect_uri : configuration_2.redirect_uri;
697
890
  authority = configuration_2.authority;
698
891
  tokenRequestTimeout_1 = configuration_2.token_request_timeout;
699
892
  return [4 /*yield*/, this.initAsync(authority, configuration_2.authority_configuration)];
700
893
  case 1:
701
894
  oidcServerConfiguration_1 = _b.sent();
895
+ queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
896
+ sessionState_2 = queryParams.session_state;
702
897
  return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration_2.service_worker_relative_url, this.configurationName)];
703
898
  case 2:
704
899
  serviceWorker = _b.sent();
705
900
  storage_1 = null;
706
- if (!serviceWorker) return [3 /*break*/, 7];
901
+ if (!serviceWorker) return [3 /*break*/, 8];
707
902
  serviceWorker.startKeepAliveServiceWorker();
708
903
  this.serviceWorker = serviceWorker;
709
904
  return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration_1, "loginCallbackAsync")];
@@ -722,19 +917,23 @@ var Oidc = /** @class */ (function () {
722
917
  return [4 /*yield*/, storage_1.removeItem("dummy")];
723
918
  case 6:
724
919
  _b.sent();
725
- return [3 /*break*/, 9];
920
+ return [4 /*yield*/, serviceWorker.setSessionStateAsync(sessionState_2)];
726
921
  case 7:
727
- this.session = (0, initSession_1.initSession)(this.configurationName, (_a = configuration_2.storage) !== null && _a !== void 0 ? _a : sessionStorage);
728
- session = (0, initSession_1.initSession)(this.configurationName);
729
- return [4 /*yield*/, session.loadItemsAsync()];
922
+ _b.sent();
923
+ return [3 /*break*/, 10];
730
924
  case 8:
925
+ this.session = (0, initSession_1.initSession)(this.configurationName, redirectUri_1, (_a = configuration_2.storage) !== null && _a !== void 0 ? _a : sessionStorage);
926
+ session = (0, initSession_1.initSession)(this.configurationName, redirectUri_1);
927
+ session.setSessionState(sessionState_2);
928
+ return [4 /*yield*/, session.loadItemsAsync()];
929
+ case 9:
731
930
  items = _b.sent();
732
931
  storage_1 = new memoryStorageBackend_1.MemoryStorageBackend(session.saveItemsAsync, items);
733
- _b.label = 9;
734
- case 9: return [2 /*return*/, new Promise(function (resolve, reject) {
932
+ _b.label = 10;
933
+ case 10: return [2 /*return*/, new Promise(function (resolve, reject) {
735
934
  // @ts-ignore
736
935
  var queryStringUtil = new noHashQueryStringUtils_1.NoHashQueryStringUtils();
737
- if (configuration_2.redirect_uri.includes("#")) {
936
+ if (redirectUri_1.includes("#")) {
738
937
  var splithash = window.location.href.split("#");
739
938
  if (splithash.length === 2 && splithash[1].includes("?")) {
740
939
  queryStringUtil = new noHashQueryStringUtils_1.HashQueryStringUtils();
@@ -766,7 +965,7 @@ var Oidc = /** @class */ (function () {
766
965
  }
767
966
  var tokenRequest = new appauth_1.TokenRequest({
768
967
  client_id: clientId_1,
769
- redirect_uri: redirectURL_1,
968
+ redirect_uri: redirectUri_1,
770
969
  grant_type: appauth_1.GRANT_TYPE_AUTHORIZATION_CODE,
771
970
  code: response.code,
772
971
  refresh_token: undefined,
@@ -780,14 +979,16 @@ var Oidc = /** @class */ (function () {
780
979
  var tokenHandler = new appauth_1.BaseTokenRequestHandler(new appauth_1.FetchRequestor());
781
980
  tokenHandler.performTokenRequest(oidcServerConfiguration_1, tokenRequest).then(function (tokenResponse) {
782
981
  if (timeoutId) {
783
- var loginParams = (0, exports.getLoginParams)(_this.configurationName);
784
982
  clearTimeout(timeoutId);
785
983
  _this.timeoutId = null;
786
- _this.publishEvent(eventNames.loginCallbackAsync_end, {});
787
- resolve({
788
- tokens: tokenResponse,
789
- state: request.state,
790
- callbackPath: loginParams.callbackPath,
984
+ var loginParams_1 = getLoginParams(_this.configurationName, redirectUri_1);
985
+ _this.startCheckSessionAsync(oidcServerConfiguration_1.check_session_iframe, clientId_1, sessionState_2, isSilentSignin).then(function () {
986
+ _this.publishEvent(eventNames.loginCallbackAsync_end, {});
987
+ resolve({
988
+ tokens: tokenResponse,
989
+ state: request.state,
990
+ callbackPath: loginParams_1.callbackPath,
991
+ });
791
992
  });
792
993
  }
793
994
  });
@@ -804,18 +1005,17 @@ var Oidc = /** @class */ (function () {
804
1005
  });
805
1006
  authorizationHandler.completeAuthorizationRequestIfPossible();
806
1007
  })];
807
- case 10:
1008
+ case 11:
808
1009
  exception_3 = _b.sent();
809
1010
  console.error(exception_3);
810
1011
  this.publishEvent(eventNames.loginCallbackAsync_error, exception_3);
811
1012
  throw exception_3;
812
- case 11: return [2 /*return*/];
1013
+ case 12: return [2 /*return*/];
813
1014
  }
814
1015
  });
815
1016
  });
816
1017
  };
817
- Oidc.prototype.refreshTokensAsync = function (refreshToken, silentEvent) {
818
- if (silentEvent === void 0) { silentEvent = false; }
1018
+ Oidc.prototype.refreshTokensAsync = function (refreshToken) {
819
1019
  return __awaiter(this, void 0, void 0, function () {
820
1020
  var localSilentSigninAsync, configuration, clientId, redirectUri, authority, tokenHandler, extras, _i, _a, _b, key, value, request, oidcServerConfiguration, token_response, exception_4;
821
1021
  var _this = this;
@@ -825,7 +1025,7 @@ var Oidc = /** @class */ (function () {
825
1025
  localSilentSigninAsync = function (exception) {
826
1026
  if (exception === void 0) { exception = null; }
827
1027
  return __awaiter(_this, void 0, void 0, function () {
828
- var silent_token_response, exceptionSilent_2;
1028
+ var silent_token_response, exceptionSilent_1;
829
1029
  return __generator(this, function (_a) {
830
1030
  switch (_a.label) {
831
1031
  case 0:
@@ -834,19 +1034,19 @@ var Oidc = /** @class */ (function () {
834
1034
  case 1:
835
1035
  silent_token_response = _a.sent();
836
1036
  if (silent_token_response) {
837
- return [2 /*return*/, silent_token_response];
1037
+ return [2 /*return*/, silent_token_response.tokens];
838
1038
  }
839
1039
  return [3 /*break*/, 3];
840
1040
  case 2:
841
- exceptionSilent_2 = _a.sent();
842
- console.error(exceptionSilent_2);
1041
+ exceptionSilent_1 = _a.sent();
1042
+ console.error(exceptionSilent_1);
843
1043
  return [3 /*break*/, 3];
844
1044
  case 3:
845
1045
  if (this.timeoutId) {
846
1046
  timer_1.default.clearTimeout(this.timeoutId);
847
1047
  this.timeoutId = null;
848
1048
  }
849
- this.publishEvent(silentEvent ? eventNames.refreshTokensAsync_silent_error : eventNames.refreshTokensAsync_error, exception);
1049
+ this.publishEvent(eventNames.refreshTokensAsync_error, exception);
850
1050
  return [2 /*return*/, null];
851
1051
  }
852
1052
  });
@@ -855,7 +1055,7 @@ var Oidc = /** @class */ (function () {
855
1055
  _c.label = 1;
856
1056
  case 1:
857
1057
  _c.trys.push([1, 6, , 8]);
858
- this.publishEvent(silentEvent ? eventNames.refreshTokensAsync_silent_begin : eventNames.refreshTokensAsync_begin, {});
1058
+ this.publishEvent(eventNames.refreshTokensAsync_begin, { refreshToken: refreshToken });
859
1059
  configuration = this.configuration;
860
1060
  clientId = configuration.client_id;
861
1061
  redirectUri = configuration.redirect_uri;
@@ -887,11 +1087,12 @@ var Oidc = /** @class */ (function () {
887
1087
  return [4 /*yield*/, tokenHandler.performTokenRequest(oidcServerConfiguration, request)];
888
1088
  case 5:
889
1089
  token_response = _c.sent();
890
- this.publishEvent(silentEvent ? eventNames.refreshTokensAsync_silent_end : eventNames.refreshTokensAsync_end, { message: "success" });
1090
+ this.publishEvent(eventNames.refreshTokensAsync_end, { message: "success" });
891
1091
  return [2 /*return*/, token_response];
892
1092
  case 6:
893
1093
  exception_4 = _c.sent();
894
1094
  console.error(exception_4);
1095
+ this.publishEvent(eventNames.refreshTokensAsync_silent_error, exception_4);
895
1096
  return [4 /*yield*/, localSilentSigninAsync(exception_4)];
896
1097
  case 7: return [2 /*return*/, _c.sent()];
897
1098
  case 8: return [2 /*return*/];
@@ -899,6 +1100,87 @@ var Oidc = /** @class */ (function () {
899
1100
  });
900
1101
  });
901
1102
  };
1103
+ Oidc.prototype.syncTokensAsync = function () {
1104
+ var _a;
1105
+ return __awaiter(this, void 0, void 0, function () {
1106
+ var configuration, oidcServerConfiguration, serviceWorker, isLogin, silent_token_response, _b, exceptionSilent_2, session, tokens;
1107
+ return __generator(this, function (_c) {
1108
+ switch (_c.label) {
1109
+ case 0:
1110
+ configuration = this.configuration;
1111
+ if (!this.tokens) {
1112
+ return [2 /*return*/];
1113
+ }
1114
+ return [4 /*yield*/, this.initAsync(configuration.authority, configuration.authority_configuration)];
1115
+ case 1:
1116
+ oidcServerConfiguration = _c.sent();
1117
+ return [4 /*yield*/, (0, initWorker_1.initWorkerAsync)(configuration.service_worker_relative_url, this.configurationName)];
1118
+ case 2:
1119
+ serviceWorker = _c.sent();
1120
+ if (!serviceWorker) return [3 /*break*/, 14];
1121
+ return [4 /*yield*/, serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync")];
1122
+ case 3:
1123
+ isLogin = (_c.sent()).isLogin;
1124
+ if (!(isLogin == false)) return [3 /*break*/, 5];
1125
+ this.publishEvent(eventNames.logout_from_another_tab, {});
1126
+ return [4 /*yield*/, this.destroyAsync()];
1127
+ case 4:
1128
+ _c.sent();
1129
+ return [3 /*break*/, 13];
1130
+ case 5:
1131
+ if (!(isLogin == null)) return [3 /*break*/, 13];
1132
+ _c.label = 6;
1133
+ case 6:
1134
+ _c.trys.push([6, 11, , 12]);
1135
+ this.publishEvent(eventNames.syncTokensAsync_begin, {});
1136
+ this.syncTokensAsyncPromise = this.silentSigninAsync({ prompt: "none" });
1137
+ return [4 /*yield*/, this.syncTokensAsyncPromise];
1138
+ case 7:
1139
+ silent_token_response = _c.sent();
1140
+ if (!(silent_token_response && silent_token_response.tokens)) return [3 /*break*/, 9];
1141
+ _b = this;
1142
+ return [4 /*yield*/, setTokensAsync(serviceWorker, silent_token_response.tokens)];
1143
+ case 8:
1144
+ _b.tokens = _c.sent();
1145
+ return [3 /*break*/, 10];
1146
+ case 9:
1147
+ this.publishEvent(eventNames.syncTokensAsync_error, null);
1148
+ if (this.timeoutId) {
1149
+ timer_1.default.clearTimeout(this.timeoutId);
1150
+ this.timeoutId = null;
1151
+ }
1152
+ return [2 /*return*/];
1153
+ case 10: return [3 /*break*/, 12];
1154
+ case 11:
1155
+ exceptionSilent_2 = _c.sent();
1156
+ console.error(exceptionSilent_2);
1157
+ this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent_2);
1158
+ if (this.timeoutId) {
1159
+ timer_1.default.clearTimeout(this.timeoutId);
1160
+ this.timeoutId = null;
1161
+ }
1162
+ return [2 /*return*/];
1163
+ case 12:
1164
+ this.syncTokensAsyncPromise = null;
1165
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1166
+ _c.label = 13;
1167
+ case 13: return [3 /*break*/, 17];
1168
+ case 14:
1169
+ session = (0, initSession_1.initSession)(this.configurationName, configuration.redirect_uri, (_a = configuration.storage) !== null && _a !== void 0 ? _a : sessionStorage);
1170
+ return [4 /*yield*/, session.initAsync()];
1171
+ case 15:
1172
+ tokens = (_c.sent()).tokens;
1173
+ if (!!tokens) return [3 /*break*/, 17];
1174
+ this.publishEvent(eventNames.logout_from_another_tab, {});
1175
+ return [4 /*yield*/, this.destroyAsync()];
1176
+ case 16:
1177
+ _c.sent();
1178
+ _c.label = 17;
1179
+ case 17: return [2 /*return*/];
1180
+ }
1181
+ });
1182
+ });
1183
+ };
902
1184
  Oidc.prototype.loginCallbackWithAutoTokensRenewAsync = function () {
903
1185
  var _this = this;
904
1186
  if (this.loginCallbackWithAutoTokensRenewPromise !== null) {
@@ -918,6 +1200,11 @@ var Oidc = /** @class */ (function () {
918
1200
  return __generator(this, function (_a) {
919
1201
  switch (_a.label) {
920
1202
  case 0:
1203
+ timer_1.default.clearTimeout(this.timeoutId);
1204
+ this.timeoutId = null;
1205
+ if (this.checkSessionIFrame) {
1206
+ this.checkSessionIFrame.stop();
1207
+ }
921
1208
  if (!this.serviceWorker) return [3 /*break*/, 2];
922
1209
  return [4 /*yield*/, this.serviceWorker.clearAsync()];
923
1210
  case 1:
@@ -933,8 +1220,6 @@ var Oidc = /** @class */ (function () {
933
1220
  this.tokens = null;
934
1221
  this.userInfo = null;
935
1222
  this.events = [];
936
- timer_1.default.clearTimeout(this.timeoutId);
937
- this.timeoutId = null;
938
1223
  return [2 /*return*/];
939
1224
  }
940
1225
  });