Code Monkey home page Code Monkey logo

Comments (15)

Thalhammer avatar Thalhammer commented on July 21, 2024 1

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

Thalhammer avatar Thalhammer commented on July 21, 2024

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.

mdy405 avatar mdy405 commented on July 21, 2024

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.

Thalhammer avatar Thalhammer commented on July 21, 2024

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.

mdy405 avatar mdy405 commented on July 21, 2024

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.

Thalhammer avatar Thalhammer commented on July 21, 2024

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

from jwt-cpp.

Thalhammer avatar Thalhammer commented on July 21, 2024

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

mdy405 avatar mdy405 commented on July 21, 2024

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

from jwt-cpp.

Thalhammer avatar Thalhammer commented on July 21, 2024

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.

mdy405 avatar mdy405 commented on July 21, 2024

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.

mdy405 avatar mdy405 commented on July 21, 2024

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

from jwt-cpp.

mdy405 avatar mdy405 commented on July 21, 2024

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.

Thalhammer avatar Thalhammer commented on July 21, 2024

@mdy405 but those look right ;)

from jwt-cpp.

Thalhammer avatar Thalhammer commented on July 21, 2024

Is this solved @mdy405 ?

from jwt-cpp.

Thalhammer avatar Thalhammer commented on July 21, 2024

Closed due to inactivity.

from jwt-cpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.