mirror of
https://github.com/allankoechke/jwt-cpp.git
synced 2026-11-30 10:18:40 +00:00
* quick poc for Thalhammer#174 * Copy paste * Rename include/jwt-cpp/traits/traits.h to include/jwt-cpp/traits/danielaparker-jsoncons/traits.h * Create traits.h * adding a namespace * mustache + action to automate defaults generation since they should always be the same * fix order * traits tests should use traits * playing with header order to make clang happy * install all possible traits switch badge name to sha * Update CMakeLists.txt * Update traits.h * try commenting corrected code * rm typescript * make sure to implace edit * dont comment when no diff * apply linting * fixup boost-json traits * move comment and render to composite action * actions/github-script does not leave context in the usual spot * enabled globstar * make sure to forward inputs * try using env normally * fix typo * pass in linter name correctly * debug print * fix linting issues * more debug printing * fix linting issues * fix typpo + more rebug print * apply linting * more debug * try npm install global to be less intrusive * wip render traits tests * Update lint.yml * global install does not work * Update action.yml * Update defaults.h * remove nodejs action poc * include jsoncons in main test application (for coverage of new files) * do not exclude json (since it's used by all the traits) * exclude bundled json libraries * apply linting * add jsoncons for coverage testing * jsoncons set default to a viable version * exploring ci failure * Revert "exploring ci failure" it's overlapping symbols This reverts commit 3d5b7f9f5d4f393f03b84a59557fecdee833c822. * add missing specialization * be explicit with the tests * Create CMakeLists.txt * Create danielaparker-jsoncons.cpp * Update lint.yml * Update traits.yml * lint * better access too decoded claims * more tests on verify * tweak test trying to find minimum * lets enable linting tests * using env impl for github script * fix typo * apply linting * add boost to the tests (very hacky) * Update lint.yml * ceate light boost installer there is no cmake install anymore so just a blanket copy should do * remove path requirement * outline for commit and push linting on comment * try different webhook target * more event types * fix if for more event types * just a test commit * Update lint.yml * Update lint.yml * Update lint.yml * Apply automatic changes * add token * screw up formatting * Update lint.yml * Update lint.yml * Apply automatic changes * fix merge conflicts * missing errors * playing with globbing * lint issues * matrix on comment should give many pushes * Apply automatic changes * Update lint.yml * Apply automatic changes * Apply automatic changes * fix rebase * apply render tests * Apply automatic changes * Apply automatic changes * Delete package-lock.json * fail linting * Update .gitignore * Update lint.yml * Update lint.yml * Update lint.yml * Apply automatic changes * Update lint.yml * Update CMakeLists.txt * refactor boost.json inclusion - find paths - compile library - code coverage - traits example * Apply automatic changes * debug log * Update action.yml * debug output * Update action.yml * Delete private-boost-json.cpp.in * Update private-find-boost-json.cmake * Update CMakeLists.txt * Update CMakeLists.txt * Create CMakeLists.txt * Update boost-json.cpp * Update boost-json.cpp * Update boost-json.cpp * Update boost-json.cpp * remove old boost tests * examples to include traits * fix compilation * test nlohmann json traits * linting * refactor traits to make badges * try with less expression syntax * trying single quotes * advertise traits support * Update lint.yml * Update lint.yml * Update lint.yml * sync (prince-chrismc/jwt-cpp#16) * 🔧 Replace RSA related deprecated functions (#189) * RSA_size() --> EVP_PKEY_size() * getting rid of deprecated functions in rsa-pss signature creation * getting rid of deprecated functions in rsa-pss signature verification * generate_hash not used in pss * Update lint.yml * Update lint.yml * Update lint.yml Co-authored-by: Marek Klein <kleinmrk@gmail.com> * Update lint.yml * make sure to use install `nlohmann-json` * make action folder match trait names * add example + badge for kazuho-picojson * link to new examples * revert debug code * dont mix matrix with uses * fix bad copy paste * spec interface library so it's no compiled * fix installer * fix to interface * fix example * attempt 2 * make sure to avoid circular reference * attempt 3 * white space * white space * Update and rename .github/actions/comment-linting/action.yml to .github/actions/process-linting-results/action.yml * Update lint.yml * Update action.yml * Update action.yml * Update README.md Co-authored-by: prince-chrismc <prince-chrismc@users.noreply.github.com> Co-authored-by: Marek Klein <kleinmrk@gmail.com>
142 lines
5.3 KiB
C++
142 lines
5.3 KiB
C++
#include "jwt-cpp/jwt.h"
|
|
#include <gtest/gtest.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());
|
|
}
|
|
|
|
TEST(ClaimTest, SetAudienceAsString) {
|
|
auto token = jwt::create().set_type("JWT").set_audience("test").sign(jwt::algorithm::hs256("test"));
|
|
ASSERT_EQ("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0.ny5Fa0vzAg7tNL95KWg_ecBNd3XP3tdAzq0SFA6diY4",
|
|
token);
|
|
}
|
|
|
|
TEST(ClaimTest, AudienceAsSet) {
|
|
std::string token = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdWQiOlsidGVzdCIsInRlc3QyIl19.";
|
|
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("none", decoded.get_algorithm());
|
|
ASSERT_EQ("JWT", decoded.get_type());
|
|
auto aud = decoded.get_audience();
|
|
ASSERT_EQ(2, aud.size());
|
|
ASSERT_TRUE(aud.count("test") > 0);
|
|
ASSERT_TRUE(aud.count("test2") > 0);
|
|
}
|
|
|
|
TEST(ClaimTest, SetAudienceAsSet) {
|
|
auto token = jwt::create()
|
|
.set_type("JWT")
|
|
.set_audience({{picojson::value("test"), picojson::value("test2")}})
|
|
.sign(jwt::algorithm::none{});
|
|
ASSERT_EQ("eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJhdWQiOlsidGVzdCIsInRlc3QyIl19.", token);
|
|
}
|
|
|
|
TEST(ClaimTest, SetArray) {
|
|
std::vector<int64_t> vect = {100, 20, 10};
|
|
auto token =
|
|
jwt::create().set_payload_claim("test", jwt::claim(vect.begin(), vect.end())).sign(jwt::algorithm::none{});
|
|
ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0.");
|
|
}
|
|
|
|
TEST(ClaimTest, SetObject) {
|
|
std::istringstream iss{"{\"api-x\": [1]}"};
|
|
jwt::claim object;
|
|
iss >> object;
|
|
ASSERT_EQ(object.get_type(), jwt::json::type::object);
|
|
|
|
auto token = jwt::create().set_payload_claim("namespace", object).sign(jwt::algorithm::hs256("test"));
|
|
ASSERT_EQ(token,
|
|
"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA");
|
|
}
|
|
|
|
TEST(ClaimTest, SetAlgorithm) {
|
|
auto token = jwt::create().set_algorithm("test").sign(jwt::algorithm::none{});
|
|
|
|
auto decoded_token = jwt::decode(token);
|
|
ASSERT_EQ(decoded_token.get_algorithm(), "test");
|
|
}
|
|
|
|
TEST(ClaimTest, AsInt) {
|
|
jwt::claim c(picojson::value(static_cast<int64_t>(10)));
|
|
ASSERT_EQ(c.as_int(), 10);
|
|
}
|
|
|
|
TEST(ClaimTest, AsDate) {
|
|
jwt::claim c(picojson::value(static_cast<int64_t>(10)));
|
|
ASSERT_EQ(c.as_date(), std::chrono::system_clock::from_time_t(10));
|
|
}
|
|
|
|
TEST(ClaimTest, PicoJSONTraitsAccessorsThrow) {
|
|
jwt::traits::kazuho_picojson::value_type val;
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::as_array(val), std::bad_cast);
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::as_bool(val), std::bad_cast);
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::as_int(val), std::bad_cast);
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::as_number(val), std::bad_cast);
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::as_object(val), std::bad_cast);
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::as_string(val), std::bad_cast);
|
|
ASSERT_THROW(jwt::traits::kazuho_picojson::get_type(val), std::logic_error);
|
|
}
|
|
|
|
TEST(ClaimTest, PicoJSONTraitsAsBool) {
|
|
jwt::traits::kazuho_picojson::value_type val(true);
|
|
ASSERT_EQ(jwt::traits::kazuho_picojson::as_bool(val), true);
|
|
ASSERT_EQ(jwt::traits::kazuho_picojson::get_type(val), jwt::json::type::boolean);
|
|
}
|
|
|
|
TEST(ClaimTest, PicoJSONTraitsAsDouble) {
|
|
jwt::traits::kazuho_picojson::value_type val(10.0);
|
|
ASSERT_EQ(jwt::traits::kazuho_picojson::as_number(val), (int)10);
|
|
ASSERT_EQ(jwt::traits::kazuho_picojson::get_type(val), jwt::json::type::number);
|
|
}
|
|
|
|
TEST(ClaimTest, MapOfClaim) {
|
|
using map = jwt::details::map_of_claims<jwt::traits::kazuho_picojson>;
|
|
ASSERT_THROW(map::parse_claims(R"##(__ not json __)##"), jwt::error::invalid_json_exception);
|
|
const map claims{
|
|
map::parse_claims(R"##({ "array": [1], "string" : "hello world", "number": 9.9, "bool": true})##")};
|
|
|
|
ASSERT_TRUE(claims.has_claim("array"));
|
|
ASSERT_TRUE(claims.has_claim("string"));
|
|
ASSERT_TRUE(claims.has_claim("number"));
|
|
ASSERT_TRUE(claims.has_claim("bool"));
|
|
ASSERT_FALSE(claims.has_claim("__missing__"));
|
|
|
|
ASSERT_EQ(map::basic_claim_t{claims.get_claim("array").as_array().at(0)}.as_int(), (int)1);
|
|
ASSERT_EQ(claims.get_claim("string").as_string(), "hello world");
|
|
ASSERT_EQ(claims.get_claim("number").as_number(), 9.9);
|
|
ASSERT_EQ(claims.get_claim("bool").as_bool(), true);
|
|
ASSERT_THROW(claims.get_claim("__missing__"), jwt::error::claim_not_present_exception);
|
|
}
|