Skip to content

Commit f4d2d33

Browse files
authored
Merge pull request #16 from Mastercard/feature/code-cleanup
Code cleanup
2 parents 70eb728 + 7c4cb85 commit f4d2d33

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

lib/mcapi/crypto/crypto.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function Crypto(config) {
4949
* @param encryptionParams encryption parameters
5050
*/
5151
this.encryptData = function (options, encryptionParams) {
52-
let data = utils.jsonToString(options.data);
52+
const data = utils.jsonToString(options.data);
5353

5454
// Generate encryption params
5555
const enc = encryptionParams || this.newEncryptionParams(options);
@@ -173,7 +173,7 @@ function generateSecretKey(algorithm, size, digest) {
173173
* @private
174174
*/
175175
function loadPrivateKey(privateKeyPath) {
176-
let privateKeyContent = fs.readFileSync(privateKeyPath, 'binary');
176+
const privateKeyContent = fs.readFileSync(privateKeyPath, 'binary');
177177

178178
if (!privateKeyContent || privateKeyContent.length <= 1) {
179179
throw new Error('Private key content not valid');
@@ -186,7 +186,7 @@ function loadPrivateKey(privateKeyPath) {
186186
* @private
187187
*/
188188
function readPublicCertificate(publicCertificatePath) {
189-
let certificateContent = fs.readFileSync(publicCertificatePath);
189+
const certificateContent = fs.readFileSync(publicCertificatePath);
190190
if (!certificateContent || certificateContent.length <= 1) {
191191
throw new Error('Public certificate content is not valid');
192192
}
@@ -197,7 +197,7 @@ function readPublicCertificate(publicCertificatePath) {
197197
* @private
198198
*/
199199
function getPrivateKey(p12Path, alias, password) {
200-
let p12Content = fs.readFileSync(p12Path, 'binary');
200+
const p12Content = fs.readFileSync(p12Path, 'binary');
201201

202202
if (!p12Content || p12Content.length <= 1) {
203203
throw new Error('p12 keystore content is empty');
@@ -212,13 +212,13 @@ function getPrivateKey(p12Path, alias, password) {
212212
}
213213

214214
// Get asn1 from DER
215-
let p12Asn1 = forge.asn1.fromDer(p12Content, false);
215+
const p12Asn1 = forge.asn1.fromDer(p12Content, false);
216216

217217
// Get p12 using the password
218-
let p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
218+
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
219219

220220
// Get Key from p12
221-
let keyObj = p12.getBags({
221+
const keyObj = p12.getBags({
222222
friendlyName: alias,
223223
bagType: forge.pki.oids.pkcs8ShroudedKeyBag
224224
}).friendlyName[0];
@@ -296,12 +296,12 @@ function createMessageDigest(digest) {
296296
* @private
297297
*/
298298
function isValidConfig(config) {
299-
let propertiesBasic = ["oaepPaddingDigestAlgorithm", "dataEncoding", "encryptionCertificate", "encryptedValueFieldName"];
300-
let propertiesField = ["ivFieldName", "encryptedKeyFieldName"];
301-
let propertiesHeader = ["ivHeaderName", "encryptedKeyHeaderName", "oaepHashingAlgorithmHeaderName"];
302-
let propertiesFingerprint = ["publicKeyFingerprintType", "publicKeyFingerprintFieldName", "publicKeyFingerprintHeaderName"];
303-
let propertiesOptionalFingerprint = ["publicKeyFingerprint"];
304-
let contains = (props) => {
299+
const propertiesBasic = ["oaepPaddingDigestAlgorithm", "dataEncoding", "encryptionCertificate", "encryptedValueFieldName"];
300+
const propertiesField = ["ivFieldName", "encryptedKeyFieldName"];
301+
const propertiesHeader = ["ivHeaderName", "encryptedKeyHeaderName", "oaepHashingAlgorithmHeaderName"];
302+
const propertiesFingerprint = ["publicKeyFingerprintType", "publicKeyFingerprintFieldName", "publicKeyFingerprintHeaderName"];
303+
const propertiesOptionalFingerprint = ["publicKeyFingerprint"];
304+
const contains = (props) => {
305305
return props.every((elem) => {
306306
return config[elem] !== null && typeof config[elem] !== "undefined";
307307
});

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ function FieldLevelEncryption(config) {
2424
function hasConfig(config, endpoint) {
2525
if (config && endpoint) {
2626
endpoint = endpoint.split("?").shift();
27-
let conf = config.paths.find((elem) => {
27+
const conf = config.paths.find((elem) => {
2828
// TODO grep from last index
29-
let regex = new RegExp(elem.path, "g");
29+
const regex = new RegExp(elem.path, "g");
3030
return endpoint.match(regex);
3131
}
3232
);
@@ -41,7 +41,7 @@ function hasConfig(config, endpoint) {
4141
function elemFromPath(path, obj) {
4242
try {
4343
let parent = null;
44-
let paths = path.split(".");
44+
const paths = path.split(".");
4545
if (path && paths.length > 0) {
4646
paths.forEach((e) => {
4747
parent = obj;
@@ -88,7 +88,7 @@ function encrypt(endpoint, header, body) {
8888
encryptBody.call(this, v, body);
8989
});
9090
} else {
91-
let encParams = this.crypto.newEncryptionParams({});
91+
const encParams = this.crypto.newEncryptionParams({});
9292
fleConfig.toEncrypt.forEach((v) => {
9393
body = encryptWithHeader.call(this, encParams, v, body);
9494
});
@@ -133,9 +133,9 @@ function decrypt(response) {
133133
* @param body Body to encrypt
134134
*/
135135
function encryptBody(path, body) {
136-
let elem = elemFromPath(path.element, body);
136+
const elem = elemFromPath(path.element, body);
137137
if (elem && elem.node){
138-
let encryptedData = this.crypto.encryptData({data: elem.node});
138+
const encryptedData = this.crypto.encryptData({data: elem.node});
139139
utils.mutateObjectProperty(path.obj,
140140
encryptedData,
141141
body);
@@ -156,8 +156,8 @@ function encryptBody(path, body) {
156156
* @returns {Object} Encrypted body
157157
*/
158158
function encryptWithHeader(encParams, path, body) {
159-
let elem = elemFromPath(path.element, body).node;
160-
let encrypted = this.crypto.encryptData({data: elem}, encParams);
159+
const elem = elemFromPath(path.element, body).node;
160+
const encrypted = this.crypto.encryptData({data: elem}, encParams);
161161
return {[path.obj]: {[this.config.encryptedValueFieldName]: encrypted[this.config.encryptedValueFieldName]}};
162162
}
163163

@@ -171,7 +171,7 @@ function encryptWithHeader(encParams, path, body) {
171171
function decryptBody(path, body) {
172172
const elem = elemFromPath(path.element, body);
173173
if (elem && elem.node) {
174-
let decryptedObj = this.crypto.decryptData(
174+
const decryptedObj = this.crypto.decryptData(
175175
elem.node[this.config.encryptedValueFieldName], // encrypted data
176176
elem.node[this.config.ivFieldName], // iv field
177177
elem.node[this.config.oaepHashingAlgorithmFieldName], // oaepHashingAlgorithm
@@ -193,7 +193,7 @@ function decryptWithHeader(path, body, response) {
193193
const elemEncryptedNode = elemFromPath(path.obj, body);
194194
if (elemEncryptedNode.node[path.element]) {
195195
const encryptedData = elemEncryptedNode.node[path.element][this.config.encryptedValueFieldName];
196-
for (let k in body) {
196+
for (const k in body) {
197197
// noinspection JSUnfilteredForInLoop
198198
delete body[k];
199199
}

lib/mcapi/utils/utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ module.exports.mutateObjectProperty = function (path, value, obj, srcPath, prope
8383
if (srcPath !== null && srcPath !== undefined) {
8484
this.deleteNode(srcPath, obj, properties); // delete src
8585
}
86-
let paths = path.split(".");
86+
const paths = path.split(".");
8787
paths.forEach((e) => {
8888
if (!tmp.hasOwnProperty(e)) {
8989
tmp[e] = {};
9090
}
9191
prev = tmp;
9292
tmp = tmp[e];
9393
});
94-
let elem = path.split(".").pop();
94+
const elem = path.split(".").pop();
9595
if (typeof value === 'object' && !(value instanceof Array)) { // decrypted value
9696
if (typeof prev[elem] !== 'object') {
9797
prev[elem] = {};
@@ -107,8 +107,8 @@ module.exports.deleteNode = function (path, obj, properties) {
107107
properties = properties || [];
108108
let prev = obj;
109109
if (path !== null && path !== undefined && obj !== null && obj !== undefined) {
110-
let paths = path.split('.');
111-
let toDelete = paths[paths.length - 1];
110+
const paths = path.split('.');
111+
const toDelete = paths[paths.length - 1];
112112
paths.forEach((e, index) => {
113113
prev = obj;
114114
if (obj.hasOwnProperty(e)) {
@@ -127,7 +127,7 @@ module.exports.deleteNode = function (path, obj, properties) {
127127
};
128128

129129
function overrideProperties(target, obj) {
130-
for (let k in obj) {
130+
for (const k in obj) {
131131
target[k] = obj[k];
132132
}
133133
}

package-lock.json

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)