Skip to content

Commit a9f8a5f

Browse files
authored
Merge pull request #35 from rfeelin/feature/removing-sonar-issues
Removing Sonar Issues
2 parents 7a055a9 + 1d62489 commit a9f8a5f

File tree

8 files changed

+140
-249
lines changed

8 files changed

+140
-249
lines changed

lib/mcapi/crypto/field-level-crypto.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ function isValidConfig(config) {
258258
throw Error("Config not valid: dataEncoding should be 'hex' or 'base64'");
259259
}
260260
validateFingerprint(config, contains);
261-
validateRootMapping(config);
261+
utils.validateRootMapping(config);
262262
}
263263

264264
function validateFingerprint(config, contains) {
@@ -280,23 +280,4 @@ function validateFingerprint(config, contains) {
280280
}
281281
}
282282

283-
function validateRootMapping(config) {
284-
function multipleRoots(elem) {
285-
return (
286-
elem.length !== 1 &&
287-
elem.some((item) => {
288-
return item.obj === "$" || item.element === "$";
289-
})
290-
);
291-
}
292-
293-
config.paths.forEach((path) => {
294-
if (multipleRoots(path.toEncrypt) || multipleRoots(path.toDecrypt)) {
295-
throw Error(
296-
"Config not valid: found multiple configurations encrypt/decrypt with root mapping"
297-
);
298-
}
299-
});
300-
}
301-
302283
module.exports = FieldLevelCrypto;

lib/mcapi/crypto/jwe-crypto.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function isValidConfig(config) {
246246
throw Error("Config not valid: paths should be not empty.");
247247
}
248248
validateFingerprint(config, contains);
249-
validateRootMapping(config);
249+
utils.validateRootMapping(config);
250250
}
251251

252252
/**
@@ -266,28 +266,6 @@ function validateFingerprint(config, contains) {
266266
}
267267
}
268268

269-
/**
270-
* @private
271-
*/
272-
function validateRootMapping(config) {
273-
function multipleRoots(elem) {
274-
return (
275-
elem.length !== 1 &&
276-
elem.some((item) => {
277-
return item.obj === "$" || item.element === "$";
278-
})
279-
);
280-
}
281-
282-
config.paths.forEach((path) => {
283-
if (multipleRoots(path.toEncrypt) || multipleRoots(path.toDecrypt)) {
284-
throw Error(
285-
"Config not valid: found multiple configurations encrypt/decrypt with root mapping"
286-
);
287-
}
288-
});
289-
}
290-
291269
/**
292270
* @private
293271
*/

lib/mcapi/encryption/field-level-encryption.js

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,6 @@ function FieldLevelEncryption(config) {
2424
];
2525
}
2626

27-
/**
28-
* @private
29-
*/
30-
function hasConfig(config, endpoint) {
31-
if (config && endpoint) {
32-
endpoint = endpoint.split("?").shift();
33-
const conf = config.paths.find((elem) => {
34-
const regex = new RegExp(elem.path, "g");
35-
return endpoint.match(regex);
36-
});
37-
return conf ? conf : null;
38-
}
39-
return null;
40-
}
41-
42-
/**
43-
* @private
44-
*/
45-
function elemFromPath(path, obj) {
46-
try {
47-
let parent = null;
48-
const paths = path.split(".");
49-
if (path && paths.length > 0) {
50-
paths.forEach((e) => {
51-
parent = obj;
52-
obj = isJsonRoot(e) ? obj : obj[e];
53-
});
54-
}
55-
return {
56-
node: obj,
57-
parent: parent,
58-
};
59-
} catch (e) {
60-
return null;
61-
}
62-
}
63-
6427
/**
6528
* Set encryption header parameters
6629
*
@@ -86,7 +49,7 @@ function setHeader(header, params) {
8649
*/
8750
function encrypt(endpoint, header, body) {
8851
let bodyMap = body;
89-
const fleConfig = hasConfig(this.config, endpoint);
52+
const fleConfig = utils.hasConfig(this.config, endpoint);
9053
if (fleConfig) {
9154
if (!this.isWithHeader) {
9255
bodyMap = fleConfig.toEncrypt.map((v) => {
@@ -102,7 +65,9 @@ function encrypt(endpoint, header, body) {
10265
}
10366
return {
10467
header: header,
105-
body: fleConfig ? computeBody(fleConfig.toEncrypt, body, bodyMap) : body,
68+
body: fleConfig
69+
? utils.computeBody(fleConfig.toEncrypt, body, bodyMap)
70+
: body,
10671
};
10772
}
10873

@@ -116,7 +81,7 @@ function encrypt(endpoint, header, body) {
11681
function decrypt(response) {
11782
const body = response.body;
11883
let bodyMap = response.body;
119-
const fleConfig = hasConfig(this.config, response.request.url);
84+
const fleConfig = utils.hasConfig(this.config, response.request.url);
12085
if (fleConfig) {
12186
bodyMap = fleConfig.toDecrypt.map((v) => {
12287
if (!this.isWithHeader) {
@@ -126,7 +91,9 @@ function decrypt(response) {
12691
}
12792
});
12893
}
129-
return fleConfig ? computeBody(fleConfig.toDecrypt, body, bodyMap) : body;
94+
return fleConfig
95+
? utils.computeBody(fleConfig.toDecrypt, body, bodyMap)
96+
: body;
13097
}
13198

13299
/**
@@ -137,13 +104,13 @@ function decrypt(response) {
137104
* @param body Body to encrypt
138105
*/
139106
function encryptBody(path, body) {
140-
const elem = elemFromPath(path.element, body);
107+
const elem = utils.elemFromPath(path.element, body);
141108
if (elem && elem.node) {
142109
const encryptedData = this.crypto.encryptData({ data: elem.node });
143110
body = utils.mutateObjectProperty(path.obj, encryptedData, body);
144111
// delete encrypted field if not overridden
145112
if (
146-
!isJsonRoot(path.obj) &&
113+
!utils.isJsonRoot(path.obj) &&
147114
path.element !== path.obj + "." + this.config.encryptedValueFieldName
148115
) {
149116
utils.deleteNode(path.element, body);
@@ -162,13 +129,13 @@ function encryptBody(path, body) {
162129
* @returns {Object} Encrypted body
163130
*/
164131
function encryptWithHeader(encParams, path, body) {
165-
const elem = elemFromPath(path.element, body).node;
132+
const elem = utils.elemFromPath(path.element, body).node;
166133
const encrypted = this.crypto.encryptData({ data: elem }, encParams);
167134
const data = {
168135
[this.config.encryptedValueFieldName]:
169136
encrypted[this.config.encryptedValueFieldName],
170137
};
171-
return isJsonRoot(path.obj) ? data : { [path.obj]: data };
138+
return utils.isJsonRoot(path.obj) ? data : { [path.obj]: data };
172139
}
173140

174141
/**
@@ -180,7 +147,7 @@ function encryptWithHeader(encParams, path, body) {
180147
* @returns {Object} Decrypted body
181148
*/
182149
function decryptBody(path, body) {
183-
const elem = elemFromPath(path.element, body);
150+
const elem = utils.elemFromPath(path.element, body);
184151
if (elem && elem.node) {
185152
const decryptedObj = this.crypto.decryptData(
186153
elem.node[this.config.encryptedValueFieldName], // encrypted data
@@ -208,8 +175,8 @@ function decryptBody(path, body) {
208175
* @param response Response with header to update
209176
*/
210177
function decryptWithHeader(path, body, response) {
211-
const elemEncryptedNode = elemFromPath(path.obj, body);
212-
const node = isJsonRoot(path.element)
178+
const elemEncryptedNode = utils.elemFromPath(path.obj, body);
179+
const node = utils.isJsonRoot(path.element)
213180
? elemEncryptedNode.node
214181
: elemEncryptedNode.node[path.element];
215182
if (node) {
@@ -224,20 +191,10 @@ function decryptWithHeader(path, body, response) {
224191
response.header[this.config.oaepHashingAlgorithmHeaderName],
225192
response.header[this.config.encryptedKeyHeaderName]
226193
);
227-
return isJsonRoot(path.obj) ? decrypted : Object.assign(body, decrypted);
194+
return utils.isJsonRoot(path.obj)
195+
? decrypted
196+
: Object.assign(body, decrypted);
228197
}
229198
}
230199

231-
function isJsonRoot(elem) {
232-
return elem === "$";
233-
}
234-
235-
function computeBody(configParam, body, bodyMap) {
236-
return hasEncryptionParam(configParam, bodyMap) ? bodyMap.pop() : body;
237-
}
238-
239-
function hasEncryptionParam(encParams, bodyMap) {
240-
return encParams && encParams.length === 1 && bodyMap && bodyMap[0];
241-
}
242-
243200
module.exports = FieldLevelEncryption;

lib/mcapi/encryption/jwe-encryption.js

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,6 @@ function JweEncryption(config) {
1515
this.crypto = new JweCrypto(config);
1616
}
1717

18-
/**
19-
* @private
20-
*/
21-
function hasConfig(config, endpoint) {
22-
if (config && endpoint) {
23-
endpoint = endpoint.split("?").shift();
24-
const conf = config.paths.find((elem) => {
25-
const regex = new RegExp(elem.path, "g");
26-
return endpoint.match(regex);
27-
});
28-
return conf ? conf : null;
29-
}
30-
return null;
31-
}
32-
33-
/**
34-
* @private
35-
*/
36-
function elemFromPath(path, obj) {
37-
try {
38-
let parent = null;
39-
const paths = path.split(".");
40-
if (path && paths.length > 0) {
41-
paths.forEach((e) => {
42-
parent = obj;
43-
obj = isJsonRoot(e) ? obj : obj[e];
44-
});
45-
}
46-
return {
47-
node: obj,
48-
parent: parent,
49-
};
50-
} catch (e) {
51-
return null;
52-
}
53-
}
54-
5518
/**
5619
* Encrypt parts of a HTTP request using the given config
5720
*
@@ -62,15 +25,17 @@ function elemFromPath(path, obj) {
6225
*/
6326
function encrypt(endpoint, header, body) {
6427
let bodyMap = body;
65-
const fleConfig = hasConfig(this.config, endpoint);
28+
const fleConfig = utils.hasConfig(this.config, endpoint);
6629
if (fleConfig) {
6730
bodyMap = fleConfig.toEncrypt.map((v) => {
6831
return encryptBody.call(this, v, body);
6932
});
7033
}
7134
return {
7235
header: header,
73-
body: fleConfig ? computeBody(fleConfig.toEncrypt, body, bodyMap) : body,
36+
body: fleConfig
37+
? utils.computeBody(fleConfig.toEncrypt, body, bodyMap)
38+
: body,
7439
};
7540
}
7641

@@ -84,13 +49,15 @@ function encrypt(endpoint, header, body) {
8449
function decrypt(response) {
8550
const body = response.body;
8651
let bodyMap = response.body;
87-
const fleConfig = hasConfig(this.config, response.request.url);
52+
const fleConfig = utils.hasConfig(this.config, response.request.url);
8853
if (fleConfig) {
8954
bodyMap = fleConfig.toDecrypt.map((v) => {
9055
return decryptBody.call(this, v, body);
9156
});
9257
}
93-
return fleConfig ? computeBody(fleConfig.toDecrypt, body, bodyMap) : body;
58+
return fleConfig
59+
? utils.computeBody(fleConfig.toDecrypt, body, bodyMap)
60+
: body;
9461
}
9562

9663
/**
@@ -101,13 +68,13 @@ function decrypt(response) {
10168
* @param body Body to encrypt
10269
*/
10370
function encryptBody(path, body) {
104-
const elem = elemFromPath(path.element, body);
71+
const elem = utils.elemFromPath(path.element, body);
10572
if (elem && elem.node) {
10673
const encryptedData = this.crypto.encryptData({ data: elem.node });
10774
body = utils.mutateObjectProperty(path.obj, encryptedData, body);
10875
// delete encrypted field if not overridden
10976
if (
110-
!isJsonRoot(path.obj) &&
77+
!utils.isJsonRoot(path.obj) &&
11178
path.element !== path.obj + "." + this.config.encryptedValueFieldName
11279
) {
11380
utils.deleteNode(path.element, body);
@@ -125,7 +92,7 @@ function encryptBody(path, body) {
12592
* @returns {Object} Decrypted body
12693
*/
12794
function decryptBody(path, body) {
128-
const elem = elemFromPath(path.element, body);
95+
const elem = utils.elemFromPath(path.element, body);
12996
if (elem && elem.node) {
13097
const decryptedObj = this.crypto.decryptData(
13198
elem.node[this.config.encryptedValueFieldName]
@@ -141,16 +108,4 @@ function decryptBody(path, body) {
141108
return body;
142109
}
143110

144-
function isJsonRoot(elem) {
145-
return elem === "$";
146-
}
147-
148-
function computeBody(configParam, body, bodyMap) {
149-
return hasEncryptionParam(configParam, bodyMap) ? bodyMap.pop() : body;
150-
}
151-
152-
function hasEncryptionParam(encParams, bodyMap) {
153-
return encParams && encParams.length === 1 && bodyMap && bodyMap[0];
154-
}
155-
156111
module.exports = JweEncryption;

0 commit comments

Comments
 (0)