curtain-web-api 1.0.41 → 1.0.42

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.
@@ -43,4 +43,6 @@ export declare class CurtainWebAPI {
43
43
  getDataAllListCategory(): Promise<import("axios").AxiosResponse<any, any>>;
44
44
  postCompareSession(idList: string[], matchType: string, studyList: string[], sessionId: string): Promise<import("axios").AxiosResponse<any, any>>;
45
45
  getStatsSummary(lastNDays: number): Promise<import("axios").AxiosResponse<any, any>>;
46
+ postEncryptionFactors(encryptedAESKey: string, encryptedIV: string, linkId: string): Promise<import("axios").AxiosResponse<any, any>>;
47
+ getEncryptionFactors(linkId: string): Promise<import("axios").AxiosResponse<any, any>>;
46
48
  }
@@ -244,9 +244,22 @@ class CurtainWebAPI {
244
244
  form.append("e2e", "False");
245
245
  }
246
246
  if (encryption.encrypted && encryption.e2e && encryption.publicKey !== undefined) {
247
- const response = yield (0, curtain_encryption_1.encryptDataRSA)(data, encryption.publicKey);
248
- const base64String = (0, curtain_encryption_1.arrayBufferToBase64String)(response);
249
- form.append("file", new Blob([base64String], { type: 'text/json' }), "curtain-settings.json");
247
+ const aesKey = yield (0, curtain_encryption_1.generateAESKey)();
248
+ console.log("Encrypting data");
249
+ const encryptedData = yield (0, curtain_encryption_1.encryptAESData)(aesKey, data);
250
+ const encryptedKey = yield (0, curtain_encryption_1.encryptAESKey)(encryption.publicKey, yield (0, curtain_encryption_1.exportAESKey)(aesKey));
251
+ console.log(encryptedKey);
252
+ const encryptedIV = yield (0, curtain_encryption_1.encryptAESKey)(encryption.publicKey, (0, curtain_encryption_1.base64ToArrayBuffer)(encryptedData.iv));
253
+ console.log(encryptedIV);
254
+ const payload = {
255
+ encryptedData: encryptedData.encrypted,
256
+ encryptedKey: (0, curtain_encryption_1.arrayBufferToBase64String)(encryptedKey),
257
+ encryptedIV: (0, curtain_encryption_1.arrayBufferToBase64String)(encryptedIV)
258
+ };
259
+ console.log(payload);
260
+ form.append("encryptedKey", payload.encryptedKey);
261
+ form.append("encryptedIV", payload.encryptedIV);
262
+ form.append("file", new Blob([payload.encryptedData], { type: 'text/json' }), "curtain-settings.json");
250
263
  }
251
264
  else {
252
265
  form.append("file", new Blob([data], { type: 'text/json' }), "curtain-settings.json");
@@ -256,10 +269,13 @@ class CurtainWebAPI {
256
269
  let headers = new axios_1.AxiosHeaders();
257
270
  headers["Accept"] = "application/json";
258
271
  headers["Content-Type"] = "multipart/form-data";
272
+ console.log(form);
259
273
  if (onUploadProgress !== undefined) {
260
274
  return yield this.axiosInstance.post(this.baseURL + "curtain/", form, { headers: headers, responseType: "json", onUploadProgress: onUploadProgress });
261
275
  }
262
- return yield this.axiosInstance.post(this.baseURL + "curtain/", form, { headers: headers, responseType: "json" });
276
+ else {
277
+ return yield this.axiosInstance.post(this.baseURL + "curtain/", form, { headers: headers, responseType: "json" });
278
+ }
263
279
  });
264
280
  }
265
281
  postSettings(id, token, onDownloadProgress = undefined) {
@@ -460,5 +476,17 @@ class CurtainWebAPI {
460
476
  headers["Content-Type"] = "application/json";
461
477
  return this.axiosInstance.get(this.baseURL + `stats/summary/${lastNDays}/`, { responseType: "json", headers });
462
478
  }
479
+ postEncryptionFactors(encryptedAESKey, encryptedIV, linkId) {
480
+ let headers = new axios_1.AxiosHeaders();
481
+ headers["Accept"] = "application/json";
482
+ headers["Content-Type"] = "application/json";
483
+ return this.axiosInstance.post(this.baseURL + "curtain/" + linkId + "/set_encryption_factors/", { encryption_key: encryptedAESKey, encryption_iv: encryptedIV }, { headers: headers, responseType: "json" }).then((response) => { return response; });
484
+ }
485
+ getEncryptionFactors(linkId) {
486
+ let headers = new axios_1.AxiosHeaders();
487
+ headers["Accept"] = "application/json";
488
+ headers["Content-Type"] = "application/json";
489
+ return this.axiosInstance.get(this.baseURL + "curtain/" + linkId + "/get_encryption_factors/", { headers: headers, responseType: "json" }).then((response) => { return response; });
490
+ }
463
491
  }
464
492
  exports.CurtainWebAPI = CurtainWebAPI;
@@ -18,3 +18,21 @@ export declare function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): str
18
18
  export declare function removeLines(str_data: string): string;
19
19
  export declare function base64ToArrayBuffer(b64: string): Uint8Array;
20
20
  export declare function pemToArrayBuffer(pem: string): Uint8Array;
21
+ export declare function encryptAESKey(publicKey: CryptoKey, aesKey: ArrayBuffer): Promise<ArrayBuffer>;
22
+ export declare function generateAESKey(): Promise<CryptoKey>;
23
+ export declare function encryptAESData(aesKey: CryptoKey, data: string): Promise<{
24
+ encrypted: string;
25
+ iv: string;
26
+ }>;
27
+ export declare function decryptAESData(aesKey: CryptoKey, data: string, iv: string): Promise<string>;
28
+ export declare function decryptAESKey(privateKey: CryptoKey, encryptedKey: ArrayBuffer): Promise<ArrayBuffer>;
29
+ export declare function exportAESKey(key: CryptoKey): Promise<ArrayBuffer>;
30
+ export declare function importAESKey(key: ArrayBuffer): Promise<CryptoKey>;
31
+ export declare function encryptDataAES(data: string, publicKey: CryptoKey): Promise<{
32
+ encryptedKey: string;
33
+ encryptedData: {
34
+ encrypted: string;
35
+ iv: string;
36
+ };
37
+ }>;
38
+ export declare function decryptDataAES(encryptedKey: ArrayBuffer, encryptedData: string, iv: string, privateKey: CryptoKey): Promise<string>;
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.pemToArrayBuffer = exports.base64ToArrayBuffer = exports.removeLines = exports.arrayBufferToBase64String = exports.loadFromLocalStorage = exports.saveToLocalStorage = exports.exportKeyString = exports.importKey = exports.exportPrivateKey = exports.exportPublicKey = exports.generateKeyPair = exports.importPrivateKey = exports.importPublicKey = exports.decryptDataRSA = exports.encryptDataRSA = void 0;
12
+ exports.decryptDataAES = exports.encryptDataAES = exports.importAESKey = exports.exportAESKey = exports.decryptAESKey = exports.decryptAESData = exports.encryptAESData = exports.generateAESKey = exports.encryptAESKey = exports.pemToArrayBuffer = exports.base64ToArrayBuffer = exports.removeLines = exports.arrayBufferToBase64String = exports.loadFromLocalStorage = exports.saveToLocalStorage = exports.exportKeyString = exports.importKey = exports.exportPrivateKey = exports.exportPublicKey = exports.generateKeyPair = exports.importPrivateKey = exports.importPublicKey = exports.decryptDataRSA = exports.encryptDataRSA = void 0;
13
13
  function encryptDataRSA(data, publicKey) {
14
14
  return crypto.subtle.encrypt({ name: 'RSA-OAEP' }, publicKey, new TextEncoder().encode(data));
15
15
  }
@@ -125,3 +125,80 @@ function pemToArrayBuffer(pem) {
125
125
  return base64ToArrayBuffer(b64Final);
126
126
  }
127
127
  exports.pemToArrayBuffer = pemToArrayBuffer;
128
+ // a function to generate to encrypt an aes key arraybuffer with a public key
129
+ function encryptAESKey(publicKey, aesKey) {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ return yield crypto.subtle.encrypt({ name: "RSA-OAEP" }, publicKey, aesKey);
132
+ });
133
+ }
134
+ exports.encryptAESKey = encryptAESKey;
135
+ // a function to generate an aes key in GCM mode with a length of 256 bits
136
+ function generateAESKey() {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ return yield crypto.subtle.generateKey({
139
+ name: "AES-GCM",
140
+ length: 256,
141
+ }, true, ["encrypt", "decrypt"]);
142
+ });
143
+ }
144
+ exports.generateAESKey = generateAESKey;
145
+ // a function to encrypt a string with an aes key
146
+ function encryptAESData(aesKey, data) {
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ const iv = crypto.getRandomValues(new Uint8Array(12));
149
+ const enc = new TextEncoder();
150
+ const encoded = enc.encode(data);
151
+ const encrypted = yield crypto.subtle.encrypt({ name: "AES-GCM", iv: iv }, aesKey, encoded);
152
+ return { encrypted: arrayBufferToBase64String(encrypted), iv: arrayBufferToBase64String(iv) };
153
+ });
154
+ }
155
+ exports.encryptAESData = encryptAESData;
156
+ // a function to decrypt a string with an aes key
157
+ function decryptAESData(aesKey, data, iv) {
158
+ return __awaiter(this, void 0, void 0, function* () {
159
+ const dec = new TextDecoder();
160
+ const decrypted = yield crypto.subtle.decrypt({ name: "AES-GCM", iv: base64ToArrayBuffer(iv) }, aesKey, base64ToArrayBuffer(data));
161
+ return dec.decode(decrypted);
162
+ });
163
+ }
164
+ exports.decryptAESData = decryptAESData;
165
+ // a function to decrypt an aes key with a private key
166
+ function decryptAESKey(privateKey, encryptedKey) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ return yield crypto.subtle.decrypt({ name: "RSA-OAEP" }, privateKey, encryptedKey);
169
+ });
170
+ }
171
+ exports.decryptAESKey = decryptAESKey;
172
+ // a function to export an aes key to a string
173
+ function exportAESKey(key) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ return yield crypto.subtle.exportKey("raw", key);
176
+ });
177
+ }
178
+ exports.exportAESKey = exportAESKey;
179
+ // a function to import an aes key from a string
180
+ function importAESKey(key) {
181
+ return __awaiter(this, void 0, void 0, function* () {
182
+ return yield crypto.subtle.importKey("raw", key, "AES-GCM", true, ["encrypt", "decrypt"]);
183
+ });
184
+ }
185
+ exports.importAESKey = importAESKey;
186
+ // a function to encrypt aes key with a public key and also use the aes key to encrypt a large string then return the encrypted aes key and the encrypted string
187
+ function encryptDataAES(data, publicKey) {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ const aesKey = yield generateAESKey();
190
+ const encryptedKey = yield encryptAESKey(publicKey, yield exportAESKey(aesKey));
191
+ const encryptedData = yield encryptAESData(aesKey, data);
192
+ return { encryptedKey: arrayBufferToBase64String(encryptedKey), encryptedData: encryptedData };
193
+ });
194
+ }
195
+ exports.encryptDataAES = encryptDataAES;
196
+ // a function to decrypt an aes key with a private key and use the aes key to decrypt a large string
197
+ function decryptDataAES(encryptedKey, encryptedData, iv, privateKey) {
198
+ return __awaiter(this, void 0, void 0, function* () {
199
+ const aesKey = yield decryptAESKey(privateKey, encryptedKey);
200
+ //import aes key
201
+ return yield decryptAESData(yield importAESKey(aesKey), encryptedData, iv);
202
+ });
203
+ }
204
+ exports.decryptDataAES = decryptDataAES;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "curtain-web-api",
3
- "version": "1.0.41",
3
+ "version": "1.0.42",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -229,14 +229,18 @@ export class CurtainWebAPI {
229
229
 
230
230
  if (encryption.encrypted && encryption.e2e && encryption.publicKey !== undefined) {
231
231
  const aesKey = await generateAESKey()
232
+ console.log("Encrypting data")
232
233
  const encryptedData = await encryptAESData(aesKey, data)
233
234
  const encryptedKey = await encryptAESKey(encryption.publicKey, await exportAESKey(aesKey))
235
+ console.log(encryptedKey)
234
236
  const encryptedIV = await encryptAESKey(encryption.publicKey, base64ToArrayBuffer(encryptedData.iv))
237
+ console.log(encryptedIV)
235
238
  const payload = {
236
239
  encryptedData: encryptedData.encrypted,
237
240
  encryptedKey: arrayBufferToBase64String(encryptedKey),
238
241
  encryptedIV: arrayBufferToBase64String(encryptedIV)
239
242
  }
243
+ console.log(payload)
240
244
  form.append("encryptedKey", payload.encryptedKey)
241
245
  form.append("encryptedIV", payload.encryptedIV)
242
246
  form.append("file", new Blob([payload.encryptedData], {type: 'text/json'}), "curtain-settings.json")
@@ -249,6 +253,7 @@ export class CurtainWebAPI {
249
253
  let headers = new AxiosHeaders();
250
254
  headers["Accept"] = "application/json";
251
255
  headers["Content-Type"] = "multipart/form-data";
256
+ console.log(form)
252
257
  if (onUploadProgress !== undefined) {
253
258
  return await this.axiosInstance.post(this.baseURL + "curtain/", form, {headers: headers, responseType:"json", onUploadProgress: onUploadProgress})
254
259
  } else {