Skip to content

Commit

Permalink
Merge pull request #56 from ShimonaR-MC/fix/support-encryption-of-str…
Browse files Browse the repository at this point in the history
…ing-fields

S3384399 : Support encryption of string fields
  • Loading branch information
joseph-neeraj authored Jun 16, 2023
2 parents 437c338 + 39e72ce commit aa2bbed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/mcapi/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ module.exports.jsonToString = function (data) {
if (typeof data === "undefined" || data === null) {
throw new Error("Input not valid");
}
if (data === "") throw new Error("Json not valid");
try {
if (typeof data === "string" || data instanceof String) {
if ((typeof data === "string" || data instanceof String) && isJson(data)) {
return JSON.stringify(JSON.parse(data));
} else {
return JSON.stringify(data);
Expand All @@ -85,6 +86,15 @@ module.exports.jsonToString = function (data) {
}
};

function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}

module.exports.mutateObjectProperty = function (
path,
value,
Expand Down Expand Up @@ -416,13 +426,13 @@ function hasEncryptionParam(encParams, bodyMap) {
}

module.exports.addEncryptedDataToBody = function(encryptedData, path, encryptedValueFieldName, body) {
body = this.mutateObjectProperty(path.obj, encryptedData, body);
if (
!isJsonRoot(path.obj) &&
path.element !== path.obj + "." + encryptedValueFieldName
) {
this.deleteNode(path.element, body);
}
body = this.mutateObjectProperty(path.obj, encryptedData, body);
return body;
};

Expand Down
16 changes: 16 additions & 0 deletions test/field-level-encryption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ describe("Field Level Encryption", () => {

assert.ok(JSON.stringify(body) === JSON.stringify(decrypted));
});

it("encrypt property of primitive type", () => {
const res = encrypt.call(fle, "/resource", null, {
elem1: {
encryptedData: "plaintext",
shouldBeThere: "shouldBeThere",
},
});
assert.ok(res.header === null);
assert.ok(res.body.elem1.shouldBeThere);
assert.ok(res.body.elem1.encryptedData);
assert.ok(res.body.elem1.encryptedKey);
assert.ok(res.body.elem1.iv);
assert.ok(res.body.elem1.oaepHashingAlgorithm);
assert.ok(res.body.elem1.publicKeyFingerprint);
});
});

describe("#decrypt", () => {
Expand Down

0 comments on commit aa2bbed

Please sign in to comment.