Skip to content

Commit bc32691

Browse files
authored
Merge pull request #4 from jaaufauvre/master
Fix for #3
2 parents 9075062 + 337a9fe commit bc32691

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/main/java/com/mastercard/developer/encryption/FieldLevelEncryption.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ private static void decryptPayloadPath(DocumentContext payloadContext, String js
223223
checkOrCreateOutObject(payloadContext, jsonPathOut);
224224
addDecryptedDataToPayload(payloadContext, decryptedValue, jsonPathOut);
225225

226-
// Remove the input object if now empty
227-
inJsonObject = readJsonObject(payloadContext, jsonPathIn);
228-
if (inJsonObject != null && 0 == jsonProvider.length(inJsonObject) && !"$".equals(jsonPathIn)) {
226+
// Remove the input if now empty
227+
Object inJsonElement = readJsonElement(payloadContext, jsonPathIn);
228+
if (inJsonElement != null && 0 == jsonProvider.length(inJsonElement) && !"$".equals(jsonPathIn)) {
229229
payloadContext.delete(jsonPathIn);
230230
}
231231
}

src/main/java/com/mastercard/developer/json/JsonEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected static Object asPrimitiveValue(String string) {
7575

7676
public String toJsonString(Object object) {
7777
if (null == object) {
78-
throw new IllegalStateException("Can get a JSON string from a null object!");
78+
throw new IllegalStateException("Can't get a JSON string from a null object!");
7979
}
8080
if (isJsonPrimitive(object)) {
8181
return object.toString();

src/test/java/com/mastercard/developer/encryption/FieldLevelEncryptionWithDefaultJsonEngineTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ public void testDecryptPayload_ShouldKeepInputObject_WhenContainsAdditionalField
10351035
}
10361036

10371037
@Test
1038-
public void testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath() throws Exception {
1038+
public void testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath_ObjectData() throws Exception {
10391039

10401040
// GIVEN
10411041
String encryptedPayload = "{" +
@@ -1060,6 +1060,36 @@ public void testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPat
10601060
assertEquals("\"field2Value\"", payloadObject.get("encryptedData").getAsJsonObject().get("field2").toString());
10611061
}
10621062

1063+
/**
1064+
* https://github.com/Mastercard/client-encryption-java/issues/3
1065+
*/
1066+
@Test
1067+
public void testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath_PrimitiveTypeData() throws Exception {
1068+
1069+
// GIVEN
1070+
String encryptedPayload = "{" +
1071+
" \"data\": {" +
1072+
" \"encryptedValue\": \"e2d6a3a76ea6e605e55b400e5a4eba11\"," +
1073+
" \"iv\": \"3ce861359fa1630c7a794901ee14bf41\"," +
1074+
" \"encryptedKey\": \"02bb8d5c7d113ef271f199c09f0d76db2b6d5d2d209ad1a20dbc4dd0d04576a92ceb917eea5f403ccf64c3c39dda564046909af96c82fad62f89c3cbbec880ea3105a0a171af904cd3b86ea68991202a2795dca07050ca58252701b7ecea06055fd43e96f4beee48b6275e86af93c88c21994ff46f0610171bd388a2c0a1f518ffc8346f7f513f3283feae5b102c8596ddcb2aea5e62ceb17222e646c599f258463405d28ac012bfd4cc431f94111ee07d79e660948485e38c13cdb8bba8e1df3f7dba0f4c77696f71930533c955f3a430658edaa03b0b0c393934d60f5ac3ea5c06ed64bf969fc01942eac432b8e0c56f7538659a72859d445d150c169ae690\"," +
1075+
" \"encryptionCertificateFingerprint\": \"80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279\"," +
1076+
" \"encryptionKeyFingerprint\": \"761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79\"," +
1077+
" \"oaepHashingAlgorithm\": \"SHA256\"" +
1078+
" }" +
1079+
"}";
1080+
1081+
FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder()
1082+
.withDecryptionPath("$.data", "$.data")
1083+
.build();
1084+
1085+
// WHEN
1086+
String payload = FieldLevelEncryption.decryptPayload(encryptedPayload, config);
1087+
1088+
// THEN
1089+
JsonObject payloadObject = new Gson().fromJson(payload, JsonObject.class);
1090+
assertEquals("string", payloadObject.get("data").getAsString());
1091+
}
1092+
10631093
@Test
10641094
public void testDecryptPayload_ShouldSupportRootAsInputPath() throws Exception {
10651095

0 commit comments

Comments
 (0)