diff --git a/include/jwt/algorithm.hpp b/include/jwt/algorithm.hpp index 6615bbf..83e8535 100644 --- a/include/jwt/algorithm.hpp +++ b/include/jwt/algorithm.hpp @@ -151,6 +151,68 @@ struct ES512 } //END Namespace algo +/*! + * JWT signing algorithm. + */ +enum class algorithm +{ + NONE = 0, + HS256, + HS384, + HS512, + RS256, + RS384, + RS512, + ES256, + ES384, + ES512, + TERM, +}; + + +/*! + */ +string_view alg_to_str(enum algorithm alg) noexcept +{ + switch (alg) { + case algorithm::HS256: return "HS256"; + case algorithm::HS384: return "HS384"; + case algorithm::HS512: return "HS512"; + case algorithm::RS256: return "RS256"; + case algorithm::RS384: return "RS384"; + case algorithm::RS512: return "RS512"; + case algorithm::ES256: return "ES256"; + case algorithm::ES384: return "ES384"; + case algorithm::ES512: return "ES512"; + case algorithm::TERM: return "TERM"; + case algorithm::NONE: return "NONE"; + default: assert (0 && "Unknown Algorithm"); + }; + + assert (0 && "Code not reached"); +} + +/*! + */ +enum algorithm str_to_alg(const string_view alg) noexcept +{ + if (!alg.length()) return algorithm::NONE; + + if (!strcasecmp(alg.data(), "none")) return algorithm::NONE; + if (!strcasecmp(alg.data(), "hs256")) return algorithm::HS256; + if (!strcasecmp(alg.data(), "hs384")) return algorithm::HS384; + if (!strcasecmp(alg.data(), "hs512")) return algorithm::HS512; + if (!strcasecmp(alg.data(), "rs256")) return algorithm::RS256; + if (!strcasecmp(alg.data(), "rs384")) return algorithm::RS384; + if (!strcasecmp(alg.data(), "rs512")) return algorithm::RS512; + if (!strcasecmp(alg.data(), "es256")) return algorithm::ES256; + if (!strcasecmp(alg.data(), "es384")) return algorithm::ES384; + if (!strcasecmp(alg.data(), "es512")) return algorithm::ES512; + + assert (0 && "Code not reached"); +} + + /*! */ template diff --git a/include/jwt/impl/jwt.ipp b/include/jwt/impl/jwt.ipp index 0e98bb6..e115579 100644 --- a/include/jwt/impl/jwt.ipp +++ b/include/jwt/impl/jwt.ipp @@ -225,6 +225,14 @@ void jwt_object::set_parameters( set_parameters(std::forward(rargs)...); } +template +void jwt_object::set_parameters( + params::detail::algorithm_param alg, Rest&&... rargs) +{ + header_.algo(alg.get()); + set_parameters(std::forward(rargs)...); +} + template void jwt_object::set_parameters( params::detail::headers_param&& header, Rest&&... rargs) diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 0c90549..8241383 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -19,67 +19,6 @@ using json_t = nlohmann::json; namespace jwt { -/*! - * JWT signing algorithm. - */ -enum class algorithm -{ - NONE = 0, - HS256, - HS384, - HS512, - RS256, - RS384, - RS512, - ES256, - ES384, - ES512, - TERM, -}; - - -/*! - */ -string_view alg_to_str(enum algorithm alg) noexcept -{ - switch (alg) { - case algorithm::HS256: return "HS256"; - case algorithm::HS384: return "HS384"; - case algorithm::HS512: return "HS512"; - case algorithm::RS256: return "RS256"; - case algorithm::RS384: return "RS384"; - case algorithm::RS512: return "RS512"; - case algorithm::ES256: return "ES256"; - case algorithm::ES384: return "ES384"; - case algorithm::ES512: return "ES512"; - case algorithm::TERM: return "TERM"; - case algorithm::NONE: return "NONE"; - default: assert (0 && "Unknown Algorithm"); - }; - - assert (0 && "Code not reached"); -} - -/*! - */ -enum algorithm str_to_alg(const string_view alg) noexcept -{ - if (!alg.length()) return algorithm::NONE; - - if (!strcasecmp(alg.data(), "none")) return algorithm::NONE; - if (!strcasecmp(alg.data(), "hs256")) return algorithm::HS256; - if (!strcasecmp(alg.data(), "hs384")) return algorithm::HS384; - if (!strcasecmp(alg.data(), "hs512")) return algorithm::HS512; - if (!strcasecmp(alg.data(), "rs256")) return algorithm::RS256; - if (!strcasecmp(alg.data(), "rs384")) return algorithm::RS384; - if (!strcasecmp(alg.data(), "rs512")) return algorithm::RS512; - if (!strcasecmp(alg.data(), "es256")) return algorithm::ES256; - if (!strcasecmp(alg.data(), "es384")) return algorithm::ES384; - if (!strcasecmp(alg.data(), "es512")) return algorithm::ES512; - - assert (0 && "Code not reached"); -} - /*! */ enum class type @@ -608,6 +547,11 @@ private: // private APIs template void set_parameters(params::detail::secret_param, Rest&&...); + /** + */ + template + void set_parameters(params::detail::algorithm_param, Rest&&...); + /** */ template diff --git a/include/jwt/parameters.hpp b/include/jwt/parameters.hpp index 672bb3e..07407cc 100644 --- a/include/jwt/parameters.hpp +++ b/include/jwt/parameters.hpp @@ -5,6 +5,7 @@ #include #include +#include "jwt/algorithm.hpp" #include "jwt/detail/meta.hpp" #include "jwt/string_view.hpp" @@ -55,6 +56,31 @@ struct secret_param string_view secret_; }; +/** + * Parameter for providing the algorithm to use. + * The parameter can accept either the string representation + * or the enum class. + * + * Modeled as ParameterConcept. + */ +struct algorithm_param +{ + algorithm_param(const string_view alg) + : alg_(str_to_alg(alg)) + {} + + algorithm_param(jwt::algorithm alg) + : alg_(alg) + {} + + jwt::algorithm get() const noexcept + { + return alg_; + } + + typename jwt::algorithm alg_; +}; + /** * Parameter for providing additional headers. * Takes a mapping concept representing @@ -108,11 +134,25 @@ payload(const param_init_list_t& kvs) /** */ -detail::secret_param secret(string_view sv) +detail::secret_param secret(const string_view sv) { return { sv }; } +/** + */ +detail::algorithm_param algorithm(const string_view sv) +{ + return { sv }; +} + +/** + */ +detail::algorithm_param algorithm(jwt::algorithm alg) +{ + return { alg }; +} + /** */ template > +detail::headers_param> headers(const param_init_list_t& kvs) { std::map m; diff --git a/include/jwt/test/test_jwt_object b/include/jwt/test/test_jwt_object index d228898..432dd74 100755 Binary files a/include/jwt/test/test_jwt_object and b/include/jwt/test/test_jwt_object differ