mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 01:08:31 +00:00
Add algorithm parameter
This commit is contained in:
parent
ffd6280026
commit
da6d4932c3
5 changed files with 117 additions and 63 deletions
|
@ -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 <typename Hasher>
|
||||
|
|
|
@ -225,6 +225,14 @@ void jwt_object::set_parameters(
|
|||
set_parameters(std::forward<Rest>(rargs)...);
|
||||
}
|
||||
|
||||
template <typename... Rest>
|
||||
void jwt_object::set_parameters(
|
||||
params::detail::algorithm_param alg, Rest&&... rargs)
|
||||
{
|
||||
header_.algo(alg.get());
|
||||
set_parameters(std::forward<Rest>(rargs)...);
|
||||
}
|
||||
|
||||
template <typename Map, typename... Rest>
|
||||
void jwt_object::set_parameters(
|
||||
params::detail::headers_param<Map>&& header, Rest&&... rargs)
|
||||
|
|
|
@ -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 <typename... Rest>
|
||||
void set_parameters(params::detail::secret_param, Rest&&...);
|
||||
|
||||
/**
|
||||
*/
|
||||
template <typename... Rest>
|
||||
void set_parameters(params::detail::algorithm_param, Rest&&...);
|
||||
|
||||
/**
|
||||
*/
|
||||
template <typename M, typename... Rest>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <utility>
|
||||
#include <unordered_map>
|
||||
|
||||
#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 <typename MappingConcept,
|
||||
|
@ -125,7 +165,7 @@ headers(MappingConcept&& mc)
|
|||
|
||||
/**
|
||||
*/
|
||||
detail::headers_param<std::map<std::string, std::string>>
|
||||
detail::headers_param<std::map<std::string, std::string>>
|
||||
headers(const param_init_list_t& kvs)
|
||||
{
|
||||
std::map<std::string, std::string> m;
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue