mirror of
https://github.com/arun11299/cpp-jwt.git
synced 2025-05-15 09:18:33 +00:00
Added tests for expiration
This commit is contained in:
parent
e42720a446
commit
e662b445dd
4 changed files with 66 additions and 2 deletions
|
@ -391,7 +391,7 @@ std::error_code jwt_object::verify(
|
||||||
auto p_exp = payload()
|
auto p_exp = payload()
|
||||||
.get_claim_value<uint64_t>(registered_claims::expiration);
|
.get_claim_value<uint64_t>(registered_claims::expiration);
|
||||||
|
|
||||||
if (p_exp < (curr_time + dparams.leeway)) {
|
if (curr_time > (p_exp + dparams.leeway)) {
|
||||||
ec = VerificationErrc::TokenExpired;
|
ec = VerificationErrc::TokenExpired;
|
||||||
return ec;
|
return ec;
|
||||||
}
|
}
|
||||||
|
|
|
@ -762,7 +762,7 @@ public: // 'tors
|
||||||
*
|
*
|
||||||
* 4. headers : Can pass a initializer list of pairs or any associative
|
* 4. headers : Can pass a initializer list of pairs or any associative
|
||||||
* containers which models `MappingConcept` (see `meta::is_mapping_concept`)
|
* containers which models `MappingConcept` (see `meta::is_mapping_concept`)
|
||||||
* to populate header. Not much use.
|
* to populate header. Can be used to set JTI.
|
||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
jwt_object(Args&&... args);
|
jwt_object(Args&&... args);
|
||||||
|
|
64
tests/test_jwt_decode_verifiy.cc
Normal file
64
tests/test_jwt_decode_verifiy.cc
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
#include "jwt/jwt.hpp"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
TEST (DecodeVerify, BeforeExpiryTest)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
|
||||||
|
obj.add_claim("iss", "arun.muralidharan")
|
||||||
|
.add_claim("exp", std::chrono::system_clock::now() + std::chrono::seconds{10})
|
||||||
|
;
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
auto enc_str = obj.signature(ec);
|
||||||
|
|
||||||
|
ASSERT_FALSE (ec);
|
||||||
|
|
||||||
|
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true));
|
||||||
|
ASSERT_FALSE (ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (DecodeVerify, AfterExpiryTest)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
|
||||||
|
obj.add_claim("iss", "arun.muralidharan")
|
||||||
|
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
|
||||||
|
;
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
auto enc_str = obj.signature(ec);
|
||||||
|
ASSERT_FALSE (ec);
|
||||||
|
|
||||||
|
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true));
|
||||||
|
ASSERT_TRUE (ec);
|
||||||
|
EXPECT_EQ (ec.value(), static_cast<int>(jwt::VerificationErrc::TokenExpired));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST (DecodeVerify, AfterExpiryWithLeeway)
|
||||||
|
{
|
||||||
|
using namespace jwt::params;
|
||||||
|
|
||||||
|
jwt::jwt_object obj{algorithm("hs256"), secret("secret")};
|
||||||
|
obj.add_claim("iss", "arun.muralidharan")
|
||||||
|
.add_claim("exp", std::chrono::system_clock::now() - std::chrono::seconds{1})
|
||||||
|
;
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
auto enc_str = obj.signature(ec);
|
||||||
|
ASSERT_FALSE (ec);
|
||||||
|
|
||||||
|
auto dec_obj = jwt::decode(enc_str, algorithms({"hs256"}), ec, secret("secret"), verify(true), leeway(2));
|
||||||
|
ASSERT_FALSE (ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
BIN
tests/test_jwt_decode_verify
Executable file
BIN
tests/test_jwt_decode_verify
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue