When the client requests a new Refresh Token, should the Api update the new Refresh Token’s expiry date or should I only send back a new Access Token and Refresh token, without updating the expiry date of Refresh Token?
I was following this guide to implement Refresh Tokens:
https://www.c-sharpcorner.com/article/jwt-authentication-with-refresh-tokens-in-net-6-0/
In the refresh-token
endpoint of controller there is some code that updates the refresh token of the user, but it isn’t updating the RefreshTokenExpiryTime
as well, is this a mistake?
While in the login
endpoint of the controller he is updating the refresh token of the user as well as the RefreshTokenExpiryTime
. Shouldn’t the same also be done in refresh-token
endpoint of controller?
2
Answers
So, I understand the question in this way:
Short answer
You need to create a new Refresh Token from scratch every time a Refresh Token is generated.
It makes sense as Refresh Tokens should be always valid for another refresh period. There is not much sense to not reset an expiration time of a new Refresh Token. Also, it’s more complicated to implement such a logic in the Identity Provider.
Examples from Public Identity Providers
Microsoft Identity Platform
I can give an example of what Microsoft Identity Platform does in such a case:
Auth0
Also, there is an example from Auth0:
Let’s look into RFC 6749 "The OAuth 2.0 Authorization Framework"
It defines Refresh Tokens as:
There are also rules on creating a new Refresh Token when refreshing an Access Token:
Also, there are some notes on Refresh Token rotation:
I read the RFC in that way:
If you’re using refresh token rotation, then IMO you probably shouldn’t reset the expiry on each new refresh token. The reason for this is so that stolen refresh tokens cannot be used indefinitely.
This same point is made in a draft IETF BCP (best current practice) for OAuth 2.0 for Browser-Based Apps – see here. Now this BCP is for a very specific scenario when using refresh tokens in browser based apps, but I think you could probably make the same underlying argument more broadly about refresh tokens.
Here’s some of the key bits from the BCP –