From 474a8708893962c8319d56441cef123e96476e5e Mon Sep 17 00:00:00 2001 From: Christopher McArthur Date: Wed, 20 May 2020 21:24:46 -0400 Subject: [PATCH] adding verification test --- include/jwt-cpp/jwt.h | 7 ++++++- tests/NlohmannTest.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index ef810ec..f3fb5e8 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -1582,7 +1582,12 @@ namespace jwt { } } else if (c.get_type() == json::type::object) { - if( c.to_json().serialize() != jc.to_json().serialize()) + auto serialize = [](const value_type& value) { + std::ostringstream oss; + oss << value; + return oss.str(); + }; + if( serialize(c.to_json()) != serialize(jc.to_json())) throw token_verification_exception("claim " + key + " does not match expected"); } else if (c.get_type() == json::type::string) { diff --git a/tests/NlohmannTest.cpp b/tests/NlohmannTest.cpp index 83d5897..9f67eb6 100644 --- a/tests/NlohmannTest.cpp +++ b/tests/NlohmannTest.cpp @@ -144,3 +144,14 @@ TEST(NholmannTest, SetObject) { .sign(jwt::algorithm::hs256("test")); ASSERT_EQ(token, "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA"); } + +TEST(NholmannTest, VerifyTokenHS256) { + std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; + + auto verify = jwt::verify({}) + .allow_algorithm(jwt::algorithm::hs256{ "secret" }) + .with_issuer("auth0"); + + auto decoded_token = jwt::decode(token); + verify.verify(decoded_token); +}