Skip to content

Commit 10d312e

Browse files
authored
Merge pull request #103 from Mastercard/feature/fix-parent-enc-node
Fixing issue where JWE parent encryption node isn't removed
2 parents 7d36218 + 001a499 commit 10d312e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ private static DocumentContext decryptPayloadPath(DocumentContext payloadContext
136136

137137
// Remove the input
138138
JsonParser.deleteIfExists(payloadContext, jsonPathIn);
139+
140+
//Strip the parent node if empty
141+
String jsonPathInStripped = jsonPathIn.replaceAll("." + config.getEncryptedValueFieldName() + "$", "");
142+
Object inJsonObjectStripped = readJsonObject(payloadContext, jsonPathIn);
143+
if (!jsonPathInStripped.equals("$") && !jsonPathInStripped.contains("[") && inJsonObjectStripped == null) {
144+
JsonParser.deleteIfExists(payloadContext, jsonPathInStripped);
145+
}
146+
139147
return payloadContext;
140148
}
141149

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,25 @@ public void testDecryptPayload_ShouldSupportPayloadWithEncryptedValueParent() th
115115
// THEN
116116
assertPayloadEquals("{\"data\": {}}", payload);
117117
}
118+
119+
@Test
120+
public void testDecryptPayload_ShouldRemoveParentEncryptedFieldIfEmpty() throws Exception {
121+
122+
// GIVEN
123+
String encryptedPayload = "{\n" +
124+
" \"encryptedDataParent\": {\n" +
125+
" \"encryptedData\": \"eyJraWQiOiI3NjFiMDAzYzFlYWRlM2E1NDkwZTUwMDBkMzc4ODdiYWE1ZTZlYzBlMjI2YzA3NzA2ZTU5OTQ1MWZjMDMyYTc5IiwiY3R5IjoiYXBwbGljYXRpb24vanNvbiIsImVuYyI6IkEyNTZHQ00iLCJhbGciOiJSU0EtT0FFUC0yNTYifQ.XVy1AR51sUvwT-AtcsogQDo_klFi1EMYW8Wz7qM0e1dA3jNX5nTa38JhRcVuyVK15OenTYfg7aaH_fLjPZI1Mukd0BBnTuonh8T9CX5tbAAYx_KGPxc7a7ekBO-xXEA762eRvIIQJDZgQ_C3U39kc-XoaxC-ZYx8Va_aPBsXI1uozAfj3j5XVDnSmGAVWc2N4STTlCKbL4EO6YXASl_PrAOIVVSUrhpYvNS7GnjrP9x49tlRmTS0Dx-_MhkIAJM6H25YAuUmO-LW3gikReOUgGeY9_JtOioDs2J4ncKqugPFKr8kYF1cKnMwFv0TS9p5qR0kiF20bxRMvhbazf_Q5Q.V2Uz5-YRNq9ZIJjhRsKYIw.jB1s8rczGEj2OjU.qs4zVUf2tHML02Rglq5ncw\"\n" +
126+
" }\n" +
127+
"}";
128+
JweConfig config = getTestJweConfigBuilder()
129+
.withEncryptedValueFieldName("encryptedData")
130+
.withDecryptionPath("$.encryptedDataParent.encryptedData", "$.unencrypted")
131+
.build();
132+
133+
// WHEN
134+
String payload = JweEncryption.decryptPayload(encryptedPayload, config);
135+
136+
// THEN
137+
assertPayloadEquals("{\"unencrypted\":{\"data\": {}}}", payload);
138+
}
118139
}

0 commit comments

Comments
 (0)