@axa-fr/react-oidc 5.13.12 → 6.0.0-alpha1

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