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