|
| 1 | +#include "jwt-cpp/traits/kazuho-picojson/traits.h" |
| 2 | + |
| 3 | +#include <gtest/gtest.h> |
| 4 | + |
| 5 | +TEST(PicoJsonTest, BasicClaims) { |
| 6 | + const auto string = |
| 7 | + jwt::basic_claim<jwt::traits::kazuho_picojson>(jwt::traits::kazuho_picojson::string_type("string")); |
| 8 | + ASSERT_EQ(string.get_type(), jwt::json::type::string); |
| 9 | + |
| 10 | + const auto array = jwt::basic_claim<jwt::traits::kazuho_picojson>( |
| 11 | + std::set<jwt::traits::kazuho_picojson::string_type>{"string", "string"}); |
| 12 | + ASSERT_EQ(array.get_type(), jwt::json::type::array); |
| 13 | + |
| 14 | + const auto integer = jwt::basic_claim<jwt::traits::kazuho_picojson>( |
| 15 | + jwt::traits::kazuho_picojson::value_type(jwt::traits::kazuho_picojson::integer_type(159816816))); |
| 16 | + ASSERT_EQ(integer.get_type(), jwt::json::type::integer); |
| 17 | +} |
| 18 | + |
| 19 | +TEST(PicoJsonTest, AudienceAsString) { |
| 20 | + jwt::traits::kazuho_picojson::string_type token = |
| 21 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0ZXN0In0.WZnM3SIiSRHsbO3O7Z2bmIzTJ4EC32HRBKfLznHhrh4"; |
| 22 | + auto decoded = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 23 | + |
| 24 | + ASSERT_TRUE(decoded.has_algorithm()); |
| 25 | + ASSERT_TRUE(decoded.has_type()); |
| 26 | + ASSERT_FALSE(decoded.has_content_type()); |
| 27 | + ASSERT_FALSE(decoded.has_key_id()); |
| 28 | + ASSERT_FALSE(decoded.has_issuer()); |
| 29 | + ASSERT_FALSE(decoded.has_subject()); |
| 30 | + ASSERT_TRUE(decoded.has_audience()); |
| 31 | + ASSERT_FALSE(decoded.has_expires_at()); |
| 32 | + ASSERT_FALSE(decoded.has_not_before()); |
| 33 | + ASSERT_FALSE(decoded.has_issued_at()); |
| 34 | + ASSERT_FALSE(decoded.has_id()); |
| 35 | + |
| 36 | + ASSERT_EQ("HS256", decoded.get_algorithm()); |
| 37 | + ASSERT_EQ("JWT", decoded.get_type()); |
| 38 | + auto aud = decoded.get_audience(); |
| 39 | + ASSERT_EQ(1, aud.size()); |
| 40 | + ASSERT_EQ("test", *aud.begin()); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(PicoJsonTest, SetArray) { |
| 44 | + std::vector<int64_t> vect = {100, 20, 10}; |
| 45 | + auto token = |
| 46 | + jwt::create<jwt::traits::kazuho_picojson>() |
| 47 | + .set_payload_claim("test", jwt::basic_claim<jwt::traits::kazuho_picojson>(vect.begin(), vect.end())) |
| 48 | + .sign(jwt::algorithm::none{}); |
| 49 | + ASSERT_EQ(token, "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0."); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(PicoJsonTest, SetObject) { |
| 53 | + std::istringstream iss{"{\"api-x\": [1]}"}; |
| 54 | + jwt::basic_claim<jwt::traits::kazuho_picojson> object; |
| 55 | + iss >> object; |
| 56 | + ASSERT_EQ(object.get_type(), jwt::json::type::object); |
| 57 | + |
| 58 | + auto token = jwt::create<jwt::traits::kazuho_picojson>() |
| 59 | + .set_payload_claim("namespace", object) |
| 60 | + .sign(jwt::algorithm::hs256("test")); |
| 61 | + ASSERT_EQ(token, |
| 62 | + "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA"); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(PicoJsonTest, VerifyTokenHS256) { |
| 66 | + jwt::traits::kazuho_picojson::string_type token = |
| 67 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE"; |
| 68 | + |
| 69 | + const auto decoded_token = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 70 | + const auto verify = jwt::verify<jwt::traits::kazuho_picojson>() |
| 71 | + .allow_algorithm(jwt::algorithm::hs256{"secret"}) |
| 72 | + .with_issuer("auth0"); |
| 73 | + verify.verify(decoded_token); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(PicoJsonTest, VerifyTokenExpirationValid) { |
| 77 | + const auto token = jwt::create<jwt::traits::kazuho_picojson>() |
| 78 | + .set_issuer("auth0") |
| 79 | + .set_issued_at(std::chrono::system_clock::now()) |
| 80 | + .set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{3600}) |
| 81 | + .sign(jwt::algorithm::hs256{"secret"}); |
| 82 | + |
| 83 | + const auto decoded_token = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 84 | + const auto verify = jwt::verify<jwt::traits::kazuho_picojson>() |
| 85 | + .allow_algorithm(jwt::algorithm::hs256{"secret"}) |
| 86 | + .with_issuer("auth0"); |
| 87 | + verify.verify(decoded_token); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(PicoJsonTest, VerifyTokenExpirationInValid) { |
| 91 | + const auto token = jwt::create<jwt::traits::kazuho_picojson>() |
| 92 | + .set_issuer("auth0") |
| 93 | + .set_issued_now() |
| 94 | + .set_expires_in(std::chrono::hours{1}) |
| 95 | + .sign(jwt::algorithm::hs256{"secret"}); |
| 96 | + |
| 97 | + const auto decoded_token = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 98 | + const auto verify = jwt::verify<jwt::traits::kazuho_picojson>() |
| 99 | + .allow_algorithm(jwt::algorithm::hs256{"secret"}) |
| 100 | + .with_issuer("auth0"); |
| 101 | + verify.verify(decoded_token); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(PicoJsonTest, VerifyTokenExpired) { |
| 105 | + const auto token = jwt::create<jwt::traits::kazuho_picojson>() |
| 106 | + .set_issuer("auth0") |
| 107 | + .set_issued_at(std::chrono::system_clock::now() - std::chrono::seconds{3601}) |
| 108 | + .set_expires_at(std::chrono::system_clock::now() - std::chrono::seconds{1}) |
| 109 | + .sign(jwt::algorithm::hs256{"secret"}); |
| 110 | + |
| 111 | + const auto decoded_token = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 112 | + const auto verify = jwt::verify<jwt::traits::kazuho_picojson>() |
| 113 | + .allow_algorithm(jwt::algorithm::hs256{"secret"}) |
| 114 | + .with_issuer("auth0"); |
| 115 | + ASSERT_THROW(verify.verify(decoded_token), jwt::error::token_verification_exception); |
| 116 | + |
| 117 | + std::error_code ec; |
| 118 | + ASSERT_NO_THROW(verify.verify(decoded_token, ec)); |
| 119 | + ASSERT_TRUE(!(!ec)); |
| 120 | + ASSERT_EQ(ec.category(), jwt::error::token_verification_error_category()); |
| 121 | + ASSERT_EQ(ec.value(), static_cast<int>(jwt::error::token_verification_error::token_expired)); |
| 122 | +} |
| 123 | + |
| 124 | +TEST(PicoJsonTest, VerifyArray) { |
| 125 | + jwt::traits::kazuho_picojson::string_type token = "eyJhbGciOiJub25lIn0.eyJ0ZXN0IjpbMTAwLDIwLDEwXX0."; |
| 126 | + const auto decoded_token = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 127 | + |
| 128 | + std::vector<int64_t> vect = {100, 20, 10}; |
| 129 | + jwt::basic_claim<jwt::traits::kazuho_picojson> array_claim(vect.begin(), vect.end()); |
| 130 | + const auto verify = jwt::verify<jwt::traits::kazuho_picojson>() |
| 131 | + .allow_algorithm(jwt::algorithm::none{}) |
| 132 | + .with_claim("test", array_claim); |
| 133 | + ASSERT_NO_THROW(verify.verify(decoded_token)); |
| 134 | +} |
| 135 | + |
| 136 | +TEST(PicoJsonTest, VerifyObject) { |
| 137 | + jwt::traits::kazuho_picojson::string_type token = |
| 138 | + "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lc3BhY2UiOnsiYXBpLXgiOlsxXX19.F8I6I2RcSF98bKa0IpIz09fRZtHr1CWnWKx2za-tFQA"; |
| 139 | + const auto decoded_token = jwt::decode<jwt::traits::kazuho_picojson>(token); |
| 140 | + |
| 141 | + jwt::basic_claim<jwt::traits::kazuho_picojson> object_claim; |
| 142 | + std::istringstream iss{"{\"api-x\": [1]}"}; |
| 143 | + iss >> object_claim; |
| 144 | + const auto verify = jwt::verify<jwt::traits::kazuho_picojson>() |
| 145 | + .allow_algorithm(jwt::algorithm::hs256("test")) |
| 146 | + .with_claim("namespace", object_claim); |
| 147 | + ASSERT_NO_THROW(verify.verify(decoded_token)); |
| 148 | +} |
0 commit comments