From 7169e4d0b8ee616f7a7aac9678d5bcffc451fbf3 Mon Sep 17 00:00:00 2001 From: Dominik Thalhammer Date: Mon, 18 Feb 2019 16:13:26 +0100 Subject: [PATCH] Bugfix invalid aud handling aud field might be a string instead of a set if there is only one. closes #13 --- ClaimTest.cpp | 25 +++++++++++++++++++++++++ include/jwt-cpp/jwt.h | 6 +++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 ClaimTest.cpp diff --git a/ClaimTest.cpp b/ClaimTest.cpp new file mode 100644 index 0000000..589f001 --- /dev/null +++ b/ClaimTest.cpp @@ -0,0 +1,25 @@ +#include +#include "include/jwt-cpp/jwt.h" + +TEST(ClaimTest, AudienceAsString) { + std::string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0.WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4"; + auto decoded = jwt::decode(token); + + ASSERT_TRUE(decoded.has_algorithm()); + ASSERT_TRUE(decoded.has_type()); + ASSERT_FALSE(decoded.has_content_type()); + ASSERT_FALSE(decoded.has_key_id()); + ASSERT_FALSE(decoded.has_issuer()); + ASSERT_FALSE(decoded.has_subject()); + ASSERT_TRUE(decoded.has_audience()); + ASSERT_FALSE(decoded.has_expires_at()); + ASSERT_FALSE(decoded.has_not_before()); + ASSERT_FALSE(decoded.has_issued_at()); + ASSERT_FALSE(decoded.has_id()); + + ASSERT_EQ("HS256", decoded.get_algorithm()); + ASSERT_EQ("JWT", decoded.get_type()); + auto aud = decoded.get_audience(); + ASSERT_EQ(1, aud.size()); + ASSERT_EQ("test", *aud.begin()); +} \ No newline at end of file diff --git a/include/jwt-cpp/jwt.h b/include/jwt-cpp/jwt.h index 49fc945..ec54856 100644 --- a/include/jwt-cpp/jwt.h +++ b/include/jwt-cpp/jwt.h @@ -914,7 +914,11 @@ namespace jwt { * \throws std::runtime_error If claim was not present * \throws std::bad_cast Claim was present but not a set (Should not happen in a valid token) */ - std::set get_audience() const { return get_payload_claim("aud").as_set(); } + std::set get_audience() const { + auto aud = get_payload_claim("aud"); + if(aud.get_type() == jwt::claim::type::string) return { aud.as_string()}; + else return aud.as_set(); + } /** * Get expires claim * \return expires as a date in utc