Search code examples
authenticationjwtjson-web-token

Time expiration issue in JWT


As you know, there are some good reasons for using token based authentication instead of session based.

In session based, of course there is a expiration time. So if user is not active for a while, his session get expired. But before expiring, if he send request to server, his time will be extended.

There is an awesome tutorial here about JWT. I have a question about expiration time for token. Imagine we set the expiration time to 100 seconds, then we sign the token. It doesn't matter user is active or not. After 100 seconds that token will not be valid anymore. This bothers the user. Is there any way to extend the time?

Is it a true approach, or maybe I have a mistake. Any idea?


Solution

  • If I understand the question correctly, it is fairly simple to alter the expiration of a JWT token during creation...

    The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the "exp" claim requires that the current date/time MUST be before the expiration date/time listed in the "exp" claim.

    More information can be found here https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4

    Basically the exp key takes a unix timestamp - set the timestamp to > 100 seconds from now and you will accomplish your goal.

    To "refresh" the token your API needs a service that receives a valid, JWT and returns the same signed JWT with the updated expiration.