drapcode-utility 1.4.6 → 1.4.8

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.
@@ -1,6 +1,6 @@
1
1
  import { AwsConfig, Encryption } from "./model";
2
- export declare const crypt: (data: any, fields: Array<object>, encryption: Encryption, decrypt: boolean) => Promise<any>;
3
- export declare const processItemEncryptDecrypt: (item: any, fields: Array<object>, encryption: Encryption, decrypt: boolean) => Promise<any>;
2
+ export declare const crypt: (data: any, fields: Array<object>, encryption: Encryption, decrypt: boolean, encrypedRefCollections?: Array<object>) => Promise<any>;
3
+ export declare const processItemEncryptDecrypt: (item: any, fields: Array<object>, encryption: Encryption, decrypt: boolean, encrypedRefCollections?: Array<object>) => Promise<any>;
4
4
  export declare const processDataEncryptionDecryption: (data: string, encryption: Encryption, decrypt: boolean) => Promise<string>;
5
5
  /**
6
6
  * This method is used to generate private KEY for KMS ARN
@@ -41,83 +41,115 @@ var client_kms_1 = require("@aws-sdk/client-kms");
41
41
  var crypt_1 = require("./crypt");
42
42
  var file_1 = require("./file");
43
43
  var KMS_1 = require("./KMS");
44
- var crypt = function (data, fields, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
45
- var _a, accessKeyId, secretAccessKey, region, config, plainTextData, promises;
46
- return __generator(this, function (_b) {
47
- switch (_b.label) {
48
- case 0:
49
- if (!(encryption.encryptionType === "KMS")) return [3 /*break*/, 2];
50
- _a = encryption.awsConfig, accessKeyId = _a.accessKeyId, secretAccessKey = _a.secretAccessKey, region = _a.region;
51
- config = {
52
- region: region,
53
- accessKeyId: accessKeyId,
54
- secretAccessKey: secretAccessKey,
55
- };
56
- return [4 /*yield*/, exports.processKMSDecryption(config, encryption.dataKey, {})];
57
- case 1:
58
- plainTextData = _b.sent();
59
- if (plainTextData.status === "FAILED") {
60
- return [2 /*return*/, plainTextData];
61
- }
62
- encryption.dataKey = plainTextData.data;
63
- _b.label = 2;
64
- case 2:
65
- if (!Array.isArray(data)) return [3 /*break*/, 4];
66
- promises = data.map(function (item) {
67
- return exports.processItemEncryptDecrypt(item, fields, encryption, decrypt);
68
- });
69
- return [4 /*yield*/, Promise.all(promises)];
70
- case 3:
71
- data = _b.sent();
72
- return [3 /*break*/, 6];
73
- case 4: return [4 /*yield*/, exports.processItemEncryptDecrypt(data, fields, encryption, decrypt)];
74
- case 5:
75
- data = _b.sent();
76
- _b.label = 6;
77
- case 6: return [2 /*return*/, data];
78
- }
44
+ var drapcode_constant_1 = require("drapcode-constant");
45
+ var crypt = function (data, fields, encryption, decrypt, encrypedRefCollections) {
46
+ if (encrypedRefCollections === void 0) { encrypedRefCollections = []; }
47
+ return __awaiter(void 0, void 0, void 0, function () {
48
+ var _a, accessKeyId, secretAccessKey, region, config, plainTextData, promises;
49
+ return __generator(this, function (_b) {
50
+ switch (_b.label) {
51
+ case 0:
52
+ if (!(encryption.encryptionType === "KMS")) return [3 /*break*/, 2];
53
+ _a = encryption.awsConfig, accessKeyId = _a.accessKeyId, secretAccessKey = _a.secretAccessKey, region = _a.region;
54
+ config = {
55
+ region: region,
56
+ accessKeyId: accessKeyId,
57
+ secretAccessKey: secretAccessKey,
58
+ };
59
+ return [4 /*yield*/, exports.processKMSDecryption(config, encryption.dataKey, {})];
60
+ case 1:
61
+ plainTextData = _b.sent();
62
+ if (plainTextData.status === "FAILED") {
63
+ return [2 /*return*/, plainTextData];
64
+ }
65
+ encryption.dataKey = plainTextData.data;
66
+ _b.label = 2;
67
+ case 2:
68
+ if (!Array.isArray(data)) return [3 /*break*/, 4];
69
+ promises = data.map(function (item) {
70
+ return exports.processItemEncryptDecrypt(item, fields, encryption, decrypt, encrypedRefCollections);
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*/, exports.processItemEncryptDecrypt(data, fields, encryption, decrypt, encrypedRefCollections)];
77
+ case 5:
78
+ data = _b.sent();
79
+ _b.label = 6;
80
+ case 6: return [2 /*return*/, data];
81
+ }
82
+ });
79
83
  });
80
- }); };
84
+ };
81
85
  exports.crypt = crypt;
82
- var processItemEncryptDecrypt = function (item, fields, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
83
- var _loop_1, _i, _a, fieldName;
84
- return __generator(this, function (_b) {
85
- switch (_b.label) {
86
- case 0:
87
- if (!(item && typeof item === "object")) return [3 /*break*/, 4];
88
- _loop_1 = function (fieldName) {
89
- var field, _c, _d;
90
- return __generator(this, function (_e) {
91
- switch (_e.label) {
92
- case 0:
93
- field = fields.find(function (field) { return field.fieldName === fieldName; });
94
- if (!(field && field.encrypted)) return [3 /*break*/, 2];
95
- _c = item;
96
- _d = fieldName;
97
- return [4 /*yield*/, exports.processDataEncryptionDecryption(item[fieldName], encryption, decrypt)];
98
- case 1:
99
- _c[_d] = _e.sent();
100
- _e.label = 2;
101
- case 2: return [2 /*return*/];
102
- }
103
- });
104
- };
105
- _i = 0, _a = Object.keys(item);
106
- _b.label = 1;
107
- case 1:
108
- if (!(_i < _a.length)) return [3 /*break*/, 4];
109
- fieldName = _a[_i];
110
- return [5 /*yield**/, _loop_1(fieldName)];
111
- case 2:
112
- _b.sent();
113
- _b.label = 3;
114
- case 3:
115
- _i++;
116
- return [3 /*break*/, 1];
117
- case 4: return [2 /*return*/, item];
118
- }
86
+ var processItemEncryptDecrypt = function (item, fields, encryption, decrypt, encrypedRefCollections) {
87
+ if (encrypedRefCollections === void 0) { encrypedRefCollections = []; }
88
+ return __awaiter(void 0, void 0, void 0, function () {
89
+ var _loop_1, _i, _a, fieldName;
90
+ return __generator(this, function (_b) {
91
+ switch (_b.label) {
92
+ case 0:
93
+ if (!(item && typeof item === "object")) return [3 /*break*/, 4];
94
+ _loop_1 = function (fieldName) {
95
+ var field, _c, _d, refField, _e, _f, _g, _h;
96
+ return __generator(this, function (_j) {
97
+ switch (_j.label) {
98
+ case 0:
99
+ field = fields.find(function (field) { return field.fieldName === fieldName; });
100
+ if (!(field && field.encrypted)) return [3 /*break*/, 2];
101
+ _c = item;
102
+ _d = fieldName;
103
+ return [4 /*yield*/, exports.processDataEncryptionDecryption(item[fieldName], encryption, decrypt)];
104
+ case 1:
105
+ _c[_d] = _j.sent();
106
+ _j.label = 2;
107
+ case 2:
108
+ if (!decrypt) return [3 /*break*/, 6];
109
+ if (![drapcode_constant_1.FieldTypes.reference.id, drapcode_constant_1.FieldTypes.createdBy.id].includes(field === null || field === void 0 ? void 0 : field.type)) return [3 /*break*/, 4];
110
+ refField = field;
111
+ if (!refField.refCollection &&
112
+ field.type === drapcode_constant_1.FieldTypes.createdBy.id) {
113
+ refField.refCollection = {
114
+ collectionField: "userName",
115
+ collectionName: "user",
116
+ };
117
+ }
118
+ _e = item;
119
+ _f = fieldName;
120
+ return [4 /*yield*/, processReferenceItemDecrypt(item[fieldName], encryption, decrypt, refField, encrypedRefCollections)];
121
+ case 3:
122
+ _e[_f] = _j.sent();
123
+ return [3 /*break*/, 6];
124
+ case 4:
125
+ if (!((field === null || field === void 0 ? void 0 : field.type) === drapcode_constant_1.FieldTypes.dynamic_option.id)) return [3 /*break*/, 6];
126
+ _g = item;
127
+ _h = fieldName;
128
+ return [4 /*yield*/, processDynamicFieldDecrypt(item[fieldName], encryption, decrypt, field, encrypedRefCollections)];
129
+ case 5:
130
+ _g[_h] = _j.sent();
131
+ _j.label = 6;
132
+ case 6: return [2 /*return*/];
133
+ }
134
+ });
135
+ };
136
+ _i = 0, _a = Object.keys(item);
137
+ _b.label = 1;
138
+ case 1:
139
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
140
+ fieldName = _a[_i];
141
+ return [5 /*yield**/, _loop_1(fieldName)];
142
+ case 2:
143
+ _b.sent();
144
+ _b.label = 3;
145
+ case 3:
146
+ _i++;
147
+ return [3 /*break*/, 1];
148
+ case 4: return [2 /*return*/, item];
149
+ }
150
+ });
119
151
  });
120
- }); };
152
+ };
121
153
  exports.processItemEncryptDecrypt = processItemEncryptDecrypt;
122
154
  var processDataEncryptionDecryption = function (data, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
123
155
  var encryptionType, dataKey, _a, result, _b;
@@ -150,6 +182,56 @@ var processDataEncryptionDecryption = function (data, encryption, decrypt) { ret
150
182
  });
151
183
  }); };
152
184
  exports.processDataEncryptionDecryption = processDataEncryptionDecryption;
185
+ var processReferenceItemDecrypt = function (data, encryption, decrypt, refField, encrypedRefCollections) { return __awaiter(void 0, void 0, void 0, function () {
186
+ var refCollection, promises;
187
+ return __generator(this, function (_a) {
188
+ switch (_a.label) {
189
+ case 0:
190
+ refCollection = getEncryptedReferenceCollection(encrypedRefCollections, refField).refCollection;
191
+ if (!refCollection) return [3 /*break*/, 2];
192
+ if (!Array.isArray(data)) return [3 /*break*/, 2];
193
+ promises = data.map(function (item) {
194
+ return exports.processItemEncryptDecrypt(item, refCollection.fields, encryption, decrypt);
195
+ });
196
+ return [4 /*yield*/, Promise.all(promises)];
197
+ case 1:
198
+ data = _a.sent();
199
+ _a.label = 2;
200
+ case 2: return [2 /*return*/, data];
201
+ }
202
+ });
203
+ }); };
204
+ var processDynamicFieldDecrypt = function (data, encryption, decrypt, refField, encrypedRefCollections) { return __awaiter(void 0, void 0, void 0, function () {
205
+ var _a, refCollectionFieldName, refCollection, refCollectionField, promises;
206
+ return __generator(this, function (_b) {
207
+ switch (_b.label) {
208
+ case 0:
209
+ _a = getEncryptedReferenceCollection(encrypedRefCollections, refField), refCollectionFieldName = _a.refCollectionFieldName, refCollection = _a.refCollection;
210
+ if (!refCollection) return [3 /*break*/, 2];
211
+ refCollectionField = refCollection.fields.find(function (field) { return field.fieldName === refCollectionFieldName; });
212
+ if (!(refCollectionField && refCollectionField.encrypted)) return [3 /*break*/, 2];
213
+ if (!Array.isArray(data)) return [3 /*break*/, 2];
214
+ promises = data.map(function (item) {
215
+ return exports.processDataEncryptionDecryption(item, encryption, decrypt);
216
+ });
217
+ return [4 /*yield*/, Promise.all(promises)];
218
+ case 1:
219
+ data = _b.sent();
220
+ _b.label = 2;
221
+ case 2: return [2 /*return*/, data];
222
+ }
223
+ });
224
+ }); };
225
+ var getEncryptedReferenceCollection = function (encrypedRefCollections, refField) {
226
+ var _a = refField === null || refField === void 0 ? void 0 : refField.refCollection, refCollectionName = _a.collectionName, refCollectionFieldName = _a.collectionField;
227
+ var refCollection;
228
+ if (encrypedRefCollections.length) {
229
+ if (refCollectionName) {
230
+ refCollection = encrypedRefCollections.find(function (encColl) { return encColl.collectionName === refCollectionName; });
231
+ }
232
+ }
233
+ return { refCollectionName: refCollectionName, refCollectionFieldName: refCollectionFieldName, refCollection: refCollection };
234
+ };
153
235
  /**
154
236
  * This method is used to generate private KEY for KMS ARN
155
237
  * It requires KMS config and ARN
@@ -0,0 +1 @@
1
+ export declare const getEncryptedReferenceFieldsQuery: (collectionFields: any, projectId: any) => any;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
3
+ for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
4
+ to[j] = from[i];
5
+ return to;
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.getEncryptedReferenceFieldsQuery = void 0;
9
+ var drapcode_constant_1 = require("drapcode-constant");
10
+ var getEncryptedReferenceFieldsQuery = function (collectionFields, projectId) {
11
+ var collectionsNamesArr = [];
12
+ collectionFields.map(function (field) {
13
+ if (__spreadArray(__spreadArray([], drapcode_constant_1.onlyReferenceField), [
14
+ drapcode_constant_1.FieldTypes.dynamic_option.id,
15
+ drapcode_constant_1.FieldTypes.createdBy.id,
16
+ ]).includes(field.type)) {
17
+ if (!field.refCollection && field.type === drapcode_constant_1.FieldTypes.createdBy.id) {
18
+ collectionsNamesArr.push("user");
19
+ }
20
+ else {
21
+ var collectionName = field.refCollection.collectionName;
22
+ collectionsNamesArr.push(collectionName);
23
+ }
24
+ }
25
+ });
26
+ var query = [
27
+ {
28
+ $match: {
29
+ projectId: projectId,
30
+ collectionName: { $in: __spreadArray([], collectionsNamesArr) },
31
+ fields: { $elemMatch: { encrypted: true } },
32
+ },
33
+ },
34
+ {
35
+ $project: {
36
+ _id: 0,
37
+ collectionName: 1,
38
+ fields: {
39
+ $filter: {
40
+ input: "$fields",
41
+ as: "item",
42
+ cond: { $eq: ["$$item.encrypted", true] },
43
+ },
44
+ },
45
+ },
46
+ },
47
+ ];
48
+ return query;
49
+ };
50
+ exports.getEncryptedReferenceFieldsQuery = getEncryptedReferenceFieldsQuery;
package/build/index.d.ts CHANGED
@@ -15,5 +15,6 @@ export * from "./utils/check-error";
15
15
  export * from "./utils/prepare-query";
16
16
  export * from "./utils/s3-util";
17
17
  export * from "./encryption/index";
18
+ export * from "./encryption/utility";
18
19
  export * from "./format-fields/index";
19
20
  export * from "./utils/token";
package/build/index.js CHANGED
@@ -27,5 +27,6 @@ __exportStar(require("./utils/check-error"), exports);
27
27
  __exportStar(require("./utils/prepare-query"), exports);
28
28
  __exportStar(require("./utils/s3-util"), exports);
29
29
  __exportStar(require("./encryption/index"), exports);
30
+ __exportStar(require("./encryption/utility"), exports);
30
31
  __exportStar(require("./format-fields/index"), exports);
31
32
  __exportStar(require("./utils/token"), exports);
@@ -112,6 +112,12 @@ export declare const prepareCollectionQuery: (matchQuery: object, filterId: stri
112
112
  })[];
113
113
  };
114
114
  };
115
+ permissionLevelSecurity: {
116
+ $first: string;
117
+ };
118
+ rowLevelSecurityFilter: {
119
+ $first: string;
120
+ };
115
121
  };
116
122
  $match?: undefined;
117
123
  $addFields?: undefined;
@@ -132,6 +138,8 @@ export declare const prepareCollectionQuery: (matchQuery: object, filterId: stri
132
138
  projectId: number;
133
139
  isPrivate: number;
134
140
  fields: number;
141
+ permissionLevelSecurity: number;
142
+ rowLevelSecurityFilter: number;
135
143
  };
136
144
  $match?: undefined;
137
145
  $addFields?: undefined;
@@ -74,6 +74,8 @@ var prepareCollectionQuery = function (matchQuery, filterId) {
74
74
  ],
75
75
  },
76
76
  },
77
+ permissionLevelSecurity: { $first: "$permissionLevelSecurity" },
78
+ rowLevelSecurityFilter: { $first: "$rowLevelSecurityFilter" },
77
79
  },
78
80
  },
79
81
  {
@@ -91,6 +93,8 @@ var prepareCollectionQuery = function (matchQuery, filterId) {
91
93
  projectId: 1,
92
94
  isPrivate: 1,
93
95
  fields: 1,
96
+ permissionLevelSecurity: 1,
97
+ rowLevelSecurityFilter: 1,
94
98
  },
95
99
  },
96
100
  ];
@@ -1,4 +1,4 @@
1
- export declare const queryParser: (collectionName: string, query: any, constants: any[], externalParams: any, currentUser: any, timezone: string, searchObj?: any, refCollectionFieldsInItems?: any, searchQueryTypeObj?: any, currentTenant?: any, currentUserSetting?: any, lookupConfig?: any) => Promise<string>;
1
+ export declare const queryParser: (collectionName: string, query: any, constants: any[], externalParams: any, currentUser: any, timezone: string, searchObj: any, refCollectionFieldsInItems: any, searchQueryTypeObj: any, currentTenant: any, currentUserSetting: any, lookupConfig: any, rowLevelSecurityFilter: any) => Promise<string>;
2
2
  export declare const isEntityInCondition: (value: any) => boolean;
3
3
  export declare const getMinMaxValue: (value: any) => {
4
4
  startValue: any;
@@ -50,7 +50,7 @@ var drapcode_constant_1 = require("drapcode-constant");
50
50
  var date_util_1 = require("./date-util");
51
51
  var util_1 = require("./util");
52
52
  var prepare_query_1 = require("./prepare-query");
53
- var queryParser = function (collectionName, query, constants, externalParams, currentUser, timezone, searchObj, refCollectionFieldsInItems, searchQueryTypeObj, currentTenant, currentUserSetting, lookupConfig) {
53
+ var queryParser = function (collectionName, query, constants, externalParams, currentUser, timezone, searchObj, refCollectionFieldsInItems, searchQueryTypeObj, currentTenant, currentUserSetting, lookupConfig, rowLevelSecurityFilter) {
54
54
  if (currentUser === void 0) { currentUser = {}; }
55
55
  if (searchObj === void 0) { searchObj = null; }
56
56
  if (refCollectionFieldsInItems === void 0) { refCollectionFieldsInItems = null; }
@@ -59,10 +59,10 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
59
59
  if (currentUserSetting === void 0) { currentUserSetting = {}; }
60
60
  if (lookupConfig === void 0) { lookupConfig = []; }
61
61
  return __awaiter(void 0, void 0, void 0, function () {
62
- var filterQuery, aggregateQuery, queryStr, searchQueryObj, _a, searchAggregateQuery, likeQuery, finalQuery, _b, searchAggregateQuery, likeQuery, projection, finder, sortBy, orderBy, offset, limit, direction, str;
63
- var _c, _d;
64
- return __generator(this, function (_e) {
65
- switch (_e.label) {
62
+ var filterQuery, rlsFilterQuery, aggregateQuery, queryStr, searchQueryObj, _a, searchAggregateQuery, likeQuery, finalQuery, _b, searchAggregateQuery, likeQuery, projection, finder, sortBy, orderBy, offset, limit, direction, str;
63
+ var _c, _d, _e;
64
+ return __generator(this, function (_f) {
65
+ switch (_f.label) {
66
66
  case 0:
67
67
  if (!collectionName)
68
68
  return [2 /*return*/, ""];
@@ -80,6 +80,13 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
80
80
  console.log("searchQueryTypeObj", JSON.stringify(searchQueryTypeObj));
81
81
  filterQuery = mongoFilterQuery(query, externalParams, constants, currentUser, currentTenant, currentUserSetting, timezone);
82
82
  console.log("filterQuery", JSON.stringify(filterQuery));
83
+ rlsFilterQuery = [];
84
+ if (rowLevelSecurityFilter && rowLevelSecurityFilter.conditions.length) {
85
+ rlsFilterQuery = mongoFilterQuery(rowLevelSecurityFilter, externalParams, constants, currentUser, currentTenant, currentUserSetting, timezone);
86
+ console.log("**************************");
87
+ console.log("rlsFilterQuery", JSON.stringify(rlsFilterQuery));
88
+ }
89
+ console.log("==> searchQueryTypeObj :>>", searchQueryTypeObj);
83
90
  aggregateQuery = [];
84
91
  queryStr = ".aggregate([])";
85
92
  if (filterQuery.length &&
@@ -97,8 +104,13 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
97
104
  console.log("aggregateQuery After search object condition", JSON.stringify(aggregateQuery));
98
105
  console.log("searchQueryObj", JSON.stringify(searchQueryObj));
99
106
  finalQuery = { $and: filterQuery };
107
+ if (rlsFilterQuery.length &&
108
+ ((rlsFilterQuery[0].$or && rlsFilterQuery[0].$or.length) ||
109
+ (rlsFilterQuery[0].$and && rlsFilterQuery[0].$and.length))) {
110
+ (_c = finalQuery.$and).push.apply(_c, rlsFilterQuery);
111
+ }
100
112
  if (searchQueryObj) {
101
- (_c = finalQuery.$and).push.apply(_c, searchQueryObj);
113
+ (_d = finalQuery.$and).push.apply(_d, searchQueryObj);
102
114
  }
103
115
  console.log("finalQuery after", JSON.stringify(finalQuery));
104
116
  aggregateQuery.push({ $match: finalQuery });
@@ -121,8 +133,8 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
121
133
  return prepare_query_1.commonLookupSetting(field, lookupConfig, aggregateQuery);
122
134
  }))];
123
135
  case 1:
124
- _e.sent();
125
- _e.label = 2;
136
+ _f.sent();
137
+ _f.label = 2;
126
138
  case 2:
127
139
  if (finder != "COUNT" && !util_1.isEmpty(projection)) {
128
140
  aggregateQuery.push({ $project: projection });
@@ -138,7 +150,7 @@ var queryParser = function (collectionName, query, constants, externalParams, cu
138
150
  }
139
151
  if (finder != "COUNT" && sortBy) {
140
152
  direction = orderBy && orderBy == "asc" ? 1 : -1;
141
- aggregateQuery.push({ $sort: (_d = {}, _d[sortBy] = direction, _d._id = 1, _d) });
153
+ aggregateQuery.push({ $sort: (_e = {}, _e[sortBy] = direction, _e._id = 1, _e) });
142
154
  }
143
155
  if (finder != "COUNT" && limit) {
144
156
  aggregateQuery.push({ $skip: +offset }, { $limit: +limit });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-utility",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",