diff --git a/include/jwt/algorithm.hpp b/include/jwt/algorithm.hpp index d27909d..799178b 100644 --- a/include/jwt/algorithm.hpp +++ b/include/jwt/algorithm.hpp @@ -52,12 +52,12 @@ using sign_result_t = std::pair; /// The result type of verification function using verify_result_t = std::pair; /// The function pointer type for the signing function -using sign_func_t = sign_result_t (*) (const string_view key, - const string_view data); +using sign_func_t = sign_result_t (*) (const jwt::string_view key, + const jwt::string_view data); /// The function pointer type for the verifying function -using verify_func_t = verify_result_t (*) (const string_view key, - const string_view head, - const string_view jwt_sign); +using verify_func_t = verify_result_t (*) (const jwt::string_view key, + const jwt::string_view head, + const jwt::string_view jwt_sign); namespace algo { @@ -216,7 +216,7 @@ enum class algorithm * Convert the algorithm enum class type to * its stringified form. */ -string_view alg_to_str(enum algorithm alg) noexcept +jwt::string_view alg_to_str(enum algorithm alg) noexcept { switch (alg) { case algorithm::HS256: return "HS256"; @@ -240,7 +240,7 @@ string_view alg_to_str(enum algorithm alg) noexcept * Convert stringified algorithm to enum class. * The string comparison is case insesitive. */ -enum algorithm str_to_alg(const string_view alg) noexcept +enum algorithm str_to_alg(const jwt::string_view alg) noexcept { if (!alg.length()) return algorithm::NONE; @@ -339,7 +339,7 @@ struct HMACSign * Any allocation failure will result in jwt::MemoryAllocationException * being thrown. */ - static sign_result_t sign(const string_view key, const string_view data) + static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) { std::string sign; sign.resize(EVP_MAX_MD_SIZE); @@ -384,7 +384,7 @@ struct HMACSign * being thrown. */ static verify_result_t - verify(const string_view key, const string_view head, const string_view sign); + verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign); }; @@ -412,7 +412,7 @@ struct HMACSign /** * Basically a no-op. Sets the error code to NoneAlgorithmUsed. */ - static sign_result_t sign(const string_view key, const string_view data) + static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) { (void)key; (void)data; @@ -426,7 +426,7 @@ struct HMACSign * Basically a no-op. Sets the error code to NoneAlgorithmUsed. */ static verify_result_t - verify(const string_view key, const string_view head, const string_view sign) + verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign) { (void)key; (void)head; @@ -469,7 +469,7 @@ public: * Any allocation failure would be thrown out as * jwt::MemoryAllocationException. */ - static sign_result_t sign(const string_view key, const string_view data) + static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) { std::error_code ec{}; @@ -493,21 +493,21 @@ public: /** */ static verify_result_t - verify(const string_view key, const string_view head, const string_view sign); + verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign); private: /*! */ - static EVP_PKEY* load_key(const string_view key, std::error_code& ec); + static EVP_PKEY* load_key(const jwt::string_view key, std::error_code& ec); /*! */ - static std::string evp_digest(EVP_PKEY* pkey, const string_view data, std::error_code& ec); + static std::string evp_digest(EVP_PKEY* pkey, const jwt::string_view data, std::error_code& ec); /*! */ - static std::string public_key_ser(EVP_PKEY* pkey, string_view sign, std::error_code& ec); + static std::string public_key_ser(EVP_PKEY* pkey, jwt::string_view sign, std::error_code& ec); //ATTN: Below 2 functions //are Taken from https://github.com/nginnever/zogminer/issues/39 diff --git a/include/jwt/impl/algorithm.ipp b/include/jwt/impl/algorithm.ipp index 7a6cf4b..cc6fd75 100644 --- a/include/jwt/impl/algorithm.ipp +++ b/include/jwt/impl/algorithm.ipp @@ -27,9 +27,9 @@ namespace jwt { template verify_result_t HMACSign::verify( - const string_view key, - const string_view head, - const string_view jwt_sign) + const jwt::string_view key, + const jwt::string_view head, + const jwt::string_view jwt_sign) { std::error_code ec{}; @@ -80,7 +80,7 @@ verify_result_t HMACSign::verify( auto new_len = jwt::base64_uri_encode(&cbuf[0], cbuf.length()); cbuf.resize(new_len); - bool ret = (string_view{cbuf} == jwt_sign); + bool ret = (jwt::string_view{cbuf} == jwt_sign); return { ret, ec }; } @@ -88,9 +88,9 @@ verify_result_t HMACSign::verify( template verify_result_t PEMSign::verify( - const string_view key, - const string_view head, - const string_view jwt_sign) + const jwt::string_view key, + const jwt::string_view head, + const jwt::string_view jwt_sign) { std::error_code ec{}; std::string dec_sig = base64_uri_decode(jwt_sign.data(), jwt_sign.length()); @@ -191,7 +191,7 @@ verify_result_t PEMSign::verify( template EVP_PKEY* PEMSign::load_key( - const string_view key, + const jwt::string_view key, std::error_code& ec) { ec.clear(); @@ -224,7 +224,7 @@ EVP_PKEY* PEMSign::load_key( template std::string PEMSign::evp_digest( EVP_PKEY* pkey, - const string_view data, + const jwt::string_view data, std::error_code& ec) { ec.clear(); @@ -270,7 +270,7 @@ std::string PEMSign::evp_digest( template std::string PEMSign::public_key_ser( EVP_PKEY* pkey, - string_view sign, + jwt::string_view sign, std::error_code& ec) { // Get the EC_KEY representing a public key and diff --git a/include/jwt/impl/jwt.ipp b/include/jwt/impl/jwt.ipp index 337ba7d..b6161fa 100644 --- a/include/jwt/impl/jwt.ipp +++ b/include/jwt/impl/jwt.ipp @@ -63,7 +63,7 @@ std::ostream& operator<< (std::ostream& os, const T& obj) //======================================================================== -void jwt_header::decode(const string_view enc_str, std::error_code& ec) +void jwt_header::decode(const jwt::string_view enc_str, std::error_code& ec) { ec.clear(); std::string json_str = base64_decode(enc_str); @@ -107,7 +107,7 @@ void jwt_header::decode(const string_view enc_str, std::error_code& ec) return; } -void jwt_header::decode(const string_view enc_str) +void jwt_header::decode(const jwt::string_view enc_str) { std::error_code ec; decode(enc_str, ec); @@ -117,7 +117,7 @@ void jwt_header::decode(const string_view enc_str) return; } -void jwt_payload::decode(const string_view enc_str, std::error_code& ec) +void jwt_payload::decode(const jwt::string_view enc_str, std::error_code& ec) { ec.clear(); std::string json_str = base64_decode(enc_str); @@ -139,7 +139,7 @@ void jwt_payload::decode(const string_view enc_str, std::error_code& ec) return; } -void jwt_payload::decode(const string_view enc_str) +void jwt_payload::decode(const jwt::string_view enc_str) { std::error_code ec; decode(enc_str, ec); @@ -185,8 +185,8 @@ std::string jwt_signature::encode(const jwt_header& header, } verify_result_t jwt_signature::verify(const jwt_header& header, - const string_view hdr_pld_sign, - const string_view jwt_sign) + const jwt::string_view hdr_pld_sign, + const jwt::string_view jwt_sign) { verify_func_t verify_fn = get_verify_algorithm_impl(header); return verify_fn(key_, hdr_pld_sign, jwt_sign); @@ -336,13 +336,13 @@ template >::value> > -jwt_object& jwt_object::add_claim(const string_view name, T&& value) +jwt_object& jwt_object::add_claim(const jwt::string_view name, T&& value) { payload_.add_claim(name, std::forward(value)); return *this; } -jwt_object& jwt_object::add_claim(const string_view name, system_time_t tp) +jwt_object& jwt_object::add_claim(const jwt::string_view name, system_time_t tp) { return add_claim( name, @@ -351,7 +351,7 @@ jwt_object& jwt_object::add_claim(const string_view name, system_time_t tp) ); } -jwt_object& jwt_object::remove_claim(const string_view name) +jwt_object& jwt_object::remove_claim(const jwt::string_view name) { payload_.remove_claim(name); return *this; @@ -475,22 +475,22 @@ std::error_code jwt_object::verify( } -std::array -jwt_object::three_parts(const string_view enc_str) +std::array +jwt_object::three_parts(const jwt::string_view enc_str) { - std::array result; + std::array result; size_t fpos = enc_str.find_first_of('.'); - assert (fpos != string_view::npos); + assert (fpos != jwt::string_view::npos); - result[0] = string_view{&enc_str[0], fpos}; + result[0] = jwt::string_view{&enc_str[0], fpos}; size_t spos = enc_str.find_first_of('.', fpos + 1); - result[1] = string_view{&enc_str[fpos + 1], spos - fpos - 1}; + result[1] = jwt::string_view{&enc_str[fpos + 1], spos - fpos - 1}; if (spos != enc_str.length()) { - result[2] = string_view{&enc_str[spos + 1], enc_str.length() - spos - 1}; + result[2] = jwt::string_view{&enc_str[spos + 1], enc_str.length() - spos - 1}; } return result; @@ -543,7 +543,7 @@ void jwt_object::set_decode_params(DecodeParams& dparams) //================================================================== template -jwt_object decode(const string_view enc_str, +jwt_object decode(const jwt::string_view enc_str, const params::detail::algorithms_param& algos, std::error_code& ec, Args&&... args) @@ -663,7 +663,7 @@ jwt_object decode(const string_view enc_str, template -jwt_object decode(const string_view enc_str, +jwt_object decode(const jwt::string_view enc_str, const params::detail::algorithms_param& algos, Args&&... args) { diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 1ff8611..b057421 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -57,7 +57,7 @@ enum class type * Converts a string representing a value of type * `enum class type` into its actual type. */ -enum type str_to_type(const string_view typ) noexcept +enum type str_to_type(const jwt::string_view typ) noexcept { assert (typ.length() && "Empty type string"); @@ -71,7 +71,7 @@ enum type str_to_type(const string_view typ) noexcept * Converts an instance of type `enum class type` * to its string equivalent. */ -string_view type_to_str(enum type typ) +jwt::string_view type_to_str(enum type typ) { switch (typ) { case type::JWT: return "JWT"; @@ -108,7 +108,7 @@ enum class registered_claims * Converts an instance of type `enum class registered_claims` * to its string equivalent representation. */ -string_view reg_claims_to_str(enum registered_claims claim) noexcept +jwt::string_view reg_claims_to_str(enum registered_claims claim) noexcept { switch (claim) { case registered_claims::expiration: return "exp"; @@ -216,7 +216,7 @@ struct base64_enc_dec /** * Does URL safe base64 decoding. */ - std::string base64_decode(const string_view encoded_str) + std::string base64_decode(const jwt::string_view encoded_str) { return jwt::base64_uri_decode(encoded_str.data(), encoded_str.length()); } @@ -249,7 +249,7 @@ public: // 'tors /** * Construct the header from an encoded string. */ - jwt_header(const string_view enc_str) + jwt_header(const jwt::string_view enc_str) { this->decode(enc_str); } @@ -276,7 +276,7 @@ public: // Exposed APIs /** * Set the algorithm. String overload. */ - void algo(const string_view sv) + void algo(const jwt::string_view sv) { alg_ = str_to_alg(sv.data()); } @@ -331,7 +331,7 @@ public: // Exposed APIs * are not translated to an error_code. The API would * still throw an exception in those cases. */ - void decode(const string_view enc_str, std::error_code& ec); + void decode(const jwt::string_view enc_str, std::error_code& ec); /** * Exception throwing API version of decode. @@ -339,7 +339,7 @@ public: // Exposed APIs * Could also throw memory allocation failure * exceptions. */ - void decode(const string_view enc_str); + void decode(const jwt::string_view enc_str); /** * Creates a `json_t` object this class instance. @@ -383,7 +383,7 @@ public: // 'tors * Construct the payload from an encoded string. * TODO: Throw an exception in case of error. */ - jwt_payload(const string_view enc_str) + jwt_payload(const jwt::string_view enc_str) { this->decode(enc_str); } @@ -414,7 +414,7 @@ public: // Exposed APIs !std::is_same>::value > > - bool add_claim(const string_view cname, T&& cvalue, bool overwrite=false) + bool add_claim(const jwt::string_view cname, T&& cvalue, bool overwrite=false) { // Duplicate claim names not allowed // if overwrite flag is set to true. @@ -440,7 +440,7 @@ public: // Exposed APIs * Adds a claim. * This overload takes string claim value. */ - bool add_claim(const string_view cname, const string_view cvalue, bool overwrite=false) + bool add_claim(const jwt::string_view cname, const jwt::string_view cvalue, bool overwrite=false) { return add_claim(cname, std::string{cvalue.data(), cvalue.length()}, overwrite); } @@ -450,7 +450,7 @@ public: // Exposed APIs * This overload takes system_time_t claim value. * @note: Useful for providing timestamp as the claim value. */ - bool add_claim(const string_view cname, system_time_t tp, bool overwrite=false) + bool add_claim(const jwt::string_view cname, system_time_t tp, bool overwrite=false) { return add_claim( cname, @@ -516,7 +516,7 @@ public: // Exposed APIs * JSON library will throw an exception. */ template - decltype(auto) get_claim_value(const string_view cname) const + decltype(auto) get_claim_value(const jwt::string_view cname) const { return payload_[cname.data()].get(); } @@ -539,7 +539,7 @@ public: // Exposed APIs /** * Remove a claim identified by a claim name. */ - bool remove_claim(const string_view cname) + bool remove_claim(const jwt::string_view cname) { auto itr = claim_names_.find(cname); if (itr == claim_names_.end()) return false; @@ -568,7 +568,7 @@ public: // Exposed APIs //TODO: Not all libc++ version agrees with this //because count() is not made const for is_transparent //based overload - bool has_claim(const string_view cname) const noexcept + bool has_claim(const jwt::string_view cname) const noexcept { return claim_names_.count(cname); } @@ -589,7 +589,7 @@ public: // Exposed APIs * value in the payload. */ template - bool has_claim_with_value(const string_view cname, T&& cvalue) const + bool has_claim_with_value(const jwt::string_view cname, T&& cvalue) const { auto itr = claim_names_.find(cname); if (itr == claim_names_.end()) return false; @@ -627,7 +627,7 @@ public: // Exposed APIs * @note: Allocation failures are still thrown out * as exceptions. */ - void decode(const string_view enc_str, std::error_code& ec); + void decode(const jwt::string_view enc_str, std::error_code& ec); /** * Overload of decode API which throws exception @@ -635,7 +635,7 @@ public: // Exposed APIs * * Throws DecodeError on failure. */ - void decode(const string_view enc_str); + void decode(const jwt::string_view enc_str); /** * Creates a JSON object of the payload. @@ -663,19 +663,19 @@ private: return (ret < 0); } - bool operator()(const string_view lhs, const string_view rhs) const + bool operator()(const jwt::string_view lhs, const jwt::string_view rhs) const { int ret = strcasecmp(lhs.data(), rhs.data()); return (ret < 0); } - bool operator()(const std::string& lhs, const string_view rhs) const + bool operator()(const std::string& lhs, const jwt::string_view rhs) const { int ret = strcasecmp(lhs.data(), rhs.data()); return (ret < 0); } - bool operator()(const string_view lhs, const std::string& rhs) const + bool operator()(const jwt::string_view lhs, const std::string& rhs) const { int ret = strcasecmp(lhs.data(), rhs.data()); return (ret < 0); @@ -705,7 +705,7 @@ public: // 'tors /** * Constructor which takes the key. */ - jwt_signature(const string_view key) + jwt_signature(const jwt::string_view key) : key_(key.data(), key.length()) { } @@ -731,8 +731,8 @@ public: // Exposed APIs * of bool and error_code. */ verify_result_t verify(const jwt_header& header, - const string_view hdr_pld_sign, - const string_view jwt_sign); + const jwt::string_view hdr_pld_sign, + const jwt::string_view jwt_sign); private: // Private implementation /*! @@ -797,8 +797,8 @@ public: // Exposed static APIs * @note: Instead of actually splitting the API * simply provides an array of view. */ - static std::array - three_parts(const string_view enc_str); + static std::array + three_parts(const jwt::string_view enc_str); public: // Exposed APIs /** @@ -878,7 +878,7 @@ public: // Exposed APIs /** * Set the secret to be used for signing. */ - void secret(const string_view sv) + void secret(const jwt::string_view sv) { secret_.assign(sv.data(), sv.length()); } @@ -888,7 +888,7 @@ public: // Exposed APIs * @note: See `jwt_payload::add_claim` for more details. */ template - jwt_object& add_claim(const string_view name, T&& value); + jwt_object& add_claim(const jwt::string_view name, T&& value); /** * Provides the glue interface for adding claim. @@ -898,7 +898,7 @@ public: // Exposed APIs * Specialization for time points. * Eg: Users can set `exp` claim to `chrono::system_clock::now()`. */ - jwt_object& add_claim(const string_view name, system_time_t time_point); + jwt_object& add_claim(const jwt::string_view name, system_time_t time_point); /** * Provides the glue interface for adding claim. @@ -917,7 +917,7 @@ public: // Exposed APIs * * @note: See `jwt_payload::remove_claim` for more details. */ - jwt_object& remove_claim(const string_view name); + jwt_object& remove_claim(const jwt::string_view name); /** * Provides the glue interface for removing claim. @@ -935,7 +935,7 @@ public: // Exposed APIs * * @note: See `jwt_payload::has_claim` for more details. */ - bool has_claim(const string_view cname) const noexcept + bool has_claim(const jwt::string_view cname) const noexcept { return payload().has_claim(cname); } @@ -1064,7 +1064,7 @@ private: // Data Members * NOTE: It is case-sensitive */ template -jwt_object decode(const string_view enc_str, +jwt_object decode(const jwt::string_view enc_str, const params::detail::algorithms_param& algos, std::error_code& ec, Args&&... args); @@ -1074,7 +1074,7 @@ jwt_object decode(const string_view enc_str, * This version reports error back by throwing exceptions. */ template -jwt_object decode(const string_view enc_str, +jwt_object decode(const jwt::string_view enc_str, const params::detail::algorithms_param& algos, Args&&... args); diff --git a/tests/test_jwt_decode b/tests/test_jwt_decode index 771249c..d44af51 100755 Binary files a/tests/test_jwt_decode and b/tests/test_jwt_decode differ diff --git a/tests/test_jwt_decode_verify b/tests/test_jwt_decode_verify index dbbdc47..1d9989a 100755 Binary files a/tests/test_jwt_decode_verify and b/tests/test_jwt_decode_verify differ diff --git a/tests/test_jwt_encode b/tests/test_jwt_encode index c4122a2..fd453c2 100755 Binary files a/tests/test_jwt_encode and b/tests/test_jwt_encode differ