Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logout/invalidate a JWT

I'm using custom authentication in Azure Mobile Services by generating a JWT (JSON Web Token) in a custom login API. Once a user has a JWT, it's valid until its encoded expiry time is reached.

Beyond explicitly checking the JWT token against a sessions table on every authenticated request, is there a way to invalidate the JWT token before its expiry time (as would happen when a user logs out) such that any subsequent request made with that token as a value in the X-ZUMO-AUTH header would never reach any table API or custom API scripts?

like image 590
Steve Avatar asked Feb 19 '14 04:02

Steve


People also ask

Should JWT tokens be invalidated on the server after logout?

We'll also want to generate a refresh token to maintain the same user session (refreshing the expiration) as long as they're logged in. Once they're logged out, we can let the JWT token expire, and invalidate it. That being said, we'll need to map a device as well as the refresh token to a user's session.

How do you invalidate a JWT token?

New jwt tokens would set their version to this. When you validate the jwt, simply check that it has a version number equal to the users current jwt version. Any time you want to invalidate old jwts, just bump the users jwt version number.

How do you expire a single JWT token?

Store the revoked JWT tokens in Redis. Use the token as the key and the value is always a boolean true . The token will be stored only for a specific amount of time, which is the time in the exp claim, after the expiration time it will be deleted from Redis. This way only revokes just one token at a time, perfect!


1 Answers

Not really. When a user logs out in the client the JWT it uses isn't really invalidated - it's just removed from the client's memory (see the code on the managed SDK, for example). The JWT validation is done by checking the its signature against the mobile service's master key, and unless this key is changed (which would invalidate all of your service's JWT tokens, which I don't think is what you want), the token will be valid until it's expired.

Since you're generating the JWTs yourself you can consider using a smaller expiration time which may help in your case.

You can also suggest this feature in the mobile service's feedback forum. There's one related feature suggestion which I created, you can also consider adding a comment to that and voting it up.

like image 163
carlosfigueira Avatar answered Oct 17 '22 03:10

carlosfigueira