curtain-web-api 1.0.40 → 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.40",
3
+ "version": "1.0.42",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -1,6 +1,12 @@
1
1
  import {User} from "./curtain-user";
2
2
  import axios, {AxiosHeaders} from "axios";
3
- import {arrayBufferToBase64String, CurtainEncryption, encryptDataRSA} from "./curtain-encryption";
3
+ import {
4
+ arrayBufferToBase64String, base64ToArrayBuffer,
5
+ CurtainEncryption,
6
+ encryptAESData, encryptAESKey,
7
+ encryptDataRSA, exportAESKey,
8
+ generateAESKey
9
+ } from "./curtain-encryption";
4
10
 
5
11
  const base: string = "https://celsus.muttsu.xyz/"
6
12
 
@@ -222,9 +228,22 @@ export class CurtainWebAPI {
222
228
  }
223
229
 
224
230
  if (encryption.encrypted && encryption.e2e && encryption.publicKey !== undefined) {
225
- const response = await encryptDataRSA(data, encryption.publicKey)
226
- const base64String = arrayBufferToBase64String(response)
227
- form.append("file", new Blob([base64String], {type: 'text/json'}), "curtain-settings.json")
231
+ const aesKey = await generateAESKey()
232
+ console.log("Encrypting data")
233
+ const encryptedData = await encryptAESData(aesKey, data)
234
+ const encryptedKey = await encryptAESKey(encryption.publicKey, await exportAESKey(aesKey))
235
+ console.log(encryptedKey)
236
+ const encryptedIV = await encryptAESKey(encryption.publicKey, base64ToArrayBuffer(encryptedData.iv))
237
+ console.log(encryptedIV)
238
+ const payload = {
239
+ encryptedData: encryptedData.encrypted,
240
+ encryptedKey: arrayBufferToBase64String(encryptedKey),
241
+ encryptedIV: arrayBufferToBase64String(encryptedIV)
242
+ }
243
+ console.log(payload)
244
+ form.append("encryptedKey", payload.encryptedKey)
245
+ form.append("encryptedIV", payload.encryptedIV)
246
+ form.append("file", new Blob([payload.encryptedData], {type: 'text/json'}), "curtain-settings.json")
228
247
  } else {
229
248
  form.append("file", new Blob([data], {type: 'text/json'}), "curtain-settings.json")
230
249
  }
@@ -234,10 +253,13 @@ export class CurtainWebAPI {
234
253
  let headers = new AxiosHeaders();
235
254
  headers["Accept"] = "application/json";
236
255
  headers["Content-Type"] = "multipart/form-data";
256
+ console.log(form)
237
257
  if (onUploadProgress !== undefined) {
238
258
  return await this.axiosInstance.post(this.baseURL + "curtain/", form, {headers: headers, responseType:"json", onUploadProgress: onUploadProgress})
259
+ } else {
260
+ return await this.axiosInstance.post(this.baseURL + "curtain/", form, {headers: headers, responseType:"json"})
239
261
  }
240
- return await this.axiosInstance.post(this.baseURL + "curtain/", form, {headers: headers, responseType:"json"})
262
+
241
263
  }
242
264
 
243
265
  postSettings(id: string, token: string, onDownloadProgress: any = undefined) {
@@ -467,5 +489,19 @@ export class CurtainWebAPI {
467
489
  headers["Content-Type"] = "application/json";
468
490
  return this.axiosInstance.get(this.baseURL + `stats/summary/${lastNDays}/`, {responseType:"json", headers})
469
491
  }
492
+
493
+ postEncryptionFactors(encryptedAESKey: string, encryptedIV: string, linkId: string) {
494
+ let headers = new AxiosHeaders();
495
+ headers["Accept"] = "application/json";
496
+ headers["Content-Type"] = "application/json";
497
+ 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;});
498
+ }
499
+
500
+ getEncryptionFactors(linkId: string) {
501
+ let headers = new AxiosHeaders();
502
+ headers["Accept"] = "application/json";
503
+ headers["Content-Type"] = "application/json";
504
+ return this.axiosInstance.get(this.baseURL + "curtain/" + linkId + "/get_encryption_factors/", {headers: headers, responseType: "json"}).then((response) => {return response;});
505
+ }
470
506
  }
471
507
 
@@ -103,4 +103,68 @@ export function pemToArrayBuffer(pem: string) {
103
103
  b64Final = b64Final.replace('-----END PUBLIC KEY-----', '');
104
104
 
105
105
  return base64ToArrayBuffer(b64Final);
106
- }
106
+ }
107
+
108
+ // a function to generate to encrypt an aes key arraybuffer with a public key
109
+ export async function encryptAESKey(publicKey: CryptoKey, aesKey: ArrayBuffer) {
110
+ return await crypto.subtle.encrypt({name: "RSA-OAEP"}, publicKey, aesKey)
111
+ }
112
+
113
+ // a function to generate an aes key in GCM mode with a length of 256 bits
114
+ export async function generateAESKey() {
115
+ return await crypto.subtle.generateKey(
116
+ {
117
+ name: "AES-GCM",
118
+ length: 256,
119
+ },
120
+ true,
121
+ ["encrypt", "decrypt"],
122
+ )
123
+ }
124
+
125
+ // a function to encrypt a string with an aes key
126
+ export async function encryptAESData(aesKey: CryptoKey, data: string) {
127
+ const iv = crypto.getRandomValues(new Uint8Array(12))
128
+ const enc = new TextEncoder()
129
+ const encoded = enc.encode(data)
130
+ const encrypted = await crypto.subtle.encrypt({name: "AES-GCM", iv: iv}, aesKey, encoded)
131
+ return {encrypted: arrayBufferToBase64String(encrypted), iv: arrayBufferToBase64String(iv)}
132
+ }
133
+
134
+ // a function to decrypt a string with an aes key
135
+ export async function decryptAESData(aesKey: CryptoKey, data: string, iv: string) {
136
+ const dec = new TextDecoder()
137
+ const decrypted = await crypto.subtle.decrypt({name: "AES-GCM", iv: base64ToArrayBuffer(iv)}, aesKey, base64ToArrayBuffer(data))
138
+ return dec.decode(decrypted)
139
+ }
140
+
141
+ // a function to decrypt an aes key with a private key
142
+ export async function decryptAESKey(privateKey: CryptoKey, encryptedKey: ArrayBuffer) {
143
+ return await crypto.subtle.decrypt({name: "RSA-OAEP"}, privateKey, encryptedKey)
144
+ }
145
+
146
+ // a function to export an aes key to a string
147
+ export async function exportAESKey(key: CryptoKey) {
148
+ return await crypto.subtle.exportKey("raw", key)
149
+ }
150
+ // a function to import an aes key from a string
151
+ export async function importAESKey(key: ArrayBuffer) {
152
+ return await crypto.subtle.importKey("raw", key, "AES-GCM", true, ["encrypt", "decrypt"])
153
+ }
154
+
155
+ // 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
156
+ export async function encryptDataAES(data: string, publicKey: CryptoKey) {
157
+ const aesKey = await generateAESKey()
158
+ const encryptedKey = await encryptAESKey(publicKey, await exportAESKey(aesKey))
159
+ const encryptedData = await encryptAESData(aesKey, data)
160
+ return {encryptedKey: arrayBufferToBase64String(encryptedKey), encryptedData: encryptedData}
161
+ }
162
+
163
+ // a function to decrypt an aes key with a private key and use the aes key to decrypt a large string
164
+ export async function decryptDataAES(encryptedKey: ArrayBuffer, encryptedData: string, iv: string, privateKey: CryptoKey) {
165
+ const aesKey = await decryptAESKey(privateKey, encryptedKey)
166
+ //import aes key
167
+ return await decryptAESData(await importAESKey(aesKey), encryptedData, iv)
168
+ }
169
+
170
+