mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-23 13:09:16 +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
tests
|
@ -15,6 +15,55 @@ TEST (DecodeTest, InvalidFinalDotForNoneAlg)
|
|||
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[]) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue