drapcode-utility 2.0.1 → 2.0.2
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.
- package/build/encryption/KMS.js +54 -102
- package/build/encryption/crypt.d.ts +4 -2
- package/build/encryption/crypt.js +76 -91
- package/build/encryption/file.d.ts +0 -2
- package/build/encryption/file.js +14 -130
- package/build/encryption/index.js +162 -334
- package/build/encryption/utility.js +7 -10
- package/build/errors/app-error.js +9 -27
- package/build/errors/axios-error.js +3 -3
- package/build/errors/bad-request-error.js +10 -28
- package/build/errors/custom-error.js +5 -23
- package/build/errors/not-found.js +9 -27
- package/build/format-fields/index.d.ts +0 -1
- package/build/format-fields/index.js +32 -65
- package/build/index.d.ts +1 -4
- package/build/index.js +1 -4
- package/build/middlewares/error-logger.d.ts +1 -1
- package/build/middlewares/error-logger.js +29 -29
- package/build/middlewares/redis/request-log.js +24 -74
- package/build/query/queryBuilder.d.ts +9 -0
- package/build/query/queryBuilder.js +567 -0
- package/build/utils/check-error.d.ts +15 -8
- package/build/utils/check-error.js +71 -160
- package/build/utils/common-util.d.ts +40 -39
- package/build/utils/common-util.js +60 -59
- package/build/utils/date-util.d.ts +28 -7
- package/build/utils/date-util.js +180 -127
- package/build/utils/file-util.d.ts +51 -6
- package/build/utils/file-util.js +36 -40
- package/build/utils/prepare-query.js +70 -43
- package/build/utils/project-util.d.ts +43 -5
- package/build/utils/project-util.js +176 -121
- package/build/utils/query-parser.d.ts +1 -1
- package/build/utils/query-parser.js +289 -342
- package/build/utils/query-utils.d.ts +2 -2
- package/build/utils/query-utils.js +103 -116
- package/build/utils/rest-client.js +236 -328
- package/build/utils/s3-util.js +238 -469
- package/build/utils/token.js +34 -81
- package/build/utils/util.d.ts +58 -13
- package/build/utils/util.js +424 -494
- package/build/utils/uuid-generator.d.ts +20 -1
- package/build/utils/uuid-generator.js +111 -47
- package/package.json +7 -5
- package/build/middlewares/interceptor-logger-new.d.ts +0 -2
- package/build/middlewares/interceptor-logger-new.js +0 -53
- package/build/middlewares/interceptor-logger.d.ts +0 -2
- package/build/middlewares/interceptor-logger.js +0 -52
- package/build/utils/query-parser-new.d.ts +0 -1
- package/build/utils/query-parser-new.js +0 -541
|
@@ -1,222 +1,96 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.processKMSDecryption = exports.cryptFile = exports.drapcodeEncryptDecrypt = exports.decryptDataWithKMS = exports.encryptDataWithKMS = exports.createKMSDataKey = exports.processDataEncryptionDecryption = exports.processItemEncryptDecrypt = exports.crypt = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
case 2:
|
|
68
|
-
if (!Array.isArray(data)) return [3 /*break*/, 4];
|
|
69
|
-
promises = data.map(function (item) {
|
|
70
|
-
return (0, exports.processItemEncryptDecrypt)(item, fields, encryption, decrypt, encryptedRefCollections);
|
|
71
|
-
});
|
|
72
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
73
|
-
case 3:
|
|
74
|
-
data = _b.sent();
|
|
75
|
-
return [3 /*break*/, 6];
|
|
76
|
-
case 4: return [4 /*yield*/, (0, exports.processItemEncryptDecrypt)(data, fields, encryption, decrypt, encryptedRefCollections)];
|
|
77
|
-
case 5:
|
|
78
|
-
data = _b.sent();
|
|
79
|
-
_b.label = 6;
|
|
80
|
-
case 6: return [2 /*return*/, data];
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
});
|
|
4
|
+
const client_kms_1 = require("@aws-sdk/client-kms");
|
|
5
|
+
const drapcode_constant_1 = require("drapcode-constant");
|
|
6
|
+
const crypt_1 = require("./crypt");
|
|
7
|
+
const file_1 = require("./file");
|
|
8
|
+
const KMS_1 = require("./KMS");
|
|
9
|
+
const crypt = async (data, fields, encryption, decrypt, encryptedRefCollections = []) => {
|
|
10
|
+
if (encryption.encryptionType === "KMS") {
|
|
11
|
+
const { accessKeyId, secretAccessKey, region } = encryption.awsConfig;
|
|
12
|
+
const config = {
|
|
13
|
+
region,
|
|
14
|
+
accessKeyId,
|
|
15
|
+
secretAccessKey,
|
|
16
|
+
};
|
|
17
|
+
const plainTextData = await (0, exports.processKMSDecryption)(config, encryption.dataKey, {});
|
|
18
|
+
if (plainTextData.status === "FAILED") {
|
|
19
|
+
return plainTextData;
|
|
20
|
+
}
|
|
21
|
+
encryption.dataKey = plainTextData.data;
|
|
22
|
+
}
|
|
23
|
+
if (Array.isArray(data)) {
|
|
24
|
+
const promises = data.map((item) => (0, exports.processItemEncryptDecrypt)(item, fields, encryption, decrypt, encryptedRefCollections));
|
|
25
|
+
data = await Promise.all(promises);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
data = await (0, exports.processItemEncryptDecrypt)(data, fields, encryption, decrypt, encryptedRefCollections);
|
|
29
|
+
}
|
|
30
|
+
return data;
|
|
84
31
|
};
|
|
85
32
|
exports.crypt = crypt;
|
|
86
|
-
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
switch (_j.label) {
|
|
100
|
-
case 0:
|
|
101
|
-
field = fields.find(function (field) { return field.fieldName === fieldName; });
|
|
102
|
-
if (!(field && field.encrypted)) return [3 /*break*/, 4];
|
|
103
|
-
if (!(Array.isArray(item[fieldName]) &&
|
|
104
|
-
drapcode_constant_1.OptionTypeFields.includes(field.type))) return [3 /*break*/, 2];
|
|
105
|
-
promises = item[fieldName].map(function (value) {
|
|
106
|
-
return (item[fieldName] = (0, exports.processDataEncryptionDecryption)(value, encryption, decrypt));
|
|
107
|
-
});
|
|
108
|
-
_c = item;
|
|
109
|
-
_d = fieldName;
|
|
110
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
111
|
-
case 1:
|
|
112
|
-
_c[_d] = _j.sent();
|
|
113
|
-
return [3 /*break*/, 4];
|
|
114
|
-
case 2:
|
|
115
|
-
_e = item;
|
|
116
|
-
_f = fieldName;
|
|
117
|
-
return [4 /*yield*/, (0, exports.processDataEncryptionDecryption)(item[fieldName], encryption, decrypt)];
|
|
118
|
-
case 3:
|
|
119
|
-
_e[_f] = _j.sent();
|
|
120
|
-
_j.label = 4;
|
|
121
|
-
case 4:
|
|
122
|
-
if (!decrypt) return [3 /*break*/, 6];
|
|
123
|
-
if (!(decrypt && drapcode_constant_1.BelongsCreatedByRefField.includes(field === null || field === void 0 ? void 0 : field.type))) return [3 /*break*/, 6];
|
|
124
|
-
refField = field;
|
|
125
|
-
if (!refField.refCollection &&
|
|
126
|
-
[drapcode_constant_1.FieldTypes.createdBy.id, drapcode_constant_1.FieldTypes.updatedBy.id].includes(field.type)) {
|
|
127
|
-
refField.refCollection = {
|
|
128
|
-
collectionField: "userName",
|
|
129
|
-
collectionName: "user",
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
_g = item;
|
|
133
|
-
_h = fieldName;
|
|
134
|
-
return [4 /*yield*/, processReferenceItemDecrypt(item[fieldName], encryption, decrypt, refField, encryptedRefCollections)];
|
|
135
|
-
case 5:
|
|
136
|
-
_g[_h] = _j.sent();
|
|
137
|
-
_j.label = 6;
|
|
138
|
-
case 6: return [2 /*return*/];
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
|
-
_i = 0, _a = Object.keys(item);
|
|
143
|
-
_b.label = 1;
|
|
144
|
-
case 1:
|
|
145
|
-
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
146
|
-
fieldName = _a[_i];
|
|
147
|
-
return [5 /*yield**/, _loop_1(fieldName)];
|
|
148
|
-
case 2:
|
|
149
|
-
_b.sent();
|
|
150
|
-
_b.label = 3;
|
|
151
|
-
case 3:
|
|
152
|
-
_i++;
|
|
153
|
-
return [3 /*break*/, 1];
|
|
154
|
-
case 4: return [2 /*return*/, item];
|
|
33
|
+
const processItemEncryptDecrypt = async (item, fields, encryption, decrypt, encryptedRefCollections = []) => {
|
|
34
|
+
if (!item && typeof item !== "object") {
|
|
35
|
+
return item;
|
|
36
|
+
}
|
|
37
|
+
//TODO: Need to refactor
|
|
38
|
+
// Instead of iterating over item, iterate over encrypted field
|
|
39
|
+
for (const fieldName of Object.keys(item)) {
|
|
40
|
+
const field = fields.find((field) => field.fieldName === fieldName);
|
|
41
|
+
if (field && field.encrypted) {
|
|
42
|
+
if (Array.isArray(item[fieldName]) &&
|
|
43
|
+
drapcode_constant_1.OptionTypeFields.includes(field.type)) {
|
|
44
|
+
const promises = item[fieldName].map((value) => (item[fieldName] = (0, exports.processDataEncryptionDecryption)(value, encryption, decrypt)));
|
|
45
|
+
item[fieldName] = await Promise.all(promises);
|
|
155
46
|
}
|
|
156
|
-
|
|
157
|
-
|
|
47
|
+
else {
|
|
48
|
+
item[fieldName] = await (0, exports.processDataEncryptionDecryption)(item[fieldName], encryption, decrypt);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (decrypt && drapcode_constant_1.BelongsCreatedByRefField.includes(field?.type)) {
|
|
52
|
+
let refField = field;
|
|
53
|
+
if (!refField.refCollection && drapcode_constant_1.byFields.has(field.type)) {
|
|
54
|
+
refField.refCollection = {
|
|
55
|
+
collectionField: "userName",
|
|
56
|
+
collectionName: "user",
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
item[fieldName] = await processReferenceItemDecrypt(item[fieldName], encryption, decrypt, refField, encryptedRefCollections);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return item;
|
|
158
63
|
};
|
|
159
64
|
exports.processItemEncryptDecrypt = processItemEncryptDecrypt;
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
case "KMS": return [3 /*break*/, 1];
|
|
172
|
-
}
|
|
173
|
-
return [3 /*break*/, 6];
|
|
174
|
-
case 1:
|
|
175
|
-
if (!decrypt) return [3 /*break*/, 3];
|
|
176
|
-
return [4 /*yield*/, (0, crypt_1.decryptData)(data, dataKey)];
|
|
177
|
-
case 2:
|
|
178
|
-
_b = _c.sent();
|
|
179
|
-
return [3 /*break*/, 5];
|
|
180
|
-
case 3: return [4 /*yield*/, (0, crypt_1.encryptData)(data, dataKey)];
|
|
181
|
-
case 4:
|
|
182
|
-
_b = _c.sent();
|
|
183
|
-
_c.label = 5;
|
|
184
|
-
case 5:
|
|
185
|
-
result = _b;
|
|
186
|
-
return [2 /*return*/, result];
|
|
187
|
-
case 6: return [2 /*return*/, data];
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
}); };
|
|
65
|
+
const processDataEncryptionDecryption = async (data, encryption, decrypt) => {
|
|
66
|
+
const { encryptionType, dataKey } = encryption;
|
|
67
|
+
if (!data)
|
|
68
|
+
return data;
|
|
69
|
+
switch (encryptionType) {
|
|
70
|
+
case "KMS":
|
|
71
|
+
return decrypt ? (0, crypt_1.decryptData)(data, dataKey) : (0, crypt_1.encryptData)(data, dataKey);
|
|
72
|
+
default:
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
191
76
|
exports.processDataEncryptionDecryption = processDataEncryptionDecryption;
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return [4 /*yield*/, Promise.all(promises)];
|
|
204
|
-
case 1:
|
|
205
|
-
data = _a.sent();
|
|
206
|
-
_a.label = 2;
|
|
207
|
-
case 2: return [2 /*return*/, data];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
}); };
|
|
211
|
-
var getEncryptedReferenceCollection = function (encryptedRefCollections, refField) {
|
|
212
|
-
var _a = refField === null || refField === void 0 ? void 0 : refField.refCollection, refCollectionName = _a.collectionName, refCollectionFieldName = _a.collectionField;
|
|
213
|
-
var refCollection;
|
|
77
|
+
const processReferenceItemDecrypt = async (data, encryption, decrypt, refField, encryptedRefCollections) => {
|
|
78
|
+
const { refCollection } = getEncryptedReferenceCollection(encryptedRefCollections, refField);
|
|
79
|
+
if (refCollection && Array.isArray(data)) {
|
|
80
|
+
const promises = data.map((item) => (0, exports.processItemEncryptDecrypt)(item, refCollection.fields, encryption, decrypt));
|
|
81
|
+
data = await Promise.all(promises);
|
|
82
|
+
}
|
|
83
|
+
return data;
|
|
84
|
+
};
|
|
85
|
+
const getEncryptedReferenceCollection = (encryptedRefCollections, refField) => {
|
|
86
|
+
const { collectionName: refCollectionName, collectionField: refCollectionFieldName, } = refField?.refCollection;
|
|
87
|
+
let refCollection;
|
|
214
88
|
if (encryptedRefCollections.length) {
|
|
215
89
|
if (refCollectionName) {
|
|
216
|
-
refCollection = encryptedRefCollections.find(
|
|
90
|
+
refCollection = encryptedRefCollections.find((encColl) => encColl.collectionName === refCollectionName);
|
|
217
91
|
}
|
|
218
92
|
}
|
|
219
|
-
return { refCollectionName
|
|
93
|
+
return { refCollectionName, refCollectionFieldName, refCollection };
|
|
220
94
|
};
|
|
221
95
|
/**
|
|
222
96
|
* This method is used to generate private KEY for KMS ARN
|
|
@@ -225,17 +99,10 @@ var getEncryptedReferenceCollection = function (encryptedRefCollections, refFiel
|
|
|
225
99
|
* @param arn KMS Key
|
|
226
100
|
* @returns {status: string, dataKey: string, message: ""}
|
|
227
101
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return
|
|
231
|
-
|
|
232
|
-
case 0: return [4 /*yield*/, (0, KMS_1.processKMSGenerateDataKey)(config, arn)];
|
|
233
|
-
case 1:
|
|
234
|
-
dataKeyRes = _a.sent();
|
|
235
|
-
return [2 /*return*/, dataKeyRes];
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
}); };
|
|
102
|
+
const createKMSDataKey = async (config, arn) => {
|
|
103
|
+
const dataKeyRes = await (0, KMS_1.processKMSGenerateDataKey)(config, arn);
|
|
104
|
+
return dataKeyRes;
|
|
105
|
+
};
|
|
239
106
|
exports.createKMSDataKey = createKMSDataKey;
|
|
240
107
|
/**
|
|
241
108
|
* This method is used to encrypt plaintext upto 4KB
|
|
@@ -246,17 +113,10 @@ exports.createKMSDataKey = createKMSDataKey;
|
|
|
246
113
|
* @param context Extra setting for extra security
|
|
247
114
|
* @returns {status: string, message: string, cipherText: string}
|
|
248
115
|
*/
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
return
|
|
252
|
-
|
|
253
|
-
case 0: return [4 /*yield*/, (0, KMS_1.processKMSEncryption)(config, arn, plainText, context)];
|
|
254
|
-
case 1:
|
|
255
|
-
cryptData = _a.sent();
|
|
256
|
-
return [2 /*return*/, cryptData];
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
}); };
|
|
116
|
+
const encryptDataWithKMS = async (config, arn, plainText, context) => {
|
|
117
|
+
const cryptData = await (0, KMS_1.processKMSEncryption)(config, arn, plainText, context);
|
|
118
|
+
return cryptData;
|
|
119
|
+
};
|
|
260
120
|
exports.encryptDataWithKMS = encryptDataWithKMS;
|
|
261
121
|
/**
|
|
262
122
|
* This method is used to decrypt secure text
|
|
@@ -266,113 +126,81 @@ exports.encryptDataWithKMS = encryptDataWithKMS;
|
|
|
266
126
|
* @param context Extra setting which given for extra security
|
|
267
127
|
* @returns {status: string, message: string, plainText: string}
|
|
268
128
|
*/
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return
|
|
272
|
-
|
|
273
|
-
case 0: return [4 /*yield*/, (0, exports.processKMSDecryption)(config, cipherText, context)];
|
|
274
|
-
case 1:
|
|
275
|
-
plainTextData = _a.sent();
|
|
276
|
-
return [2 /*return*/, plainTextData];
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
}); };
|
|
129
|
+
const decryptDataWithKMS = async (config, cipherText, context) => {
|
|
130
|
+
const plainTextData = await (0, exports.processKMSDecryption)(config, cipherText, context);
|
|
131
|
+
return plainTextData;
|
|
132
|
+
};
|
|
280
133
|
exports.decryptDataWithKMS = decryptDataWithKMS;
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
response = _a.sent();
|
|
325
|
-
return [3 /*break*/, 5];
|
|
326
|
-
case 3:
|
|
327
|
-
console.log("Decrypting");
|
|
328
|
-
return [4 /*yield*/, (0, crypt_1.decryptData)(data, publicKey)];
|
|
329
|
-
case 4:
|
|
330
|
-
response = _a.sent();
|
|
331
|
-
_a.label = 5;
|
|
332
|
-
case 5: return [2 /*return*/, { status: "SUCCESS", message: "", data: response }];
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
}); };
|
|
134
|
+
const drapcodeEncryptDecrypt = async (data, encrypt) => {
|
|
135
|
+
const region = process.env.AWS_KMS_REGION;
|
|
136
|
+
const accessKey = process.env.AWS_KMS_ACCESS_KEY;
|
|
137
|
+
const secretKey = process.env.AWS_KMS_SECRET_KEY;
|
|
138
|
+
const privateDataKey = process.env.AWS_KMS_PRIVATE_DATA_KEY;
|
|
139
|
+
if (!region)
|
|
140
|
+
return { status: "FAILED", message: "AWS Region is missing", data: "" };
|
|
141
|
+
if (!accessKey)
|
|
142
|
+
return { status: "FAILED", message: "AWS Access Key is missing", data: "" };
|
|
143
|
+
if (!secretKey)
|
|
144
|
+
return {
|
|
145
|
+
status: "FAILED",
|
|
146
|
+
message: "AWS Access Secret Key is missing",
|
|
147
|
+
data: "",
|
|
148
|
+
};
|
|
149
|
+
if (!privateDataKey)
|
|
150
|
+
return {
|
|
151
|
+
status: "FAILED",
|
|
152
|
+
message: "KMS Private Key is missing",
|
|
153
|
+
data: "",
|
|
154
|
+
};
|
|
155
|
+
const config = {
|
|
156
|
+
region,
|
|
157
|
+
accessKeyId: accessKey,
|
|
158
|
+
secretAccessKey: secretKey,
|
|
159
|
+
};
|
|
160
|
+
//Generate Public Key from
|
|
161
|
+
const plainTextData = await (0, exports.processKMSDecryption)(config, privateDataKey, {});
|
|
162
|
+
if (plainTextData.status === "FAILED") {
|
|
163
|
+
return plainTextData;
|
|
164
|
+
}
|
|
165
|
+
const publicKey = plainTextData.data;
|
|
166
|
+
let response = null;
|
|
167
|
+
if (encrypt) {
|
|
168
|
+
console.log("Encrypting");
|
|
169
|
+
response = (0, crypt_1.encryptData)(data, publicKey);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
console.log("Decrypting");
|
|
173
|
+
response = (0, crypt_1.decryptData)(data, publicKey);
|
|
174
|
+
}
|
|
175
|
+
return { status: "SUCCESS", message: "", data: response };
|
|
176
|
+
};
|
|
336
177
|
exports.drapcodeEncryptDecrypt = drapcodeEncryptDecrypt;
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
return
|
|
340
|
-
|
|
341
|
-
case 0: return [4 /*yield*/, (0, file_1.processFileEncryptionDecryption)(filePath, encryption, decrypt)];
|
|
342
|
-
case 1:
|
|
343
|
-
data = _a.sent();
|
|
344
|
-
return [2 /*return*/, data];
|
|
345
|
-
}
|
|
346
|
-
});
|
|
347
|
-
}); };
|
|
178
|
+
const cryptFile = async (filePath, encryption, decrypt) => {
|
|
179
|
+
const data = await (0, file_1.processFileEncryptionDecryption)(filePath, encryption, decrypt);
|
|
180
|
+
return data;
|
|
181
|
+
};
|
|
348
182
|
exports.cryptFile = cryptFile;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
error_1 = _a.sent();
|
|
373
|
-
return [2 /*return*/, { status: "FAILED", data: "", message: error_1.message }];
|
|
374
|
-
case 3: return [2 /*return*/];
|
|
375
|
-
}
|
|
376
|
-
});
|
|
377
|
-
}); };
|
|
183
|
+
const processKMSDecryption = async (config, cipherText, context) => {
|
|
184
|
+
try {
|
|
185
|
+
const { accessKeyId, secretAccessKey, region } = config;
|
|
186
|
+
const client = new client_kms_1.KMSClient({
|
|
187
|
+
region,
|
|
188
|
+
credentials: { accessKeyId, secretAccessKey },
|
|
189
|
+
maxAttempts: 3,
|
|
190
|
+
});
|
|
191
|
+
const dCipherText = Buffer.from(cipherText, "base64");
|
|
192
|
+
const input = {
|
|
193
|
+
CiphertextBlob: dCipherText,
|
|
194
|
+
EncryptionContext: context,
|
|
195
|
+
};
|
|
196
|
+
const command = new client_kms_1.DecryptCommand(input);
|
|
197
|
+
const response = await client.send(command);
|
|
198
|
+
//@ts-ignore it is important else it will throw error
|
|
199
|
+
const plainText = Buffer.from(response.Plaintext).toString("base64");
|
|
200
|
+
return { status: "SUCCESS", data: plainText, message: "" };
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
return { status: "FAILED", data: "", message: error.message };
|
|
204
|
+
}
|
|
205
|
+
};
|
|
378
206
|
exports.processKMSDecryption = processKMSDecryption;
|
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getEncryptedReferenceFields = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
collectionFields = collectionFields.filter(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
collectionFields.forEach(function (field) {
|
|
11
|
-
if (!field.refCollection &&
|
|
12
|
-
[drapcode_constant_1.FieldTypes.createdBy.id, drapcode_constant_1.FieldTypes.updatedBy.id].includes(field.type)) {
|
|
4
|
+
const drapcode_constant_1 = require("drapcode-constant");
|
|
5
|
+
const getEncryptedReferenceFields = (collectionFields) => {
|
|
6
|
+
let collectionsNamesArr = [];
|
|
7
|
+
collectionFields = collectionFields.filter((field) => drapcode_constant_1.BelongsCreatedByRefField.includes(field.type));
|
|
8
|
+
collectionFields.forEach((field) => {
|
|
9
|
+
if (!field.refCollection && drapcode_constant_1.byFields.has(field.type)) {
|
|
13
10
|
collectionsNamesArr.push("user");
|
|
14
11
|
}
|
|
15
12
|
else {
|
|
16
|
-
|
|
13
|
+
const { collectionName } = field.refCollection;
|
|
17
14
|
collectionsNamesArr.push(collectionName);
|
|
18
15
|
}
|
|
19
16
|
});
|
|
@@ -1,32 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.AppError = void 0;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Object.setPrototypeOf(_this, AppError.prototype);
|
|
28
|
-
return _this;
|
|
4
|
+
class AppError extends Error {
|
|
5
|
+
constructor(message, statusCode) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.message = message;
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this.statusCode = statusCode;
|
|
10
|
+
this.isOperational = true;
|
|
11
|
+
Object.setPrototypeOf(this, AppError.prototype);
|
|
29
12
|
}
|
|
30
|
-
|
|
31
|
-
}(Error));
|
|
13
|
+
}
|
|
32
14
|
exports.AppError = AppError;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseAxiosError = void 0;
|
|
4
|
-
|
|
4
|
+
const parseAxiosError = (error) => {
|
|
5
5
|
if (!error) {
|
|
6
6
|
return {};
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const { response, request, message } = error;
|
|
9
|
+
let errData = "", errStatus = 400;
|
|
10
10
|
if (response) {
|
|
11
11
|
console.error("err.response.data: ", response.data);
|
|
12
12
|
console.error("err.response.status: ", response.status);
|