@icure/api 5.0.25 → 5.0.27

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.
@@ -415,8 +415,9 @@ class IccCryptoXApi {
415
415
  const { owner: modifiedOwner, aesExchangeKeys } = yield this.getOrCreateHcPartyKeys(owner, ownerId);
416
416
  const importedAESHcPartyKey = yield this.decryptAnyAesExchangeKeyForOwner(aesExchangeKeys, ownerId, ownerId, ownerId, publicKeys);
417
417
  const encryptedDelegation = yield this._AES.encrypt(importedAESHcPartyKey.key, (0, binary_utils_1.string2ua)(createdObject.id + ':' + secretId).buffer, importedAESHcPartyKey.rawKey);
418
- const encryptedSecretForeignKey = parentObject &&
419
- this._AES.encrypt(importedAESHcPartyKey.key, (0, binary_utils_1.string2ua)(createdObject.id + ':' + parentObject.id).buffer, importedAESHcPartyKey.rawKey);
418
+ const encryptedSecretForeignKey = parentObject
419
+ ? yield this._AES.encrypt(importedAESHcPartyKey.key, (0, binary_utils_1.string2ua)(createdObject.id + ':' + parentObject.id).buffer, importedAESHcPartyKey.rawKey)
420
+ : undefined;
420
421
  return {
421
422
  owner: modifiedOwner,
422
423
  delegations: _.fromPairs([
@@ -431,8 +432,8 @@ class IccCryptoXApi {
431
432
  ],
432
433
  ],
433
434
  ]),
434
- cryptedForeignKeys: (encryptedSecretForeignKey &&
435
- _.fromPairs([
435
+ cryptedForeignKeys: (encryptedSecretForeignKey
436
+ ? _.fromPairs([
436
437
  [
437
438
  ownerId,
438
439
  [
@@ -443,8 +444,8 @@ class IccCryptoXApi {
443
444
  },
444
445
  ],
445
446
  ],
446
- ])) ||
447
- {},
447
+ ])
448
+ : _) || {},
448
449
  secretForeignKeys: (secretForeignKeyOfParent && [secretForeignKeyOfParent]) || [],
449
450
  secretId: secretId,
450
451
  };
@@ -670,50 +671,46 @@ class IccCryptoXApi {
670
671
  return __awaiter(this, arguments, void 0, function* () {
671
672
  this.throwDetailedExceptionForInvalidParameter('modifiedObject.id', modifiedObject.id, 'appendEncryptionKeys', arguments); //modifiedObject should never be null
672
673
  this.throwDetailedExceptionForInvalidParameter('secretEncryptionKeyOfObject', secretEncryptionKeyOfObject, 'appendEncryptionKeys', arguments);
673
- return this.getDataOwner(ownerId)
674
- .then(({ dataOwner: owner }) => __awaiter(this, void 0, void 0, function* () {
675
- const publicKeys = yield this.getPublicKeys();
676
- const { owner: modifiedOwner, aesExchangeKeys } = yield this.getOrCreateHcPartyKeys(owner, delegateId);
677
- const importedAESHcPartyKey = yield this.decryptAnyAesExchangeKeyForOwner(aesExchangeKeys, ownerId, ownerId, delegateId, publicKeys);
678
- return {
679
- previousDecryptedEncryptionKeys: yield Promise.all(((modifiedObject.encryptionKeys || {})[delegateId] || []).map((d) => (d.key &&
680
- d.owner === ownerId &&
681
- this._AES.decrypt(importedAESHcPartyKey.key, (0, binary_utils_1.hex2ua)(d.key), importedAESHcPartyKey.rawKey).catch(() => {
682
- console.log(`Cannot decrypt encryption key from ${d.owner} to ${d.delegatedTo} for object with id ${modifiedObject.id}:`, modifiedObject);
683
- return Promise.resolve();
684
- })) ||
685
- Promise.resolve())),
686
- encryptedEncryptionKey: yield this._AES.encrypt(importedAESHcPartyKey.key, (0, binary_utils_1.string2ua)(modifiedObject.id + ':' + secretEncryptionKeyOfObject)),
687
- };
688
- }))
689
- .then(({ previousDecryptedEncryptionKeys, encryptedEncryptionKey }) => {
690
- //try to limit the extent of the modifications to the delegations by preserving the redundant encryption keys already present and removing duplicates
691
- //For delegate delegateId, we create:
692
- // 1. an array of objects { d : {owner,delegatedTo,encrypted(key)}} with one object for the existing encryption keys and the new key concatenated
693
- // 2. an array of objects { k : decrypted(key)} with one object for the existing delegations and the new key concatenated
694
- // We merge them to get one array of objects: { d: {owner,delegatedTo,encrypted(key)}, k: decrypted(key)}
695
- const encryptionKeysCryptedDecrypted = _.merge(((modifiedObject.encryptionKeys || {})[delegateId] || []).map((d) => ({
696
- d,
697
- })), (previousDecryptedEncryptionKeys || []).map((dd) => (dd ? (0, binary_utils_1.ua2string)(dd) : null)).map((k) => ({ k })))
698
- .filter(({ d, k }) => !!k || d.owner !== ownerId) //Only keep the ones created by us that can still be decrypted
699
- .map(({ d, k }) => ({ d, k: k || this.randomUuid() }))
700
- .concat([
701
- {
702
- d: {
703
- owner: ownerId,
704
- delegatedTo: delegateId,
705
- key: (0, binary_utils_1.ua2hex)(encryptedEncryptionKey),
706
- },
707
- k: modifiedObject.id + ':' + secretEncryptionKeyOfObject,
674
+ const owner = (yield this.getDataOwner(ownerId)).dataOwner;
675
+ const publicKeys = yield this.getPublicKeys();
676
+ const { owner: modifiedOwner, aesExchangeKeys } = yield this.getOrCreateHcPartyKeys(owner, delegateId);
677
+ modifiedObject = (modifiedObject === null || modifiedObject === void 0 ? void 0 : modifiedObject.id) === owner.id ? modifiedOwner : modifiedObject;
678
+ const importedAESHcPartyKey = yield this.decryptAnyAesExchangeKeyForOwner(aesExchangeKeys, ownerId, ownerId, delegateId, publicKeys);
679
+ const previousDecryptedEncryptionKeys = yield Promise.all(((modifiedObject.encryptionKeys || {})[delegateId] || []).map((d) => (d.key &&
680
+ d.owner === ownerId &&
681
+ this._AES.decrypt(importedAESHcPartyKey.key, (0, binary_utils_1.hex2ua)(d.key), importedAESHcPartyKey.rawKey).catch(() => {
682
+ console.log(`Cannot decrypt encryption key from ${d.owner} to ${d.delegatedTo} for object with id ${modifiedObject.id}:`, modifiedObject);
683
+ return Promise.resolve();
684
+ })) ||
685
+ Promise.resolve()));
686
+ const encryptedEncryptionKey = yield this._AES.encrypt(importedAESHcPartyKey.key, (0, binary_utils_1.string2ua)(modifiedObject.id + ':' + secretEncryptionKeyOfObject));
687
+ //try to limit the extent of the modifications to the delegations by preserving the redundant encryption keys already present and removing duplicates
688
+ //For delegate delegateId, we create:
689
+ // 1. an array of objects { d : {owner,delegatedTo,encrypted(key)}} with one object for the existing encryption keys and the new key concatenated
690
+ // 2. an array of objects { k : decrypted(key)} with one object for the existing delegations and the new key concatenated
691
+ // We merge them to get one array of objects: { d: {owner,delegatedTo,encrypted(key)}, k: decrypted(key)}
692
+ const encryptionKeysCryptedDecrypted = _.merge(((modifiedObject.encryptionKeys || {})[delegateId] || []).map((d) => ({
693
+ d,
694
+ })), (previousDecryptedEncryptionKeys || []).map((dd) => (dd ? (0, binary_utils_1.ua2string)(dd) : null)).map((k) => ({ k })))
695
+ .filter(({ d, k }) => !!k || d.owner !== ownerId) //Only keep the ones created by us that can still be decrypted
696
+ .map(({ d, k }) => ({ d, k: k || this.randomUuid() }))
697
+ .concat([
698
+ {
699
+ d: {
700
+ owner: ownerId,
701
+ delegatedTo: delegateId,
702
+ key: (0, binary_utils_1.ua2hex)(encryptedEncryptionKey),
708
703
  },
709
- ]);
710
- const allEncryptionKeys = _.cloneDeep(modifiedObject.encryptionKeys);
711
- allEncryptionKeys[delegateId] = _.uniqBy(encryptionKeysCryptedDecrypted, (x) => x.k).map((x) => x.d);
712
- return {
713
- encryptionKeys: allEncryptionKeys,
714
- secretId: secretEncryptionKeyOfObject,
715
- };
716
- });
704
+ k: modifiedObject.id + ':' + secretEncryptionKeyOfObject,
705
+ },
706
+ ]);
707
+ const allEncryptionKeys = _.cloneDeep(modifiedObject.encryptionKeys || {});
708
+ allEncryptionKeys[delegateId] = _.uniqBy(encryptionKeysCryptedDecrypted, (x) => x.k).map((x) => x.d);
709
+ return {
710
+ modifiedObject: modifiedObject,
711
+ encryptionKeys: allEncryptionKeys,
712
+ secretId: secretEncryptionKeyOfObject,
713
+ };
717
714
  });
718
715
  }
719
716
  /**
@@ -731,25 +728,18 @@ class IccCryptoXApi {
731
728
  addDelegationsAndEncryptionKeys(
732
729
  //TODO: suggested name: updateChildGenericDelegationsFromDelegatorToDelegate
733
730
  parent, child, ownerId, delegateId, secretDelegationKey, secretEncryptionKey) {
734
- if (parent)
735
- this.throwDetailedExceptionForInvalidParameter('parent.id', parent.id, 'addDelegationsAndEncryptionKeys', arguments);
736
- this.throwDetailedExceptionForInvalidParameter('child.id', child.id, 'addDelegationsAndEncryptionKeys', arguments);
737
- return (secretDelegationKey
738
- ? this.extendedDelegationsAndCryptedForeignKeys(child, parent, ownerId, delegateId, secretDelegationKey)
739
- : Promise.resolve({ modifiedObject: child, delegations: {}, cryptedForeignKeys: {}, secretId: null }))
740
- .then((extendedChildObjectSPKsAndCFKs) => secretEncryptionKey
741
- ? this.appendEncryptionKeys(extendedChildObjectSPKsAndCFKs.modifiedObject, ownerId, delegateId, secretEncryptionKey).then(
742
- //TODO: extendedDelegationsAndCryptedForeignKeys and appendEncryptionKeys can be done in parallel
743
- (extendedChildObjectEKs) => ({
744
- extendedSPKsAndCFKs: extendedChildObjectSPKsAndCFKs,
745
- extendedEKs: extendedChildObjectEKs,
746
- }))
747
- : Promise.resolve({
748
- extendedSPKsAndCFKs: extendedChildObjectSPKsAndCFKs,
749
- extendedEKs: { encryptionKeys: {} },
750
- }))
751
- .then(({ extendedSPKsAndCFKs: extendedChildObjectSPKsAndCFKs, extendedEKs: extendedChildObjectEKs }) => {
752
- return _.assign(extendedChildObjectSPKsAndCFKs.modifiedObject, {
731
+ return __awaiter(this, arguments, void 0, function* () {
732
+ if (parent)
733
+ this.throwDetailedExceptionForInvalidParameter('parent.id', parent.id, 'addDelegationsAndEncryptionKeys', arguments);
734
+ this.throwDetailedExceptionForInvalidParameter('child.id', child.id, 'addDelegationsAndEncryptionKeys', arguments);
735
+ const extendedChildObjectSPKsAndCFKs = secretDelegationKey
736
+ ? yield this.extendedDelegationsAndCryptedForeignKeys(child, parent, ownerId, delegateId, secretDelegationKey)
737
+ : { modifiedObject: child, delegations: {}, cryptedForeignKeys: {}, secretId: null };
738
+ const extendedChildObjectEKs = secretEncryptionKey
739
+ ? yield this.appendEncryptionKeys(extendedChildObjectSPKsAndCFKs.modifiedObject, ownerId, delegateId, secretEncryptionKey)
740
+ : { encryptionKeys: {}, modifiedObject: extendedChildObjectSPKsAndCFKs.modifiedObject };
741
+ const latestObject = extendedChildObjectEKs.modifiedObject;
742
+ return _.assign(latestObject, {
753
743
  // Conservative version ... We might want to be more aggressive with the deduplication of keys
754
744
  // For each delegate, we are going to concatenate to the src (the new delegations), the object in dest (the current delegations)
755
745
  // for which we do not find an equivalent delegation (same delegator, same delegate)
@@ -1341,24 +1331,44 @@ class IccCryptoXApi {
1341
1331
  }
1342
1332
  addNewKeyPairForOwner(maintenanceTasksApi, user, cdo, generateTransferKey = true) {
1343
1333
  return __awaiter(this, void 0, void 0, function* () {
1344
- const { publicKey, privateKey } = yield this.RSA.generateKeyPair();
1345
- const publicKeyHex = (0, binary_utils_1.ua2hex)(yield this.RSA.exportKey(publicKey, 'spki'));
1334
+ const generatedKeypair = yield this.RSA.generateKeyPair();
1335
+ return this.addKeyPairForOwner(maintenanceTasksApi, user, cdo, generatedKeypair, generateTransferKey);
1336
+ });
1337
+ }
1338
+ addRawKeyPairForOwnerId(maintenanceTasksApi, user, ownerId, keypair, generateTransferKey = true) {
1339
+ return __awaiter(this, void 0, void 0, function* () {
1340
+ return this.addRawKeyPairForOwner(maintenanceTasksApi, user, yield this.getDataOwner(ownerId), keypair, generateTransferKey);
1341
+ });
1342
+ }
1343
+ addRawKeyPairForOwner(maintenanceTasksApi, user, cdo, keypair, generateTransferKey = true) {
1344
+ return __awaiter(this, void 0, void 0, function* () {
1345
+ const importedPrivateKey = yield this._RSA.importKey('pkcs8', (0, binary_utils_1.hex2ua)(keypair.privateKey), ['decrypt']);
1346
+ const importedPublicKey = yield this._RSA.importKey('spki', (0, binary_utils_1.hex2ua)(keypair.publicKey), ['encrypt']);
1347
+ return this.addKeyPairForOwner(maintenanceTasksApi, user, cdo, { publicKey: importedPublicKey, privateKey: importedPrivateKey }, generateTransferKey);
1348
+ });
1349
+ }
1350
+ addKeyPairForOwner(maintenanceTasksApi, user, cdo, keypair, generateTransferKey = true) {
1351
+ return __awaiter(this, void 0, void 0, function* () {
1352
+ const publicKeyHex = (0, binary_utils_1.ua2hex)(yield this.RSA.exportKey(keypair.publicKey, 'spki'));
1346
1353
  const gen = (yield this._AES.generateCryptoKey(true));
1347
- yield this.cacheKeyPair({ publicKey: yield this.RSA.exportKey(publicKey, 'jwk'), privateKey: yield this.RSA.exportKey(privateKey, 'jwk') });
1354
+ yield this.cacheKeyPair({
1355
+ publicKey: yield this.RSA.exportKey(keypair.publicKey, 'jwk'),
1356
+ privateKey: yield this.RSA.exportKey(keypair.privateKey, 'jwk'),
1357
+ });
1348
1358
  const { type: ownerType, dataOwner: ownerToUpdate } = yield this.createOrUpdateAesExchangeKeysFor(cdo, gen, {
1349
- pubKey: publicKey,
1350
- privKey: privateKey,
1359
+ pubKey: keypair.publicKey,
1360
+ privKey: keypair.privateKey,
1351
1361
  }).then((dataOwnerWithUpdatedAesKeys) => generateTransferKey
1352
- ? this.createOrUpdateTransferKeysFor(dataOwnerWithUpdatedAesKeys, gen, { pubKey: publicKey, privKey: privateKey })
1362
+ ? this.createOrUpdateTransferKeysFor(dataOwnerWithUpdatedAesKeys, gen, { pubKey: keypair.publicKey, privKey: keypair.privateKey })
1353
1363
  : dataOwnerWithUpdatedAesKeys);
1354
1364
  const modifiedDataOwnerAndType = yield this._saveDataOwner({ type: ownerType, dataOwner: ownerToUpdate });
1355
- const sentMaintenanceTasks = yield this.sendMaintenanceTasks(maintenanceTasksApi, user, modifiedDataOwnerAndType.dataOwner, publicKey);
1365
+ const sentMaintenanceTasks = yield this.sendMaintenanceTasks(maintenanceTasksApi, user, modifiedDataOwnerAndType.dataOwner, keypair.publicKey);
1356
1366
  return {
1357
1367
  dataOwner: sentMaintenanceTasks.length
1358
1368
  ? yield this.retrieveDataOwnerInfoAfterPotentialUpdate(modifiedDataOwnerAndType.dataOwner)
1359
1369
  : modifiedDataOwnerAndType.dataOwner,
1360
1370
  publicKey: publicKeyHex,
1361
- privateKey: (0, binary_utils_1.ua2hex)((yield this.RSA.exportKey(privateKey, 'pkcs8'))),
1371
+ privateKey: (0, binary_utils_1.ua2hex)((yield this.RSA.exportKey(keypair.privateKey, 'pkcs8'))),
1362
1372
  };
1363
1373
  });
1364
1374
  }