From 4993fc96a8ff9c8d3ba675e1377e2a694e8b7aa9 Mon Sep 17 00:00:00 2001 From: Christopher McArthur Date: Wed, 3 Jun 2020 20:58:19 -0400 Subject: [PATCH] more type checking --- include/jwt-cpp/jwt.h | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index b45a369..47a69eb 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -939,6 +939,19 @@ namespace jwt { // TODO: Stream operators }; + template + struct is_valid_json_object { + static constexpr auto value = + std::is_same::value && + std::is_same::value; + }; + + template + struct is_valid_json_array { + static constexpr auto value = + std::is_same::value; + }; + struct picojson_traits { static json::type get_type(const picojson::value& val) { using json::type; @@ -998,7 +1011,9 @@ namespace jwt { } /** - * Generic claim wrapper + * \brief a class to store a generic JSON value as claim + * + * The default template parameters use [picojson](https://github.com/kazuho/picojson) * * \tparam value_type : type for a generic JSON value * \tparam object_type : type for a JSON object @@ -1007,6 +1022,8 @@ namespace jwt { * \tparam number_type : type for a JSON floating point number * \tparam integer_type : type for a JSON integer number * \tparam boolean_type : type for a JSON boolean + * + * \see [RFC 7519: JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) */ template::value, "string_type must be a std::string."); static_assert(details::is_valid_json_value::value, "value type must meet basic requirements, default constructor, copyable, moveable"); - // TODO: valid object_type "key string_stype, value value_type, interable" - // TODO: valid array_type "value value_type, iterable" + static_assert(details::is_valid_json_object::value, "object_type much a string_type to value_type container"); + static_assert(details::is_valid_json_array::value, ""); 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&)`"); static_assert(details::supports_as_array::value, "traits must provide `array_type as_array(const value_type&)`"); @@ -1383,6 +1400,18 @@ namespace jwt { } }; + namespace details { + template + using is_decode_signature = typename std::is_same; + + template + struct is_valid_decode { + static constexpr auto value = + std::is_function::value && + is_decode_signature::value; + }; + } + /** * Class containing all information about a decoded token */ @@ -1429,6 +1458,8 @@ namespace jwt { decoded_jwt(const string_type& token, Decode decode) : token(token) { + //static_assert(details::is_valid_decode::value, ""); + auto hdr_end = token.find('.'); if (hdr_end == string_type::npos) throw std::invalid_argument("invalid token supplied");