| 1 |
#include <iostream> |
| 2 |
#include <chrono> |
| 3 |
#include <ctime> |
| 4 |
|
| 5 |
#include "jwt/jwt.hpp" |
| 6 |
#include "gtest/gtest.h" |
| 7 |
|
| 8 |
TEST (DecodeVerifyExp, BeforeExpiryTest) |
| 9 |
{ |
| 10 |
using namespace jwt::params; |
| 11 |
|
| 12 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret")}; |
| 13 |
obj.add_claim("iss", "arun.muralidharan") |
| 14 |
.add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10}) |
| 15 |
; |
| 16 |
|
| 17 |
auto enc_str = obj.signature(); |
| 18 |
|
| 19 |
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true)); |
| 20 |
} |
| 21 |
|
| 22 |
TEST (DecodeVerifyExp, AfterExpiryTest) |
| 23 |
{ |
| 24 |
using namespace jwt::params; |
| 25 |
|
| 26 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret")}; |
| 27 |
obj.add_claim("iss", "arun.muralidharan") |
| 28 |
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1}) |
| 29 |
; |
| 30 |
|
| 31 |
auto enc_str = obj.signature(); |
| 32 |
|
| 33 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true)), |
| 34 |
jwt::TokenExpiredError); |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
TEST (DecodeVerifyExp, AfterExpiryWithLeeway) |
| 39 |
{ |
| 40 |
using namespace jwt::params; |
| 41 |
|
| 42 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret")}; |
| 43 |
obj.add_claim("iss", "arun.muralidharan") |
| 44 |
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1}) |
| 45 |
; |
| 46 |
|
| 47 |
auto enc_str = obj.signature(); |
| 48 |
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), verify(true), leeway(2)); |
| 49 |
(void)dec_obj; |
| 50 |
} |
| 51 |
|
| 52 |
TEST (DecodeVerifyExp, ValidIssuerTest) |
| 53 |
{ |
| 54 |
using namespace jwt::params; |
| 55 |
|
| 56 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret")}; |
| 57 |
obj.add_claim("iss", "arun.muralidharan") |
| 58 |
.add_claim("sub", "test") |
| 59 |
; |
| 60 |
|
| 61 |
auto enc_str = obj.signature(); |
| 62 |
|
| 63 |
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), issuer("arun.muralidharan")); |
| 64 |
(void)dec_obj; |
| 65 |
} |
| 66 |
|
| 67 |
TEST (DecodeVerifyExp, InvalidIssuerTest_1) |
| 68 |
{ |
| 69 |
using namespace jwt::params; |
| 70 |
|
| 71 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})}; |
| 72 |
auto enc_str = obj.signature(); |
| 73 |
|
| 74 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), issuer("arun.muralidharan")), |
| 75 |
jwt::InvalidIssuerError); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
TEST (DecodeVerifyExp, InvalidIssuerTest_2) |
| 80 |
{ |
| 81 |
using namespace jwt::params; |
| 82 |
|
| 83 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})}; |
| 84 |
obj.add_claim("iss", "arun.muralidharan"); |
| 85 |
|
| 86 |
auto enc_str = obj.signature(); |
| 87 |
|
| 88 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), issuer("arun.murali")), |
| 89 |
jwt::InvalidIssuerError); |
| 90 |
} |
| 91 |
|
| 92 |
TEST (DecodeVerifyExp, NotImmatureSignatureTest) |
| 93 |
{ |
| 94 |
using namespace jwt::params; |
| 95 |
|
| 96 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})}; |
| 97 |
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() - std::chrono::seconds{10}); |
| 98 |
|
| 99 |
auto enc_str = obj.signature(); |
| 100 |
|
| 101 |
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret")); |
| 102 |
(void)dec_obj; |
| 103 |
} |
| 104 |
|
| 105 |
TEST (DecodeVerifyExp, ImmatureSignatureTest) |
| 106 |
{ |
| 107 |
using namespace jwt::params; |
| 108 |
|
| 109 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})}; |
| 110 |
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10}); |
| 111 |
|
| 112 |
auto enc_str = obj.signature(); |
| 113 |
|
| 114 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret")), |
| 115 |
jwt::ImmatureSignatureError); |
| 116 |
} |
| 117 |
|
| 118 |
TEST (DecodeVerifyExp, ImmatureSignatureTestWithLeeway) |
| 119 |
{ |
| 120 |
using namespace jwt::params; |
| 121 |
|
| 122 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}})}; |
| 123 |
obj.add_claim(jwt::registered_claims::not_before, std::chrono::system_clock::now() + std::chrono::seconds{10}); |
| 124 |
|
| 125 |
auto enc_str = obj.signature(); |
| 126 |
|
| 127 |
auto dec_obj = jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), leeway(10)); |
| 128 |
(void)dec_obj; |
| 129 |
} |
| 130 |
|
| 131 |
TEST (DecodeVerifyExp, InvalidAudienceTest) |
| 132 |
{ |
| 133 |
using namespace jwt::params; |
| 134 |
|
| 135 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})}; |
| 136 |
|
| 137 |
auto enc_str = obj.signature(); |
| 138 |
|
| 139 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), aud("ww")), |
| 140 |
jwt::InvalidAudienceError); |
| 141 |
} |
| 142 |
|
| 143 |
TEST (DecodeVerifyExp, InvalidSignatureTest) |
| 144 |
{ |
| 145 |
using namespace jwt::params; |
| 146 |
|
| 147 |
const char* inv_enc_str = |
| 148 |
"eyJhbGciOiJOT05FIiwidHlwIjoiSldUIn0.eyJhdWQiOiJyaWZ0LmlvIiwiZXhwIjoxNTEzODYzMzcxLCJzdWIiOiJub3RoaW5nIG11Y2gifQ"; |
| 149 |
|
| 150 |
EXPECT_THROW (jwt::decode(inv_enc_str, algorithms({"none", "HS256"})), |
| 151 |
jwt::SignatureFormatError); |
| 152 |
} |
| 153 |
|
| 154 |
TEST (DecodeVerifyExp, KeyNotPresentTest) |
| 155 |
{ |
| 156 |
using namespace jwt::params; |
| 157 |
|
| 158 |
const char* enc_str = |
| 159 |
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." |
| 160 |
"eyJpYXQiOjE1MTM4NjIzNzEsImlkIjoiYS1iLWMtZC1lLWYtMS0yLTMiLCJpc3MiOiJhcnVuLm11cmFsaWRoYXJhbiIsInN1YiI6ImFkbWluIn0." |
| 161 |
"jk7bRQKTLvs1RcuvMc2B_rt6WBYPoVPirYi_QRBPiuk"; |
| 162 |
|
| 163 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"none", "HS256"}), verify(true)), |
| 164 |
jwt::KeyNotPresentError); |
| 165 |
} |
| 166 |
|
| 167 |
TEST (DecodeVerifyExp, InvalidSubjectTest) |
| 168 |
{ |
| 169 |
using namespace jwt::params; |
| 170 |
|
| 171 |
jwt::jwt_object obj{algorithm("HS256"), secret("secret"), payload({{"sub", "test"}, {"aud", "www"}})}; |
| 172 |
|
| 173 |
auto enc_str = obj.signature(); |
| 174 |
|
| 175 |
EXPECT_THROW (jwt::decode(enc_str, algorithms({"HS256"}), secret("secret"), sub("TEST")), |
| 176 |
jwt::InvalidSubjectError); |
| 177 |
} |
| 178 |
|