cozy-harvest-lib 9.23.1 → 9.23.2

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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [9.23.2](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@9.23.1...cozy-harvest-lib@9.23.2) (2022-07-26)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Invalidate temporary token cache when there is a change of BI user ([0fa3893](https://github.com/cozy/cozy-libs/commit/0fa3893bb6ac3c02bc88d15dae79e3772a6aef97))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [9.23.1](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@9.23.0...cozy-harvest-lib@9.23.1) (2022-07-26)
7
18
 
8
19
 
@@ -576,16 +576,22 @@ function _getBiTemporaryTokenFromCache() {
576
576
  return _getBiTemporaryTokenFromCache.apply(this, arguments);
577
577
  }
578
578
 
579
- function isCacheExpired(_ref17) {
580
- var tokenCache = _ref17.tokenCache;
579
+ export function isCacheExpired(_ref17) {
580
+ var tokenCache = _ref17.tokenCache,
581
+ biUser = _ref17.biUser;
581
582
  var cacheAge = Date.now() - Number(tokenCache === null || tokenCache === void 0 ? void 0 : tokenCache.timestamp);
582
583
  logger.debug('tokenCache age', cacheAge / 1000 / 60, 'minutes');
583
584
  var MAX_TOKEN_CACHE_AGE = 29 * 60 * 1000;
585
+ var isSameUserId = tokenCache.userId === (biUser === null || biUser === void 0 ? void 0 : biUser.userId);
584
586
 
585
- if (tokenCache && cacheAge < MAX_TOKEN_CACHE_AGE) {
587
+ if (tokenCache && cacheAge < MAX_TOKEN_CACHE_AGE && isSameUserId) {
586
588
  return false;
587
589
  }
588
590
 
591
+ if (!isSameUserId) {
592
+ logger.warn("BI user id in cache ".concat(tokenCache.userId, " is different than current user id ").concat(biUser === null || biUser === void 0 ? void 0 : biUser.userId));
593
+ }
594
+
589
595
  return true;
590
596
  }
591
597
  /**
@@ -598,7 +604,6 @@ function isCacheExpired(_ref17) {
598
604
  * @return {createTemporaryTokenResponse}
599
605
  */
600
606
 
601
-
602
607
  function updateCache(_x9) {
603
608
  return _updateCache.apply(this, arguments);
604
609
  }
@@ -663,7 +668,7 @@ export var createTemporaryToken = /*#__PURE__*/function () {
663
668
  var _ref20 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(_ref19) {
664
669
  var _tokenCache;
665
670
 
666
- var client, konnector, account, tokenCache, cozyBankIds, _tokenCache2, biMapping;
671
+ var client, konnector, account, tokenCache, cozyBankIds, _yield$client$query, biUser, _tokenCache2, biMapping;
667
672
 
668
673
  return _regeneratorRuntime.wrap(function _callee8$(_context8) {
669
674
  while (1) {
@@ -682,16 +687,23 @@ export var createTemporaryToken = /*#__PURE__*/function () {
682
687
  konnector: konnector,
683
688
  account: account
684
689
  });
690
+ _context8.next = 8;
691
+ return client.query(Q('io.cozy.accounts').getById('bi-aggregator-user'));
692
+
693
+ case 8:
694
+ _yield$client$query = _context8.sent;
695
+ biUser = _yield$client$query.data;
685
696
 
686
697
  if (!isCacheExpired({
687
- tokenCache: tokenCache
698
+ tokenCache: tokenCache,
699
+ biUser: biUser
688
700
  })) {
689
- _context8.next = 11;
701
+ _context8.next = 15;
690
702
  break;
691
703
  }
692
704
 
693
705
  logger.debug('temporaryToken cache is expired. Updating');
694
- _context8.next = 10;
706
+ _context8.next = 14;
695
707
  return updateCache({
696
708
  client: client,
697
709
  konnector: konnector,
@@ -699,10 +711,10 @@ export var createTemporaryToken = /*#__PURE__*/function () {
699
711
  cozyBankIds: cozyBankIds
700
712
  });
701
713
 
702
- case 10:
714
+ case 14:
703
715
  tokenCache = _context8.sent;
704
716
 
705
- case 11:
717
+ case 15:
706
718
  assert(cozyBankIds.length, 'createTemporaryToken: Could not determine cozyBankIds from account or konnector');
707
719
  assert((_tokenCache = tokenCache) === null || _tokenCache === void 0 ? void 0 : _tokenCache.biMapping, 'createTemporaryToken: could not find a BI mapping in createTemporaryToken response, you should update your konnector to the last version');
708
720
  _tokenCache2 = tokenCache, biMapping = _tokenCache2.biMapping;
@@ -711,7 +723,7 @@ export var createTemporaryToken = /*#__PURE__*/function () {
711
723
  })));
712
724
  return _context8.abrupt("return", tokenCache);
713
725
 
714
- case 16:
726
+ case 20:
715
727
  case "end":
716
728
  return _context8.stop();
717
729
  }
@@ -7,7 +7,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
7
7
 
8
8
  import _regeneratorRuntime from "@babel/runtime/regenerator";
9
9
  import CozyClient from 'cozy-client';
10
- import { handleOAuthAccount, checkBIConnection, isBiWebViewConnector, fetchContractSynchronizationUrl, refreshContracts, fetchExtraOAuthUrlParams } from './biWebView';
10
+ import { handleOAuthAccount, checkBIConnection, isBiWebViewConnector, fetchContractSynchronizationUrl, refreshContracts, fetchExtraOAuthUrlParams, isCacheExpired } from './biWebView';
11
11
  import ConnectionFlow from '../models/ConnectionFlow';
12
12
  import { waitForRealtimeEvent } from './jobUtils';
13
13
  import biPublicKeyProd from './bi-public-key-prod.json';
@@ -539,6 +539,72 @@ describe('refreshContracts', function () {
539
539
  }, _callee9);
540
540
  })));
541
541
  });
542
+ describe('isCacheExpired', function () {
543
+ it('should not be marked as expired when userId did not change and cache is not old', function () {
544
+ var tokenCache = {
545
+ timestamp: Date.now(),
546
+ userId: 666
547
+ };
548
+ var biUser = {
549
+ userId: 666
550
+ };
551
+ expect(isCacheExpired({
552
+ tokenCache: tokenCache,
553
+ biUser: biUser
554
+ })).toBe(false);
555
+ });
556
+ it('should be marked as expired when userId did not change and cache is old', function () {
557
+ var tokenCache = {
558
+ timestamp: 0,
559
+ userId: 666
560
+ };
561
+ var biUser = {
562
+ userId: 666
563
+ };
564
+ expect(isCacheExpired({
565
+ tokenCache: tokenCache,
566
+ biUser: biUser
567
+ })).toBe(true);
568
+ });
569
+ it('should be marked as expired when userId did change and cache is old', function () {
570
+ var tokenCache = {
571
+ timestamp: 0,
572
+ userId: 666
573
+ };
574
+ var biUser = {
575
+ userId: 667
576
+ };
577
+ expect(isCacheExpired({
578
+ tokenCache: tokenCache,
579
+ biUser: biUser
580
+ })).toBe(true);
581
+ });
582
+ it('should be marked as expired when userId did change and cache is not old', function () {
583
+ var tokenCache = {
584
+ timestamp: Date.now(),
585
+ userId: 666
586
+ };
587
+ var biUser = {
588
+ userId: 667
589
+ };
590
+ expect(isCacheExpired({
591
+ tokenCache: tokenCache,
592
+ biUser: biUser
593
+ })).toBe(true);
594
+ });
595
+ it('should be marked as expired when cache has no user id', function () {
596
+ var tokenCache = {
597
+ timestamp: Date.now()
598
+ };
599
+ var biUser = {
600
+ userId: 666
601
+ };
602
+ expect(isCacheExpired({
603
+ tokenCache: tokenCache,
604
+ biUser: biUser
605
+ })).toBe(true);
606
+ });
607
+ });
542
608
  describe('fetchExtraOAuthUrlParams', function () {
543
609
  it('should asynchronously fetch BI token', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
544
610
  var client, result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-harvest-lib",
3
- "version": "9.23.1",
3
+ "version": "9.23.2",
4
4
  "description": "Provides logic, modules and components for Cozy's harvest applications.",
5
5
  "main": "dist/index.js",
6
6
  "author": "Cozy",
@@ -90,5 +90,5 @@
90
90
  "react-router-dom": "^5.0.1"
91
91
  },
92
92
  "sideEffects": false,
93
- "gitHead": "3e2319da1add48da77bf8dd3f9c8113e831ea752"
93
+ "gitHead": "c5c6e0d609fafbc41a4b8cbfa8e1d4582544bbdd"
94
94
  }
@@ -362,13 +362,20 @@ async function getBiTemporaryTokenFromCache({ client }) {
362
362
  * @param {createTemporaryTokenResponse} options.tokenCache
363
363
  * @return {Boolean}
364
364
  */
365
- function isCacheExpired({ tokenCache }) {
365
+ export function isCacheExpired({ tokenCache, biUser }) {
366
366
  const cacheAge = Date.now() - Number(tokenCache?.timestamp)
367
367
  logger.debug('tokenCache age', cacheAge / 1000 / 60, 'minutes')
368
368
  const MAX_TOKEN_CACHE_AGE = 29 * 60 * 1000
369
- if (tokenCache && cacheAge < MAX_TOKEN_CACHE_AGE) {
369
+ const isSameUserId = tokenCache.userId === biUser?.userId
370
+ if (tokenCache && cacheAge < MAX_TOKEN_CACHE_AGE && isSameUserId) {
370
371
  return false
371
372
  }
373
+
374
+ if (!isSameUserId) {
375
+ logger.warn(
376
+ `BI user id in cache ${tokenCache.userId} is different than current user id ${biUser?.userId}`
377
+ )
378
+ }
372
379
  return true
373
380
  }
374
381
 
@@ -425,7 +432,12 @@ export const createTemporaryToken = async ({ client, konnector, account }) => {
425
432
 
426
433
  let tokenCache = await getBiTemporaryTokenFromCache({ client })
427
434
  const cozyBankIds = getCozyBankIds({ konnector, account })
428
- if (isCacheExpired({ tokenCache })) {
435
+
436
+ const { data: biUser } = await client.query(
437
+ Q('io.cozy.accounts').getById('bi-aggregator-user')
438
+ )
439
+
440
+ if (isCacheExpired({ tokenCache, biUser })) {
429
441
  logger.debug('temporaryToken cache is expired. Updating')
430
442
  tokenCache = await updateCache({
431
443
  client,
@@ -5,7 +5,8 @@ import {
5
5
  isBiWebViewConnector,
6
6
  fetchContractSynchronizationUrl,
7
7
  refreshContracts,
8
- fetchExtraOAuthUrlParams
8
+ fetchExtraOAuthUrlParams,
9
+ isCacheExpired
9
10
  } from './biWebView'
10
11
  import ConnectionFlow from '../models/ConnectionFlow'
11
12
  import { waitForRealtimeEvent } from './jobUtils'
@@ -330,6 +331,34 @@ describe('refreshContracts', () => {
330
331
  })
331
332
  })
332
333
 
334
+ describe('isCacheExpired', () => {
335
+ it('should not be marked as expired when userId did not change and cache is not old', () => {
336
+ const tokenCache = { timestamp: Date.now(), userId: 666 }
337
+ const biUser = { userId: 666 }
338
+ expect(isCacheExpired({ tokenCache, biUser })).toBe(false)
339
+ })
340
+ it('should be marked as expired when userId did not change and cache is old', () => {
341
+ const tokenCache = { timestamp: 0, userId: 666 }
342
+ const biUser = { userId: 666 }
343
+ expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
344
+ })
345
+ it('should be marked as expired when userId did change and cache is old', () => {
346
+ const tokenCache = { timestamp: 0, userId: 666 }
347
+ const biUser = { userId: 667 }
348
+ expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
349
+ })
350
+ it('should be marked as expired when userId did change and cache is not old', () => {
351
+ const tokenCache = { timestamp: Date.now(), userId: 666 }
352
+ const biUser = { userId: 667 }
353
+ expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
354
+ })
355
+ it('should be marked as expired when cache has no user id', () => {
356
+ const tokenCache = { timestamp: Date.now() }
357
+ const biUser = { userId: 666 }
358
+ expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
359
+ })
360
+ })
361
+
333
362
  describe('fetchExtraOAuthUrlParams', () => {
334
363
  it('should asynchronously fetch BI token', async () => {
335
364
  const client = new CozyClient({