@icure/api 7.0.0-beta.1 → 7.0.0-beta.3

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 (103) hide show
  1. package/icc-api/api/IccDataownerApi.d.ts +13 -2
  2. package/icc-api/api/IccDataownerApi.js +30 -4
  3. package/icc-api/api/IccDataownerApi.js.map +1 -1
  4. package/icc-api/api/IccPatientApi.d.ts +4 -0
  5. package/icc-api/api/IccPatientApi.js +17 -0
  6. package/icc-api/api/IccPatientApi.js.map +1 -1
  7. package/icc-api/model/CryptoActorStub.d.ts +55 -0
  8. package/icc-api/model/CryptoActorStub.js +35 -0
  9. package/icc-api/model/CryptoActorStub.js.map +1 -0
  10. package/icc-api/model/DataOwnerTypeEnum.d.ts +8 -0
  11. package/icc-api/model/DataOwnerTypeEnum.js +13 -0
  12. package/icc-api/model/DataOwnerTypeEnum.js.map +1 -0
  13. package/icc-api/model/DataOwnerWithType.d.ts +16 -3
  14. package/icc-api/model/DataOwnerWithType.js +22 -7
  15. package/icc-api/model/DataOwnerWithType.js.map +1 -1
  16. package/icc-api/model/Device.d.ts +4 -0
  17. package/icc-api/model/Device.js.map +1 -1
  18. package/icc-api/model/HealthcareParty.d.ts +4 -0
  19. package/icc-api/model/HealthcareParty.js.map +1 -1
  20. package/icc-api/model/Patient.d.ts +4 -0
  21. package/icc-api/model/Patient.js.map +1 -1
  22. package/icc-x-api/basexapi/EncryptedEntityXApi.d.ts +48 -0
  23. package/icc-x-api/basexapi/EncryptedEntityXApi.js +3 -0
  24. package/icc-x-api/basexapi/EncryptedEntityXApi.js.map +1 -0
  25. package/icc-x-api/crypto/AES.js +9 -1
  26. package/icc-x-api/crypto/AES.js.map +1 -1
  27. package/icc-x-api/crypto/BaseExchangeKeysManager.d.ts +4 -2
  28. package/icc-x-api/crypto/BaseExchangeKeysManager.js +32 -31
  29. package/icc-x-api/crypto/BaseExchangeKeysManager.js.map +1 -1
  30. package/icc-x-api/crypto/CryptoStrategies.d.ts +5 -4
  31. package/icc-x-api/crypto/CryptoStrategies.js.map +1 -1
  32. package/icc-x-api/crypto/EntitiesEncryption.d.ts +23 -4
  33. package/icc-x-api/crypto/EntitiesEncryption.js +48 -24
  34. package/icc-x-api/crypto/EntitiesEncryption.js.map +1 -1
  35. package/icc-x-api/crypto/ExchangeKeysManager.d.ts +3 -2
  36. package/icc-x-api/crypto/ExchangeKeysManager.js +3 -3
  37. package/icc-x-api/crypto/ExchangeKeysManager.js.map +1 -1
  38. package/icc-x-api/crypto/KeyManager.js +4 -4
  39. package/icc-x-api/crypto/KeyManager.js.map +1 -1
  40. package/icc-x-api/crypto/KeyRecovery.d.ts +2 -1
  41. package/icc-x-api/crypto/KeyRecovery.js.map +1 -1
  42. package/icc-x-api/crypto/LegacyCryptoStrategies.d.ts +5 -4
  43. package/icc-x-api/crypto/LegacyCryptoStrategies.js +1 -1
  44. package/icc-x-api/crypto/LegacyCryptoStrategies.js.map +1 -1
  45. package/icc-x-api/crypto/ShamirKeysManager.d.ts +4 -3
  46. package/icc-x-api/crypto/ShamirKeysManager.js +8 -7
  47. package/icc-x-api/crypto/ShamirKeysManager.js.map +1 -1
  48. package/icc-x-api/crypto/TransferKeysManager.d.ts +3 -2
  49. package/icc-x-api/crypto/TransferKeysManager.js +6 -6
  50. package/icc-x-api/crypto/TransferKeysManager.js.map +1 -1
  51. package/icc-x-api/crypto/utils.d.ts +7 -6
  52. package/icc-x-api/crypto/utils.js +4 -7
  53. package/icc-x-api/crypto/utils.js.map +1 -1
  54. package/icc-x-api/icc-accesslog-x-api.d.ts +27 -1
  55. package/icc-x-api/icc-accesslog-x-api.js +34 -1
  56. package/icc-x-api/icc-accesslog-x-api.js.map +1 -1
  57. package/icc-x-api/icc-calendar-item-x-api.d.ts +36 -1
  58. package/icc-x-api/icc-calendar-item-x-api.js +61 -1
  59. package/icc-x-api/icc-calendar-item-x-api.js.map +1 -1
  60. package/icc-x-api/icc-classification-x-api.d.ts +27 -1
  61. package/icc-x-api/icc-classification-x-api.js +34 -1
  62. package/icc-x-api/icc-classification-x-api.js.map +1 -1
  63. package/icc-x-api/icc-contact-x-api.d.ts +27 -1
  64. package/icc-x-api/icc-contact-x-api.js +34 -1
  65. package/icc-x-api/icc-contact-x-api.js.map +1 -1
  66. package/icc-x-api/icc-crypto-x-api.d.ts +6 -6
  67. package/icc-x-api/icc-crypto-x-api.js +4 -4
  68. package/icc-x-api/icc-crypto-x-api.js.map +1 -1
  69. package/icc-x-api/icc-data-owner-x-api.d.ts +21 -52
  70. package/icc-x-api/icc-data-owner-x-api.js +36 -68
  71. package/icc-x-api/icc-data-owner-x-api.js.map +1 -1
  72. package/icc-x-api/icc-document-x-api.d.ts +71 -4
  73. package/icc-x-api/icc-document-x-api.js +96 -6
  74. package/icc-x-api/icc-document-x-api.js.map +1 -1
  75. package/icc-x-api/icc-form-x-api.d.ts +27 -1
  76. package/icc-x-api/icc-form-x-api.js +34 -1
  77. package/icc-x-api/icc-form-x-api.js.map +1 -1
  78. package/icc-x-api/icc-helement-x-api.d.ts +27 -1
  79. package/icc-x-api/icc-helement-x-api.js +34 -1
  80. package/icc-x-api/icc-helement-x-api.js.map +1 -1
  81. package/icc-x-api/icc-icure-maintenance-x-api.js +4 -4
  82. package/icc-x-api/icc-icure-maintenance-x-api.js.map +1 -1
  83. package/icc-x-api/icc-invoice-x-api.d.ts +27 -1
  84. package/icc-x-api/icc-invoice-x-api.js +34 -1
  85. package/icc-x-api/icc-invoice-x-api.js.map +1 -1
  86. package/icc-x-api/icc-maintenance-task-x-api.d.ts +25 -2
  87. package/icc-x-api/icc-maintenance-task-x-api.js +26 -2
  88. package/icc-x-api/icc-maintenance-task-x-api.js.map +1 -1
  89. package/icc-x-api/icc-message-x-api.d.ts +30 -1
  90. package/icc-x-api/icc-message-x-api.js +37 -1
  91. package/icc-x-api/icc-message-x-api.js.map +1 -1
  92. package/icc-x-api/icc-patient-x-api.d.ts +67 -1
  93. package/icc-x-api/icc-patient-x-api.js +80 -6
  94. package/icc-x-api/icc-patient-x-api.js.map +1 -1
  95. package/icc-x-api/icc-receipt-x-api.d.ts +47 -2
  96. package/icc-x-api/icc-receipt-x-api.js +58 -4
  97. package/icc-x-api/icc-receipt-x-api.js.map +1 -1
  98. package/icc-x-api/icc-time-table-x-api.d.ts +25 -1
  99. package/icc-x-api/icc-time-table-x-api.js +27 -2
  100. package/icc-x-api/icc-time-table-x-api.js.map +1 -1
  101. package/icc-x-api/index.js +4 -3
  102. package/icc-x-api/index.js.map +1 -1
  103. package/package.json +1 -1
@@ -9,26 +9,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.IccDataOwnerXApi = exports.DataOwnerTypeEnum = void 0;
12
+ exports.IccDataOwnerXApi = void 0;
13
13
  const utils_1 = require("./crypto/utils");
14
- const XHR_1 = require("../icc-api/api/XHR");
15
- exports.DataOwnerTypeEnum = {
16
- Patient: 'patient',
17
- Device: 'device',
18
- Hcp: 'hcp',
19
- };
20
- class IccDataOwnerXApi {
14
+ const IccDataownerApi_1 = require("../icc-api/api/IccDataownerApi");
15
+ const AuthenticationProvider_1 = require("./auth/AuthenticationProvider");
16
+ class IccDataOwnerXApi extends IccDataownerApi_1.IccDataownerApi {
21
17
  get selfDataOwnerId() {
22
18
  if (!this.currentDataOwnerHierarchyIds)
23
19
  return undefined;
24
20
  return this.currentDataOwnerHierarchyIds[this.currentDataOwnerHierarchyIds.length - 1];
25
21
  }
26
- constructor(userBaseApi, hcpartyBaseApi, //Init with a hcparty x api for better performances
27
- patientBaseApi, deviceBaseApi) {
28
- this.userBaseApi = userBaseApi;
29
- this.hcpartyBaseApi = hcpartyBaseApi;
30
- this.patientBaseApi = patientBaseApi;
31
- this.deviceBaseApi = deviceBaseApi;
22
+ constructor(host, headers, authenticationProvider = new AuthenticationProvider_1.NoAuthenticationProvider(), fetchImpl = typeof window !== 'undefined'
23
+ ? window.fetch
24
+ : typeof self !== 'undefined'
25
+ ? self.fetch
26
+ : fetch) {
27
+ super(host, headers, authenticationProvider, fetchImpl);
32
28
  }
33
29
  /**
34
30
  * If the logged user is a data owner get the current data owner. This information is cached without expiration, and will only be updated in case
@@ -76,20 +72,6 @@ class IccDataOwnerXApi {
76
72
  throw new Error(`${parentId} is not part of the data owner hierarchy for the current user`);
77
73
  });
78
74
  }
79
- /**
80
- * If the logged user is a data owner get the current data owner.
81
- * @throws if the current user is not a data owner.
82
- */
83
- getCurrentDataOwner() {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- if (!this.currentDataOwnerHierarchyIds) {
86
- const dataOwnerHierarchy = yield this.forceLoadCurrentDataOwnerHierarchyAndCacheIds();
87
- return dataOwnerHierarchy[dataOwnerHierarchy.length - 1];
88
- }
89
- else
90
- return this.getDataOwner(yield this.getCurrentDataOwnerId());
91
- });
92
- }
93
75
  /**
94
76
  * If the logged user is a data owner get the current data owner and all of his parents.
95
77
  * @throws if the current user is not a data owner.
@@ -131,23 +113,38 @@ class IccDataOwnerXApi {
131
113
  return dataOwnerId;
132
114
  }
133
115
  /**
134
- * Get a data owner. Uses a cache to improve performance.
116
+ * Get a data owner. Note that this does not decrpyt patient data owners.
135
117
  * @param ownerId id of the data owner to retrieve (patient, medical device, hcp, ...)
136
- * @return the data owner or undefined if loadIfMissingFromCache is false and there is no data owner with provided id in cache.
137
- * @throws if no data owner with the provided id could be found on an error occurred while attempting to retrieve it.
118
+ * @return the data owner with the provided id
119
+ * @throws if you have no access to the data owner. Use {@link getCryptoActorStub}.
138
120
  */
139
121
  getDataOwner(ownerId) {
122
+ const _super = Object.create(null, {
123
+ getDataOwner: { get: () => super.getDataOwner }
124
+ });
140
125
  return __awaiter(this, void 0, void 0, function* () {
141
- const _url = this.userBaseApi.host + `/dataowner/` + ownerId + '?ts=' + new Date().getTime();
142
- let headers = this.userBaseApi.headers;
143
- return XHR_1.XHR.sendCommand('GET', _url, headers, null, this.userBaseApi.fetchImpl, undefined, this.userBaseApi.authenticationProvider.getAuthService())
144
- .then((doc) => doc.body)
145
- .then((dowt) => {
126
+ return _super.getDataOwner.call(this, ownerId).then((dowt) => {
146
127
  if (dowt.dataOwner.id === this.selfDataOwnerId)
147
128
  this.checkDataOwnerIntegrity(dowt.dataOwner);
148
129
  return dowt;
149
- })
150
- .catch((err) => this.handleError(err));
130
+ });
131
+ });
132
+ }
133
+ /**
134
+ * Get a crypto actor stub for a data owner.
135
+ * @param ownerId id of the data owner for which you want to retrieve the stub (patient, medical device, hcp, ...)
136
+ * @return the crypto actor stub of the data owner with the provided id
137
+ */
138
+ getCryptoActorStub(ownerId) {
139
+ const _super = Object.create(null, {
140
+ getCryptoActorStub: { get: () => super.getCryptoActorStub }
141
+ });
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ return _super.getCryptoActorStub.call(this, ownerId).then((dowt) => {
144
+ if (dowt.stub.id === this.selfDataOwnerId)
145
+ this.checkDataOwnerIntegrity(dowt.stub);
146
+ return dowt;
147
+ });
151
148
  });
152
149
  }
153
150
  /**
@@ -165,31 +162,6 @@ class IccDataOwnerXApi {
165
162
  clearCurrentDataOwnerIdsCache() {
166
163
  this.currentDataOwnerHierarchyIds = undefined;
167
164
  }
168
- /**
169
- * @internal This method is intended only for internal use and may be changed without notice.
170
- * Updates a data owner and its cached value.
171
- * @param dataOwner a data owner with updated value.
172
- * @return the updated data owner, with updated revision.
173
- */
174
- updateDataOwner(dataOwner) {
175
- return __awaiter(this, void 0, void 0, function* () {
176
- const ownerType = dataOwner.type;
177
- const ownerToUpdate = dataOwner.dataOwner;
178
- if (ownerType === 'hcp') {
179
- return yield this.hcpartyBaseApi
180
- .modifyHealthcareParty(ownerToUpdate)
181
- .then((x) => ({ type: 'hcp', dataOwner: x }));
182
- }
183
- else if (ownerType === 'patient') {
184
- return yield this.patientBaseApi.modifyPatient(ownerToUpdate).then((x) => ({ type: 'patient', dataOwner: x }));
185
- }
186
- else if (ownerType === 'device') {
187
- return yield this.deviceBaseApi.updateDevice(ownerToUpdate).then((x) => ({ type: 'device', dataOwner: x }));
188
- }
189
- else
190
- throw new Error(`Unrecognised data owner type: ${ownerType}`);
191
- });
192
- }
193
165
  checkDataOwnerIntegrity(dataOwner) {
194
166
  const keys = this.getHexPublicKeysOf(dataOwner);
195
167
  if (new Set(Array.from(keys).map((x) => x.slice(-32))).size != keys.size)
@@ -197,8 +169,7 @@ class IccDataOwnerXApi {
197
169
  }
198
170
  forceLoadCurrentDataOwnerHierarchyAndCacheIds() {
199
171
  return __awaiter(this, void 0, void 0, function* () {
200
- const currentUser = yield this.userBaseApi.getCurrentUser();
201
- let curr = yield this.getDataOwner(this.getDataOwnerIdOf(currentUser));
172
+ let curr = yield this.getCurrentDataOwner();
202
173
  this.checkDataOwnerIntegrity(curr.dataOwner);
203
174
  this.currentDataOwnerType = curr.type;
204
175
  let res = [curr];
@@ -210,9 +181,6 @@ class IccDataOwnerXApi {
210
181
  return res;
211
182
  });
212
183
  }
213
- handleError(e) {
214
- throw e;
215
- }
216
184
  }
217
185
  exports.IccDataOwnerXApi = IccDataOwnerXApi;
218
186
  //# sourceMappingURL=icc-data-owner-x-api.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"icc-data-owner-x-api.js","sourceRoot":"","sources":["../../icc-x-api/icc-data-owner-x-api.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,0CAAgD;AAEhD,4CAAwC;AAM3B,QAAA,iBAAiB,GAAyC;IACrE,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,KAAK;CACX,CAAA;AAgBD,MAAa,gBAAgB;IAQ3B,IAAY,eAAe;QACzB,IAAI,CAAC,IAAI,CAAC,4BAA4B;YAAE,OAAO,SAAS,CAAA;QACxD,OAAO,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,4BAA4B,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACxF,CAAC;IAED,YACE,WAAuB,EACvB,cA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AA;YAChB,OAAQ,IAAI,CAAC,SAA6B,CAAC,QAAQ,EAAE;gBACnD,IAAI,GAAG,MAAM,IAAI,CAAC,YAAY,CAAE,IAAI,CAAC,SAA6B,CAAC,QAAS,CAAC,CAAA;gBAC7E,GAAG,GAAG,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAA;aACrB;YACD,IAAI,CAAC,4BAA4B,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,EAAG,CAAC,CAAA;YACnE,OAAO,GAAG,CAAA;QACZ,CAAC;KAAA;IAEO,WAAW,CAAC,CAAe;QACjC,MAAM,CAAC,CAAA;IACT,CAAC;CACF;AA1MD,4CA0MC","sourcesContent":["import { User } from '../icc-api/model/User'\nimport { IccDeviceApi, IccPatientApi, IccUserApi } from '../icc-api'\nimport { HealthcareParty } from '../icc-api/model/HealthcareParty'\nimport { Patient } from '../icc-api/model/Patient'\nimport { Device } from '../icc-api/model/Device'\nimport { hexPublicKeysOf } from './crypto/utils'\nimport { IccHcpartyXApi } from './icc-hcparty-x-api'\nimport { XHR } from '../icc-api/api/XHR'\n\n/**\n * Represents a type of data owner.\n */\nexport type DataOwnerTypeEnum = 'patient' | 'device' | 'hcp'\nexport const DataOwnerTypeEnum: { [key: string]: DataOwnerTypeEnum } = {\n Patient: 'patient',\n Device: 'device',\n Hcp: 'hcp',\n}\n\n/**\n * Represents any data owner.\n */\n// Trick to make typechecking work: without this you can pass anything to a data owner\nexport type DataOwner = (Patient | Device | HealthcareParty) & { id: string }\n\n/**\n * Represents any data owner enriched with type information.\n */\nexport type DataOwnerWithType =\n | { type: 'patient'; dataOwner: Patient & DataOwner }\n | { type: 'device'; dataOwner: Device & DataOwner }\n | { type: 'hcp'; dataOwner: HealthcareParty & DataOwner }\n\nexport class IccDataOwnerXApi {\n private readonly userBaseApi: IccUserApi\n private readonly hcpartyBaseApi: IccHcpartyXApi\n private readonly patientBaseApi: IccPatientApi\n private readonly deviceBaseApi: IccDeviceApi\n private currentDataOwnerType: DataOwnerTypeEnum | undefined\n private currentDataOwnerHierarchyIds: string[] | undefined\n\n private get selfDataOwnerId(): string | undefined {\n if (!this.currentDataOwnerHierarchyIds) return undefined\n return this.currentDataOwnerHierarchyIds[this.currentDataOwnerHierarchyIds.length - 1]\n }\n\n constructor(\n userBaseApi: IccUserApi,\n hcpartyBaseApi: IccHcpartyXApi, //Init with a hcparty x api for better performances\n patientBaseApi: IccPatientApi,\n deviceBaseApi: IccDeviceApi\n ) {\n this.userBaseApi = userBaseApi\n this.hcpartyBaseApi = hcpartyBaseApi\n this.patientBaseApi = patientBaseApi\n this.deviceBaseApi = deviceBaseApi\n }\n\n /**\n * If the logged user is a data owner get the current data owner. This information is cached without expiration, and will only be updated in case\n * of forced refresh.\n * @throws if the current user is not a data owner.\n */\n async getCurrentDataOwnerId(): Promise<string> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.getCurrentDataOwnerHierarchyIds()\n }\n return this.currentDataOwnerHierarchyIds![this.currentDataOwnerHierarchyIds!.length - 1]\n }\n\n /**\n * If the logged user is a data owner get its parent hierarchy. This information is cached without expiration, and will only be updated in case\n * of forced refresh.\n * The resulting array starts with the topmost parent (the only ancestor without a parent) and ends with the data owner itself.\n * @throws if the current user is not a data owner.\n */\n async getCurrentDataOwnerHierarchyIds(): Promise<string[]> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n }\n return [...this.currentDataOwnerHierarchyIds!]\n }\n\n /**\n * Get the hierarchy for the current data owner starting from the specified parent.\n * @throws an array starting at the topmost parent and ending at the provided parent id. If the provided id is not part of the hierarchy throws an\n * error.\n */\n async getCurrentDataOwnerHierarchyIdsFrom(parentId: string): Promise<string[]> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.getCurrentDataOwnerHierarchyIds()\n }\n const res = []\n for (const dataOwnerId of this.currentDataOwnerHierarchyIds!) {\n res.push(dataOwnerId)\n if (dataOwnerId === parentId) return res\n }\n throw new Error(`${parentId} is not part of the data owner hierarchy for the current user`)\n }\n\n /**\n * If the logged user is a data owner get the current data owner.\n * @throws if the current user is not a data owner.\n */\n async getCurrentDataOwner(): Promise<DataOwnerWithType> {\n if (!this.currentDataOwnerHierarchyIds) {\n const dataOwnerHierarchy = await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n return dataOwnerHierarchy[dataOwnerHierarchy.length - 1]\n } else return this.getDataOwner(await this.getCurrentDataOwnerId())\n }\n\n /**\n * If the logged user is a data owner get the current data owner and all of his parents.\n * @throws if the current user is not a data owner.\n * @return the current data owner hierarchy, starting from the topmost parent to the current data owner.\n */\n async getCurrentDataOwnerHierarchy(): Promise<DataOwnerWithType[]> {\n if (!this.currentDataOwnerHierarchyIds) {\n return await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n } else return Promise.all(this.currentDataOwnerHierarchyIds!.map((id) => this.getDataOwner(id)))\n }\n\n /**\n * If the logged user is a data owner get the type of the current data owner. This information is cached.\n * @throws if the current user is not a data owner\n */\n async getCurrentDataOwnerType(): Promise<DataOwnerTypeEnum> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n }\n return this.currentDataOwnerType!\n }\n\n /**\n * Get the id of the data owner entity for a user.\n * @param user a user that is also a data owner.\n * @return the id of the data owner corresponding to the provided user.\n * @throws if the user is not a data owner.\n */\n getDataOwnerIdOf(user: User): string {\n const dataOwnerId = user.healthcarePartyId ?? user.patientId ?? user.deviceId\n if (dataOwnerId == undefined) {\n throw Error(`User ${user.id} is not a data owner`)\n }\n return dataOwnerId\n }\n\n /**\n * Get a data owner. Uses a cache to improve performance.\n * @param ownerId id of the data owner to retrieve (patient, medical device, hcp, ...)\n * @return the data owner or undefined if loadIfMissingFromCache is false and there is no data owner with provided id in cache.\n * @throws if no data owner with the provided id could be found on an error occurred while attempting to retrieve it.\n */\n async getDataOwner(ownerId: string): Promise<DataOwnerWithType> {\n const _url = this.userBaseApi.host + `/dataowner/` + ownerId + '?ts=' + new Date().getTime()\n let headers = this.userBaseApi.headers\n return XHR.sendCommand(\n 'GET',\n _url,\n headers,\n null,\n this.userBaseApi.fetchImpl,\n undefined,\n this.userBaseApi.authenticationProvider.getAuthService()\n )\n .then((doc) => doc.body as DataOwnerWithType)\n .then((dowt) => {\n if (dowt.dataOwner.id === this.selfDataOwnerId) this.checkDataOwnerIntegrity(dowt.dataOwner)\n return dowt\n })\n .catch((err) => this.handleError(err))\n }\n\n /**\n * Gets the public keys of a data owner in hex format.\n * @param dataOwner a data owner.\n * @return the public keys for the data owner in hex format.\n */\n getHexPublicKeysOf(dataOwner: DataOwner): Set<string> {\n return hexPublicKeysOf(dataOwner)\n }\n\n /**\n * Clears the cache of current data owner id and parent hierarchy ids. The hierarchy of a data owner should not normally change over time, so this\n * method should be rarely needed.\n */\n clearCurrentDataOwnerIdsCache() {\n this.currentDataOwnerHierarchyIds = undefined\n }\n\n /**\n * @internal This method is intended only for internal use and may be changed without notice.\n * Updates a data owner and its cached value.\n * @param dataOwner a data owner with updated value.\n * @return the updated data owner, with updated revision.\n */\n async updateDataOwner(dataOwner: DataOwnerWithType): Promise<DataOwnerWithType> {\n const ownerType = dataOwner.type\n const ownerToUpdate = dataOwner.dataOwner\n if (ownerType === 'hcp') {\n return await this.hcpartyBaseApi\n .modifyHealthcareParty(ownerToUpdate as HealthcareParty)\n .then((x) => ({ type: 'hcp', dataOwner: x } as DataOwnerWithType))\n } else if (ownerType === 'patient') {\n return await this.patientBaseApi.modifyPatient(ownerToUpdate as Patient).then((x) => ({ type: 'patient', dataOwner: x } as DataOwnerWithType))\n } else if (ownerType === 'device') {\n return await this.deviceBaseApi.updateDevice(ownerToUpdate as Device).then((x) => ({ type: 'device', dataOwner: x } as DataOwnerWithType))\n } else throw new Error(`Unrecognised data owner type: ${ownerType}`)\n }\n\n private checkDataOwnerIntegrity(dataOwner: DataOwner) {\n const keys = this.getHexPublicKeysOf(dataOwner)\n if (new Set(Array.from(keys).map((x) => x.slice(-32))).size != keys.size)\n throw new Error(\n `Different public keys for ${dataOwner.id} have the same fingerprint; this should not happen in normal circumstances. Please report this error to iCure.`\n )\n }\n\n private async forceLoadCurrentDataOwnerHierarchyAndCacheIds(): Promise<DataOwnerWithType[]> {\n const currentUser = await this.userBaseApi.getCurrentUser()\n let curr = await this.getDataOwner(this.getDataOwnerIdOf(currentUser))\n this.checkDataOwnerIntegrity(curr.dataOwner)\n this.currentDataOwnerType = curr.type\n let res = [curr]\n while ((curr.dataOwner as HealthcareParty).parentId) {\n curr = await this.getDataOwner((curr.dataOwner as HealthcareParty).parentId!)\n res = [curr, ...res]\n }\n this.currentDataOwnerHierarchyIds = res.map((x) => x.dataOwner.id!)\n return res\n }\n\n private handleError(e: XHR.XHRError): never {\n throw e\n }\n}\n"]}
1
+ {"version":3,"file":"icc-data-owner-x-api.js","sourceRoot":"","sources":["../../icc-x-api/icc-data-owner-x-api.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,0CAAgD;AAGhD,oEAAgE;AAChE,0EAAgG;AAMhG,MAAa,gBAAiB,SAAQ,iCAAe;IAInD,IAAY,eAAe;QACzB,IAAI,CAAC,IAAI,CAAC,4BAA4B;YAAE,OAAO,SAAS,CAAA;QACxD,OAAO,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,4BAA4B,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IACxF,CAAC;IAED,YACE,IAAY,EACZ,OAAkC,EAClC,yBAAiD,IAAI,iDAAwB,EAAE,EAC/E,YAA2E,OAAO,MAAM,KAAK,WAAW;QACtG,CAAC,CAAC,MAAM,CAAC,KAAK;QACd,CAAC,CAAC,OAAO,IAAI,KAAK,WAAW;YAC7B,CAAC,CAAC,IAAI,CAAC,KAAK;YACZ,CAAC,CAAC,KAAK;QAET,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,sBAAsB,EAAE,SAAS,CAAC,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AAI,CAAC,4BAA4B,GAAG,SAAS,CAAA;IAC/C,CAAC;IAEO,uBAAuB,CAAC,SAA0B;QACxD,MAAM,IAAI,GAAG,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAA;QAC/C,IAAI,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI;YACtE,MAAM,IAAI,KAAK,CACb,6BAA6B,SAAS,CAAC,EAAE,gHAAgH,CAC1J,CAAA;IACL,CAAC;IAEa,6CAA6C;;YACzD,IAAI,IAAI,GAAG,MAAM,IAAI,CAAC,mBAAmB,EAAE,CAAA;YAC3C,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;YAC5C,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,IAAI,CAAA;YACrC,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,CAAA;YAChB,OAAQ,IAAI,CAAC,SAA6B,CAAC,QAAQ,EAAE;gBACnD,IAAI,GAAG,MAAM,IAAI,CAAC,YAAY,CAAE,IAAI,CAAC,SAA6B,CAAC,QAAS,CAAC,CAAA;gBAC7E,GAAG,GAAG,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,CAAA;aACrB;YACD,IAAI,CAAC,4BAA4B,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,EAAG,CAAC,CAAA;YACnE,OAAO,GAAG,CAAA;QACZ,CAAC;KAAA;CACF;AAlKD,4CAkKC","sourcesContent":["import { User } from '../icc-api/model/User'\nimport { HealthcareParty } from '../icc-api/model/HealthcareParty'\nimport { Patient } from '../icc-api/model/Patient'\nimport { Device } from '../icc-api/model/Device'\nimport { hexPublicKeysOf } from './crypto/utils'\nimport { DataOwnerTypeEnum } from 'icc-api/model/DataOwnerTypeEnum'\nimport { CryptoActorStub, CryptoActorStubWithType } from '../icc-api/model/CryptoActorStub'\nimport { IccDataownerApi } from '../icc-api/api/IccDataownerApi'\nimport { AuthenticationProvider, NoAuthenticationProvider } from './auth/AuthenticationProvider'\nimport { DataOwnerWithType } from '../icc-api/model/DataOwnerWithType'\n\nexport type DataOwnerOrStub = DataOwner | CryptoActorStub\nexport type DataOwner = HealthcareParty | Patient | Device\n\nexport class IccDataOwnerXApi extends IccDataownerApi {\n private currentDataOwnerType: DataOwnerTypeEnum | undefined\n private currentDataOwnerHierarchyIds: string[] | undefined\n\n private get selfDataOwnerId(): string | undefined {\n if (!this.currentDataOwnerHierarchyIds) return undefined\n return this.currentDataOwnerHierarchyIds[this.currentDataOwnerHierarchyIds.length - 1]\n }\n\n constructor(\n host: string,\n headers: { [key: string]: string },\n authenticationProvider: AuthenticationProvider = new NoAuthenticationProvider(),\n fetchImpl: (input: RequestInfo, init?: RequestInit) => Promise<Response> = typeof window !== 'undefined'\n ? window.fetch\n : typeof self !== 'undefined'\n ? self.fetch\n : fetch\n ) {\n super(host, headers, authenticationProvider, fetchImpl)\n }\n\n /**\n * If the logged user is a data owner get the current data owner. This information is cached without expiration, and will only be updated in case\n * of forced refresh.\n * @throws if the current user is not a data owner.\n */\n async getCurrentDataOwnerId(): Promise<string> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.getCurrentDataOwnerHierarchyIds()\n }\n return this.currentDataOwnerHierarchyIds![this.currentDataOwnerHierarchyIds!.length - 1]\n }\n\n /**\n * If the logged user is a data owner get its parent hierarchy. This information is cached without expiration, and will only be updated in case\n * of forced refresh.\n * The resulting array starts with the topmost parent (the only ancestor without a parent) and ends with the data owner itself.\n * @throws if the current user is not a data owner.\n */\n async getCurrentDataOwnerHierarchyIds(): Promise<string[]> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n }\n return [...this.currentDataOwnerHierarchyIds!]\n }\n\n /**\n * Get the hierarchy for the current data owner starting from the specified parent.\n * @throws an array starting at the topmost parent and ending at the provided parent id. If the provided id is not part of the hierarchy throws an\n * error.\n */\n async getCurrentDataOwnerHierarchyIdsFrom(parentId: string): Promise<string[]> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.getCurrentDataOwnerHierarchyIds()\n }\n const res = []\n for (const dataOwnerId of this.currentDataOwnerHierarchyIds!) {\n res.push(dataOwnerId)\n if (dataOwnerId === parentId) return res\n }\n throw new Error(`${parentId} is not part of the data owner hierarchy for the current user`)\n }\n\n /**\n * If the logged user is a data owner get the current data owner and all of his parents.\n * @throws if the current user is not a data owner.\n * @return the current data owner hierarchy, starting from the topmost parent to the current data owner.\n */\n async getCurrentDataOwnerHierarchy(): Promise<DataOwnerWithType[]> {\n if (!this.currentDataOwnerHierarchyIds) {\n return await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n } else return Promise.all(this.currentDataOwnerHierarchyIds!.map((id) => this.getDataOwner(id)))\n }\n\n /**\n * If the logged user is a data owner get the type of the current data owner. This information is cached.\n * @throws if the current user is not a data owner\n */\n async getCurrentDataOwnerType(): Promise<DataOwnerTypeEnum> {\n if (!this.currentDataOwnerHierarchyIds) {\n await this.forceLoadCurrentDataOwnerHierarchyAndCacheIds()\n }\n return this.currentDataOwnerType!\n }\n\n /**\n * Get the id of the data owner entity for a user.\n * @param user a user that is also a data owner.\n * @return the id of the data owner corresponding to the provided user.\n * @throws if the user is not a data owner.\n */\n getDataOwnerIdOf(user: User): string {\n const dataOwnerId = user.healthcarePartyId ?? user.patientId ?? user.deviceId\n if (dataOwnerId == undefined) {\n throw Error(`User ${user.id} is not a data owner`)\n }\n return dataOwnerId\n }\n\n /**\n * Get a data owner. Note that this does not decrpyt patient data owners.\n * @param ownerId id of the data owner to retrieve (patient, medical device, hcp, ...)\n * @return the data owner with the provided id\n * @throws if you have no access to the data owner. Use {@link getCryptoActorStub}.\n */\n async getDataOwner(ownerId: string): Promise<DataOwnerWithType> {\n return super.getDataOwner(ownerId).then((dowt) => {\n if (dowt.dataOwner.id === this.selfDataOwnerId) this.checkDataOwnerIntegrity(dowt.dataOwner)\n return dowt\n })\n }\n\n /**\n * Get a crypto actor stub for a data owner.\n * @param ownerId id of the data owner for which you want to retrieve the stub (patient, medical device, hcp, ...)\n * @return the crypto actor stub of the data owner with the provided id\n */\n async getCryptoActorStub(ownerId: string): Promise<CryptoActorStubWithType> {\n return super.getCryptoActorStub(ownerId).then((dowt) => {\n if (dowt.stub.id === this.selfDataOwnerId) this.checkDataOwnerIntegrity(dowt.stub)\n return dowt\n })\n }\n\n /**\n * Gets the public keys of a data owner in hex format.\n * @param dataOwner a data owner.\n * @return the public keys for the data owner in hex format.\n */\n getHexPublicKeysOf(dataOwner: DataOwnerOrStub): Set<string> {\n return hexPublicKeysOf(dataOwner)\n }\n\n /**\n * Clears the cache of current data owner id and parent hierarchy ids. The hierarchy of a data owner should not normally change over time, so this\n * method should be rarely needed.\n */\n clearCurrentDataOwnerIdsCache() {\n this.currentDataOwnerHierarchyIds = undefined\n }\n\n private checkDataOwnerIntegrity(dataOwner: DataOwnerOrStub) {\n const keys = this.getHexPublicKeysOf(dataOwner)\n if (new Set(Array.from(keys).map((x) => x.slice(-32))).size != keys.size)\n throw new Error(\n `Different public keys for ${dataOwner.id} have the same fingerprint; this should not happen in normal circumstances. Please report this error to iCure.`\n )\n }\n\n private async forceLoadCurrentDataOwnerHierarchyAndCacheIds(): Promise<DataOwnerWithType[]> {\n let curr = await this.getCurrentDataOwner()\n this.checkDataOwnerIntegrity(curr.dataOwner)\n this.currentDataOwnerType = curr.type\n let res = [curr]\n while ((curr.dataOwner as HealthcareParty).parentId) {\n curr = await this.getDataOwner((curr.dataOwner as HealthcareParty).parentId!)\n res = [curr, ...res]\n }\n this.currentDataOwnerHierarchyIds = res.map((x) => x.dataOwner.id!)\n return res\n }\n}\n"]}
@@ -4,7 +4,8 @@ import * as models from '../icc-api/model/models';
4
4
  import { IccDataOwnerXApi } from './icc-data-owner-x-api';
5
5
  import { AuthenticationProvider } from './auth/AuthenticationProvider';
6
6
  import { ShareMetadataBehaviour } from './crypto/ShareMetadataBehaviour';
7
- export declare class IccDocumentXApi extends IccDocumentApi {
7
+ import { EncryptedEntityXApi } from './basexapi/EncryptedEntityXApi';
8
+ export declare class IccDocumentXApi extends IccDocumentApi implements EncryptedEntityXApi<models.Document> {
8
9
  private crypto;
9
10
  private authApi;
10
11
  fetchImpl: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
@@ -65,6 +66,13 @@ export declare class IccDocumentXApi extends IccDocumentApi {
65
66
  * @return the updated document.
66
67
  */
67
68
  encryptAndSetDocumentAttachment(document: models.Document, attachment: ArrayBuffer | Uint8Array): Promise<models.Document>;
69
+ /**
70
+ * Adds an unencrypted attachment to a document.
71
+ * @param document a document.
72
+ * @param attachment a new main attachment for the document.
73
+ * @return the updated document.
74
+ */
75
+ setClearDocumentAttachment(document: models.Document, attachment: ArrayBuffer | Uint8Array): Promise<models.Document>;
68
76
  /**
69
77
  * Adds a secondary attachment to a document, encrypting it on client side using the encryption keys of the provided document.
70
78
  * @param document a document.
@@ -74,7 +82,15 @@ export declare class IccDocumentXApi extends IccDocumentApi {
74
82
  */
75
83
  encryptAndSetSecondaryDocumentAttachment(document: models.Document, secondaryAttachmentKey: string, attachment: ArrayBuffer | Uint8Array): Promise<models.Document>;
76
84
  /**
77
- * Gets the main attachment of a document and tries to decrypt it using the encryption keys of the document.
85
+ * Adds an unencrypted secondary attachment to a document.
86
+ * @param document a document.
87
+ * @param secondaryAttachmentKey key for the secondary attachment.
88
+ * @param attachment a new secondary attachment for the document.
89
+ * @return the updated document.
90
+ */
91
+ setClearSecondaryDocumentAttachment(document: models.Document, secondaryAttachmentKey: string, attachment: ArrayBuffer | Uint8Array): Promise<models.Document>;
92
+ /**
93
+ * Gets the main attachment of a document and tries to decrypt it using the encryption keys of the document, throwing an error if the operation fails.
78
94
  * @param document a document.
79
95
  * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
80
96
  * keys and it is unclear which one should be used this function can help to detect bad decryptions.
@@ -82,14 +98,41 @@ export declare class IccDocumentXApi extends IccDocumentApi {
82
98
  */
83
99
  getAndDecryptDocumentAttachment(document: models.Document, validator?: (decrypted: ArrayBuffer) => Promise<boolean>): Promise<ArrayBuffer>;
84
100
  /**
85
- * Gets the secondary attachment of a document and tries to decrypt it using the encryption keys of the document.
101
+ * Gets the main attachment of a document and tries to decrypt it using the encryption keys of the document.
102
+ * @param document a document.
103
+ * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
104
+ * keys and it is unclear which one should be used this function can help to detect bad decryptions.
105
+ * @return an object containing:
106
+ * - data: the decrypted attachment, if it could be decrypted, else the encrypted attachment.
107
+ * - wasDecrypted: if the data was successfully decrypted or not
108
+ */
109
+ getAndTryDecryptDocumentAttachment(document: models.Document, validator?: (decrypted: ArrayBuffer) => Promise<boolean>): Promise<{
110
+ data: ArrayBuffer;
111
+ wasDecrypted: boolean;
112
+ }>;
113
+ /**
114
+ * Gets the secondary attachment of a document and tries to decrypt it using the encryption keys of the document, throwing an error if the operation fails.
86
115
  * @param document a document.
87
116
  * @param secondaryAttachmentKey key of the secondary attachment.
88
117
  * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
89
118
  * keys and it is unclear which one should be used this function can help to detect bad decryptions.
90
- * @return the decrypted attachment, if it could be decrypted, else the encrypted attachment.
119
+ * @return the decrypted attachment.
91
120
  */
92
121
  getAndDecryptSecondaryDocumentAttachment(document: models.Document, secondaryAttachmentKey: string, validator?: (decrypted: ArrayBuffer) => Promise<boolean>): Promise<ArrayBuffer>;
122
+ /**
123
+ * Gets the secondary attachment of a document and tries to decrypt it using the encryption keys of the document.
124
+ * @param document a document.
125
+ * @param secondaryAttachmentKey key of the secondary attachment.
126
+ * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
127
+ * keys and it is unclear which one should be used this function can help to detect bad decryptions.
128
+ * @return an object containing:
129
+ * - data: the decrypted attachment, if it could be decrypted, else the encrypted attachment.
130
+ * - wasDecrypted: if the data was successfully decrypted or not
131
+ */
132
+ getAndTryDecryptSecondaryDocumentAttachment(document: models.Document, secondaryAttachmentKey: string, validator?: (decrypted: ArrayBuffer) => Promise<boolean>): Promise<{
133
+ data: ArrayBuffer;
134
+ wasDecrypted: boolean;
135
+ }>;
93
136
  /**
94
137
  * @param document a document
95
138
  * @return the id of the message that the document refers to, retrieved from the encrypted metadata. Normally there should only be one element
@@ -112,4 +155,28 @@ export declare class IccDocumentXApi extends IccDocumentApi {
112
155
  shareEncryptionKey?: ShareMetadataBehaviour;
113
156
  shareMessageId?: ShareMetadataBehaviour;
114
157
  }): Promise<models.Document>;
158
+ /**
159
+ * Share an existing document with other data owners, allowing them to access the non-encrypted data of the document and optionally also
160
+ * the encrypted content.
161
+ * @param document the document to share.
162
+ * @param delegates sharing options for each delegate.
163
+ * - shareEncryptionKey: specifies if the encryption key of the access log should be shared with the delegate, giving access to all encrypted
164
+ * content of the entity, excluding other encrypted metadata (defaults to {@link ShareMetadataBehaviour.IF_AVAILABLE}).
165
+ * - shareMessageId: specifies if the id of the message that this document refers to should be shared with the delegate (defaults to
166
+ * {@link ShareMetadataBehaviour.IF_AVAILABLE}).
167
+ * @return a promise which will contain the updated document.
168
+ */
169
+ shareWithMany(document: models.Document, delegates: {
170
+ [delegateId: string]: {
171
+ shareEncryptionKey?: ShareMetadataBehaviour;
172
+ shareMessageId?: ShareMetadataBehaviour;
173
+ };
174
+ }): Promise<models.Document>;
175
+ getDataOwnersWithAccessTo(entity: models.Document): Promise<{
176
+ permissionsByDataOwnerId: {
177
+ [p: string]: 'WRITE';
178
+ };
179
+ hasUnknownAnonymousDataOwners: boolean;
180
+ }>;
181
+ getEncryptionKeysOf(entity: models.Document): Promise<string[]>;
115
182
  }
@@ -563,6 +563,17 @@ class IccDocumentXApi extends icc_api_1.IccDocumentApi {
563
563
  return yield this.setDocumentAttachment(document.id, undefined, encryptedData);
564
564
  });
565
565
  }
566
+ /**
567
+ * Adds an unencrypted attachment to a document.
568
+ * @param document a document.
569
+ * @param attachment a new main attachment for the document.
570
+ * @return the updated document.
571
+ */
572
+ setClearDocumentAttachment(document, attachment) {
573
+ return __awaiter(this, void 0, void 0, function* () {
574
+ return yield this.setDocumentAttachment(document.id, undefined, attachment);
575
+ });
576
+ }
566
577
  /**
567
578
  * Adds a secondary attachment to a document, encrypting it on client side using the encryption keys of the provided document.
568
579
  * @param document a document.
@@ -577,7 +588,19 @@ class IccDocumentXApi extends icc_api_1.IccDocumentApi {
577
588
  });
578
589
  }
579
590
  /**
580
- * Gets the main attachment of a document and tries to decrypt it using the encryption keys of the document.
591
+ * Adds an unencrypted secondary attachment to a document.
592
+ * @param document a document.
593
+ * @param secondaryAttachmentKey key for the secondary attachment.
594
+ * @param attachment a new secondary attachment for the document.
595
+ * @return the updated document.
596
+ */
597
+ setClearSecondaryDocumentAttachment(document, secondaryAttachmentKey, attachment) {
598
+ return __awaiter(this, void 0, void 0, function* () {
599
+ return yield this.setSecondaryAttachment(document.id, secondaryAttachmentKey, document.rev, attachment);
600
+ });
601
+ }
602
+ /**
603
+ * Gets the main attachment of a document and tries to decrypt it using the encryption keys of the document, throwing an error if the operation fails.
581
604
  * @param document a document.
582
605
  * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
583
606
  * keys and it is unclear which one should be used this function can help to detect bad decryptions.
@@ -585,20 +608,55 @@ class IccDocumentXApi extends icc_api_1.IccDocumentApi {
585
608
  */
586
609
  getAndDecryptDocumentAttachment(document, validator = () => Promise.resolve(true)) {
587
610
  return __awaiter(this, void 0, void 0, function* () {
588
- return yield this.crypto.entities.decryptDataOf(document, yield this.getDocumentAttachment(document.id, 'ignored'), (x) => validator(x));
611
+ const { data, wasDecrypted } = yield this.crypto.entities.tryDecryptDataOf(document, yield this.getDocumentAttachment(document.id, 'ignored'), (x) => validator(x));
612
+ if (!wasDecrypted)
613
+ throw new Error(`No valid key found to decrypt data of document ${document.id}.`);
614
+ return data;
589
615
  });
590
616
  }
591
617
  /**
592
- * Gets the secondary attachment of a document and tries to decrypt it using the encryption keys of the document.
618
+ * Gets the main attachment of a document and tries to decrypt it using the encryption keys of the document.
619
+ * @param document a document.
620
+ * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
621
+ * keys and it is unclear which one should be used this function can help to detect bad decryptions.
622
+ * @return an object containing:
623
+ * - data: the decrypted attachment, if it could be decrypted, else the encrypted attachment.
624
+ * - wasDecrypted: if the data was successfully decrypted or not
625
+ */
626
+ getAndTryDecryptDocumentAttachment(document, validator = () => Promise.resolve(true)) {
627
+ return __awaiter(this, void 0, void 0, function* () {
628
+ return yield this.crypto.entities.tryDecryptDataOf(document, yield this.getDocumentAttachment(document.id, 'ignored'), (x) => validator(x));
629
+ });
630
+ }
631
+ /**
632
+ * Gets the secondary attachment of a document and tries to decrypt it using the encryption keys of the document, throwing an error if the operation fails.
593
633
  * @param document a document.
594
634
  * @param secondaryAttachmentKey key of the secondary attachment.
595
635
  * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
596
636
  * keys and it is unclear which one should be used this function can help to detect bad decryptions.
597
- * @return the decrypted attachment, if it could be decrypted, else the encrypted attachment.
637
+ * @return the decrypted attachment.
598
638
  */
599
639
  getAndDecryptSecondaryDocumentAttachment(document, secondaryAttachmentKey, validator = () => Promise.resolve(true)) {
600
640
  return __awaiter(this, void 0, void 0, function* () {
601
- return yield this.crypto.entities.decryptDataOf(document, yield this.getSecondaryAttachment(document.id, secondaryAttachmentKey), (x) => validator(x));
641
+ const { data, wasDecrypted } = yield this.crypto.entities.tryDecryptDataOf(document, yield this.getSecondaryAttachment(document.id, secondaryAttachmentKey), (x) => validator(x));
642
+ if (!wasDecrypted)
643
+ throw new Error(`No valid key found to decrypt data of document ${document.id}.`);
644
+ return data;
645
+ });
646
+ }
647
+ /**
648
+ * Gets the secondary attachment of a document and tries to decrypt it using the encryption keys of the document.
649
+ * @param document a document.
650
+ * @param secondaryAttachmentKey key of the secondary attachment.
651
+ * @param validator optionally a validator function which checks if the decryption was successful. In cases where the document has many encryption
652
+ * keys and it is unclear which one should be used this function can help to detect bad decryptions.
653
+ * @return an object containing:
654
+ * - data: the decrypted attachment, if it could be decrypted, else the encrypted attachment.
655
+ * - wasDecrypted: if the data was successfully decrypted or not
656
+ */
657
+ getAndTryDecryptSecondaryDocumentAttachment(document, secondaryAttachmentKey, validator = () => Promise.resolve(true)) {
658
+ return __awaiter(this, void 0, void 0, function* () {
659
+ return yield this.crypto.entities.tryDecryptDataOf(document, yield this.getSecondaryAttachment(document.id, secondaryAttachmentKey), (x) => validator(x));
602
660
  });
603
661
  }
604
662
  /**
@@ -625,7 +683,39 @@ class IccDocumentXApi extends icc_api_1.IccDocumentApi {
625
683
  */
626
684
  shareWith(delegateId, document, options = {}) {
627
685
  return __awaiter(this, void 0, void 0, function* () {
628
- return yield this.modifyDocument(yield this.crypto.entities.entityWithAutoExtendedEncryptedMetadata(document, delegateId, undefined, options.shareEncryptionKey, options.shareMessageId));
686
+ return this.shareWithMany(document, { [delegateId]: options });
687
+ });
688
+ }
689
+ /**
690
+ * Share an existing document with other data owners, allowing them to access the non-encrypted data of the document and optionally also
691
+ * the encrypted content.
692
+ * @param document the document to share.
693
+ * @param delegates sharing options for each delegate.
694
+ * - shareEncryptionKey: specifies if the encryption key of the access log should be shared with the delegate, giving access to all encrypted
695
+ * content of the entity, excluding other encrypted metadata (defaults to {@link ShareMetadataBehaviour.IF_AVAILABLE}).
696
+ * - shareMessageId: specifies if the id of the message that this document refers to should be shared with the delegate (defaults to
697
+ * {@link ShareMetadataBehaviour.IF_AVAILABLE}).
698
+ * @return a promise which will contain the updated document.
699
+ */
700
+ shareWithMany(document, delegates) {
701
+ return __awaiter(this, void 0, void 0, function* () {
702
+ return yield this.modifyDocument(yield this.crypto.entities.entityWithAutoExtendedEncryptedMetadata(document, true, Object.fromEntries(Object.entries(delegates).map(([delegateId, options]) => [
703
+ delegateId,
704
+ {
705
+ shareEncryptionKey: options.shareEncryptionKey,
706
+ shareOwningEntityIds: options.shareMessageId,
707
+ },
708
+ ]))));
709
+ });
710
+ }
711
+ getDataOwnersWithAccessTo(entity) {
712
+ return __awaiter(this, void 0, void 0, function* () {
713
+ return yield this.crypto.entities.getDataOwnersWithAccessTo(entity);
714
+ });
715
+ }
716
+ getEncryptionKeysOf(entity) {
717
+ return __awaiter(this, void 0, void 0, function* () {
718
+ return yield this.crypto.entities.encryptionKeysOf(entity);
629
719
  });
630
720
  }
631
721
  }