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
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue