Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validating issued_at alway return token expired? #14

Closed
mdy405 opened this issue Apr 8, 2019 · 15 comments
Closed

validating issued_at alway return token expired? #14

mdy405 opened this issue Apr 8, 2019 · 15 comments

Comments

@mdy405
Copy link

mdy405 commented Apr 8, 2019

if (jwt.has_issued_at()) { auto leeway = claims.count("iat") == 1 ? std::chrono::system_clock::to_time_t(claims.at("iat").as_date()) : default_leeway; auto iat = jwt.get_issued_at(); if (time < iat - std::chrono::seconds(leeway)) throw token_verification_exception("token expired"); }

leeway = 0 so time < iat! who else got this ?

@Thalhammer
Copy link
Owner

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 ?

@mdy405
Copy link
Author

mdy405 commented Apr 8, 2019

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

@Thalhammer
Copy link
Owner

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.

@mdy405
Copy link
Author

mdy405 commented Apr 8, 2019

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))

@Thalhammer
Copy link
Owner

@mdy405 That should indeed be fine. Ill take a look later and test it.

@Thalhammer
Copy link
Owner

@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.

@mdy405
Copy link
Author

mdy405 commented Apr 8, 2019

try this one : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJhdWQiOiJhcHBsaWNhdGlvbl90ZXN0IiwiZGV2aWNlIjoiZGV2aWNlX3Rlc3QiLCJleHAiOjE1NTQ3NDk0OTEsImlhdCI6MTU1NDc1MjE5MSwiaXNzIjoiUFNsaWNlbmNlIiwibGFuZyI6IiIsInNlc3Npb24iOiJlMWM4Zjg4Yi1mMGExLTQ3NjgtYjYwNS01OGU2MGJjNzNiMTMiLCJzdWIiOiIyIn0.IC0Ycv_essCYeSsbxQuuH42RLGr8bjj5b-g-OQpzoIw"
algo: Hs256
secret: "secret"

@Thalhammer
Copy link
Owner

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).

@mdy405
Copy link
Author

mdy405 commented Apr 8, 2019

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"});

@Thalhammer
Copy link
Owner

Thalhammer commented Apr 8, 2019

@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)

@mdy405
Copy link
Author

mdy405 commented Apr 8, 2019

it's definitely weird i used the same code but unable to validate the token!

@mdy405
Copy link
Author

mdy405 commented Apr 8, 2019

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?

@Thalhammer
Copy link
Owner

@mdy405 but those look right ;)

@Thalhammer
Copy link
Owner

Is this solved @mdy405 ?

@Thalhammer
Copy link
Owner

Closed due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants