Skip to content

Commit c9cca0a

Browse files
authored
Merge pull request #36 from rfeelin/feature/removing-sonar-issues-pt2
Cleaning up some code duplication from sonar
2 parents 2514a92 + 3909ef7 commit c9cca0a

File tree

5 files changed

+68
-85
lines changed

5 files changed

+68
-85
lines changed

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -228,32 +228,7 @@ function isValidConfig(config) {
228228
"encryptedKeyHeaderName",
229229
"oaepHashingAlgorithmHeaderName",
230230
];
231-
const contains = (props) => {
232-
return props.every((elem) => {
233-
return config[elem] !== null && typeof config[elem] !== "undefined";
234-
});
235-
};
236-
if (typeof config !== "object" || config === null) {
237-
throw Error("Config not valid: config should be an object.");
238-
}
239-
if (
240-
config["paths"] === null ||
241-
typeof config["paths"] === "undefined" ||
242-
!(config["paths"] instanceof Array)
243-
) {
244-
throw Error("Config not valid: paths should be an array of path element.");
245-
}
246-
if (
247-
!contains(propertiesBasic) ||
248-
(!contains(propertiesField) && !contains(propertiesHeader))
249-
) {
250-
throw Error(
251-
"Config not valid: please check that all the properties are defined."
252-
);
253-
}
254-
if (config["paths"].length === 0) {
255-
throw Error("Config not valid: paths should be not empty.");
256-
}
231+
const contains = utils.checkConfigFieldsArePopulated(config, propertiesBasic, propertiesField, propertiesHeader);
257232
if (config["dataEncoding"] !== c.HEX && config["dataEncoding"] !== c.BASE64) {
258233
throw Error("Config not valid: dataEncoding should be 'hex' or 'base64'");
259234
}

lib/mcapi/crypto/jwe-crypto.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,9 @@ function isValidConfig(config) {
222222
throw Error("JWE Encryption is only supported on Node 13+");
223223
}
224224
const propertiesBasic = ["encryptionCertificate", "encryptedValueFieldName"];
225-
const contains = (props) => {
226-
return props.every((elem) => {
227-
return config[elem] !== null && typeof config[elem] !== "undefined";
228-
});
229-
};
230-
if (typeof config !== "object" || config === null) {
231-
throw Error("Config not valid: config should be an object.");
232-
}
233-
if (
234-
config["paths"] === null ||
235-
typeof config["paths"] === "undefined" ||
236-
!(config["paths"] instanceof Array)
237-
) {
238-
throw Error("Config not valid: paths should be an array of path element.");
239-
}
240-
if (!contains(propertiesBasic)) {
241-
throw Error(
242-
"Config not valid: please check that all the properties are defined."
243-
);
244-
}
245-
if (config["paths"].length === 0) {
246-
throw Error("Config not valid: paths should be not empty.");
247-
}
248-
validateFingerprint(config, contains);
225+
const contains = utils.checkConfigFieldsArePopulated(config, propertiesBasic);
249226
utils.validateRootMapping(config);
227+
validateFingerprint(config, contains);
250228
}
251229

252230
/**

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ function encrypt(endpoint, header, body) {
6565
}
6666
return {
6767
header: header,
68-
body: fleConfig
69-
? utils.computeBody(fleConfig.toEncrypt, body, bodyMap)
70-
: body,
68+
body: fleConfig ? utils.computeBody(fleConfig.toEncrypt, body, bodyMap) : body,
7169
};
7270
}
7371

@@ -91,9 +89,7 @@ function decrypt(response) {
9189
}
9290
});
9391
}
94-
return fleConfig
95-
? utils.computeBody(fleConfig.toDecrypt, body, bodyMap)
96-
: body;
92+
return fleConfig ? utils.computeBody(fleConfig.toDecrypt, body, bodyMap) : body;
9793
}
9894

9995
/**
@@ -191,9 +187,7 @@ function decryptWithHeader(path, body, response) {
191187
response.header[this.config.oaepHashingAlgorithmHeaderName],
192188
response.header[this.config.encryptedKeyHeaderName]
193189
);
194-
return utils.isJsonRoot(path.obj)
195-
? decrypted
196-
: Object.assign(body, decrypted);
190+
return utils.isJsonRoot(path.obj) ? decrypted : Object.assign(body, decrypted);
197191
}
198192
}
199193

lib/mcapi/encryption/jwe-encryption.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ function encrypt(endpoint, header, body) {
3333
}
3434
return {
3535
header: header,
36-
body: fleConfig
37-
? utils.computeBody(fleConfig.toEncrypt, body, bodyMap)
38-
: body,
36+
body: fleConfig ? utils.computeBody(fleConfig.toEncrypt, body, bodyMap) : body,
3937
};
4038
}
4139

@@ -55,9 +53,7 @@ function decrypt(response) {
5553
return decryptBody.call(this, v, body);
5654
});
5755
}
58-
return fleConfig
59-
? utils.computeBody(fleConfig.toDecrypt, body, bodyMap)
60-
: body;
56+
return fleConfig ? utils.computeBody(fleConfig.toDecrypt, body, bodyMap) : body;
6157
}
6258

6359
/**

lib/mcapi/utils/utils.js

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,62 @@ module.exports.nodeVersionSupportsJWE = function () {
313313
return nodeMajorVersion > 12;
314314
};
315315

316-
module.exports.hasConfig = function (config, endpoint) {
316+
module.exports.checkConfigFieldsArePopulated = function(config, propertiesBasic, propertiesField, propertiesHeader) {
317+
const contains = (props) => {
318+
return props.every((elem) => {
319+
return config[elem] !== null && typeof config[elem] !== "undefined";
320+
});
321+
};
322+
if (typeof config !== "object" || config === null) {
323+
throw Error("Config not valid: config should be an object.");
324+
}
325+
if (
326+
config["paths"] === null ||
327+
typeof config["paths"] === "undefined" ||
328+
!(config["paths"] instanceof Array)
329+
) {
330+
throw Error("Config not valid: paths should be an array of path element.");
331+
}
332+
if (
333+
!contains(propertiesBasic)
334+
) {
335+
throw Error(
336+
"Config not valid: please check that all the properties are defined."
337+
);
338+
}
339+
if (
340+
propertiesField && propertiesHeader && !contains(propertiesField) && !contains(propertiesHeader)
341+
) {
342+
throw Error(
343+
"Config not valid: please check that all the properties are defined."
344+
);
345+
}
346+
if (config["paths"].length === 0) {
347+
throw Error("Config not valid: paths should be not empty.");
348+
}
349+
return contains;
350+
};
351+
352+
module.exports.validateRootMapping = function(config) {
353+
function multipleRoots(elem) {
354+
return (
355+
elem.length !== 1 &&
356+
elem.some((item) => {
357+
return item.obj === "$" || item.element === "$";
358+
})
359+
);
360+
}
361+
362+
config.paths.forEach((path) => {
363+
if (multipleRoots(path.toEncrypt) || multipleRoots(path.toDecrypt)) {
364+
throw Error(
365+
"Config not valid: found multiple configurations encrypt/decrypt with root mapping"
366+
);
367+
}
368+
});
369+
};
370+
371+
module.exports.hasConfig = function(config, endpoint) {
317372
if (config && endpoint) {
318373
endpoint = endpoint.split("?").shift();
319374
const conf = config.paths.find((elem) => {
@@ -344,10 +399,11 @@ module.exports.elemFromPath = function (path, obj) {
344399
}
345400
};
346401

347-
module.exports.isJsonRoot = function (elem) {
402+
module.exports.isJsonRoot = function(elem) {
348403
return isJsonRoot(elem);
349404
};
350-
function isJsonRoot(elem) {
405+
406+
function isJsonRoot(elem){
351407
return elem === "$";
352408
}
353409

@@ -359,21 +415,5 @@ function hasEncryptionParam(encParams, bodyMap) {
359415
return encParams && encParams.length === 1 && bodyMap && bodyMap[0];
360416
}
361417

362-
module.exports.validateRootMapping = function (config) {
363-
function multipleRoots(elem) {
364-
return (
365-
elem.length !== 1 &&
366-
elem.some((item) => {
367-
return item.obj === "$" || item.element === "$";
368-
})
369-
);
370-
}
371418

372-
config.paths.forEach((path) => {
373-
if (multipleRoots(path.toEncrypt) || multipleRoots(path.toDecrypt)) {
374-
throw Error(
375-
"Config not valid: found multiple configurations encrypt/decrypt with root mapping"
376-
);
377-
}
378-
});
379-
};
419+

0 commit comments

Comments
 (0)