diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index 69dfa7b0e..ccb9e7a34 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -1012,7 +1012,7 @@ namespace jwt { JWT_BASIC_CLAIM_TPL_DECLARATION class basic_claim { // TODO: FixMe - static_assert(std::is_same::value, "current this only supports an `std::string` due to the sining and base64 encoding that is required by JWT."); + // static_assert(std::is_same::value, "current this only supports an `std::string` due to the sining and base64 encoding that is required by JWT."); static_assert(details::supports_get_type::value, "traits must provide `jwt::json::type get_type(const value_type&)`"); static_assert(details::supports_as_object::value, "traits must provide `object_type as_object(const value_type&)`"); diff --git a/tests/NlohmannTest.cpp b/tests/NlohmannTest.cpp index 7311763b2..3a6a55e58 100644 --- a/tests/NlohmannTest.cpp +++ b/tests/NlohmannTest.cpp @@ -2,84 +2,82 @@ #include "jwt-cpp/jwt.h" #include "nlohmann/json.hpp" -template struct nlohmann_traits { - using json = json_type; - using value_enum = nlohmann::detail::value_t; + using json = nlohmann::json; - static jwt::json::type get_type(const typename json::value_t &val) { + static jwt::json::type get_type(const json &val) { using jwt::json::type; - if (val.type() == value_enum::null) + if (val.type() == json::value_t::null) return type::null; - else if (val.type() == value_enum::boolean) + else if (val.type() == json::value_t::boolean) return type::boolean; - else if (val.type() == value_enum::number_integer) + else if (val.type() == json::value_t::number_integer) return type::integer; - else if (val.type() == value_enum::number_float) + else if (val.type() == json::value_t::number_float) return type::number; - else if (val.type() == value_enum::string) + else if (val.type() == json::value_t::string) return type::string; - else if (val.type() == value_enum::array) + else if (val.type() == json::value_t::array) return type::array; - else if (val.type() == value_enum::object) + else if (val.type() == json::value_t::object) return type::object; else throw std::logic_error("invalid type"); } - static typename json::object_t as_object(const typename json::value_t &val) { - if (val.type() != value_enum::object) + static json::object_t as_object(const json &val) { + if (val.type() != json::value_t::object) throw std::bad_cast(); - return val.get(); + return val.get(); } - static typename json::string_t as_string(const typename json::value_t &val) { - if (val.type() != value_enum::string) + static std::string as_string(const json &val) { + if (val.type() != json::value_t::string) throw std::bad_cast(); - return val.get(); + return val.get(); } - static typename json::array_t as_array(const typename json::value_t &val) { - if (val.type() != value_enum::array) + static json::array_t as_array(const json &val) { + if (val.type() != json::value_t::array) throw std::bad_cast(); - return val.get(); + return val.get(); } - static std::set as_set(const typename json::value_t &val) { - std::set res; + static std::set as_set(const json &val) { + std::set res; for (auto &e : as_array(val)) { - if (val.type() != value_enum::string) + if (val.type() != json::value_t::string) throw std::bad_cast(); - res.insert(e.get()); + res.insert(e.get()); } return res; } - static typename json::number_integer_t as_int(const typename json::value_t &val) { - if (val.type() != value_enum::number_integer) + static int64_t as_int(const json &val) { + if (val.type() != json::value_t::number_integer) throw std::bad_cast(); - return val.get(); + return val.get(); } - static typename json::boolean_t as_bool(const typename json::value_t &val) { - if (val.type() != value_enum::boolean) + static bool as_bool(const json &val) { + if (val.type() != json::value_t::boolean) throw std::bad_cast(); - return val.get(); + return val.get(); } - static typename json::number_float_t as_number(const typename json::value_t &val) { - if (val.type() != value_enum::number_float) + static double as_number(const json &val) { + if (val.type() != json::value_t::number_float) throw std::bad_cast(); - return val.get(); + return val.get(); } - static bool parse(typename json::value_t &val, typename json::string_t str) { + static bool parse(json &val, std::string str) { val = json::parse(str.begin(), str.end()); return true; } - static typename json::string_t serialize(const typename json::value_t &val) { + static std::string serialize(const json &val) { return val.dump(); } }; @@ -89,7 +87,7 @@ struct nlohmann_traits { nlohmann::json::array_t, nlohmann::json::string_t, \ nlohmann::json::boolean_t, \ nlohmann::json::number_integer_t, \ - nlohmann::json::number_float_t, nlohmann_traits<> + nlohmann::json::number_float_t, nlohmann_traits TEST(NholmannTest, BasicClaims) { using nholmann_claim = @@ -162,22 +160,102 @@ TEST(NholmannTest, VerifyTokenHS256) { verify.verify(decoded_token); } + using wide_json = nlohmann::basic_json< std::map, std::vector,std::wstring, bool, std::int64_t, std::uint64_t, double>; +struct nlohmann_wide_traits { + using json = wide_json; + + static jwt::json::type get_type(const json &val) { + using jwt::json::type; + + if (val.type() == json::value_t::null) + return type::null; + else if (val.type() == json::value_t::boolean) + return type::boolean; + else if (val.type() == json::value_t::number_integer) + return type::integer; + else if (val.type() == json::value_t::number_float) + return type::number; + else if (val.type() == json::value_t::string) + return type::string; + else if (val.type() == json::value_t::array) + return type::array; + else if (val.type() == json::value_t::object) + return type::object; + else + throw std::logic_error("invalid type"); + } + + static json::object_t as_object(const json &val) { + if (val.type() != json::value_t::object) + throw std::bad_cast(); + return val.get(); + } + + static std::wstring as_string(const json &val) { + if (val.type() != json::value_t::string) + throw std::bad_cast(); + return val.get(); + } + + static json::array_t as_array(const json &val) { + if (val.type() != json::value_t::array) + throw std::bad_cast(); + return val.get(); + } + + static std::set as_set(const json &val) { + std::set res; + for (auto &e : as_array(val)) { + if (val.type() != json::value_t::string) + throw std::bad_cast(); + res.insert(e.get()); + } + return res; + } + + static int64_t as_int(const json &val) { + if (val.type() != json::value_t::number_integer) + throw std::bad_cast(); + return val.get(); + } + + static bool as_bool(const json &val) { + if (val.type() != json::value_t::boolean) + throw std::bad_cast(); + return val.get(); + } + + static double as_number(const json &val) { + if (val.type() != json::value_t::number_float) + throw std::bad_cast(); + return val.get(); + } + + static bool parse(json &val, std::wstring str) { + val = json::parse(str.begin(), str.end()); + return true; + } + + static std::wstring serialize(const json &val) { + return val.dump(); + } +}; #define JWT_NHOLMANN_WIDE_CLAIM_TPL \ wide_json::value_type, wide_json::object_t, \ wide_json::array_t, wide_json::string_t, \ wide_json::boolean_t, \ wide_json::number_integer_t, \ - wide_json::number_float_t, nlohmann_traits + wide_json::number_float_t, nlohmann_wide_traits TEST(NholmannWideTest, AudienceAsString) { std::wstring token = - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0." - "WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4"; + L"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0." + L"WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4"; auto decoded = jwt::decode(token); @@ -193,11 +271,11 @@ TEST(NholmannWideTest, AudienceAsString) { ASSERT_FALSE(decoded.has_issued_at()); ASSERT_FALSE(decoded.has_id()); - ASSERT_EQ("HS256", decoded.get_algorithm()); - ASSERT_EQ("JWT", decoded.get_type()); + ASSERT_EQ(L"HS256", decoded.get_algorithm()); + ASSERT_EQ(L"JWT", decoded.get_type()); auto aud = decoded.get_audience(); ASSERT_EQ(1, aud.size()); - ASSERT_EQ("test", *aud.begin()); + ASSERT_EQ(L"test", *aud.begin()); } TEST(NholmannWideTest, SetArray) { @@ -207,7 +285,7 @@ TEST(NholmannWideTest, SetArray) { 10 }; auto token = jwt::create() - .set_payload_claim("test", jwt::basic_claim(vect.begin(), vect.end())) + .set_payload_claim(L"test", jwt::basic_claim(vect.begin(), vect.end())) .sign(jwt::algorithm::none{}); ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0."); } @@ -219,17 +297,17 @@ TEST(NholmannWideTest, SetObject) { ASSERT_EQ(object.get_type() , jwt::json::type::object); auto token = jwt::create() - .set_payload_claim("namespace", object) + .set_payload_claim(L"namespace", object) .sign(jwt::algorithm::hs256("test")); - ASSERT_EQ(token, "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA"); + ASSERT_EQ(token, L"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA"); } TEST(NholmannWideTest, VerifyTokenHS256) { - std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; + std::wstring token = L"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; auto verify = jwt::verify({}) .allow_algorithm(jwt::algorithm::hs256{ "secret" }) - .with_issuer("auth0"); + .with_issuer(L"auth0"); auto decoded_token = jwt::decode(token); verify.verify(decoded_token);