From 5c163234ce00f91707a73d7274c9c0d7ba198879 Mon Sep 17 00:00:00 2001 From: Greg Hewett Date: Wed, 13 Sep 2023 18:51:52 -0500 Subject: [PATCH 1/4] changed userinfo_vc to not publicize nlohmann::json --- lib/hpke/include/hpke/userinfo_vc.h | 5 ++--- lib/hpke/src/userinfo_vc.cpp | 14 +++++++++----- lib/hpke/test/userinfo_vc.cpp | 12 ++++++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/hpke/include/hpke/userinfo_vc.h b/lib/hpke/include/hpke/userinfo_vc.h index 902fed53..55f575a6 100644 --- a/lib/hpke/include/hpke/userinfo_vc.h +++ b/lib/hpke/include/hpke/userinfo_vc.h @@ -6,7 +6,6 @@ #include #include #include -#include using namespace MLS_NAMESPACE::bytes_ns; @@ -21,7 +20,7 @@ struct UserInfoClaimsAddress std::optional postal_code; std::optional country; - static UserInfoClaimsAddress from_json(const nlohmann::json& address_json); + static UserInfoClaimsAddress from_json(const std::string& address); }; struct UserInfoClaims @@ -48,7 +47,7 @@ struct UserInfoClaims std::optional address; std::optional updated_at; - static UserInfoClaims from_json(const nlohmann::json& cred_subject_json); + static UserInfoClaims from_json(const std::string& cred_subject); }; struct UserInfoVC diff --git a/lib/hpke/src/userinfo_vc.cpp b/lib/hpke/src/userinfo_vc.cpp index 563c1788..dadf208f 100644 --- a/lib/hpke/src/userinfo_vc.cpp +++ b/lib/hpke/src/userinfo_vc.cpp @@ -265,7 +265,7 @@ struct UserInfoVC::ParsedCredential epoch_time(payload.at("nbf").get()), epoch_time(payload.at("exp").get()), - UserInfoClaims::from_json(vc.at("credentialSubject")), + UserInfoClaims::from_json(vc.at("credentialSubject").dump()), std::move(public_key), to_be_signed, @@ -282,8 +282,10 @@ struct UserInfoVC::ParsedCredential /// UserInfoClaimsAddress /// UserInfoClaimsAddress -UserInfoClaimsAddress::from_json(const nlohmann::json& address_json) +UserInfoClaimsAddress::from_json(const std::string& address) { + const auto& address_json = nlohmann::json::parse(address); + return { get_optional(address_json, address_formatted_attr), get_optional(address_json, address_street_address_attr), @@ -298,13 +300,15 @@ UserInfoClaimsAddress::from_json(const nlohmann::json& address_json) /// UserInfoClaims /// UserInfoClaims -UserInfoClaims::from_json(const nlohmann::json& cred_subject_json) +UserInfoClaims::from_json(const std::string& cred_subject) { + const auto& cred_subject_json = nlohmann::json::parse(cred_subject); + std::optional address_opt = {}; if (cred_subject_json.contains(address_attr)) { - address_opt = - UserInfoClaimsAddress::from_json(cred_subject_json.at(address_attr)); + address_opt = UserInfoClaimsAddress::from_json( + cred_subject_json.at(address_attr).dump()); } return { diff --git a/lib/hpke/test/userinfo_vc.cpp b/lib/hpke/test/userinfo_vc.cpp index f083f573..97452da6 100644 --- a/lib/hpke/test/userinfo_vc.cpp +++ b/lib/hpke/test/userinfo_vc.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace opt = MLS_NAMESPACE::tls::opt; @@ -175,7 +176,8 @@ TEST_CASE("UserInfoClaims Field Parsing") { "updated_at", 42 } }; - const auto userinfo_claims = UserInfoClaims::from_json(credentialSubject); + const auto userinfo_claims = + UserInfoClaims::from_json(credentialSubject.dump()); CHECK(userinfo_claims.sub == credentialSubject.at("sub")); CHECK(userinfo_claims.name == credentialSubject.at("name")); @@ -214,14 +216,16 @@ TEST_CASE("UserInfoClaims Field Parsing") TEST_CASE("UserInfoClaims Edge Cases") { CHECK_THROWS_WITH( - UserInfoClaims::from_json({ { "updated_at", "42" } }), + UserInfoClaims::from_json( + nlohmann::json({ { "updated_at", "42" } }).dump()), "[json.exception.type_error.302] type must be number, but is string"); CHECK_THROWS_WITH( - UserInfoClaims::from_json({ { "name", true } }), + UserInfoClaims::from_json(nlohmann::json({ { "name", true } }).dump()), "[json.exception.type_error.302] type must be string, but is boolean"); CHECK_THROWS_WITH( - UserInfoClaims::from_json({ { "email_verified", "true" } }), + UserInfoClaims::from_json( + nlohmann::json({ { "email_verified", "true" } }).dump()), "[json.exception.type_error.302] type must be boolean, but is string"); } From cc9e878ae87f94d75a2fa6a16a27ed88ef266a6e Mon Sep 17 00:00:00 2001 From: Greg Hewett Date: Mon, 23 Oct 2023 15:50:51 -0500 Subject: [PATCH 2/4] moved nlohmann_json to an include dependency instead of a link dependency --- lib/hpke/CMakeLists.txt | 16 +++++++++++++--- lib/hpke/test/userinfo_vc.cpp | 1 - 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/hpke/CMakeLists.txt b/lib/hpke/CMakeLists.txt index d57afed0..eb7c1267 100644 --- a/lib/hpke/CMakeLists.txt +++ b/lib/hpke/CMakeLists.txt @@ -13,13 +13,23 @@ find_package(OpenSSL 1.1 REQUIRED) file(GLOB_RECURSE LIB_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.h") file(GLOB_RECURSE LIB_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp") +# https://gitlab.kitware.com/cmake/cmake/-/issues/15415#note_334852 +# Warning: this will fail once nlohman_json stops being header-only! +get_target_property(JSON_INCLUDE_INTERFACE nlohmann_json::nlohmann_json INTERFACE_INCLUDE_DIRECTORIES) + add_library(${CURRENT_LIB_NAME} ${LIB_HEADERS} ${LIB_SOURCES}) add_dependencies(${CURRENT_LIB_NAME} bytes tls_syntax) -target_link_libraries(${CURRENT_LIB_NAME} +target_include_directories(${CURRENT_LIB_NAME} PRIVATE - nlohmann_json::nlohmann_json OpenSSL::Crypto + "${JSON_INCLUDE_INTERFACE}") + +target_link_libraries(${CURRENT_LIB_NAME} PUBLIC - bytes tls_syntax) + bytes tls_syntax + PRIVATE + OpenSSL::Crypto +) + target_include_directories(${CURRENT_LIB_NAME} PUBLIC $ diff --git a/lib/hpke/test/userinfo_vc.cpp b/lib/hpke/test/userinfo_vc.cpp index 97452da6..787b0edd 100644 --- a/lib/hpke/test/userinfo_vc.cpp +++ b/lib/hpke/test/userinfo_vc.cpp @@ -2,7 +2,6 @@ #include #include -#include #include namespace opt = MLS_NAMESPACE::tls::opt; From 9345027659d4590e774bfba67305563e4be17848 Mon Sep 17 00:00:00 2001 From: Greg Hewett Date: Tue, 24 Oct 2023 21:14:20 -0500 Subject: [PATCH 3/4] construct string instead of json object Co-authored-by: Richard Barnes --- lib/hpke/test/userinfo_vc.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/hpke/test/userinfo_vc.cpp b/lib/hpke/test/userinfo_vc.cpp index 787b0edd..0c207cf5 100644 --- a/lib/hpke/test/userinfo_vc.cpp +++ b/lib/hpke/test/userinfo_vc.cpp @@ -215,8 +215,7 @@ TEST_CASE("UserInfoClaims Field Parsing") TEST_CASE("UserInfoClaims Edge Cases") { CHECK_THROWS_WITH( - UserInfoClaims::from_json( - nlohmann::json({ { "updated_at", "42" } }).dump()), + UserInfoClaims::from_json(R"({"updated_at", "42"})"), "[json.exception.type_error.302] type must be number, but is string"); CHECK_THROWS_WITH( From 7e000797e30ee6b136e9fb467c31c1b73ba7b00b Mon Sep 17 00:00:00 2001 From: Greg Hewett Date: Tue, 24 Oct 2023 21:43:52 -0500 Subject: [PATCH 4/4] Fixed partial updated test --- lib/hpke/test/userinfo_vc.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/hpke/test/userinfo_vc.cpp b/lib/hpke/test/userinfo_vc.cpp index 0c207cf5..da43b0b5 100644 --- a/lib/hpke/test/userinfo_vc.cpp +++ b/lib/hpke/test/userinfo_vc.cpp @@ -215,15 +215,14 @@ TEST_CASE("UserInfoClaims Field Parsing") TEST_CASE("UserInfoClaims Edge Cases") { CHECK_THROWS_WITH( - UserInfoClaims::from_json(R"({"updated_at", "42"})"), + UserInfoClaims::from_json(R"({"updated_at": "42"})"), "[json.exception.type_error.302] type must be number, but is string"); CHECK_THROWS_WITH( - UserInfoClaims::from_json(nlohmann::json({ { "name", true } }).dump()), + UserInfoClaims::from_json(R"({"name": true})"), "[json.exception.type_error.302] type must be string, but is boolean"); CHECK_THROWS_WITH( - UserInfoClaims::from_json( - nlohmann::json({ { "email_verified", "true" } }).dump()), + UserInfoClaims::from_json(R"({"email_verified": "true"})"), "[json.exception.type_error.302] type must be boolean, but is string"); }