mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 09:18:33 +00:00
Handle exception for header and payload decode into error code
This commit is contained in:
parent
204092e588
commit
c62a9498d9
5 changed files with 92 additions and 20 deletions
|
@ -306,7 +306,7 @@ void jwt_object::set_parameters(
|
||||||
|
|
||||||
void jwt_object::set_parameters()
|
void jwt_object::set_parameters()
|
||||||
{
|
{
|
||||||
//setinel call
|
//sentinel call
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -465,50 +465,46 @@ jwt_object::three_parts(const string_view enc_str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//====================================================================
|
|
||||||
|
|
||||||
namespace { // Anonymous namespace
|
|
||||||
|
|
||||||
template <typename DecodeParams, typename... Rest>
|
template <typename DecodeParams, typename... Rest>
|
||||||
void set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args)
|
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args)
|
||||||
{
|
{
|
||||||
dparams.leeway = l.get();
|
dparams.leeway = l.get();
|
||||||
set_decode_params(dparams, std::forward<Rest>(args)...);
|
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename DecodeParams, typename... Rest>
|
template <typename DecodeParams, typename... Rest>
|
||||||
void set_decode_params(DecodeParams& dparams, params::detail::verify_param v, Rest&&... args)
|
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::verify_param v, Rest&&... args)
|
||||||
{
|
{
|
||||||
dparams.verify = v.get();
|
dparams.verify = v.get();
|
||||||
set_decode_params(dparams, std::forward<Rest>(args)...);
|
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename DecodeParams, typename... Rest>
|
template <typename DecodeParams, typename... Rest>
|
||||||
void set_decode_params(DecodeParams& dparams, params::detail::issuer_param i, Rest&&... args)
|
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::issuer_param i, Rest&&... args)
|
||||||
{
|
{
|
||||||
dparams.issuer = std::move(i).get();
|
dparams.issuer = std::move(i).get();
|
||||||
dparams.has_issuer = true;
|
dparams.has_issuer = true;
|
||||||
set_decode_params(dparams, std::forward<Rest>(args)...);
|
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename DecodeParams, typename... Rest>
|
template <typename DecodeParams, typename... Rest>
|
||||||
void set_decode_params(DecodeParams& dparams, params::detail::audience_param a, Rest&&... args)
|
void jwt_object::set_decode_params(DecodeParams& dparams, params::detail::audience_param a, Rest&&... args)
|
||||||
{
|
{
|
||||||
dparams.aud = std::move(a).get();
|
dparams.aud = std::move(a).get();
|
||||||
dparams.has_aud = true;
|
dparams.has_aud = true;
|
||||||
set_decode_params(dparams, std::forward<Rest>(args)...);
|
jwt_object::set_decode_params(dparams, std::forward<Rest>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename DecodeParams>
|
template <typename DecodeParams>
|
||||||
void set_decode_params(DecodeParams& dparams)
|
void jwt_object::set_decode_params(DecodeParams& dparams)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // END anonymous namespace
|
//==================================================================
|
||||||
|
|
||||||
template <typename SequenceT, typename... Args>
|
template <typename SequenceT, typename... Args>
|
||||||
jwt_object decode(const string_view enc_str,
|
jwt_object decode(const string_view enc_str,
|
||||||
|
@ -542,7 +538,7 @@ jwt_object decode(const string_view enc_str,
|
||||||
};
|
};
|
||||||
|
|
||||||
decode_params dparams{};
|
decode_params dparams{};
|
||||||
set_decode_params(dparams, std::forward<Args>(args)...);
|
jwt_object::set_decode_params(dparams, std::forward<Args>(args)...);
|
||||||
|
|
||||||
//Signature must have atleast 2 dots
|
//Signature must have atleast 2 dots
|
||||||
auto dot_cnt = std::count_if(std::begin(enc_str), std::end(enc_str),
|
auto dot_cnt = std::count_if(std::begin(enc_str), std::end(enc_str),
|
||||||
|
@ -555,7 +551,13 @@ jwt_object decode(const string_view enc_str,
|
||||||
auto parts = jwt_object::three_parts(enc_str);
|
auto parts = jwt_object::three_parts(enc_str);
|
||||||
|
|
||||||
//throws decode error
|
//throws decode error
|
||||||
obj.header(jwt_header{parts[0]});
|
jwt_header hdr{};
|
||||||
|
hdr.decode(parts[0], ec);
|
||||||
|
if (ec) {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
//obj.header(jwt_header{parts[0]});
|
||||||
|
obj.header(std::move(hdr));
|
||||||
|
|
||||||
//If the algorithm is not NONE, it must not
|
//If the algorithm is not NONE, it must not
|
||||||
//have more than two dots ('.') and the split
|
//have more than two dots ('.') and the split
|
||||||
|
@ -572,7 +574,12 @@ jwt_object decode(const string_view enc_str,
|
||||||
}
|
}
|
||||||
|
|
||||||
//throws decode error
|
//throws decode error
|
||||||
obj.payload(jwt_payload{parts[1]});
|
jwt_payload payload{};
|
||||||
|
payload.decode(parts[1], ec);
|
||||||
|
if (ec) {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
obj.payload(std::move(payload));
|
||||||
|
|
||||||
if (dparams.verify) {
|
if (dparams.verify) {
|
||||||
ec = obj.verify(dparams, algos);
|
ec = obj.verify(dparams, algos);
|
||||||
|
@ -700,8 +707,7 @@ void jwt_throw_exception(const std::error_code& ec)
|
||||||
assert (0 && "Unknown error code or not to be treated as an error");
|
assert (0 && "Unknown error code or not to be treated as an error");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
assert (0 && "Unknown error code category");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // END namespace jwt
|
} // END namespace jwt
|
||||||
|
|
|
@ -720,6 +720,23 @@ private: // private APIs
|
||||||
*/
|
*/
|
||||||
void set_parameters();
|
void set_parameters();
|
||||||
|
|
||||||
|
public: //TODO: Not good
|
||||||
|
/// Decode parameters
|
||||||
|
template <typename DecodeParams, typename... Rest>
|
||||||
|
static void set_decode_params(DecodeParams& dparams, params::detail::leeway_param l, Rest&&... args);
|
||||||
|
|
||||||
|
template <typename DecodeParams, typename... Rest>
|
||||||
|
static void set_decode_params(DecodeParams& dparams, params::detail::verify_param v, Rest&&... args);
|
||||||
|
|
||||||
|
template <typename DecodeParams, typename... Rest>
|
||||||
|
static void set_decode_params(DecodeParams& dparams, params::detail::issuer_param i, Rest&&... args);
|
||||||
|
|
||||||
|
template <typename DecodeParams, typename... Rest>
|
||||||
|
static void set_decode_params(DecodeParams& dparams, params::detail::audience_param a, Rest&&... args);
|
||||||
|
|
||||||
|
template <typename DecodeParams>
|
||||||
|
static void set_decode_params(DecodeParams& dparams);
|
||||||
|
|
||||||
private: // Data Members
|
private: // Data Members
|
||||||
|
|
||||||
/// JWT header section
|
/// JWT header section
|
||||||
|
|
Binary file not shown.
|
@ -15,6 +15,55 @@ TEST (DecodeTest, InvalidFinalDotForNoneAlg)
|
||||||
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::SignatureFormatError));
|
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::SignatureFormatError));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST (DecodeTest, DecodeNoneAlgSign)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
const char* enc_str =
|
||||||
|
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
auto obj = jwt::decode(enc_str, "", algorithms({"none"}), ec, verify(false));
|
||||||
|
EXPECT_TRUE (ec);
|
||||||
|
EXPECT_EQ (ec.value(), static_cast<int>(jwt::AlgorithmErrc::NoneAlgorithmUsed));
|
||||||
|
|
||||||
|
std::cout << obj.payload() << std::endl;
|
||||||
|
|
||||||
|
EXPECT_FALSE (obj.has_claim("iss"));
|
||||||
|
EXPECT_FALSE (obj.has_claim("ISS"));
|
||||||
|
|
||||||
|
EXPECT_TRUE (obj.has_claim("aud"));
|
||||||
|
EXPECT_TRUE (obj.has_claim("exp"));
|
||||||
|
|
||||||
|
EXPECT_EQ (obj.payload().get_claim_value<uint64_t>("exp"), 1513863371);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (DecodeTest, DecodeWrongAlgo)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* enc_str =
|
||||||
|
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
auto obj = jwt::decode(enc_str, "", algorithms({"hs256"}), ec, verify(true));
|
||||||
|
EXPECT_TRUE (ec);
|
||||||
|
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::InvalidAlgorithm));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (DecodeTest, DecodeInvalidHeader)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
const char* enc_str =
|
||||||
|
"ehbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
auto obj = jwt::decode(enc_str, "", algorithms({"hs256"}), ec, verify(true));
|
||||||
|
ASSERT_TRUE (ec);
|
||||||
|
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue