ChatGPT解决这个技术问题 Extra ChatGPT

How can I revoke a JWT token?

I am using Spring Security OAuth2 and JWT tokens. My question is: How can I revoke a JWT token?

As mentioned here http://projects.spring.io/spring-security-oauth/docs/oauth2.html, revocation is done by refresh token. But it does not seem to work.

You can't, unless you build some record of tokens issued on the authorization server. The resource API which consumes of the token would also have to check whether the token was revoked.
did you find anything or not ?? also if i am going to build a record of tokens i think Oauth is not stateless ??
+1 to Shaun's comments, adding that it would usually defeat the point of having JWTs (or by-value) tokens to do so.
Shaun's comment is wrong or at least not completely correct. You have to distinguish between access and refresh tokens. While it does not make sense to invalidate Access tokens you can do that with refresh tokens. In a scenario where you have a expiration time of for example 15 for access tokens and maybe a week for refresh tokens you can easily see what you can achieve by invalidating the refresh token. This way you do not have to propagate anything to resource servers and you do not loose statelessness.
...This of course assumes that you only refresh your Access tokens on the auth-server.

C
Community

In general the easiest answer would be to say that you cannot revoke a JWT token, but that's simply not true. The honest answer is that the cost of supporting JWT revocation is sufficiently big for not being worth most of the times or plainly reconsider an alternative to JWT.

Having said that, in some scenarios you might need both JWT and immediate token revocation so lets go through what it would take, but first we'll cover some concepts.

JWT (Learn JSON Web Tokens) just specifies a token format, this revocation problem would also apply to any format used in what's usually known as a self-contained or by-value token. I like the latter terminology, because it makes a good contrast with by-reference tokens.

by-value token - associated information, including token lifetime, is contained in the token itself and the information can be verified as originating from a trusted source (digital signatures to the rescue) by-reference token - associated information is kept on server-side storage that is then obtained using the token value as the key; being server-side storage the associated information is implicitly trusted

Before the JWT Big Bang we already dealt with tokens in our authentication systems; it was common for an application to create a session identifier upon user login that would then be used so that the user did not had to repeat the login process each time. These session identifiers were used as key indexes for server-side storage and if this sounds similar to something you recently read, you're right, this indeed classifies as a by-reference token.

Using the same analogy, understanding revocation for by-reference tokens is trivial; we just delete the server-side storage mapped to that key and the next time the key is provided it will be invalid.

For by-value tokens we just need to implement the opposite. When you request the revocation of the token you store something that allows you to uniquely identify that token so that next time you receive it you can additionally check if it was revoked. If you're already thinking that something like this will not scale, have in mind that you only need to store the data until the time the token would expire and in most cases you could probably just store an hash of the token so it would always be something of a known size.

As a last note and to center this on OAuth 2.0, the revocation of by-value access tokens is currently not standardized. Nonetheless, the OAuth 2.0 Token revocation specifically states that it can still be achieved as long as both the authorization server and resource server agree to a custom way of handling this:

In the former case (self-contained tokens), some (currently non-standardized) backend interaction between the authorization server and the resource server may be used when immediate access token revocation is desired.

If you control both the authorization server and resource server this is very easy to achieve. On the other hand if you delegate the authorization server role to a cloud provider like Auth0 or a third-party component like Spring OAuth 2.0 you most likely need to approach things differently as you'll probably only get what's already standardized.

An interesting reference

This article explain a another way to do that: Blacklist JWT It contains some interesting pratices and pattern followed by RFC7523


Instead of storing a hash per unexpired token (which, although manageable, is potentially a lot of hashes), could you just store a hash per revoked token, and check for its absence instead of presence?
The text is not very clear about it, but yes, I was talking about storing something only for revoked by-value tokens and also only until their normal expiration was not reached. Every token would be validated as usually and then would have an additional check to see if it was not revoked.
+2/2 (= 100) for introducing the terms "by-value" and "by-reference" :)
"By-value" tokens are indeed the opposite to the session concept that you store the pair (jti, exp) when session is "deleted" and distribute all those pairs, while servers can keep removing them on exp independently from any centralized store. Scales very well, but still has stateful "anti-sessions" in terms of these revocation jti ids.
H
Huanghq

The JWT cann't be revoked.

But here is the a alternative solution called as JWT old for new exchange schema.

Because we can’t invalidate the issued token before expire time, we always use short-time token, such as 30 minute. When the token expired, we use the old token exchange a new token. The critical point is one old token can exchange one new token only.

In center auth server, we maintain a table like this:

table auth_tokens(
    user_id,
    jwt_hash,
    expire
)

user_id contained in JWT string. jwt_hash is a hash value of whole JWT string,Such as SHA256. expire field is optional.

The following is work flow:

User request the login API with username and password, the auth server issue one token, and register the token ( add one row in the table. ) When the token expired, user request the exchange API with the old token. Firstly the auth server validate the old token as normal except expire checking, then create the token hash value, then lookup above table by user id: If found record and user_id and jwt_hash is match, then issue new token and update the table. If found record, but user_id and jwt_hash is not match , it means someone has use the token exchanged new token before. The token be hacked, delete records by user_id and response with alert information. if not found record, user need login again or only input password. when use changed the password or login out, delete record by user id.

To use token continuously ,both legal user and hacker need exchange new token continuously, but only one can succeed, when one fails, both need to login again at next exchange time.

So if hacker got the token, it can be used for a short time, but can't exchange for a new one if a legal user exchanged new one next time, because the token validity period is short. It is more secure this way.

If there is no hacker, normal user also need exchange new token periodically ,such as every 30 minutes, this is just like login automatically. The extra load is not high and we can adjust expire time for our application.

source: http://www.jianshu.com/p/b11accc40ba7


Major drawback of this solution is that at the same time there can be only one user signed in.
What if the user stops using the system and the hacker exchanges for a new token? Sounds like the hacker gets to keep using and exchanging tokens until the user logs in again. Yuck.
This is an answer which should not have been upvoted. As mentioned the proposed answer mixes blacklisting with refreshing, which means the token is now both 'stateful' and a 'singleton' (single login only), both which are entirely against the JWT principles. The proposed solution however still has the problem of depending on expiry (leaving a major weakness), while introducing a new one (single login). Therefore this is even worse than having a simple auth jwt + refresh token and blacklisting mechanism. Too be honest: it's not the simplest to implement as well (bugs possible).
I
Ian Storm Taylor

This doesn't exactly answer you question in regards to the Spring framework, but here's an article that talks about why if you need the ability to revoke JWT's, you might not want to go with JWT's in the first place, and instead use regular, opaque Bearer tokens.

https://www.dinochiesa.net/?p=1388


why not use a cache or a rdbm with a cron an delete each entry at periodically or something like that instead of waiting?
k
kstra

One way to revoke a JWT is by leveraging a distributed event system that notifies services when refresh tokens have been revoked. The identity provider broadcasts an event when a refresh token is revoked and other backends/services listen for the event. When an event is received the backends/services update a local cache that maintains a set of users whose refresh tokens have been revoked.

This cache is then checked whenever a JWT is verified to determine if the JWT should be revoked or not. This is all based on the duration of JWTs and expiration instant of individual JWTs.

This article, Revoking JWTs, illustrates this concept and has a sample app on Github.


That "revoking JWTs" link is returning a 404 now.
@DenverCoder9 Maybe token was successfully revoked
d
dz902

For Googlers:

If you implement pure stateless authentication there is no way to revoke the token as the token itself is the sole source of truth

If you save a list of revoked token IDs on the server and check every request against the list, then it is essentially a variant of stateful authentication

OAuth2 providers like Cognito provides a way to "sign out" a user, however, it only really revokes refresh token, which is usually long-lived and could be used multiple times to generate new access tokens thus has to be revoked; the existing access tokens are still valid until they expire


Is there any alternatives to JWT that can support token revocation yet can be used to implement pure stateless authentication?
c
codepushr

What about storing the JWT token and referencing it to the user in the database? By extending the Guards/Security Systems in your backend application with an additional DB join after performing the JWT comparison, you would be able to practically 'revoke' it by removing or soft-deleting it from the DB.


You have to treat the token as you would a password, so rather than storing the token itself you would store a hash of it a la bcrypt, scrypt, etc. However, you would be introducing an additional token validation step that requires centralization, negating one of the strengths of JWT so do you really want JWT at that point? Actually, someone linked to the following article that expresses the same opinion in more detail: dinochiesa.net/?p=1388
b
bigkahunaburger

In general, the answer about tokens by reference vs. tokens by value has nailed it. For those that stumble upon this space in future.

How to implement revocation on RS side: TL;DR: Take a cache or db that is visible to all your backend service instances that are verifying tokens. When a new token arrives for revocation, if it's a valid one, (i.e. verifies against your jwt verification algo), take the exp and jti claims, and save jti to cache until exp is reached. Then expire jti in cache once unixNow becomes > exp.

Then on authorization on other endpoints, you check everytime if a given jti is matching something in this cache, and if yes, you error with 403 saying token revoked. Once it expires, regular Token Expired error kicks in from your verification algo.

P.S. By saving only jti in cache, you make this data useless to anyone since it's just a unique token identifier.


I wanted to comment w.r.t. "on authorization on other endpoints, you check everytime if a given jti is matching something in this cache" that it adds some consistency synchronization latency on revoked tokens while distributing the revoked lists. JWTs are not magic, they simply invert the concept of session, being a conceptual "complement" of it. Still, audit trail is required to force revoke and see what IP:port were used and what set of credentials. Fun stuff!
Fun stuff indeed. My idea is that you still get to avoid the latency. Most of the tokens will drop by the verification algo phase, that's how you implement. Then make a cheap cache query for revoked tokens. Then if someone is really persistent with revoked token, your intelligence system should figure it out and isolate such case.
G
Gleno

The best solution for JWT revocation, is short exp window, refresh and keeping issued JWT tokens in a shared nearline cache. With Redis for example, this is particularly easy as you can set the cache key as the token itself (or a hash of the token), and specify expiry so that the tokens get automatically evicted.


S
Sindhu Raju

I found one way of resolving the issue, How to expire already generated existing JWT token using Java?

In this case, we need to use any DB or in-memory where,

Step 1: As soon as the token is generated for the first time for a user, store it in a db with the token and it's "issuedAt()" time.

I stored it in DB in this JSON format,

Ex: {"username" : "username", "token" : "token", "issuedAt" : "issuedAt" }

Step 2: Once you get a web service request for the same user with a token to validate, fetch "issuedAt()" timestamp from the token and compare it with stored(DB/in-memory) issued timestamp.

Step 3: If stored issued timestamp is new (using after()/before() method) then return that the token is invalid (in this case we are not actually expiring the token but we are stop giving access on that token).

This is how I resolved the issue.


This is like storing passwords in a DB. Don't do that. You could store a hash instead a la bcrypt, scrypt, etc. but the point is to treat the token like you would a password.
If you take storing the username/token out of consideration, isn't this a valid approach? Say you just store an issuedAt timestamp on the user record, you can use that to invalidate any previous tokens issued before the most recent one when it is checked against the DB. The only drawback is that only the most recent token will be valid, but you could set the new issedAt in the database at a defined time instead of each time the token is generated, and any tokens generated after that time will be valid.
Question is about a self contained token, not a stateful token.