mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 01:08:31 +00:00
Changed decode function signature
This commit is contained in:
parent
09bc72c9f7
commit
a4a4bd5dff
6 changed files with 124 additions and 14 deletions
|
@ -27,6 +27,11 @@ using void_t = typename make_void<T...>::type;
|
|||
*/
|
||||
struct empty_type {};
|
||||
|
||||
/**
|
||||
* A type list.
|
||||
*/
|
||||
template <typename... T> struct list{};
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
|
@ -118,6 +123,43 @@ struct is_parameter_concept<T,
|
|||
|
||||
/**
|
||||
*/
|
||||
/*
|
||||
template <typename T, typename=void>
|
||||
struct is_sequence_concept: std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_sequence_concept<>: std::true_type
|
||||
{
|
||||
};
|
||||
*/
|
||||
|
||||
/**
|
||||
* Find if a type is present in the typelist.
|
||||
* Eg: has_type<int, list<int, char, float>>{} == true
|
||||
* has_type<long, list<int, char, float>>{} == false
|
||||
*/
|
||||
template <typename F, typename T> struct has_type;
|
||||
|
||||
template <typename F>
|
||||
struct has_type<F, list<>>: std::false_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename F, typename... T>
|
||||
struct has_type<F, list<F, T...>>: std::true_type
|
||||
{
|
||||
};
|
||||
|
||||
template <typename F, typename H, typename... T>
|
||||
struct has_type<F, list<H,T...>>: has_type<F, list<T...>>
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* A pack of bools for the bool trick.
|
||||
*/
|
||||
template <bool... V>
|
||||
struct bool_pack {};
|
||||
|
||||
|
|
|
@ -351,26 +351,69 @@ jwt_object::three_parts(const string_view enc_str)
|
|||
|
||||
//====================================================================
|
||||
|
||||
jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool validate)
|
||||
namespace { // Anonymous namespace
|
||||
|
||||
template <typename DecodeParams, typename... Rest>
|
||||
void set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args)
|
||||
{
|
||||
//TODO: implement error_code
|
||||
jwt_object jobj;
|
||||
dparams.leeway = l.get();
|
||||
set_decode_params(dparams, std::forward<Rest>(args)...);
|
||||
return;
|
||||
}
|
||||
|
||||
auto parts = jwt_object::three_parts(encoded_str);
|
||||
template <typename DecodeParams, typename... Rest>
|
||||
void set_decode_params(DecodeParams& dparams, params::detail::verify_param v, Rest&&... args)
|
||||
{
|
||||
dparams.verify = v.get();
|
||||
set_decode_params(dparams, std::forward<Rest>(args)...);
|
||||
return;
|
||||
}
|
||||
|
||||
template <typename DecodeParams>
|
||||
void set_decode_params(DecodeParams& dparams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
} // END anonymous namespace
|
||||
|
||||
template <typename SequenceT, typename... Args>
|
||||
jwt_object decode(const string_view enc_str,
|
||||
const string_view key,
|
||||
const params::detail::algorithms_param<SequenceT>& algos,
|
||||
Args&&... args)
|
||||
{
|
||||
if (algos.get().size() == 0) {
|
||||
throw DecodeError("Algorithms list cannot be empty");
|
||||
}
|
||||
|
||||
struct decode_params
|
||||
{
|
||||
/// Verify parameter. Defaulted to true.
|
||||
bool verify = true;
|
||||
/// Leeway parameter. Defaulted to zero seconds.
|
||||
uint32_t leeway = 0;
|
||||
};
|
||||
|
||||
decode_params dparams{};
|
||||
set_decode_params(dparams, std::forward<Args>(args)...);
|
||||
|
||||
jwt_object obj;
|
||||
|
||||
auto parts = jwt_object::three_parts(enc_str);
|
||||
//throws decode error
|
||||
jobj.header(jwt_header{parts[0]});
|
||||
obj.header(jwt_header{parts[0]});
|
||||
//throws decode error
|
||||
jobj.payload(jwt_payload{parts[1]});
|
||||
obj.payload(jwt_payload{parts[1]});
|
||||
|
||||
jwt_signature jsign{key};
|
||||
|
||||
//length of the encoded header and payload only.
|
||||
//Addition of '1' to account for the '.' character.
|
||||
// Length of the encoded header and payload only.
|
||||
// Addition of '1' to account for the '.' character.
|
||||
auto l = parts[0].length() + 1 + parts[1].length();
|
||||
auto res = jsign.verify(jobj.header(), encoded_str.substr(0, l), parts[2]);
|
||||
auto res = jsign.verify(obj.header(), enc_str.substr(0, l), parts[2]);
|
||||
|
||||
return jobj;
|
||||
return obj;
|
||||
}
|
||||
|
||||
} // END namespace jwt
|
||||
|
|
|
@ -629,8 +629,11 @@ jwt_object jwt_decode(const string_view encoded_str, const string_view key, bool
|
|||
|
||||
/**
|
||||
*/
|
||||
template <typename... Args>
|
||||
jwt_object decode(const string_view enc_str, const string_view key, Args&&... args);
|
||||
template <typename SequenceT, typename... Args>
|
||||
jwt_object decode(const string_view enc_str,
|
||||
const string_view key,
|
||||
const params::detail::algorithms_param<SequenceT>& algos,
|
||||
Args&&... args);
|
||||
|
||||
|
||||
} // END namespace jwt
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define CPP_JWT_PARAMETERS_HPP
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <unordered_map>
|
||||
|
||||
|
@ -146,6 +147,7 @@ struct leeway_param
|
|||
|
||||
// Useful typedef
|
||||
using param_init_list_t = std::initializer_list<std::pair<jwt::string_view, jwt::string_view>>;
|
||||
using param_seq_list_t = std::initializer_list<jwt::string_view>;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -238,6 +240,26 @@ leeway(uint32_t l)
|
|||
return { l };
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
detail::algorithms_param<std::vector<std::string>>
|
||||
algorithms(const param_seq_list_t& seq)
|
||||
{
|
||||
std::vector<std::string> vec;
|
||||
vec.reserve(seq.size());
|
||||
|
||||
for (const auto& e: seq) { vec.emplace_back(e.data(), e.length()); }
|
||||
|
||||
return { std::move(vec) };
|
||||
}
|
||||
|
||||
template <typename SequenceConcept>
|
||||
detail::algorithms_param<SequenceConcept>
|
||||
algorithms(SequenceConcept&& sc)
|
||||
{
|
||||
return { std::forward<SequenceConcept>(sc) };
|
||||
}
|
||||
|
||||
} // END namespace params
|
||||
} // END namespace jwt
|
||||
|
||||
|
|
Binary file not shown.
|
@ -44,7 +44,7 @@ void basic_jwt_object_test()
|
|||
obj3.secret("secret");
|
||||
obj3.header().algo("hs256");
|
||||
|
||||
auto dec_obj = jwt::jwt_decode(obj3.signature(), "secret");
|
||||
auto dec_obj = jwt::decode(obj3.signature(), "secret", algorithms({"hs256"}));
|
||||
}
|
||||
|
||||
void jwt_object_pem_test()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue