diff --git a/.github/workflows/traits.yml b/.github/workflows/traits.yml index 52193c6..e17fcc1 100644 --- a/.github/workflows/traits.yml +++ b/.github/workflows/traits.yml @@ -34,3 +34,24 @@ jobs: cmake . -DCMAKE_FIND_DEBUG_MODE=1 cmake --build . ./jsoncons-traits-narrow + + boost-json: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: lukka/get-cmake@latest + - uses: ./.github/actions/install/gtest + + - name: setup + run: | + mkdir build + cd build + cmake .. -DJWT_BUILD_EXAMPLES=OFF + sudo cmake --install . + + - name: test + run: | + cd tests/boost + cmake . + cmake --build . + ./boostjson-traits diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index 97425ce..c043386 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -1779,7 +1779,7 @@ namespace jwt { std::is_constructible::value && // a more generic is_copy_constructible std::is_move_constructible::value && std::is_assignable::value && std::is_copy_assignable::value && std::is_move_assignable::value; - // TODO(cmcarthur): Stream operators + // TODO(prince-chrismc): Stream operators }; template @@ -1847,7 +1847,8 @@ namespace jwt { is_detected::value && std::is_same::value && is_detected::value && - std::is_same::value && + (std::is_same::value || + std::is_constructible::value) && supports_begin::value && supports_end::value && is_count_signature::value && is_subcription_operator_signature::value && @@ -1863,11 +1864,57 @@ namespace jwt { static constexpr auto value = std::is_same::value; }; - template + template + using is_substr_start_end_index_signature = + typename std::is_same().substr(std::declval(), + std::declval())), + string_type>; + + template + using is_substr_start_index_signature = + typename std::is_same().substr(std::declval())), + string_type>; + + template + struct has_operate_plus_method { // https://stackoverflow.com/a/9154394/8480874 + template + struct sfinae_true : std::true_type {}; + + template + static auto test_operator_plus(int) + -> sfinae_true().operator+(std::declval()))>; + template + static auto test_operator_plus(long) -> std::false_type; + + static constexpr auto value = decltype(test_operator_plus(0)){}; + }; + + template + using is_std_operate_plus_signature = + typename std::is_same(), std::declval())), + string_type>; + + template + struct is_valid_json_string { + static constexpr auto substr = is_substr_start_end_index_signature::value && + is_substr_start_index_signature::value; + static_assert(substr, "string_type must have a substr method taking only a start index and an overload " + "taking a start and end index, both must return a string_type"); + + static constexpr auto operator_plus = + has_operate_plus_method::value || is_std_operate_plus_signature::value; + static_assert(operator_plus, + "string_type must have a '+' operator implemented which returns the concatenated string"); + + static constexpr auto value = substr && operator_plus; + }; + + template struct is_valid_json_types { // Internal assertions for better feedback static_assert(is_valid_json_value::value, - "value type must meet basic requirements, default constructor, copyable, moveable"); + "value_type must meet basic requirements, default constructor, copyable, moveable"); static_assert(is_valid_json_object::value, "object_type must be a string_type to value_type container"); static_assert(is_valid_json_array::value, @@ -1875,7 +1922,8 @@ namespace jwt { static constexpr auto value = is_valid_json_object::value && is_valid_json_value::value && - is_valid_json_array::value; + is_valid_json_array::value && + is_valid_json_string::value; }; } // namespace details @@ -1896,12 +1944,15 @@ namespace jwt { * https://github.com/nlohmann/json/issues/774. It maybe be expanded to * support custom string types. */ - static_assert(std::is_same::value, - "string_type must be a std::string."); + static_assert(std::is_same::value || + std::is_convertible::value || + std::is_constructible::value, + "string_type must be a std::string, convertible to a std::string, or construct a std::string."); static_assert( details::is_valid_json_types::value, + typename json_traits::integer_type, typename json_traits::object_type, + typename json_traits::array_type>::value, "must staisfy json container requirements"); static_assert(details::is_valid_traits::value, "traits must satisfy requirements"); diff --git a/tests/boost/.gitignore b/tests/boost/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/tests/boost/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/tests/boost/BoostJsonTest.cpp b/tests/boost/BoostJsonTest.cpp new file mode 100644 index 0000000..dd0cd08 --- /dev/null +++ b/tests/boost/BoostJsonTest.cpp @@ -0,0 +1,173 @@ +#define JWT_DISABLE_PICOJSON +#include "jwt-cpp/jwt.h" + +#include +#include +#include + +#include +#include + +namespace json = boost::json; + +struct boostjson_traits { + using value_type = json::value; + using object_type = json::object; + using array_type = json::array; + using string_type = std::string; + using number_type = double; + using integer_type = std::int64_t; + using boolean_type = bool; + + static jwt::json::type get_type(const value_type& val) { + using jwt::json::type; + + if (val.kind() == json::kind::bool_) return type::boolean; + if (val.kind() == json::kind::int64) return type::integer; + if (val.kind() == json::kind::uint64) // boost internally tracks two types of integers + return type::integer; + if (val.kind() == json::kind::double_) return type::number; + if (val.kind() == json::kind::string) return type::string; + if (val.kind() == json::kind::array) return type::array; + if (val.kind() == json::kind::object) return type::object; + + throw std::logic_error("invalid type"); + } + + static object_type as_object(const value_type& val) { + if (val.kind() != json::kind::object) throw std::bad_cast(); + return val.get_object(); + } + + static array_type as_array(const value_type& val) { + if (val.kind() != json::kind::array) throw std::bad_cast(); + return val.get_array(); + } + + static string_type as_string(const value_type& val) { + if (val.kind() != json::kind::string) throw std::bad_cast(); + return string_type{val.get_string()}; + } + + static integer_type as_int(const value_type& val) { + switch (val.kind()) { + case json::kind::int64: return val.get_int64(); + case json::kind::uint64: return static_cast(val.get_uint64()); + default: throw std::bad_cast(); + } + } + + static boolean_type as_bool(const value_type& val) { + if (val.kind() != json::kind::bool_) throw std::bad_cast(); + return val.get_bool(); + } + + static number_type as_number(const value_type& val) { + if (val.kind() != json::kind::double_) throw std::bad_cast(); + return val.get_double(); + } + + static bool parse(value_type& val, string_type str) { + val = json::parse(str); + return true; + } + + static std::string serialize(const value_type& val) { return json::serialize(val); } +}; + +TEST(BoostJSONTest, BasicClaims) { + using boostjson_claim = jwt::basic_claim; + + const auto string = boostjson_claim(boostjson_traits::string_type("string")); + const auto array = boostjson_claim(std::set{"string", "string"}); + const auto integer = boostjson_claim(boostjson_traits::value_type{159816816}); +} + +TEST(BoostJSONTest, AudienceAsString) { + boostjson_traits::string_type token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0." + "WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4"; + auto decoded = jwt::decode(token); + + ASSERT_TRUE(decoded.has_algorithm()); + ASSERT_TRUE(decoded.has_type()); + ASSERT_FALSE(decoded.has_content_type()); + ASSERT_FALSE(decoded.has_key_id()); + ASSERT_FALSE(decoded.has_issuer()); + ASSERT_FALSE(decoded.has_subject()); + ASSERT_TRUE(decoded.has_audience()); + ASSERT_FALSE(decoded.has_expires_at()); + ASSERT_FALSE(decoded.has_not_before()); + ASSERT_FALSE(decoded.has_issued_at()); + ASSERT_FALSE(decoded.has_id()); + + ASSERT_EQ("HS256", decoded.get_algorithm()); + ASSERT_EQ("JWT", decoded.get_type()); + auto aud = decoded.get_audience(); + ASSERT_EQ(1, aud.size()); + ASSERT_EQ("test", *aud.begin()); +} + +TEST(BoostJSONTest, SetArray) { + std::vector vect = {100, 20, 10}; + auto token = jwt::create() + .set_payload_claim("test", jwt::basic_claim(vect.begin(), vect.end())) + .sign(jwt::algorithm::none{}); + ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0."); +} + +TEST(BoostJSONTest, SetObject) { + jwt::basic_claim object{json::parse("{\"api-x\": [1]}")}; + ASSERT_EQ(object.get_type(), jwt::json::type::object); + + auto token = + jwt::create().set_payload_claim("namespace", object).sign(jwt::algorithm::hs256("test")); + ASSERT_EQ(token, + "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA"); +} + +TEST(BoostJSONTest, VerifyTokenHS256) { + boostjson_traits::string_type token = + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; + + auto verify = jwt::verify({}) + .allow_algorithm(jwt::algorithm::hs256{"secret"}) + .with_issuer("auth0"); + + auto decoded_token = jwt::decode(token); + verify.verify(decoded_token); +} + +TEST(BoostJSONTest, VerifyTokenExpirationValid) { + const auto token = jwt::create() + .set_issuer("auth0") + .set_issued_at(std::chrono::system_clock::now()) + .set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{3600}) + .sign(jwt::algorithm::hs256{"secret"}); + + auto verify = jwt::verify({}) + .allow_algorithm(jwt::algorithm::hs256{"secret"}) + .with_issuer("auth0"); + + auto decoded_token = jwt::decode(token); + verify.verify(decoded_token); +} + +TEST(BoostJSONTest, VerifyTokenExpired) { + const auto token = jwt::create() + .set_issuer("auth0") + .set_issued_at(std::chrono::system_clock::now() - std::chrono::seconds{3601}) + .set_expires_at(std::chrono::system_clock::now() - std::chrono::seconds{1}) + .sign(jwt::algorithm::hs256{"secret"}); + + auto verify = jwt::verify({}) + .allow_algorithm(jwt::algorithm::hs256{"secret"}) + .with_issuer("auth0"); + + auto decoded_token = jwt::decode(token); + ASSERT_THROW(verify.verify(decoded_token), jwt::token_verification_exception); + std::error_code ec; + ASSERT_NO_THROW(verify.verify(decoded_token, ec)); + ASSERT_TRUE(!(!ec)); + ASSERT_EQ(ec.category(), jwt::error::token_verification_error_category()); + ASSERT_EQ(ec.value(), static_cast(jwt::error::token_verification_error::token_expired)); +} diff --git a/tests/boost/CMakeLists.txt b/tests/boost/CMakeLists.txt new file mode 100644 index 0000000..c1b4d83 --- /dev/null +++ b/tests/boost/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.18) +project(jwt-cpp-boostjson-tests) + +find_package(OpenSSL REQUIRED) +find_package(GTest REQUIRED) +if(NOT TARGET jwt-cpp::jwt-cpp) + find_package(jwt-cpp CONFIG REQUIRED) +endif() + +file(DOWNLOAD https://github.com/boostorg/json/archive/refs/tags/boost-1.77.0.tar.gz + ${CMAKE_CURRENT_BINARY_DIR}/boost-1.77.0.tar.gz) +file(ARCHIVE_EXTRACT INPUT ${CMAKE_CURRENT_BINARY_DIR}/boost-1.77.0.tar.gz DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + +add_executable(boostjson-traits BoostJsonTest.cpp) +target_link_libraries(boostjson-traits jwt-cpp::jwt-cpp GTest::GTest GTest::Main) +target_compile_definitions(boostjson-traits PRIVATE BOOST_JSON_STANDALONE) +target_compile_features(boostjson-traits PRIVATE cxx_std_17) +target_include_directories(boostjson-traits PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/json-boost-1.77.0/include")