mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-14 16:58:34 +00:00
Change signature for has_claim_with_value API
This commit is contained in:
parent
c62a9498d9
commit
d7e4add937
4 changed files with 47 additions and 4 deletions
|
@ -421,12 +421,20 @@ public: // Exposed APIs
|
|||
/**
|
||||
*/
|
||||
template <typename T>
|
||||
bool has_claim_with_value(const std::string& cname, T&& cvalue) const
|
||||
bool has_claim_with_value(const string_view cname, T&& cvalue) const
|
||||
{
|
||||
auto itr = claim_names_.find(cname);
|
||||
if (itr == claim_names_.end()) return false;
|
||||
|
||||
return (cvalue == payload_[cname]);
|
||||
return (cvalue == payload_[cname.data()]);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
template <typename T>
|
||||
bool has_claim_with_value(registered_claims cname, T&& value) const
|
||||
{
|
||||
return has_claim_with_value(reg_claims_to_str(cname), std::forward<T>(value));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Binary file not shown.
|
@ -64,6 +64,41 @@ TEST (DecodeTest, DecodeInvalidHeader)
|
|||
|
||||
}
|
||||
|
||||
TEST (DecodeTest, DecodeInvalidPayload)
|
||||
{
|
||||
using namespace jwt::params;
|
||||
|
||||
const char* enc_str =
|
||||
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyfhuWcikiJyaWZ0LmlvIiwiZXhwIsexNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ.";
|
||||
|
||||
std::error_code ec;
|
||||
auto obj = jwt::decode(enc_str, "", algorithms({"none"}), ec, verify(true));
|
||||
ASSERT_TRUE (ec);
|
||||
|
||||
EXPECT_EQ (ec.value(), static_cast<int>(jwt::DecodeErrc::JsonParseError));
|
||||
}
|
||||
|
||||
TEST (DecodeTest, DecodeHS256)
|
||||
{
|
||||
using namespace jwt::params;
|
||||
|
||||
const char* enc_str =
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
|
||||
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0."
|
||||
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk";
|
||||
|
||||
std::error_code ec;
|
||||
auto obj = jwt::decode(enc_str, "secret", algorithms({"none", "hs256"}), ec, verify(false));
|
||||
ASSERT_FALSE (ec);
|
||||
|
||||
EXPECT_TRUE (obj.has_claim("iss"));
|
||||
EXPECT_TRUE (obj.payload().has_claim_with_value("iss", "arun.muralidharan"));
|
||||
EXPECT_TRUE (obj.has_claim("IAT"));
|
||||
EXPECT_TRUE (obj.payload().has_claim_with_value(jwt::registered_claims::issued_at, 1513862371));
|
||||
|
||||
EXPECT_FALSE (obj.payload().has_claim_with_value(jwt::registered_claims::issued_at, 1513862372));
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include "gtest/gtest.h"
|
||||
#include "jwt/jwt.hpp"
|
||||
|
||||
TEST (EncodeTest, StrEncodeHS2561)
|
||||
TEST (EncodeTest, StrEncodeHS256_1)
|
||||
{
|
||||
using namespace jwt::params;
|
||||
|
||||
|
@ -30,7 +30,7 @@ TEST (EncodeTest, StrEncodeHS2561)
|
|||
EXPECT_EQ (enc_str, expected_sign);
|
||||
}
|
||||
|
||||
TEST (EncodeTest, StrEncodeHS2562)
|
||||
TEST (EncodeTest, StrEncodeHS256_2)
|
||||
{
|
||||
using namespace jwt::params;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue