Comments (15)
@mdy405 I cleaned your code a bit and added a static time.
std::string now = "2019-04-08T21:35:21Z";
std::tm tm = {};
std::istringstream iss(now);
iss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
auto expiresAt = tp+std::chrono::minutes(60);
auto token = jwt::create()
.set_issuer("licence")
.set_expires_at(jwt::date(expiresAt))
.set_issued_at(jwt::date(tp))
.set_type("JWS")
.sign(jwt::algorithm::hs256{"secret"});
std::cout << token << std::endl;
Maybe you mixed up set_expires_at and set_issued_at ?
It is generating a valid token and the issue is not present there, are you sure the code you posted is the code you use to generate the token?
Are you using the current version (master branch 2b3ddae I did not fix anything related to time as far as I know but just to make sure)?
You can check your token here:
https://jwt.io/
The number on the right side on iat should be less than the number on exp and iat should be less than the current unix time (https://www.unixtimestamp.com/index.php)
from jwt-cpp.
Sorry for the long delay.
iat is the time where the token was created.
It should not be valid before that time for obvious reasons.
Time is the current time as a time_t, so it is valid if the current time is larger than the time specified in iat.
Are you referring to the fact that a token is not valid in the second it was created (time == iat) ?
I couldn't find any information on how this case should be handled in the spec.
I might have understood your issue wrong, can you please provide more information on your concern ?
from jwt-cpp.
thank you for responding generously,
i think you show up the problem...i m testing the verify function where the iat and the current time is separate by a few seconds .... I always got token expired exception
from jwt-cpp.
Yeah the message might be a bit missleading.
Add a leeway, its common practice to do so anyway as clocks can be off a couple of seconds.
from jwt-cpp.
iat = 2019-04-08 14:30:24-04
expiration = 2019-04-08 14:45:24-04
curentTime = 2019-04-08 14:33:24-04
leeway =60 (seconds)
but validation failed at if (time < iat - std::chrono::seconds(leeway))
from jwt-cpp.
@mdy405 That should indeed be fine. Ill take a look later and test it.
from jwt-cpp.
@mdy405 I can't reproduce the issue. Can you send me a sample token for debugging purposes?
Does not matter if it is still valid, I can adjust my clock to match.
from jwt-cpp.
try this one : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJhdWQiOiJhcHBsaWNhdGlvbl90ZXN0IiwiZGV2aWNlIjoiZGV2aWNlX3Rlc3QiLCJleHAiOjE1NTQ3NDk0OTEsImlhdCI6MTU1NDc1MjE5MSwiaXNzIjoiUFNsaWNlbmNlIiwibGFuZyI6IiIsInNlc3Npb24iOiJlMWM4Zjg4Yi1mMGExLTQ3NjgtYjYwNS01OGU2MGJjNzNiMTMiLCJzdWIiOiIyIn0.IC0Ycv_essCYeSsbxQuuH42RLGr8bjj5b-g-OQpzoIw"
algo: Hs256
secret: "secret"
from jwt-cpp.
Do you use jwt-cpp for creating that token?
Because for some reason the iat time is in the future (well beyong exp and even the current time).
from jwt-cpp.
yes i use jwt-cpp like below: (now is a string formatted datetime)
std::tm tm = {};
std::istringstream iss(now);
iss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
auto expiresAt = tp+std::chrono::minutes(configuration_>getTokenDuration());
auto token = jwt::create()
.set_issuer("licence")
.set_subject(result.userId)
.set_audience(result.applicationName)
.set_expires_at(jwt::date(expiresAt))
.set_issued_at(jwt::date(tp))
.set_type("JWS")
.set_payload_claim("session",result.id)
.set_payload_claim("device",result.deviceId)
.set_payload_claim("lang", result.language)
.sign(jwt::algorithm::hs256{"secret"});
from jwt-cpp.
it's definitely weird i used the same code but unable to validate the token!
from jwt-cpp.
I think i found the issue for some unknown reason(at the moment) i got a bad timestamp while converting my string to timepoint. i got:
iat =1554757219000000000 && exp = 1554758119000000000
@Thalhammer is that right, the current time used to verify the token is UTC ? but i use local time to set iat and eat, do you think it can cause the issue?
from jwt-cpp.
@mdy405 but those look right ;)
from jwt-cpp.
Is this solved @mdy405 ?
from jwt-cpp.
Closed due to inactivity.
from jwt-cpp.
Related Issues (20)
- undefined reference error in compiling HOT 9
- what secret format should i use for HS512 algorithm HOT 1
- Release status of 0.7.0-rc.0 HOT 3
- Guide to Setting Up jwt-cpp? HOT 4
- Building example: no matching function for call to ‘decode(std::string&)’ HOT 1
- Compilation error: no matching function for call to ‘decode(std::string&)’ HOT 2
- Build Errors For picojson.h? HOT 8
- Getting Error - namespace "jwt" has no member "claims" HOT 6
- Unable to generate token using ed25519 HOT 2
- Use of Builder explicitly
- Need help : How to Implement JWT Operations from Python Using jwt-cpp ? HOT 5
- How to use payload and get the payload data HOT 7
- Boost JSON traits kind of buggy HOT 5
- How to verify a jwt HOT 3
- Invalid input: not within alphabet HOT 6
- Locking in libcrypto in multi threaded app HOT 6
- Linker error for OpenSSL on Windows HOT 5
- Exception is thrown with the exmple private-claims.cpp HOT 10
- Flattened JWS JSON Serialization HOT 7
- unexpected exception(expired) throws in verify JWT token HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jwt-cpp.