@opengeoweb/authentication 14.0.1 → 14.1.0

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.
package/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import React__default, { useRef } from 'react';
3
+ import React__default, { useRef, useEffect } from 'react';
4
4
  import axios from 'axios';
5
5
  import { Navigate, Link } from 'react-router-dom';
6
6
  import { sessionStorageProvider, useIsMounted, SHARED_NAMESPACE, AlertBanner, useDebounce } from '@opengeoweb/shared';
@@ -346,28 +346,18 @@ function _regeneratorRuntime() {
346
346
  })();
347
347
  }
348
348
 
349
- var API_NAMESPACE$1 = 'api';
350
349
  var DEFAULT_TIMEOUT = 15000;
351
350
  var KEEP_ALIVE_POLLER_IN_SECONDS = 60; // Number of seconds between the checks if the token should be refreshed.
352
351
  var REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75; // Refresh token when 75% expired. Set to (10 / 3600) * 100 = 0.2777778% to test with 10 second interval (assuming 1 hour token expiration).
353
352
  var DEFAULT_TOKEN_EXPIRES_IN = 3600; // Number of seconds a token expires by default
354
353
  var MILLISECOND_TO_SECOND = 1 / 1000;
355
- var ns$1 = API_NAMESPACE$1;
356
354
  var GEOWEB_ROLE_PRESETS_ADMIN = {
357
355
  name: 'ROLE_PRESET_ADMIN',
358
- getTitle: function getTitle(t) {
359
- return t('api-role-title-preset-admin', {
360
- ns: ns$1
361
- });
362
- }
356
+ title: 'api-role-title-preset-admin'
363
357
  };
364
358
  var GEOWEB_ROLE_USER = {
365
359
  name: 'ROLE_USER',
366
- getTitle: function getTitle(t) {
367
- return t('api-role-title-user', {
368
- ns: ns$1
369
- });
370
- }
360
+ title: 'api-role-title-user'
371
361
  };
372
362
  /**
373
363
  * Creates a Credentials object based on the axios response from the token service.
@@ -378,7 +368,8 @@ var GEOWEB_ROLE_USER = {
378
368
  * @returns Credentials object.
379
369
  */
380
370
  var makeCredentialsFromTokenResponse = function makeCredentialsFromTokenResponse(tokenResponse, authConfig) {
381
- var token = tokenResponse.data.body || tokenResponse.data;
371
+ var _tokenResponse$data$b;
372
+ var token = (_tokenResponse$data$b = tokenResponse.data.body) != null ? _tokenResponse$data$b : tokenResponse.data;
382
373
  // eslint-disable-next-line @typescript-eslint/naming-convention
383
374
  var access_token = token.access_token,
384
375
  refresh_token = token.refresh_token,
@@ -402,60 +393,67 @@ var makeCredentialsFromTokenResponse = function makeCredentialsFromTokenResponse
402
393
  username: isAuthCognito ? userInfo['cognito:username'] : userInfo.email,
403
394
  roles: groupsToRoles(groups, authConfig),
404
395
  token: access_token,
405
- refresh_token: refresh_token || '',
396
+ refresh_token: refresh_token != null ? refresh_token : '',
406
397
  expires_at: epochTimeTokenExpirationInSeconds,
407
398
  has_connection_issue: false
408
399
  };
409
400
  return newAuth;
410
401
  };
411
- var refreshAccessToken = function refreshAccessToken(_ref) {
412
- var auth = _ref.auth,
413
- _ref$config = _ref.config,
414
- _ref$config2 = _ref$config === void 0 ? {} : _ref$config,
415
- authTokenURL = _ref$config2.authTokenURL,
416
- authClientId = _ref$config2.authClientId,
417
- appURL = _ref$config2.appURL,
418
- _ref$timeout = _ref.timeout,
419
- timeout = _ref$timeout === void 0 ? DEFAULT_TIMEOUT : _ref$timeout;
420
- // Refresh token request with a new axios instance
421
- // without request interceptor
422
- var tokenAxiosInstance = axios.create({
423
- headers: {},
424
- timeout: timeout
425
- });
426
- var refreshPayload = {
427
- refresh_token: auth.refresh_token,
428
- redirect_uri: appURL + "/code",
429
- grant_type: 'refresh_token',
430
- client_id: authClientId
431
- };
432
- /* Send data in the "application/x-www-form-urlencoded" format.
433
- If only JSON is supported, use Axios' default content type ("application/x-www-form-urlencoded"). */
434
- var useDefaultContentType = authTokenURL.includes('amazonaws.com');
435
- var data = useDefaultContentType ? refreshPayload : new URLSearchParams(refreshPayload);
436
- var axiosConfig = {
437
- headers: {
438
- 'Content-Type': useDefaultContentType ? 'application/json' : 'application/x-www-form-urlencoded'
439
- }
440
- };
441
- return tokenAxiosInstance.post(authTokenURL, data, axiosConfig);
442
- };
443
- var refreshAccessTokenAndSetAuthContext = /*#__PURE__*/function () {
444
- var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref2) {
445
- var auth, onSetAuth, config, _ref2$timeout, timeout, configURLS, onLogin, refreshedToken, newAuth, _e$response, status;
402
+ var refreshAccessToken = /*#__PURE__*/function () {
403
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
404
+ var auth, _ref$config, _ref$config2, authTokenURL, authClientId, appURL, _ref$timeout, timeout, tokenAxiosInstance, refreshPayload, useDefaultContentType, data, axiosConfig;
446
405
  return _regeneratorRuntime().wrap(function _callee$(_context) {
447
406
  while (1) switch (_context.prev = _context.next) {
448
407
  case 0:
449
- auth = _ref2.auth, onSetAuth = _ref2.onSetAuth, config = _ref2.config, _ref2$timeout = _ref2.timeout, timeout = _ref2$timeout === void 0 ? DEFAULT_TIMEOUT : _ref2$timeout, configURLS = _ref2.configURLS, onLogin = _ref2.onLogin;
450
- _context.prev = 1;
451
- _context.next = 4;
408
+ auth = _ref.auth, _ref$config = _ref.config, _ref$config2 = _ref$config === void 0 ? {} : _ref$config, authTokenURL = _ref$config2.authTokenURL, authClientId = _ref$config2.authClientId, appURL = _ref$config2.appURL, _ref$timeout = _ref.timeout, timeout = _ref$timeout === void 0 ? DEFAULT_TIMEOUT : _ref$timeout;
409
+ // Refresh token request with a new axios instance
410
+ // without request interceptor
411
+ tokenAxiosInstance = axios.create({
412
+ headers: {},
413
+ timeout: timeout
414
+ });
415
+ refreshPayload = {
416
+ refresh_token: auth.refresh_token,
417
+ redirect_uri: appURL + "/code",
418
+ grant_type: 'refresh_token',
419
+ client_id: authClientId
420
+ };
421
+ /* Send data in the "application/x-www-form-urlencoded" format.
422
+ If only JSON is supported, use Axios' default content type ("application/x-www-form-urlencoded"). */
423
+ useDefaultContentType = authTokenURL.includes('amazonaws.com');
424
+ data = useDefaultContentType ? refreshPayload : new URLSearchParams(refreshPayload);
425
+ axiosConfig = {
426
+ headers: {
427
+ 'Content-Type': useDefaultContentType ? 'application/json' : 'application/x-www-form-urlencoded'
428
+ }
429
+ };
430
+ return _context.abrupt("return", tokenAxiosInstance.post(authTokenURL, data, axiosConfig));
431
+ case 7:
432
+ case "end":
433
+ return _context.stop();
434
+ }
435
+ }, _callee);
436
+ }));
437
+ return function refreshAccessToken(_x) {
438
+ return _ref2.apply(this, arguments);
439
+ };
440
+ }();
441
+ var refreshAccessTokenAndSetAuthContext = /*#__PURE__*/function () {
442
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(_ref3) {
443
+ var auth, onSetAuth, config, _ref3$timeout, timeout, configURLS, onLogin, refreshedToken, newAuth, _e$response, status;
444
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
445
+ while (1) switch (_context2.prev = _context2.next) {
446
+ case 0:
447
+ auth = _ref3.auth, onSetAuth = _ref3.onSetAuth, config = _ref3.config, _ref3$timeout = _ref3.timeout, timeout = _ref3$timeout === void 0 ? DEFAULT_TIMEOUT : _ref3$timeout, configURLS = _ref3.configURLS, onLogin = _ref3.onLogin;
448
+ _context2.prev = 1;
449
+ _context2.next = 4;
452
450
  return refreshAccessToken({
453
451
  auth: auth,
454
452
  config: config,
455
453
  timeout: timeout
456
454
  });
457
455
  case 4:
458
- refreshedToken = _context.sent;
456
+ refreshedToken = _context2.sent;
459
457
  newAuth = makeCredentialsFromTokenResponse(refreshedToken, configURLS); // Cognito does not send a new refresh token, but gitlab does. Set it here into the auth context.
460
458
  if (!newAuth.refresh_token || newAuth.refresh_token.length === 0) {
461
459
  newAuth.refresh_token = auth.refresh_token;
@@ -465,12 +463,12 @@ var refreshAccessTokenAndSetAuthContext = /*#__PURE__*/function () {
465
463
  newAuth.roles = auth.roles;
466
464
  }
467
465
  onSetAuth(newAuth);
468
- _context.next = 15;
466
+ _context2.next = 15;
469
467
  break;
470
468
  case 11:
471
- _context.prev = 11;
472
- _context.t0 = _context["catch"](1);
473
- status = (_e$response = _context.t0.response) == null ? void 0 : _e$response.status;
469
+ _context2.prev = 11;
470
+ _context2.t0 = _context2["catch"](1);
471
+ status = (_e$response = _context2.t0.response) == null ? void 0 : _e$response.status;
474
472
  if (status === 400) {
475
473
  onLogin(false);
476
474
  } else {
@@ -480,21 +478,21 @@ var refreshAccessTokenAndSetAuthContext = /*#__PURE__*/function () {
480
478
  }
481
479
  case 15:
482
480
  case "end":
483
- return _context.stop();
481
+ return _context2.stop();
484
482
  }
485
- }, _callee, null, [[1, 11]]);
483
+ }, _callee2, null, [[1, 11]]);
486
484
  }));
487
- return function refreshAccessTokenAndSetAuthContext(_x) {
488
- return _ref3.apply(this, arguments);
485
+ return function refreshAccessTokenAndSetAuthContext(_x2) {
486
+ return _ref4.apply(this, arguments);
489
487
  };
490
488
  }();
491
- var createApiInstance = function createApiInstance(_ref4) {
492
- var auth = _ref4.auth,
493
- _ref4$config = _ref4.config,
494
- _ref4$config2 = _ref4$config === void 0 ? {} : _ref4$config,
495
- baseURL = _ref4$config2.baseURL,
496
- _ref4$timeout = _ref4.timeout,
497
- timeout = _ref4$timeout === void 0 ? DEFAULT_TIMEOUT : _ref4$timeout;
489
+ var createApiInstance = function createApiInstance(_ref5) {
490
+ var auth = _ref5.auth,
491
+ _ref5$config = _ref5.config,
492
+ _ref5$config2 = _ref5$config === void 0 ? {} : _ref5$config,
493
+ baseURL = _ref5$config2.baseURL,
494
+ _ref5$timeout = _ref5.timeout,
495
+ timeout = _ref5$timeout === void 0 ? DEFAULT_TIMEOUT : _ref5$timeout;
498
496
  var axiosInstance = axios.create({
499
497
  baseURL: baseURL,
500
498
  headers: {},
@@ -502,10 +500,10 @@ var createApiInstance = function createApiInstance(_ref4) {
502
500
  });
503
501
  // Request interceptor for API calls done BEFORE the request is made.
504
502
  axiosInstance.interceptors.request.use(/*#__PURE__*/function () {
505
- var _ref5 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(axiosConfig) {
503
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(axiosConfig) {
506
504
  var newConfig;
507
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
508
- while (1) switch (_context2.prev = _context2.next) {
505
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
506
+ while (1) switch (_context3.prev = _context3.next) {
509
507
  case 0:
510
508
  // Add the access token to the headers of the request.
511
509
  newConfig = Object.assign({}, axiosConfig, {
@@ -513,47 +511,48 @@ var createApiInstance = function createApiInstance(_ref4) {
513
511
  'Content-Type': 'application/json',
514
512
  Authorization: "Bearer " + auth.token,
515
513
  Accept: 'application/json'
516
- }, axiosConfig.headers)
514
+ }, axiosConfig.headers || {})
517
515
  });
518
- return _context2.abrupt("return", newConfig);
516
+ return _context3.abrupt("return", newConfig);
519
517
  case 2:
520
518
  case "end":
521
- return _context2.stop();
519
+ return _context3.stop();
522
520
  }
523
- }, _callee2);
521
+ }, _callee3);
524
522
  }));
525
- return function (_x2) {
526
- return _ref5.apply(this, arguments);
523
+ return function (_x3) {
524
+ return _ref6.apply(this, arguments);
527
525
  };
528
526
  }(), /*#__PURE__*/function () {
529
- var _ref6 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(error) {
530
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
531
- while (1) switch (_context3.prev = _context3.next) {
527
+ var _ref7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(error) {
528
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
529
+ while (1) switch (_context4.prev = _context4.next) {
532
530
  case 0:
533
- _context3.next = 2;
531
+ _context4.next = 2;
534
532
  return Promise.reject(error);
535
533
  case 2:
536
534
  case "end":
537
- return _context3.stop();
535
+ return _context4.stop();
538
536
  }
539
- }, _callee3);
537
+ }, _callee4);
540
538
  }));
541
- return function (_x3) {
542
- return _ref6.apply(this, arguments);
539
+ return function (_x4) {
540
+ return _ref7.apply(this, arguments);
543
541
  };
544
542
  }());
545
543
  // Response interceptor for API calls done AFTER the request is made.
546
544
  axiosInstance.interceptors.response.use(function (response) {
547
545
  return response;
548
546
  }, /*#__PURE__*/function () {
549
- var _ref7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(error) {
547
+ var _ref8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(error) {
548
+ var _error$response;
550
549
  var originalRequest, _originalRequest$head;
551
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
552
- while (1) switch (_context4.prev = _context4.next) {
550
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
551
+ while (1) switch (_context5.prev = _context5.next) {
553
552
  case 0:
554
553
  originalRequest = error.config; // If request fails with 401, retry the request once.
555
- if (!(error.response && error.response.status && error.response.status === 401 && !originalRequest.inRetry)) {
556
- _context4.next = 5;
554
+ if (!((_error$response = error.response) != null && _error$response.status && error.response.status === 401 && !originalRequest.inRetry)) {
555
+ _context5.next = 5;
557
556
  break;
558
557
  }
559
558
  originalRequest.inRetry = true;
@@ -561,27 +560,27 @@ var createApiInstance = function createApiInstance(_ref4) {
561
560
  if ((_originalRequest$head = originalRequest.headers) != null && _originalRequest$head.Authorization) {
562
561
  originalRequest.headers.Authorization = "Bearer " + (auth == null ? void 0 : auth.token);
563
562
  }
564
- return _context4.abrupt("return", axiosInstance(originalRequest));
563
+ return _context5.abrupt("return", axiosInstance(originalRequest));
565
564
  case 5:
566
- return _context4.abrupt("return", Promise.reject(error));
565
+ return _context5.abrupt("return", Promise.reject(error));
567
566
  case 6:
568
567
  case "end":
569
- return _context4.stop();
568
+ return _context5.stop();
570
569
  }
571
- }, _callee4);
570
+ }, _callee5);
572
571
  }));
573
- return function (_x4) {
574
- return _ref7.apply(this, arguments);
572
+ return function (_x5) {
573
+ return _ref8.apply(this, arguments);
575
574
  };
576
575
  }());
577
576
  return axiosInstance;
578
577
  };
579
- var createNonAuthApiInstance = function createNonAuthApiInstance(_ref8) {
580
- var _ref8$config = _ref8.config,
581
- _ref8$config2 = _ref8$config === void 0 ? {} : _ref8$config,
582
- baseURL = _ref8$config2.baseURL,
583
- _ref8$timeout = _ref8.timeout,
584
- timeout = _ref8$timeout === void 0 ? DEFAULT_TIMEOUT : _ref8$timeout;
578
+ var createNonAuthApiInstance = function createNonAuthApiInstance(_ref9) {
579
+ var _ref9$config = _ref9.config,
580
+ _ref9$config2 = _ref9$config === void 0 ? {} : _ref9$config,
581
+ baseURL = _ref9$config2.baseURL,
582
+ _ref9$timeout = _ref9.timeout,
583
+ timeout = _ref9$timeout === void 0 ? DEFAULT_TIMEOUT : _ref9$timeout;
585
584
  var axiosInstance = axios.create({
586
585
  baseURL: baseURL,
587
586
  headers: {},
@@ -589,74 +588,157 @@ var createNonAuthApiInstance = function createNonAuthApiInstance(_ref8) {
589
588
  });
590
589
  // Request interceptor for API calls
591
590
  axiosInstance.interceptors.request.use(/*#__PURE__*/function () {
592
- var _ref9 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(config) {
591
+ var _ref0 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(config) {
593
592
  var newConfig;
594
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
595
- while (1) switch (_context5.prev = _context5.next) {
593
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
594
+ while (1) switch (_context6.prev = _context6.next) {
596
595
  case 0:
597
596
  newConfig = Object.assign({}, config, {
598
597
  headers: Object.assign({
599
598
  'Content-Type': 'application/json',
600
599
  Accept: 'application/json'
601
- }, config.headers)
600
+ }, config.headers || {})
602
601
  });
603
- return _context5.abrupt("return", newConfig);
602
+ return _context6.abrupt("return", newConfig);
604
603
  case 2:
605
604
  case "end":
606
- return _context5.stop();
605
+ return _context6.stop();
607
606
  }
608
- }, _callee5);
607
+ }, _callee6);
609
608
  }));
610
- return function (_x5) {
611
- return _ref9.apply(this, arguments);
609
+ return function (_x6) {
610
+ return _ref0.apply(this, arguments);
612
611
  };
613
612
  }(), /*#__PURE__*/function () {
614
- var _ref0 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(error) {
615
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
616
- while (1) switch (_context6.prev = _context6.next) {
613
+ var _ref1 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(error) {
614
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
615
+ while (1) switch (_context7.prev = _context7.next) {
617
616
  case 0:
618
- _context6.next = 2;
617
+ _context7.next = 2;
619
618
  return Promise.reject(error);
620
619
  case 2:
621
620
  case "end":
622
- return _context6.stop();
621
+ return _context7.stop();
623
622
  }
624
- }, _callee6);
623
+ }, _callee7);
625
624
  }));
626
- return function (_x6) {
627
- return _ref0.apply(this, arguments);
625
+ return function (_x7) {
626
+ return _ref1.apply(this, arguments);
628
627
  };
629
628
  }());
630
629
  return axiosInstance;
631
630
  };
632
- var fakeApiRequest = function fakeApiRequest(signal) {
633
- return new Promise(function (resolve, reject) {
634
- var timer = setTimeout(function () {
635
- resolve();
636
- }, 300);
637
- signal == null || signal.signal.addEventListener('abort', function () {
638
- clearTimeout(timer);
639
- reject(new Error('canceled'));
640
- });
641
- });
642
- };
631
+ var fakeApiRequest = /*#__PURE__*/function () {
632
+ var _ref10 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(signal) {
633
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
634
+ while (1) switch (_context8.prev = _context8.next) {
635
+ case 0:
636
+ return _context8.abrupt("return", new Promise(function (resolve, reject) {
637
+ var timer = setTimeout(function () {
638
+ resolve();
639
+ }, 300);
640
+ signal == null || signal.signal.addEventListener('abort', function () {
641
+ clearTimeout(timer);
642
+ reject(new Error('canceled'));
643
+ });
644
+ }));
645
+ case 1:
646
+ case "end":
647
+ return _context8.stop();
648
+ }
649
+ }, _callee8);
650
+ }));
651
+ return function fakeApiRequest(_x8) {
652
+ return _ref10.apply(this, arguments);
653
+ };
654
+ }();
643
655
  var createFakeApiInstance = function createFakeApiInstance() {
644
656
  return {
645
- get: function get(_url, _params) {
646
- return fakeApiRequest();
647
- },
648
- put: function put(_url, _params) {
649
- return fakeApiRequest();
650
- },
651
- post: function post(_url, _params) {
652
- return fakeApiRequest();
653
- },
654
- "delete": function _delete(_url, _params) {
655
- return fakeApiRequest();
656
- },
657
- patch: function patch(_url, _params, _signal) {
658
- return fakeApiRequest(_signal);
659
- }
657
+ get: function () {
658
+ var _get = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(_url, _params) {
659
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
660
+ while (1) switch (_context9.prev = _context9.next) {
661
+ case 0:
662
+ return _context9.abrupt("return", fakeApiRequest());
663
+ case 1:
664
+ case "end":
665
+ return _context9.stop();
666
+ }
667
+ }, _callee9);
668
+ }));
669
+ function get(_x9, _x0) {
670
+ return _get.apply(this, arguments);
671
+ }
672
+ return get;
673
+ }(),
674
+ put: function () {
675
+ var _put = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee0(_url, _params) {
676
+ return _regeneratorRuntime().wrap(function _callee0$(_context0) {
677
+ while (1) switch (_context0.prev = _context0.next) {
678
+ case 0:
679
+ return _context0.abrupt("return", fakeApiRequest());
680
+ case 1:
681
+ case "end":
682
+ return _context0.stop();
683
+ }
684
+ }, _callee0);
685
+ }));
686
+ function put(_x1, _x10) {
687
+ return _put.apply(this, arguments);
688
+ }
689
+ return put;
690
+ }(),
691
+ post: function () {
692
+ var _post = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee1(_url, _params) {
693
+ return _regeneratorRuntime().wrap(function _callee1$(_context1) {
694
+ while (1) switch (_context1.prev = _context1.next) {
695
+ case 0:
696
+ return _context1.abrupt("return", fakeApiRequest());
697
+ case 1:
698
+ case "end":
699
+ return _context1.stop();
700
+ }
701
+ }, _callee1);
702
+ }));
703
+ function post(_x11, _x12) {
704
+ return _post.apply(this, arguments);
705
+ }
706
+ return post;
707
+ }(),
708
+ "delete": function () {
709
+ var _delete2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(_url, _params) {
710
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
711
+ while (1) switch (_context10.prev = _context10.next) {
712
+ case 0:
713
+ return _context10.abrupt("return", fakeApiRequest());
714
+ case 1:
715
+ case "end":
716
+ return _context10.stop();
717
+ }
718
+ }, _callee10);
719
+ }));
720
+ function _delete(_x13, _x14) {
721
+ return _delete2.apply(this, arguments);
722
+ }
723
+ return _delete;
724
+ }(),
725
+ patch: function () {
726
+ var _patch = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(_url, _params, _signal) {
727
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
728
+ while (1) switch (_context11.prev = _context11.next) {
729
+ case 0:
730
+ return _context11.abrupt("return", fakeApiRequest(_signal));
731
+ case 1:
732
+ case "end":
733
+ return _context11.stop();
734
+ }
735
+ }, _callee11);
736
+ }));
737
+ function patch(_x15, _x16, _x17) {
738
+ return _patch.apply(this, arguments);
739
+ }
740
+ return patch;
741
+ }()
660
742
  };
661
743
  };
662
744
  var getCurrentTimeInSeconds = function getCurrentTimeInSeconds() {
@@ -751,10 +833,15 @@ var AuthenticationContext = /*#__PURE__*/React__default.createContext({
751
833
  sessionStorageProvider: null,
752
834
  currentRole: null
753
835
  });
836
+ var authChangeListener = null;
837
+ var setAuthChangeListener = function setAuthChangeListener(listener) {
838
+ authChangeListener = listener;
839
+ };
754
840
  var useAuthenticationDefaultProps = function useAuthenticationDefaultProps() {
841
+ var _auth$roles$, _auth$roles;
755
842
  var _React$useState = React__default.useState(false),
756
843
  isLoggedIn = _React$useState[0],
757
- onLogin = _React$useState[1];
844
+ setIsLoggedIn = _React$useState[1];
758
845
  var _React$useState2 = React__default.useState(false),
759
846
  hasConnectionIssue = _React$useState2[0],
760
847
  setHasConnectionIssue = _React$useState2[1];
@@ -766,27 +853,32 @@ var useAuthenticationDefaultProps = function useAuthenticationDefaultProps() {
766
853
  expires_at: 0,
767
854
  has_connection_issue: false
768
855
  };
769
- var auth = React__default.useRef(Object.assign({}, emptyCredentials)).current;
770
- var onSetAuth = function onSetAuth(newAuth) {
856
+ var _React$useState3 = React__default.useState(Object.assign({}, emptyCredentials)),
857
+ auth = _React$useState3[0],
858
+ setAuth = _React$useState3[1];
859
+ var onSetAuth = React__default.useCallback(function (newAuth) {
771
860
  if (newAuth) {
772
- Object.assign(auth, newAuth);
773
- if (newAuth.has_connection_issue !== undefined && hasConnectionIssue !== auth.has_connection_issue) {
774
- /*
775
- * The hasconnection issue should trigger a render.
776
- * This is needed to make the connection banner show that there is an issue.
777
- */
778
- setHasConnectionIssue(auth.has_connection_issue === true);
861
+ setAuth(function (prevAuth) {
862
+ return Object.assign({}, prevAuth, newAuth);
863
+ });
864
+ if (newAuth.has_connection_issue !== undefined && hasConnectionIssue !== newAuth.has_connection_issue) {
865
+ setHasConnectionIssue(newAuth.has_connection_issue);
779
866
  }
780
867
  } else {
781
- Object.assign(auth, emptyCredentials);
868
+ setAuth(Object.assign({}, emptyCredentials));
782
869
  }
783
- };
784
- var _React$useState3 = React__default.useState(auth.roles && auth.roles[0] || GEOWEB_ROLE_USER),
785
- currentRole = _React$useState3[0],
786
- setCurrentRole = _React$useState3[1];
870
+ },
871
+ // eslint-disable-next-line react-hooks/exhaustive-deps
872
+ [hasConnectionIssue]);
873
+ var _React$useState4 = React__default.useState((_auth$roles$ = (_auth$roles = auth.roles) == null ? void 0 : _auth$roles[0]) != null ? _auth$roles$ : GEOWEB_ROLE_USER),
874
+ currentRole = _React$useState4[0],
875
+ setCurrentRole = _React$useState4[1];
876
+ useEffect(function () {
877
+ authChangeListener == null || authChangeListener(isLoggedIn, auth);
878
+ }, [isLoggedIn, auth]);
787
879
  return {
788
880
  isLoggedIn: isLoggedIn,
789
- onLogin: onLogin,
881
+ onLogin: setIsLoggedIn,
790
882
  auth: isLoggedIn ? auth : null,
791
883
  onSetAuth: onSetAuth,
792
884
  sessionStorageProvider: sessionStorageProvider,
@@ -800,7 +892,7 @@ var AuthenticationProvider = function AuthenticationProvider(_ref2) {
800
892
  _ref2$configURLS = _ref2.configURLS,
801
893
  configURLS = _ref2$configURLS === void 0 ? authConfigKeys : _ref2$configURLS;
802
894
  var defaultValues = useAuthenticationDefaultProps();
803
- var _ref3 = value || defaultValues,
895
+ var _ref3 = value != null ? value : defaultValues,
804
896
  isLoggedIn = _ref3.isLoggedIn,
805
897
  onLogin = _ref3.onLogin,
806
898
  auth = _ref3.auth,
@@ -1155,7 +1247,7 @@ var UserMenuRoles = function UserMenuRoles(_ref) {
1155
1247
  var _useAuthenticationTra = useAuthenticationTranslation(),
1156
1248
  t = _useAuthenticationTra.t;
1157
1249
  return jsxs(Fragment, {
1158
- children: [isLoading && jsx(Box, {
1250
+ children: [isLoading ? jsx(Box, {
1159
1251
  sx: {
1160
1252
  position: 'absolute',
1161
1253
  top: 0,
@@ -1166,7 +1258,7 @@ var UserMenuRoles = function UserMenuRoles(_ref) {
1166
1258
  "data-testid": "loading-bar",
1167
1259
  color: "secondary"
1168
1260
  })
1169
- }), jsxs(FormGroup, {
1261
+ }) : null, jsxs(FormGroup, {
1170
1262
  children: [jsx(FormLabel, {
1171
1263
  component: "legend",
1172
1264
  sx: {
@@ -1176,13 +1268,13 @@ var UserMenuRoles = function UserMenuRoles(_ref) {
1176
1268
  opacity: 1
1177
1269
  },
1178
1270
  children: t('auth-role-title')
1179
- }), roles && roles.map(function (role) {
1271
+ }), roles == null ? void 0 : roles.map(function (role) {
1180
1272
  return jsx(FormControlLabel, {
1181
1273
  control: jsx(Radio, {
1182
1274
  disabled: isLoading,
1183
1275
  checked: currentRole === role.name
1184
1276
  }),
1185
- label: role.getTitle(t),
1277
+ label: t(role.title),
1186
1278
  onChange: function onChange() {
1187
1279
  return onChangeRole(role);
1188
1280
  }
@@ -1208,13 +1300,26 @@ var UserMenuRolesConnect = function UserMenuRolesConnect(_ref) {
1208
1300
  var dispatch = useDispatch();
1209
1301
  var _useAuthenticationTra = useAuthenticationTranslation(),
1210
1302
  t = _useAuthenticationTra.t;
1211
- var onFakeRequest = function onFakeRequest() {
1212
- return new Promise(function (resolve) {
1213
- setTimeout(function () {
1214
- resolve();
1215
- }, 300);
1216
- });
1217
- };
1303
+ var onFakeRequest = /*#__PURE__*/function () {
1304
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
1305
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1306
+ while (1) switch (_context.prev = _context.next) {
1307
+ case 0:
1308
+ return _context.abrupt("return", new Promise(function (resolve) {
1309
+ setTimeout(function () {
1310
+ resolve();
1311
+ }, 300);
1312
+ }));
1313
+ case 1:
1314
+ case "end":
1315
+ return _context.stop();
1316
+ }
1317
+ }, _callee);
1318
+ }));
1319
+ return function onFakeRequest() {
1320
+ return _ref2.apply(this, arguments);
1321
+ };
1322
+ }();
1218
1323
  var onChangeRole = function onChangeRole(role) {
1219
1324
  setIsLoading(true);
1220
1325
  onFakeRequest().then(function () {
@@ -1224,7 +1329,7 @@ var UserMenuRolesConnect = function UserMenuRolesConnect(_ref) {
1224
1329
  type: snackbarTypes.SnackbarMessageType.TRANSLATABLE_MESSAGE,
1225
1330
  key: 'auth-role-message-assigned',
1226
1331
  params: {
1227
- role: role.getTitle(t)
1332
+ role: t(role.title)
1228
1333
  }
1229
1334
  };
1230
1335
  dispatch(snackbarActions.openSnackbar(message));
@@ -1382,4 +1487,4 @@ params, callbacks) {
1382
1487
  };
1383
1488
  };
1384
1489
 
1385
- export { AUTH_NAMESPACE, ApiProvider, AuthenticationContext, AuthenticationProvider, HandleOAuth2Code as Code, GEOWEB_ROLE_PRESETS_ADMIN, GEOWEB_ROLE_USER, KEEP_ALIVE_POLLER_IN_SECONDS, OAuth2Login as Login, OAuth2Logout as Logout, MILLISECOND_TO_SECOND, REFRESH_TOKEN_WHEN_PCT_EXPIRED, UserMenuRoles, UserMenuRolesConnect, apiTranslations, authTranslations, createApiInstance, createFakeApiInstance, createNonAuthApiInstance, fakeApiRequest, getApi, getAuthConfig, getCodeChallenge, getCurrentTimeInSeconds, getCurrentUrlLocation, getRandomString, groupsToRoles, makeCredentialsFromTokenResponse, refreshAccessToken, refreshAccessTokenAndSetAuthContext, useApi, useApiContext, useAuthenticationContext, useAuthenticationDefaultProps };
1490
+ export { AUTH_NAMESPACE, ApiProvider, AuthenticationContext, AuthenticationProvider, HandleOAuth2Code as Code, GEOWEB_ROLE_PRESETS_ADMIN, GEOWEB_ROLE_USER, KEEP_ALIVE_POLLER_IN_SECONDS, OAuth2Login as Login, OAuth2Logout as Logout, MILLISECOND_TO_SECOND, REFRESH_TOKEN_WHEN_PCT_EXPIRED, UserMenuRoles, UserMenuRolesConnect, apiTranslations, authTranslations, createApiInstance, createFakeApiInstance, createNonAuthApiInstance, fakeApiRequest, getApi, getAuthConfig, getCodeChallenge, getCurrentTimeInSeconds, getCurrentUrlLocation, getRandomString, groupsToRoles, makeCredentialsFromTokenResponse, refreshAccessToken, refreshAccessTokenAndSetAuthContext, setAuthChangeListener, useApi, useApiContext, useAuthenticationContext, useAuthenticationDefaultProps };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opengeoweb/authentication",
3
- "version": "14.0.1",
3
+ "version": "14.1.0",
4
4
  "description": "GeoWeb authentication library for the opengeoweb project",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -10,11 +10,11 @@
10
10
  "dependencies": {
11
11
  "axios": "^1.7.7",
12
12
  "react-router-dom": "^6.23.1",
13
- "@opengeoweb/shared": "14.0.1",
14
- "@opengeoweb/theme": "14.0.1",
13
+ "@opengeoweb/shared": "14.1.0",
14
+ "@opengeoweb/theme": "14.1.0",
15
15
  "i18next": "^25.0.1",
16
16
  "react-i18next": "^15.1.1",
17
- "@opengeoweb/snackbar": "14.0.1",
17
+ "@opengeoweb/snackbar": "14.1.0",
18
18
  "react-redux": "^9.2.0",
19
19
  "@reduxjs/toolkit": "^2.6.1",
20
20
  "@mui/material": "^7.0.1"
@@ -1,4 +1,3 @@
1
- import { TFunction } from 'i18next';
2
1
  export interface GeoWebJWT {
3
2
  access_token: string;
4
3
  refresh_token?: string;
@@ -32,5 +31,5 @@ export interface CreateApiProps extends ApiModule {
32
31
  export type CreateApiFn = (props: CreateApiProps) => void;
33
32
  export interface Role {
34
33
  name: string;
35
- getTitle: (t: TFunction) => string;
34
+ title: string;
36
35
  }
@@ -1,6 +1,6 @@
1
- import { AxiosInstance, AxiosResponse } from 'axios';
2
- import { ConfigType } from '@opengeoweb/shared';
3
- import { CreateApiProps, Credentials, Role } from './types';
1
+ import type { AxiosInstance, AxiosResponse } from 'axios';
2
+ import type { ConfigType } from '@opengeoweb/shared';
3
+ import type { CreateApiProps, Credentials, Role } from './types';
4
4
  export declare const KEEP_ALIVE_POLLER_IN_SECONDS = 60;
5
5
  export declare const REFRESH_TOKEN_WHEN_PCT_EXPIRED = 75;
6
6
  export declare const MILLISECOND_TO_SECOND: number;
@@ -1,10 +1,13 @@
1
1
  import React from 'react';
2
- import { AuthenticationConfig, AuthenticationContextProps, AuthenticationDefaultStateProps } from './types';
2
+ import type { AuthenticationConfig, AuthenticationContextProps, AuthenticationDefaultStateProps } from './types';
3
+ import type { Credentials } from '../ApiContext/types';
3
4
  export declare const getRandomString: () => string;
4
5
  export declare const getCodeChallenge: (codeVerifier: string) => Promise<string>;
5
6
  export declare const replaceTemplateKeys: (url: string, clientId: string, appUrl: string, oauthState?: string, codeChallenge?: string) => string;
6
7
  export declare const getAuthConfig: (_configUrls: AuthenticationConfig) => AuthenticationConfig;
7
8
  export declare const AuthenticationContext: React.Context<AuthenticationContextProps>;
9
+ type OnAuthChange = (isLoggedIn: boolean, auth: Credentials | null) => void;
10
+ export declare const setAuthChangeListener: (listener: OnAuthChange) => void;
8
11
  export declare const useAuthenticationDefaultProps: () => AuthenticationDefaultStateProps;
9
12
  interface AuthenticationProviderProps {
10
13
  children: React.ReactNode;
@@ -1,2 +1,2 @@
1
- export { useAuthenticationContext, AuthenticationProvider, useAuthenticationDefaultProps, AuthenticationContext, getAuthConfig, getRandomString, getCodeChallenge, } from './AuthenticationContext';
2
- export * from './types';
1
+ export { useAuthenticationContext, AuthenticationProvider, useAuthenticationDefaultProps, AuthenticationContext, getAuthConfig, getRandomString, getCodeChallenge, setAuthChangeListener, } from './AuthenticationContext';
2
+ export type * from './types';
@@ -1,5 +1,5 @@
1
- import { SessionStorageProvider } from '@opengeoweb/shared';
2
- import { Credentials, Role } from '../ApiContext/types';
1
+ import type { SessionStorageProvider } from '@opengeoweb/shared';
2
+ import type { Credentials, Role } from '../ApiContext/types';
3
3
  export interface AuthenticationConfig {
4
4
  GW_AUTH_LOGIN_URL: string;
5
5
  GW_AUTH_LOGOUT_URL: string;
@@ -22,3 +22,4 @@ export interface AuthenticationDefaultStateProps {
22
22
  export interface AuthenticationContextProps extends AuthenticationDefaultStateProps {
23
23
  authConfig: AuthenticationConfig;
24
24
  }
25
+ export type AuthChangeListener = (isLoggedIn: boolean, auth: Credentials | null) => void;
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { Role } from '../ApiContext/types';
2
+ import type { Role } from '../ApiContext/types';
3
3
  export declare const UserMenuRoles: React.FC<{
4
4
  roles?: Role[];
5
5
  currentRole?: string;
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { Role } from '../ApiContext/types';
2
+ import type { Role } from '../ApiContext/types';
3
3
  export declare const UserMenuRolesConnect: React.FC<{
4
4
  temporaryOnChangeRoleForDemo?: (role: Role) => void;
5
5
  roles?: Role[];