@axa-fr/react-oidc 6.0.0-beta1 → 6.0.0-beta10

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 (29) hide show
  1. package/README.md +10 -8
  2. package/dist/OidcProvider.d.ts +1 -1
  3. package/dist/OidcProvider.d.ts.map +1 -1
  4. package/dist/OidcProvider.js +5 -4
  5. package/dist/OidcProvider.js.map +1 -1
  6. package/dist/OidcServiceWorker.js +21 -4
  7. package/dist/OidcTrustedDomains.js +7 -2
  8. package/dist/core/default-component/SilentCallback.component.d.ts.map +1 -1
  9. package/dist/core/default-component/SilentCallback.component.js +5 -19
  10. package/dist/core/default-component/SilentCallback.component.js.map +1 -1
  11. package/dist/core/routes/OidcRoutes.d.ts.map +1 -1
  12. package/dist/core/routes/OidcRoutes.js +1 -4
  13. package/dist/core/routes/OidcRoutes.js.map +1 -1
  14. package/dist/vanilla/oidc.d.ts +5 -5
  15. package/dist/vanilla/oidc.d.ts.map +1 -1
  16. package/dist/vanilla/oidc.js +218 -132
  17. package/dist/vanilla/oidc.js.map +1 -1
  18. package/package.json +1 -1
  19. package/src/oidc/OidcProvider.tsx +8 -8
  20. package/src/oidc/core/default-component/SilentCallback.component.tsx +1 -6
  21. package/src/oidc/core/routes/OidcRoutes.tsx +0 -4
  22. package/src/oidc/vanilla/OidcServiceWorker.js +21 -4
  23. package/src/oidc/vanilla/OidcTrustedDomains.js +7 -2
  24. package/src/oidc/vanilla/oidc.ts +153 -104
  25. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts +0 -4
  26. package/dist/core/default-component/ServiceWorkerInstall.component.d.ts.map +0 -1
  27. package/dist/core/default-component/ServiceWorkerInstall.component.js +0 -131
  28. package/dist/core/default-component/ServiceWorkerInstall.component.js.map +0 -1
  29. package/src/oidc/core/default-component/ServiceWorkerInstall.component.tsx +0 -60
@@ -21,8 +21,7 @@ import {getParseQueryStringFromLocation} from "./route-utils";
21
21
  import {AuthorizationServiceConfigurationJson} from "@openid/appauth/src/authorization_service_configuration";
22
22
 
23
23
  const performTokenRequestAsync= async (url, details, extras) => {
24
-
25
-
24
+
26
25
  for (let [key, value] of Object.entries(extras)) {
27
26
  if (details[key] === undefined) {
28
27
  details[key] = value;
@@ -47,15 +46,22 @@ const performTokenRequestAsync= async (url, details, extras) => {
47
46
  if(response.status !== 200){
48
47
  return {success:false, status: response.status}
49
48
  }
50
- const result = await response.json();
49
+ const tokens = await response.json();
50
+
51
+ if(!tokens.issued_at) {
52
+ const currentTimeUnixSecond = new Date().getTime() /1000;
53
+ tokens.issued_at = currentTimeUnixSecond;
54
+ }
55
+
51
56
  return { success : true,
52
57
  data : {
53
- accessToken: result.access_token,
54
- expiresIn: result.expires_in,
55
- idToken: result.id_token,
56
- refreshToken: result.refresh_token,
57
- scope: result.scope,
58
- tokenType: result.token_type,
58
+ accessToken: tokens.access_token,
59
+ expiresIn: tokens.expires_in,
60
+ idToken: tokens.id_token,
61
+ refreshToken: tokens.refresh_token,
62
+ scope: tokens.scope,
63
+ tokenType: tokens.token_type,
64
+ issuedAt: tokens.issued_at
59
65
  }
60
66
  };
61
67
  }
@@ -210,9 +216,9 @@ const autoRenewTokens = (oidc, refreshToken, expiresAt) => {
210
216
  oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt);
211
217
  }
212
218
  } else{
213
- await oidc.syncTokensAsync();
214
- if(oidc.timeoutId) {
215
- oidc.timeoutId = autoRenewTokens(oidc, refreshToken, expiresAt);
219
+ const tokens = await oidc.syncTokensAsync();
220
+ if(tokens && oidc.timeoutId) {
221
+ oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, tokens.expiresAt);
216
222
  }
217
223
  }
218
224
  }, 1000);
@@ -225,7 +231,6 @@ const getLoginParams = (configurationName, redirectUri) => {
225
231
  return JSON.parse(sessionStorage[getLoginSessionKey(configurationName, redirectUri)]);
226
232
  }
227
233
 
228
-
229
234
  const userInfoAsync = async (oidc) => {
230
235
  if(oidc.userInfo != null){
231
236
  return oidc.userInfo;
@@ -244,7 +249,7 @@ const userInfoAsync = async (oidc) => {
244
249
  const res = await fetch(url, {
245
250
  headers: {
246
251
  authorization: `Bearer ${accessToken}`,
247
- credentials: 'same-origin'
252
+ credentials: 'include'
248
253
  }
249
254
  });
250
255
 
@@ -274,7 +279,10 @@ const setTokensAsync = async (serviceWorker, tokens) =>{
274
279
  accessTokenPayload = extractAccessTokenPayload(tokens);
275
280
  }
276
281
  const _idTokenPayload = idTokenPayload(tokens.idToken);
277
- const expiresAt = (_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
282
+
283
+ const idTokenExipreAt =(_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp: Number.MAX_VALUE;
284
+ const accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp)? accessTokenPayload.exp : tokens.issuedAt + tokens.expiresIn;
285
+ const expiresAt = idTokenExipreAt < accessTokenExpiresAt ? idTokenExipreAt : accessTokenExpiresAt;
278
286
  return {...tokens, idTokenPayload: _idTokenPayload, accessTokenPayload, expiresAt};
279
287
  }
280
288
 
@@ -304,31 +312,30 @@ const eventNames = {
304
312
  syncTokensAsync_begin: "syncTokensAsync_begin",
305
313
  syncTokensAsync_end: "syncTokensAsync_end",
306
314
  syncTokensAsync_error: "syncTokensAsync_error"
307
-
308
315
  }
309
316
 
310
317
  const getRandomInt = (max) => {
311
318
  return Math.floor(Math.random() * max);
312
319
  }
313
320
 
314
- const WELL_KNOWN_PATH = '.well-known';
315
- const OPENID_CONFIGURATION = 'openid-configuration';
316
-
317
-
318
321
  const oneHourSecond = 60 * 60;
319
- const fetchFromIssuer = async (openIdIssuerUrl: string, timeCacheSecond = oneHourSecond):
322
+ let fetchFromIssuerCache = null;
323
+ const fetchFromIssuer = async (openIdIssuerUrl: string, timeCacheSecond = oneHourSecond, storage= window.sessionStorage):
320
324
  Promise<OidcAuthorizationServiceConfiguration> => {
321
- const fullUrl = `${openIdIssuerUrl}/${WELL_KNOWN_PATH}/${OPENID_CONFIGURATION}`;
325
+ const fullUrl = `${openIdIssuerUrl}/.well-known/openid-configuration`;
322
326
 
323
327
  const localStorageKey = `oidc.server:${openIdIssuerUrl}`;
324
- const cacheJson = window.sessionStorage.getItem(localStorageKey);
325
-
328
+ if(!fetchFromIssuerCache && storage) {
329
+ const cacheJson = storage.getItem(localStorageKey);
330
+ if(cacheJson){
331
+ fetchFromIssuerCache = JSON.parse(cacheJson);
332
+ }
333
+ }
326
334
  const oneHourMinisecond = 1000 * timeCacheSecond;
327
335
  // @ts-ignore
328
- if(cacheJson && (cacheJson.timestamp + oneHourMinisecond) > Date.now()){
329
- return new OidcAuthorizationServiceConfiguration(JSON.parse(cacheJson));
336
+ if(fetchFromIssuerCache && (fetchFromIssuerCache.timestamp + oneHourMinisecond) > Date.now()){
337
+ return new OidcAuthorizationServiceConfiguration(fetchFromIssuerCache.result);
330
338
  }
331
-
332
339
  const response = await fetch(fullUrl);
333
340
 
334
341
  if (response.status != 200) {
@@ -336,8 +343,12 @@ const fetchFromIssuer = async (openIdIssuerUrl: string, timeCacheSecond = oneHou
336
343
  }
337
344
 
338
345
  const result = await response.json();
339
- window.sessionStorage.setItem(localStorageKey, JSON.stringify({result, timestamp:Date.now()}));
340
346
 
347
+ const timestamp = Date.now();
348
+ fetchFromIssuerCache = {result, timestamp};
349
+ if(storage) {
350
+ storage.setItem(localStorageKey, JSON.stringify({result, timestamp}));
351
+ }
341
352
  return new OidcAuthorizationServiceConfiguration(result);
342
353
  }
343
354
 
@@ -366,7 +377,12 @@ export class Oidc {
366
377
  private session?: any;
367
378
  private checkSessionIFrame: CheckSessionIFrame;
368
379
  constructor(configuration:OidcConfiguration, configurationName="default") {
369
- this.configuration = configuration
380
+ let silent_login_uri = configuration.silent_login_uri;
381
+ if(configuration.silent_redirect_uri && !configuration.silent_login_uri){
382
+ silent_login_uri = `${configuration.silent_redirect_uri.replace("-callback", "").replace("callback", "")}-login`;
383
+ }
384
+
385
+ this.configuration = {...configuration, silent_login_uri};
370
386
  this.configurationName= configurationName;
371
387
  this.tokens = null
372
388
  this.userInfo = null;
@@ -384,6 +400,8 @@ export class Oidc {
384
400
  this.publishEvent.bind(this);
385
401
  this.destroyAsync.bind(this);
386
402
  this.logoutAsync.bind(this);
403
+
404
+ this.initAsync(this.configuration.authority, this.configuration.authority_configuration);
387
405
  }
388
406
 
389
407
  subscriveEvents(func){
@@ -415,18 +433,29 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
415
433
  }
416
434
  static eventNames = eventNames;
417
435
 
418
- silentLoginCallbackFromIFrame(){
436
+ _silentLoginCallbackFromIFrame(){
419
437
  if (this.configuration.silent_redirect_uri && this.configuration.silent_login_uri) {
420
438
  const queryParams = getParseQueryStringFromLocation(window.location.href);
421
439
  window.top.postMessage(`${this.configurationName}_oidc_tokens:${JSON.stringify({tokens:this.tokens, sessionState:queryParams.session_state})}`, window.location.origin);
422
440
  }
423
441
  }
424
- silentLoginErrorCallbackFromIFrame(){
442
+ _silentLoginErrorCallbackFromIFrame() {
425
443
  if (this.configuration.silent_redirect_uri && this.configuration.silent_login_uri) {
426
444
  const queryParams = getParseQueryStringFromLocation(window.location.href);
427
- window.top.postMessage(`${this.configurationName}_oidc_error:${JSON.stringify({error:queryParams.error})}`, window.location.origin);
445
+ window.top.postMessage(`${this.configurationName}_oidc_error:${JSON.stringify({error: queryParams.error})}`, window.location.origin);
428
446
  }
429
447
  }
448
+
449
+ async silentLoginCallBackAsync() {
450
+ try {
451
+ await this.loginCallbackAsync(true);
452
+ this._silentLoginCallbackFromIFrame();
453
+ } catch (error) {
454
+ console.error(error)
455
+ this._silentLoginErrorCallbackFromIFrame();
456
+ }
457
+ }
458
+
430
459
  async silentLoginAsync(extras:StringMap=null, state:string=null, scope:string=null) {
431
460
  if (!this.configuration.silent_redirect_uri || !this.configuration.silent_login_uri) {
432
461
  return Promise.resolve(null);
@@ -435,6 +464,13 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
435
464
  await sleepAsync(1000);
436
465
  this.publishEvent(eventNames.silentLoginAsync, {message:"wait because document is hidden"});
437
466
  }
467
+
468
+ let numberTryOnline = 6;
469
+ while (!navigator.onLine && numberTryOnline > 0) {
470
+ await sleepAsync(1000);
471
+ numberTryOnline--;
472
+ this.publishEvent(eventNames.refreshTokensAsync, {message: `wait because navigator is offline try ${numberTryOnline}` });
473
+ }
438
474
 
439
475
  try {
440
476
  this.publishEvent(eventNames.silentLoginAsync_begin, {});
@@ -499,7 +535,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
499
535
  self.publishEvent(eventNames.silentLoginAsync_error, result);
500
536
  iframe.remove();
501
537
  isResolved = true;
502
- reject(result);
538
+ reject(new Error("oidc_"+result.error));
503
539
  }
504
540
  }
505
541
  }
@@ -508,10 +544,10 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
508
544
  const silentSigninTimeout = configuration.silent_login_timeout ?? 12000
509
545
  setTimeout(() => {
510
546
  if (!isResolved) {
511
- self.publishEvent(eventNames.silentLoginAsync_error, "timeout");
547
+ self.publishEvent(eventNames.silentLoginAsync_error, {reason: "timeout"});
512
548
  iframe.remove();
513
549
  isResolved = true;
514
- reject("timeout");
550
+ reject(new Error("timeout"));
515
551
  }
516
552
  }, silentSigninTimeout);
517
553
  } catch (e) {
@@ -525,7 +561,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
525
561
  throw e;
526
562
  }
527
563
  }
528
- initAsyncPromise = null;
529
564
  async initAsync(authority:string, authorityConfiguration:AuthorityConfiguration) {
530
565
  if (authorityConfiguration != null) {
531
566
  return new OidcAuthorizationServiceConfiguration( {
@@ -537,12 +572,11 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
537
572
  check_session_iframe:authorityConfiguration.check_session_iframe,
538
573
  });
539
574
  }
540
- if(this.initAsyncPromise){
541
- return this.initAsyncPromise;
542
- }
543
-
544
- this.initAsyncPromise = await fetchFromIssuer(authority, this.configuration.authority_time_cache_wellknowurl_in_second ?? 60 * 60);
545
- return this.initAsyncPromise;
575
+
576
+ const serviceWorker = await initWorkerAsync(this.configuration.service_worker_relative_url, this.configurationName);
577
+ const storage = serviceWorker ? window.localStorage : null;
578
+ const initAsyncPromise = await fetchFromIssuer(authority, this.configuration.authority_time_cache_wellknowurl_in_second ?? 60 * 60, storage);
579
+ return initAsyncPromise;
546
580
  }
547
581
 
548
582
  tryKeepExistingSessionPromise = null;
@@ -572,13 +606,15 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
572
606
  expiresIn: tokens.expires_in,
573
607
  idToken: tokens.id_token,
574
608
  scope: tokens.scope,
575
- tokenType: tokens.token_type
609
+ tokenType: tokens.token_type,
610
+ issuedAt: tokens.issued_at
576
611
  }
577
612
  this.tokens = await setTokensAsync(serviceWorker, reformattedToken);
578
613
  this.serviceWorker = serviceWorker;
579
614
  // @ts-ignore
580
615
  this.timeoutId = autoRenewTokens(this, this.tokens.refreshToken, this.tokens.expiresAt);
581
616
  const sessionState = await serviceWorker.getSessionStateAsync();
617
+ // @ts-ignore
582
618
  await this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState);
583
619
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
584
620
  success: true,
@@ -606,6 +642,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
606
642
  // @ts-ignore
607
643
  this.timeoutId = autoRenewTokens(this, tokens.refreshToken, this.tokens.expiresAt);
608
644
  const sessionState = session.getSessionState();
645
+ // @ts-ignore
609
646
  await this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, configuration.client_id, sessionState);
610
647
  this.publishEvent(eventNames.tryKeepExistingSessionAsync_end, {
611
648
  success: true,
@@ -659,23 +696,6 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
659
696
 
660
697
  let serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, this.configurationName);
661
698
  const oidcServerConfiguration = await this.initAsync(configuration.authority, configuration.authority_configuration);
662
- /*if (serviceWorker && installServiceWorker) {
663
- const isServiceWorkerProxyActive = await serviceWorker.isServiceWorkerProxyActiveAsync();
664
- if (!isServiceWorkerProxyActive) {
665
- const isUnregistered = await serviceWorker.unregisterAsync();
666
- console.log("isUnregistered")
667
- console.log(isUnregistered)
668
- if(isUnregistered){
669
- serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, this.configurationName);
670
- }
671
- const extrasQueries = extras != null ? {...extras}: {};
672
- extrasQueries.callbackPath = url;
673
- extrasQueries.state = state;
674
- const queryString = buildQueries(extrasQueries);
675
- window.location.href = `${redirectUri}/service-worker-install${queryString}`;
676
- //return;
677
- }
678
- }*/
679
699
  let storage;
680
700
  if (serviceWorker) {
681
701
  serviceWorker.startKeepAliveServiceWorker();
@@ -878,6 +898,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
878
898
  clearTimeout(timeoutId);
879
899
  this.timeoutId=null;
880
900
  const loginParams = getLoginParams(this.configurationName, redirectUri);
901
+ // @ts-ignore
881
902
  this.startCheckSessionAsync(oidcServerConfiguration.check_session_iframe, clientId, sessionState, isSilentSignin).then(() =>{
882
903
  this.publishEvent(eventNames.loginCallbackAsync_end, {});
883
904
  resolve({
@@ -911,7 +932,8 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
911
932
 
912
933
  const localsilentLoginAsync= async () => {
913
934
  try {
914
- const silent_token_response = await this.silentLoginAsync();
935
+ const loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
936
+ const silent_token_response = await this.silentLoginAsync(loginParams.extras, loginParams.state);
915
937
  if (silent_token_response) {
916
938
  return silent_token_response.tokens;
917
939
  }
@@ -952,7 +974,7 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
952
974
  };
953
975
 
954
976
  let index = 0;
955
- while (index <=2) {
977
+ while (index <=4) {
956
978
  try {
957
979
  this.publishEvent(eventNames.refreshTokensAsync_begin, {refreshToken:refreshToken, tryNumber: index});
958
980
  if(index > 1) {
@@ -960,6 +982,12 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
960
982
  await sleepAsync(1000);
961
983
  this.publishEvent(eventNames.refreshTokensAsync, {message: "wait because document is hidden"});
962
984
  }
985
+ let numberTryOnline = 6;
986
+ while (!navigator.onLine && numberTryOnline > 0) {
987
+ await sleepAsync(1000);
988
+ numberTryOnline--;
989
+ this.publishEvent(eventNames.refreshTokensAsync, {message: `wait because navigator is offline try ${numberTryOnline}` });
990
+ }
963
991
  }
964
992
  const tokenResponse = await performTokenRequestAsync(oidcServerConfiguration.tokenEndpoint, details, extras)
965
993
  if (tokenResponse.success) {
@@ -980,55 +1008,76 @@ Please checkout that you are using OIDC hook inside a <OidcProvider configuratio
980
1008
 
981
1009
  syncTokensAsyncPromise=null;
982
1010
  async syncTokensAsync() {
983
- // Service Worker can be killed by the browser (when it wants,for example after 10 seconds of inactivity, so we retreieve the session if it happen)
984
- const configuration = this.configuration;
985
- if(!this.tokens){
986
- return;
987
- }
988
-
989
- const oidcServerConfiguration = await this.initAsync(configuration.authority, configuration.authority_configuration);
990
- const serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, this.configurationName);
991
- if (serviceWorker) {
992
- const { isLogin } = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync");
993
- if(isLogin == false){
994
- this.publishEvent(eventNames.logout_from_another_tab, {});
995
- await this.destroyAsync();
1011
+
1012
+ const localSyncTokensAsync = async () => {
1013
+ // Service Worker can be killed by the browser (when it wants,for example after 10 seconds of inactivity, so we retreieve the session if it happen)
1014
+ const configuration = this.configuration;
1015
+ if (!this.tokens) {
1016
+ return null;
996
1017
  }
997
- else if (isLogin == null){
998
- try {
999
- this.publishEvent(eventNames.syncTokensAsync_begin, {});
1000
- this.syncTokensAsyncPromise = this.silentLoginAsync({prompt:"none"});
1001
- const silent_token_response = await this.syncTokensAsyncPromise;
1002
- if (silent_token_response && silent_token_response.tokens) {
1003
- this.tokens = await setTokensAsync(serviceWorker, silent_token_response.tokens);
1004
- } else{
1005
- this.publishEvent(eventNames.syncTokensAsync_error, {message:"no token found in result"});
1006
- if(this.timeoutId){
1018
+
1019
+ const oidcServerConfiguration = await this.initAsync(configuration.authority, configuration.authority_configuration);
1020
+ const serviceWorker = await initWorkerAsync(configuration.service_worker_relative_url, this.configurationName);
1021
+ if (serviceWorker) {
1022
+ const {isLogin} = await serviceWorker.initAsync(oidcServerConfiguration, "syncTokensAsync");
1023
+ if (isLogin == false) {
1024
+ this.publishEvent(eventNames.logout_from_another_tab, {});
1025
+ await this.destroyAsync();
1026
+ return null;
1027
+ } else if (isLogin == null) {
1028
+ try {
1029
+ this.publishEvent(eventNames.syncTokensAsync_begin, {});
1030
+ const loginParams = getLoginParams(this.configurationName, configuration.redirect_uri);
1031
+ const silent_token_response = await this.silentLoginAsync({...loginParams.extras,prompt: "none"}, loginParams.state);
1032
+ if (silent_token_response && silent_token_response.tokens) {
1033
+ this.tokens = await setTokensAsync(serviceWorker, silent_token_response.tokens);
1034
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1035
+ return this.tokens;
1036
+ } else {
1037
+ this.publishEvent(eventNames.syncTokensAsync_error, {message: "no token found in result"});
1038
+ if (this.timeoutId) {
1039
+ timer.clearTimeout(this.timeoutId);
1040
+ this.timeoutId = null;
1041
+ }
1042
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1043
+ return null;
1044
+ }
1045
+ } catch (exceptionSilent) {
1046
+ console.error(exceptionSilent);
1047
+ this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent);
1048
+ if (this.timeoutId) {
1007
1049
  timer.clearTimeout(this.timeoutId);
1008
- this.timeoutId=null;
1050
+ this.timeoutId = null;
1009
1051
  }
1010
- return;
1011
- }
1012
- } catch (exceptionSilent) {
1013
- console.error(exceptionSilent);
1014
- this.publishEvent(eventNames.syncTokensAsync_error, exceptionSilent);
1015
- if(this.timeoutId){
1016
- timer.clearTimeout(this.timeoutId);
1017
- this.timeoutId=null;
1052
+ this.publishEvent(eventNames.syncTokensAsync_end, {});
1053
+ return null;
1018
1054
  }
1019
- return;
1055
+
1056
+ }
1057
+ } else {
1058
+ const session = initSession(this.configurationName, configuration.redirect_uri, configuration.storage ?? sessionStorage);
1059
+ const {tokens} = await session.initAsync();
1060
+ if (!tokens) {
1061
+ this.publishEvent(eventNames.logout_from_another_tab, {});
1062
+ await this.destroyAsync();
1063
+ return null;
1020
1064
  }
1021
- this.syncTokensAsyncPromise = null;
1022
- this.publishEvent(eventNames.syncTokensAsync_end, {});
1023
- }
1024
- } else {
1025
- const session = initSession(this.configurationName, configuration.redirect_uri, configuration.storage ?? sessionStorage);
1026
- const {tokens} = await session.initAsync();
1027
- if(!tokens){
1028
- this.publishEvent(eventNames.logout_from_another_tab, {});
1029
- await this.destroyAsync();
1030
1065
  }
1066
+ return this.tokens;
1031
1067
  }
1068
+
1069
+ if(this.syncTokensAsyncPromise){
1070
+ return this.syncTokensAsyncPromise;
1071
+ }
1072
+
1073
+ this.syncTokensAsyncPromise = localSyncTokensAsync().then(result =>{
1074
+ if(this.syncTokensAsyncPromise){
1075
+ this.syncTokensAsyncPromise = null;
1076
+ }
1077
+ return result;
1078
+ });
1079
+
1080
+ return this.syncTokensAsyncPromise
1032
1081
  }
1033
1082
 
1034
1083
 
@@ -1,4 +0,0 @@
1
- import { ComponentType } from 'react';
2
- declare const ServiceWorkerInstall: ComponentType<any>;
3
- export default ServiceWorkerInstall;
4
- //# sourceMappingURL=ServiceWorkerInstall.component.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ServiceWorkerInstall.component.d.ts","sourceRoot":"","sources":["../../../src/oidc/core/default-component/ServiceWorkerInstall.component.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAsB,aAAa,EAAC,MAAM,OAAO,CAAC;AAMhE,QAAA,MAAM,oBAAoB,EAAE,aAAa,CAAC,GAAG,CAmD5C,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
@@ -1,131 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (_) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
- var __importDefault = (this && this.__importDefault) || function (mod) {
62
- return (mod && mod.__esModule) ? mod : { "default": mod };
63
- };
64
- Object.defineProperty(exports, "__esModule", { value: true });
65
- var react_1 = __importStar(require("react"));
66
- var AuthenticateError_component_1 = __importDefault(require("./AuthenticateError.component"));
67
- var oidc_1 = __importDefault(require("../../vanilla/oidc"));
68
- var Authenticating_component_1 = __importDefault(require("./Authenticating.component"));
69
- var route_utils_1 = require("../../vanilla/route-utils");
70
- var ServiceWorkerInstall = function (_a) {
71
- var callBackError = _a.callBackError, authenticating = _a.authenticating, configurationName = _a.configurationName;
72
- var getOidc = oidc_1.default.get;
73
- var _b = (0, react_1.useState)(false), error = _b[0], setError = _b[1];
74
- var _c = (0, react_1.useState)(true), isLoading = _c[0], setLoading = _c[1];
75
- var CallbackErrorComponent = callBackError || AuthenticateError_component_1.default;
76
- var CallbackSuccessComponent = authenticating || Authenticating_component_1.default;
77
- (0, react_1.useEffect)(function () {
78
- var isMounted = true;
79
- var playCallbackAsync = function () { return __awaiter(void 0, void 0, void 0, function () {
80
- var queryParams, extras, _i, _a, _b, key, value, error_1;
81
- return __generator(this, function (_c) {
82
- switch (_c.label) {
83
- case 0:
84
- _c.trys.push([0, 2, , 3]);
85
- queryParams = (0, route_utils_1.getParseQueryStringFromLocation)(window.location.href);
86
- extras = null;
87
- for (_i = 0, _a = Object.entries(queryParams); _i < _a.length; _i++) {
88
- _b = _a[_i], key = _b[0], value = _b[1];
89
- if (key === "state" || key == "callbackPath") {
90
- continue;
91
- }
92
- if (extras === null) {
93
- extras = {};
94
- }
95
- extras[key] = value;
96
- }
97
- // @ts-ignore
98
- return [4 /*yield*/, getOidc(configurationName).loginAsync(queryParams.callbackPath, extras, false, queryParams.state)];
99
- case 1:
100
- // @ts-ignore
101
- _c.sent();
102
- if (isMounted) {
103
- setLoading(false);
104
- }
105
- return [3 /*break*/, 3];
106
- case 2:
107
- error_1 = _c.sent();
108
- if (isMounted) {
109
- setError(true);
110
- setLoading(false);
111
- }
112
- return [3 /*break*/, 3];
113
- case 3: return [2 /*return*/];
114
- }
115
- });
116
- }); };
117
- playCallbackAsync();
118
- return function () {
119
- isMounted = false;
120
- };
121
- }, []);
122
- if (isLoading) {
123
- return null;
124
- }
125
- if (error) {
126
- return react_1.default.createElement(CallbackErrorComponent, { configurationName: configurationName });
127
- }
128
- return react_1.default.createElement(CallbackSuccessComponent, { configurationName: configurationName });
129
- };
130
- exports.default = ServiceWorkerInstall;
131
- //# sourceMappingURL=ServiceWorkerInstall.component.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ServiceWorkerInstall.component.js","sourceRoot":"","sources":["../../../src/oidc/core/default-component/ServiceWorkerInstall.component.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAgE;AAChE,8FAAgE;AAChE,4DAAsC;AACtC,wFAAwD;AACxD,yDAA0E;AAE1E,IAAM,oBAAoB,GAAuB,UAAC,EAAmD;QAAlD,aAAa,mBAAA,EAAE,cAAc,oBAAA,EAAE,iBAAiB,uBAAA;IAC/F,IAAM,OAAO,GAAI,cAAI,CAAC,GAAG,CAAC;IACpB,IAAA,KAAoB,IAAA,gBAAQ,EAAC,KAAK,CAAC,EAAlC,KAAK,QAAA,EAAE,QAAQ,QAAmB,CAAC;IACpC,IAAA,KAA0B,IAAA,gBAAQ,EAAC,IAAI,CAAC,EAAvC,SAAS,QAAA,EAAE,UAAU,QAAkB,CAAC;IAE/C,IAAM,sBAAsB,GAAG,aAAa,IAAI,qCAAmB,CAAC;IACpE,IAAM,wBAAwB,GAAG,cAAc,IAAI,kCAAc,CAAC;IAElE,IAAA,iBAAS,EAAC;QACN,IAAI,SAAS,GAAG,IAAI,CAAC;QACrB,IAAM,iBAAiB,GAAG;;;;;;wBAEZ,WAAW,GAAG,IAAA,6CAA+B,EAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;wBACtE,MAAM,GAAG,IAAI,CAAC;wBAClB,WAAoD,EAA3B,KAAA,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAA3B,cAA2B,EAA3B,IAA2B,EAAE;4BAA7C,WAAY,EAAX,GAAG,QAAA,EAAE,KAAK,QAAA;4BAChB,IAAG,GAAG,KAAK,OAAO,IAAI,GAAG,IAAI,cAAc,EAAC;gCACxC,SAAS;6BACZ;4BACD,IAAG,MAAM,KAAK,IAAI,EAAC;gCACf,MAAM,GAAG,EAAE,CAAC;6BACf;4BACD,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;yBACvB;wBAED,aAAa;wBACb,qBAAM,OAAO,CAAC,iBAAiB,CAAC,CAAC,UAAU,CAAC,WAAW,CAAC,YAAY,EAAE,MAAM,EAAC,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC,EAAA;;wBADtG,aAAa;wBACb,SAAsG,CAAC;wBACvG,IAAG,SAAS,EAAE;4BACV,UAAU,CAAC,KAAK,CAAC,CAAC;yBACrB;;;;wBAED,IAAG,SAAS,EAAE;4BACV,QAAQ,CAAC,IAAI,CAAC,CAAC;4BACf,UAAU,CAAC,KAAK,CAAC,CAAC;yBACrB;;;;;aAER,CAAC;QACF,iBAAiB,EAAE,CAAC;QACpB,OAAO;YACH,SAAS,GAAG,KAAK,CAAC;QACtB,CAAC,CAAC;IACN,CAAC,EAAC,EAAE,CAAC,CAAC;IAEN,IAAG,SAAS,EAAC;QACT,OAAO,IAAI,CAAC;KACf;IAED,IAAG,KAAK,EAAC;QACL,OAAO,8BAAC,sBAAsB,IAAC,iBAAiB,EAAE,iBAAiB,GAAI,CAAA;KAC1E;IAED,OAAO,8BAAC,wBAAwB,IAAC,iBAAiB,EAAE,iBAAiB,GAAI,CAAC;AAC9E,CAAC,CAAC;AAEF,kBAAe,oBAAoB,CAAC"}
@@ -1,60 +0,0 @@
1
- import React, {useEffect, useState, ComponentType} from 'react';
2
- import AuthenticatingError from "./AuthenticateError.component";
3
- import Oidc from "../../vanilla/oidc";
4
- import Authenticating from "./Authenticating.component";
5
- import {getParseQueryStringFromLocation} from "../../vanilla/route-utils";
6
-
7
- const ServiceWorkerInstall: ComponentType<any> = ({callBackError, authenticating, configurationName }) => {
8
- const getOidc = Oidc.get;
9
- const [error, setError] = useState(false);
10
- const [isLoading, setLoading] = useState(true);
11
-
12
- const CallbackErrorComponent = callBackError || AuthenticatingError;
13
- const CallbackSuccessComponent = authenticating || Authenticating;
14
-
15
- useEffect(() => {
16
- let isMounted = true;
17
- const playCallbackAsync = async () => {
18
- try {
19
- const queryParams = getParseQueryStringFromLocation(window.location.href);
20
- let extras = null;
21
- for (let [key, value] of Object.entries(queryParams)) {
22
- if(key === "state" || key == "callbackPath"){
23
- continue;
24
- }
25
- if(extras === null){
26
- extras = {};
27
- }
28
- extras[key] = value;
29
- }
30
-
31
- // @ts-ignore
32
- await getOidc(configurationName).loginAsync(queryParams.callbackPath, extras,false, queryParams.state);
33
- if(isMounted) {
34
- setLoading(false);
35
- }
36
- } catch (error) {
37
- if(isMounted) {
38
- setError(true);
39
- setLoading(false);
40
- }
41
- }
42
- };
43
- playCallbackAsync();
44
- return () => {
45
- isMounted = false;
46
- };
47
- },[]);
48
-
49
- if(isLoading){
50
- return null;
51
- }
52
-
53
- if(error){
54
- return <CallbackErrorComponent configurationName={configurationName} />
55
- }
56
-
57
- return <CallbackSuccessComponent configurationName={configurationName} />;
58
- };
59
-
60
- export default ServiceWorkerInstall;