jwt-auth icon indicating copy to clipboard operation
jwt-auth copied to clipboard

Refresh token does not work if access_token is expired?

Open kamleshwebtech opened this issue 4 years ago • 12 comments
trafficstars

I am using Lumen 8.0 with this jwt-auth package. I am facing an issue that refresh_token does not regenerate access_token when access_token is expired?

Kindly share any suggestion, if you have. Thanks a lot. Look forward to hear you. Thanks.

kamleshwebtech avatar Apr 28 '21 05:04 kamleshwebtech

There is a setting called JWT_REFRESH_TTL or simply refresh_ttl, which defines how long the old token would be accepted for a refresh. Maybe check if it is configured correctly! Once this time-frame has expired, the old token is not accepted for generating a new one.

BenceSzalai avatar Apr 30 '21 18:04 BenceSzalai

Related issue: https://github.com/tymondesigns/jwt-auth/issues/2056

CodeNinja1337 avatar May 06 '21 06:05 CodeNinja1337

@BenceSzalai have you tried it by yourself? I mean refresh_ttl because it doesn't seem to work.

zvermafia avatar May 15 '21 16:05 zvermafia

Sorry, not, I've only used it with Laravel, where it worked fine. I just thought it may worth to check those settings.

BenceSzalai avatar May 15 '21 21:05 BenceSzalai

@BenceSzalai I use Laravel, too, but it doesn't work

zvermafia avatar May 17 '21 09:05 zvermafia

Well, I've never used Lumen myself, so I don't know what are the differences compared to regular Laravel. Maybe if you have shared some of your code and what you are trying to achieve someone may be able to help, but it seems more like a StackOverflow topic to me than an issue here, since it does not seem to be an issue with jwt-auth. I can generate a refreshed token by $newToken = auth()->refresh(); and I have to assume it works for thousands of other users of the package...

BenceSzalai avatar May 18 '21 08:05 BenceSzalai

I just follow the doc, and set ttl = 1, refresh_ttl = 2. After the minute (when token time is expired) I'm trying to refresh the token, but it gives me 401 (sure, because the token time is expired and you can't authenticate with it, or maybe you could authenticate but you don't have authorization for all the actions except refreshing the token?!). I said ok and removed authentication from the refreshing route, now it works but doesn't refresh the refresh token expiration time, also there is not a method to get actual expiration times for the user rather than this strange one: auth()->blacklist()->getRefreshTTL(), why it needs to be called through blacklist()?!.

I think it must work when you write the code as shown in the documention

zvermafia avatar May 19 '21 05:05 zvermafia

And I'm not fighting with you, I just don't understand and write my thoughts...

zvermafia avatar May 19 '21 05:05 zvermafia

I'm not fighting either, just saying it is not really a discussion for this forum. Being that said, sure, the refresh token route must be excluded from the auth middleware. I'm generating refreshed token like this:

$token = (string) auth()->refresh();
$expiration = auth()->setToken($token)->getPayload()->get('exp');

I think auth()->blacklist()->getRefreshTTL() gives you the default TTL, my code above gives you the actual expiration time of the very token just generated. To return an "expires_in" in the response you can than just use $expiration - time().

BenceSzalai avatar May 19 '21 08:05 BenceSzalai

Hi In my case the problem arises when I try to invalidate an expired token. The user tries to acces an endpoint, the token is expired, it then tries to refresh it. All works unless the refresh logic tries to invalidate (blacklist) the expired token. Thanks

dhcmega

dhcmega avatar Jun 15 '21 20:06 dhcmega

I ran into the same issue described here, the way I resolved this was to remove any JWT middleware from my refresh route and refresh manually:

	public function refreshToken()
	{
		try {
			$token = JWTAuth::parseToken()->refresh();
		} catch (JWTException $e) {
			return $this->sendError("Unable to refresh token", Response::HTTP_INTERNAL_SERVER_ERROR);
		}

		return $this->sendResponse(["token" => $token], "Successfully refreshed token");
	}

mattmcardle avatar Jul 22 '21 09:07 mattmcardle

I have the same issue. Is this a bug in documentation then? Both "login" and "refresh" should be public routes?

erikroznbeker avatar Aug 20 '21 13:08 erikroznbeker