@icure/api 5.2.35 → 5.3.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.
@@ -407,13 +407,11 @@ export declare class IccCryptoXApi {
407
407
  * When a user lost his keys, people to whom he shared information may call this method to give access back to him, re-encrypting their common
408
408
  * AES key using the new user public key.
409
409
  *
410
- * @param delegateUser Delegate Data Owner User, in charge of giving access back to the person who previously gave him some access
411
- * @param ownerId Id of the data owner to which we would like to give access back
410
+ * @param accessGiver User, in charge of giving access back to the person who previously gave him some access (should be the current user)
411
+ * @param ownerId Id of the data owner to which we would like to get access back
412
412
  * @param ownerNewPublicKey New Data Owner Public Key we want to use to re-encrypt previously created AES key
413
- *
414
- * @return The DataOwner, updated by the delegateUser to add the new encrypted AES Key using the new provided public key
415
413
  */
416
- giveAccessBackTo(delegateUser: User, ownerId: string, ownerNewPublicKey: string): Promise<CachedDataOwner>;
414
+ giveAccessBackTo(accessGiver: User, ownerId: string, ownerNewPublicKey: string): Promise<void>;
417
415
  addNewKeyPairForOwnerId(maintenanceTasksApi: IccMaintenanceTaskXApi, user: User, ownerId: string, generateTransferKey?: boolean, sendMaintenanceTasks?: boolean): Promise<{
418
416
  dataOwner: HealthcareParty | Patient | Device;
419
417
  publicKey: string;
@@ -1287,60 +1287,56 @@ class IccCryptoXApi {
1287
1287
  * When a user lost his keys, people to whom he shared information may call this method to give access back to him, re-encrypting their common
1288
1288
  * AES key using the new user public key.
1289
1289
  *
1290
- * @param delegateUser Delegate Data Owner User, in charge of giving access back to the person who previously gave him some access
1291
- * @param ownerId Id of the data owner to which we would like to give access back
1290
+ * @param accessGiver User, in charge of giving access back to the person who previously gave him some access (should be the current user)
1291
+ * @param ownerId Id of the data owner to which we would like to get access back
1292
1292
  * @param ownerNewPublicKey New Data Owner Public Key we want to use to re-encrypt previously created AES key
1293
- *
1294
- * @return The DataOwner, updated by the delegateUser to add the new encrypted AES Key using the new provided public key
1295
1293
  */
1296
- giveAccessBackTo(delegateUser, ownerId, ownerNewPublicKey) {
1297
- var _a, _b, _c, _d;
1294
+ giveAccessBackTo(accessGiver, ownerId, ownerNewPublicKey) {
1295
+ var _a, _b;
1298
1296
  return __awaiter(this, void 0, void 0, function* () {
1299
- const delegateId = (_b = (_a = delegateUser.healthcarePartyId) !== null && _a !== void 0 ? _a : delegateUser.patientId) !== null && _b !== void 0 ? _b : delegateUser.deviceId;
1300
- if (!delegateId) {
1301
- throw new Error(`DelegateUser ${delegateUser.id} must be a data Owner`);
1297
+ const newPubKeyCryptoKey = yield this._RSA.importKey('spki', (0, binary_utils_1.hex2ua)(ownerNewPublicKey), ['encrypt']);
1298
+ const giverDoId = (_b = (_a = accessGiver.healthcarePartyId) !== null && _a !== void 0 ? _a : accessGiver.patientId) !== null && _b !== void 0 ? _b : accessGiver.deviceId;
1299
+ if (!giverDoId) {
1300
+ throw new Error(`Access giver ${accessGiver.id} must be a data Owner`);
1302
1301
  }
1303
- const delegatePublicKeys = yield this.getPublicKeys();
1304
- const newPubKeyCryptoKey = yield this._RSA.importKey('jwk', (0, utils_1.spkiToJwk)((0, binary_utils_1.hex2ua)(ownerNewPublicKey)), ['encrypt']);
1305
- const dataOwnerToUpdate = yield this.getDataOwner(ownerId);
1306
- const newAesExchangeKeys = yield (0, utils_1.foldAsync)(Object.entries((_c = dataOwnerToUpdate.dataOwner.aesExchangeKeys) !== null && _c !== void 0 ? _c : {}), (_d = dataOwnerToUpdate.dataOwner.aesExchangeKeys) !== null && _d !== void 0 ? _d : {}, (pubAcc, [pubKey, newAesExcKeys]) => __awaiter(this, void 0, void 0, function* () {
1307
- var _e;
1308
- const existingKeys = (_e = pubAcc[pubKey]) !== null && _e !== void 0 ? _e : {};
1309
- pubAcc[pubKey] = yield (0, utils_1.foldAsync)(Object.entries(newAesExcKeys), existingKeys, (delAcc, [delId, delKeys]) => __awaiter(this, void 0, void 0, function* () {
1310
- if (delId == delegateId && pubKey != ownerNewPublicKey) {
1311
- // Add the AES Key encrypted with the new public key in the aesExchangeKeys
1312
- try {
1313
- // First, we decrypt it using the delegate RSA Public Key
1314
- const encrAesKeyInfo = yield this.decryptHcPartyKey(delegateId, dataOwnerToUpdate.dataOwner.id, delegateId, pubKey, delKeys, delegatePublicKeys).then((delegatorAndKeys) => __awaiter(this, void 0, void 0, function* () {
1315
- // Then, we encrypt it using the owner new RSA Public Key (provided in argument)
1316
- return yield this.encryptAesKeyOnlyForProvidedKeys(delegatorAndKeys === null || delegatorAndKeys === void 0 ? void 0 : delegatorAndKeys.rawKey, dataOwnerToUpdate, [newPubKeyCryptoKey])
1317
- .then((encrAes) => Object.values(encrAes)[0])
1318
- .then((encrAesInfo) => {
1319
- return { pubKeyUsedToEncryptAes: Object.keys(encrAesInfo)[0], encryptedAes: Object.values(encrAesInfo)[0] };
1320
- });
1321
- }));
1322
- delKeys[encrAesKeyInfo.pubKeyUsedToEncryptAes] = encrAesKeyInfo.encryptedAes;
1323
- }
1324
- catch (e) {
1325
- console.log(`${delegateId} could not re-encrypt AES Key of ${dataOwnerToUpdate.dataOwner.id}`);
1326
- }
1327
- finally {
1328
- delAcc[delId] = delKeys;
1302
+ const giverDoKeys = yield this.getDataOwner(giverDoId).then((doData) => { var _a; return [doData.dataOwner.publicKey, ...Object.keys((_a = doData.dataOwner.aesExchangeKeys) !== null && _a !== void 0 ? _a : {})].filter((x) => !!x); });
1303
+ const giveAccessBackToExchangeKey = (exchangeKeys, delegatorId, // Getter for getter exchange keys, giver for giver exchange keys
1304
+ delegateId // Giver for getter exchange keys, getter for giver exchange keys
1305
+ ) => __awaiter(this, void 0, void 0, function* () {
1306
+ const updatedExchangeKeys = _.cloneDeep(exchangeKeys);
1307
+ for (const [publicKey, mapsByDelegate] of Object.entries(updatedExchangeKeys)) {
1308
+ const mapOfDelegate = mapsByDelegate[delegateId];
1309
+ if (!!mapOfDelegate && !mapOfDelegate[ownerNewPublicKey.slice(-32)]) {
1310
+ const decryptedKey = yield this.decryptHcPartyKey(giverDoId, delegatorId, delegateId, publicKey, mapOfDelegate, giverDoKeys)
1311
+ .then((x) => x === null || x === void 0 ? void 0 : x.rawKey)
1312
+ .catch(() => undefined);
1313
+ if (decryptedKey) {
1314
+ mapOfDelegate[ownerNewPublicKey.slice(-32)] = yield this.encryptAesKeyOnlyForProvidedKeys(decryptedKey, delegateId, [
1315
+ newPubKeyCryptoKey,
1316
+ ]).then((encrAes) => Object.values(Object.values(encrAes)[0])[0]);
1329
1317
  }
1330
1318
  }
1331
- else {
1332
- // Otherwise, we don't transform the aesExchangeKeys for this delegate
1333
- delAcc[delId] = delKeys;
1334
- }
1335
- return delAcc;
1336
- }));
1337
- return pubAcc;
1338
- }));
1339
- // After adding the potential new aesExchangeKeys, we save the updated data owner
1340
- return this._saveDataOwner({
1341
- type: dataOwnerToUpdate.type,
1342
- dataOwner: Object.assign(Object.assign({}, dataOwnerToUpdate.dataOwner), { aesExchangeKeys: newAesExchangeKeys }),
1319
+ }
1320
+ return updatedExchangeKeys;
1343
1321
  });
1322
+ yield (0, utils_1.retry)(() => __awaiter(this, void 0, void 0, function* () {
1323
+ var _c;
1324
+ const getterDo = yield this.getDataOwner(ownerId);
1325
+ const updatedGetterKeys = yield giveAccessBackToExchangeKey((_c = getterDo.dataOwner.aesExchangeKeys) !== null && _c !== void 0 ? _c : {}, ownerId, giverDoId);
1326
+ yield this._saveDataOwner({
1327
+ type: getterDo.type,
1328
+ dataOwner: Object.assign(Object.assign({}, getterDo.dataOwner), { aesExchangeKeys: updatedGetterKeys }),
1329
+ });
1330
+ }), 3, 5000, 1);
1331
+ yield (0, utils_1.retry)(() => __awaiter(this, void 0, void 0, function* () {
1332
+ var _d;
1333
+ const giverDo = yield this.getDataOwner(giverDoId);
1334
+ const updatedGiverKeys = yield giveAccessBackToExchangeKey((_d = giverDo.dataOwner.aesExchangeKeys) !== null && _d !== void 0 ? _d : {}, giverDoId, ownerId);
1335
+ yield this._saveDataOwner({
1336
+ type: giverDo.type,
1337
+ dataOwner: Object.assign(Object.assign({}, giverDo.dataOwner), { aesExchangeKeys: updatedGiverKeys }),
1338
+ });
1339
+ }), 3, 5000, 1);
1344
1340
  });
1345
1341
  }
1346
1342
  addNewKeyPairForOwnerId(maintenanceTasksApi, user, ownerId, generateTransferKey = true, sendMaintenanceTasks = true) {
@@ -1442,16 +1438,16 @@ class IccCryptoXApi {
1442
1438
  encryptAesKeyFor(aesKey, dataOwner, doNewPublicKey) {
1443
1439
  return __awaiter(this, void 0, void 0, function* () {
1444
1440
  const dataOwnerAllPubKeys = [doNewPublicKey].concat(yield this.getDataOwnerPublicKeys(dataOwner));
1445
- return this.encryptAesKeyOnlyForProvidedKeys(aesKey, dataOwner, dataOwnerAllPubKeys);
1441
+ return this.encryptAesKeyOnlyForProvidedKeys(aesKey, dataOwner.id, dataOwnerAllPubKeys);
1446
1442
  });
1447
1443
  }
1448
- encryptAesKeyOnlyForProvidedKeys(aesKey, dataOwner, dataOwnerPubKeys) {
1444
+ encryptAesKeyOnlyForProvidedKeys(aesKey, dataOwnerId, dataOwnerPubKeys) {
1449
1445
  return __awaiter(this, void 0, void 0, function* () {
1450
1446
  const encryptedAesForDataOwner = yield (0, utils_1.foldAsync)(dataOwnerPubKeys, {}, (encrAes, pubKey) => __awaiter(this, void 0, void 0, function* () {
1451
1447
  encrAes[(0, binary_utils_1.ua2hex)(yield this.RSA.exportKey(pubKey, 'spki')).slice(-32)] = (0, binary_utils_1.ua2hex)(yield this._RSA.encrypt(pubKey, (0, binary_utils_1.hex2ua)(aesKey)));
1452
1448
  return encrAes;
1453
1449
  }));
1454
- return { [dataOwner.id]: encryptedAesForDataOwner };
1450
+ return { [dataOwnerId]: encryptedAesForDataOwner };
1455
1451
  });
1456
1452
  }
1457
1453
  retrieveDataOwnerInfoAfterPotentialUpdate(dataOwnerToUpdate) {