From ebca66e89e3f32eae03d7165830ea90e8662b092 Mon Sep 17 00:00:00 2001 From: Matteo Bitussi Date: Wed, 13 Mar 2024 13:28:02 +0100 Subject: [PATCH] feat: edit op add jwt now defaults value to null. resolves #7 --- tool/src/main/java/migt/Module.java | 8 +++--- tool/src/main/java/migt/Tools.java | 7 +++-- tool/src/test/java/DecodeOperation_Test.java | 3 ++- tool/src/test/java/EditOperation_test.java | 27 +++++++++++++++++++- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tool/src/main/java/migt/Module.java b/tool/src/main/java/migt/Module.java index 6d87630..e393bc0 100644 --- a/tool/src/main/java/migt/Module.java +++ b/tool/src/main/java/migt/Module.java @@ -94,10 +94,6 @@ public boolean setResult(T module) { return module.result; } - public void setResult(boolean result) { - this.result = result; - } - public void setApplicable(boolean applicable) { this.applicable = applicable; } @@ -111,6 +107,10 @@ public boolean getResult() { return this.result & this.applicable; } + public void setResult(boolean result) { + this.result = result; + } + /** * Execute this module and give a result * diff --git a/tool/src/main/java/migt/Tools.java b/tool/src/main/java/migt/Tools.java index 64a57da..a80f67e 100644 --- a/tool/src/main/java/migt/Tools.java +++ b/tool/src/main/java/migt/Tools.java @@ -754,8 +754,11 @@ public static String editJson(EditOperation.Jwt_action action, document = jsonPath.set(document, newValue, Configuration.defaultConfiguration()); break; case ADD: - document = jsonPath.put(document, newKey, newValue, Configuration.defaultConfiguration()); - //TODO: check if set also adds in case it is not found + document = jsonPath.put( + document, + newKey, + newValue.isEmpty() ? null : newValue, + Configuration.defaultConfiguration()); break; case SAVE: Object to_save = JsonPath.read(content, j_path); diff --git a/tool/src/test/java/DecodeOperation_Test.java b/tool/src/test/java/DecodeOperation_Test.java index 2309366..f003894 100644 --- a/tool/src/test/java/DecodeOperation_Test.java +++ b/tool/src/test/java/DecodeOperation_Test.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class DecodeOperation_Test { String input = "{\n" + @@ -140,7 +141,7 @@ void test_decode_jwt() throws ParsingException { dop.loader(op_api); dop.execute(null); - assertEquals(true, dop.getResult()); + assertTrue(dop.getResult()); DecodeOperation_API dop_api = dop.getAPI(); assertEquals("{\"exp\":1710327846,\"iat\":1710325866,\"iss\":\"http://trust-anchor.org:8000\",\"sub\":\"http://trust-anchor.org:8000\",\"jwks\":{\"keys\":[{\"kty\":\"RSA\",\"n\":\"o8IolRjZlkzct-48rhrVlTnYU1pkMbVJD-DU05oMS9RVGrsFypg98m-Kw4H4qNPyQVx2OQORi-xShgk7HU-gK_2pVguYkv06FajL_edEAqqsqt_74Qf2WLRC5pfJG_z9OPzY8JGyk-z3SbeHN_BXKI8GY5E4WU2SstmQ9fyL4CxtRfjUia8limTC_3MOpT3zi5nr03jfbjpnjga51qXurxnlzc3a_xjk5RAApKxUvNwhJ275M0CmB99DjPwF6BLvUgJqgyCpUOn36LOhI4FquVqhqhiwKlMmiMe3yy0yNQ7FXBWxjzhexbpyc3Vu7zFIHPAcC4UyIQhc3waEj2viXw\",\"e\":\"AQAB\",\"kid\":\"BXvfrlnhAMuHR07ajUmAcBRQcSzmw0c_RAgJnpS-9WQ\"}]},\"metadata\":{\"federation_entity\":{\"contacts\":[\"ops@localhost\"],\"federation_fetch_endpoint\":\"http://trust-anchor.org:8000/fetch\",\"federation_resolve_endpoint\":\"http://trust-anchor.org:8000/resolve\",\"federation_trust_mark_status_endpoint\":\"http://trust-anchor.org:8000/trust_mark_status\",\"homepage_uri\":\"http://trust-anchor.org:8000\",\"organization_name\":\"example TA\",\"policy_uri\":\"http://trust-anchor.org:8000/en/website/legal-information\",\"logo_uri\":\"http://trust-anchor.org:8000/static/svg/spid-logo-c-lb.svg\",\"federation_list_endpoint\":\"http://trust-anchor.org:8000/list\"}},\"trust_mark_issuers\":{\"https://www.spid.gov.it/certification/rp/public\":[\"https://registry.spid.agid.gov.it\",\"https://public.intermediary.spid.it\"],\"https://www.spid.gov.it/certification/rp/private\":[\"https://registry.spid.agid.gov.it\",\"https://private.other.intermediary.it\"],\"https://sgd.aa.it/onboarding\":[\"https://sgd.aa.it\"]},\"constraints\":{\"max_path_length\":1}}", dop_api.jwt.payload); assertEquals("{\"kid\":\"BXvfrlnhAMuHR07ajUmAcBRQcSzmw0c_RAgJnpS-9WQ\",\"typ\":\"entity-statement+jwt\",\"alg\":\"RS256\"}", dop_api.jwt.header); diff --git a/tool/src/test/java/EditOperation_test.java b/tool/src/test/java/EditOperation_test.java index 7a172b5..260f5b4 100644 --- a/tool/src/test/java/EditOperation_test.java +++ b/tool/src/test/java/EditOperation_test.java @@ -173,8 +173,33 @@ public void test_add_json_jwt() throws ParsingException { dop.loader(new Operation_API(DecodeOperation_Test.get_test_message_with_jwt(), false)); dop.execute(null); - assertEquals(true, dop.editOperations.get(0).getResult()); + assertTrue(dop.editOperations.get(0).getResult()); assertEquals("{\"exp\":1710327846,\"iat\":1710325866,\"iss\":\"http://trust-anchor.org:8000\",\"sub\":\"http://trust-anchor.org:8000\",\"jwks\":{\"keys\":[{\"kty\":\"RSA\",\"n\":\"o8IolRjZlkzct-48rhrVlTnYU1pkMbVJD-DU05oMS9RVGrsFypg98m-Kw4H4qNPyQVx2OQORi-xShgk7HU-gK_2pVguYkv06FajL_edEAqqsqt_74Qf2WLRC5pfJG_z9OPzY8JGyk-z3SbeHN_BXKI8GY5E4WU2SstmQ9fyL4CxtRfjUia8limTC_3MOpT3zi5nr03jfbjpnjga51qXurxnlzc3a_xjk5RAApKxUvNwhJ275M0CmB99DjPwF6BLvUgJqgyCpUOn36LOhI4FquVqhqhiwKlMmiMe3yy0yNQ7FXBWxjzhexbpyc3Vu7zFIHPAcC4UyIQhc3waEj2viXw\",\"e\":\"AQAB\",\"kid\":\"BXvfrlnhAMuHR07ajUmAcBRQcSzmw0c_RAgJnpS-9WQ\"}]},\"metadata\":{\"federation_entity\":{\"contacts\":[\"ops@localhost\"],\"federation_fetch_endpoint\":\"http://trust-anchor.org:8000/fetch\",\"federation_resolve_endpoint\":\"http://trust-anchor.org:8000/resolve\",\"federation_trust_mark_status_endpoint\":\"http://trust-anchor.org:8000/trust_mark_status\",\"homepage_uri\":\"http://trust-anchor.org:8000\",\"organization_name\":\"example TA\",\"policy_uri\":\"http://trust-anchor.org:8000/en/website/legal-information\",\"logo_uri\":\"http://trust-anchor.org:8000/static/svg/spid-logo-c-lb.svg\",\"federation_list_endpoint\":\"http://trust-anchor.org:8000/list\"}},\"trust_mark_issuers\":{\"https://www.spid.gov.it/certification/rp/public\":[\"https://registry.spid.agid.gov.it\",\"https://public.intermediary.spid.it\"],\"https://www.spid.gov.it/certification/rp/private\":[\"https://registry.spid.agid.gov.it\",\"https://private.other.intermediary.it\"],\"https://sgd.aa.it/onboarding\":[\"https://sgd.aa.it\"]},\"constraints\":{\"max_path_length\":1},\"new\":\"valuenew\"}", dop.getAPI().jwt.payload); } + + @Test + public void test_add_json_jwt_null() throws ParsingException { + String input = "{\n" + + " \"from\": \"body\",\n" + + " \"type\": \"jwt\",\n" + + " \"decode regex\": \"[^\\\\n\\\\r]*\",\n" + + " \"edits\": [\n" + + " {\n" + + " \"jwt from\": \"payload\",\n" + + " \"jwt add\": \"$\",\n" + + " \"key\": \"new\",\n" + + " \"value\": \"\"\n" + + " }\n" + + " ]\n" + + "}"; + + DecodeOperation dop = new DecodeOperation(new JSONObject(input)); + dop.loader(new Operation_API(DecodeOperation_Test.get_test_message_with_jwt(), false)); + + dop.execute(null); + assertTrue(dop.editOperations.get(0).getResult()); + + assertEquals("{\"exp\":1710327846,\"iat\":1710325866,\"iss\":\"http://trust-anchor.org:8000\",\"sub\":\"http://trust-anchor.org:8000\",\"jwks\":{\"keys\":[{\"kty\":\"RSA\",\"n\":\"o8IolRjZlkzct-48rhrVlTnYU1pkMbVJD-DU05oMS9RVGrsFypg98m-Kw4H4qNPyQVx2OQORi-xShgk7HU-gK_2pVguYkv06FajL_edEAqqsqt_74Qf2WLRC5pfJG_z9OPzY8JGyk-z3SbeHN_BXKI8GY5E4WU2SstmQ9fyL4CxtRfjUia8limTC_3MOpT3zi5nr03jfbjpnjga51qXurxnlzc3a_xjk5RAApKxUvNwhJ275M0CmB99DjPwF6BLvUgJqgyCpUOn36LOhI4FquVqhqhiwKlMmiMe3yy0yNQ7FXBWxjzhexbpyc3Vu7zFIHPAcC4UyIQhc3waEj2viXw\",\"e\":\"AQAB\",\"kid\":\"BXvfrlnhAMuHR07ajUmAcBRQcSzmw0c_RAgJnpS-9WQ\"}]},\"metadata\":{\"federation_entity\":{\"contacts\":[\"ops@localhost\"],\"federation_fetch_endpoint\":\"http://trust-anchor.org:8000/fetch\",\"federation_resolve_endpoint\":\"http://trust-anchor.org:8000/resolve\",\"federation_trust_mark_status_endpoint\":\"http://trust-anchor.org:8000/trust_mark_status\",\"homepage_uri\":\"http://trust-anchor.org:8000\",\"organization_name\":\"example TA\",\"policy_uri\":\"http://trust-anchor.org:8000/en/website/legal-information\",\"logo_uri\":\"http://trust-anchor.org:8000/static/svg/spid-logo-c-lb.svg\",\"federation_list_endpoint\":\"http://trust-anchor.org:8000/list\"}},\"trust_mark_issuers\":{\"https://www.spid.gov.it/certification/rp/public\":[\"https://registry.spid.agid.gov.it\",\"https://public.intermediary.spid.it\"],\"https://www.spid.gov.it/certification/rp/private\":[\"https://registry.spid.agid.gov.it\",\"https://private.other.intermediary.it\"],\"https://sgd.aa.it/onboarding\":[\"https://sgd.aa.it\"]},\"constraints\":{\"max_path_length\":1},\"new\":null}", dop.getAPI().jwt.payload); + } }