diff --git a/include/jwt/jwt.hpp b/include/jwt/jwt.hpp index 75908a7..20e53df 100644 --- a/include/jwt/jwt.hpp +++ b/include/jwt/jwt.hpp @@ -300,7 +300,9 @@ public: // Exposed APIs */ template >::value> + !std::is_same>::value || + !std::is_same>::value + > > bool add_claim(const string_view cname, T&& cvalue, bool overwrite=false) { @@ -320,6 +322,13 @@ public: // Exposed APIs return true; } + /** + */ + bool add_claim(const string_view cname, const string_view cvalue, bool overwrite=false) + { + return add_claim(cname, std::string{cvalue.data(), cvalue.length()}, overwrite); + } + /** */ bool add_claim(const string_view cname, system_time_t tp, bool overwrite=false) diff --git a/tests/test_jwt_encode b/tests/test_jwt_encode index 20b43f8..18266aa 100755 Binary files a/tests/test_jwt_encode and b/tests/test_jwt_encode differ diff --git a/tests/test_jwt_encode.cc b/tests/test_jwt_encode.cc index 20080bc..fdda646 100644 --- a/tests/test_jwt_encode.cc +++ b/tests/test_jwt_encode.cc @@ -155,6 +155,31 @@ TEST (EncodeTest, StrEncodeHS512WithKey) EXPECT_EQ (enc_str, expected_sign); } +TEST (EncodeTest, StrEncodeChangeAlg) +{ + using namespace jwt::params; + + const char* expected_none_sign = + "eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ."; + + jwt::string_view key = "00112233445566778899"; + + std::map p; + p["aud"] = "rift.io"; + p["sub"] = "nothing much"; + + jwt::jwt_object obj{algorithm(jwt::algorithm::HS512), + secret(key), + payload(std::move(p)) + }; + obj.add_claim("exp", 1513863371); + + obj.header().algo("none"); + std::string enc_str = obj.signature(); + + EXPECT_EQ (expected_none_sign, enc_str); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv);