在使用 JWT 令牌身份验证时,真的需要刷新令牌吗?

我引用了另一篇讨论在 JWT 中使用刷新令牌的 SO 文章。

JWT (JSON Web Token)自动延长有效期

我有一个应用程序,它有一个非常常见的架构,我的客户端(web 和移动端)可以与 REST API 交谈,然后 REST API 与服务层和数据层交谈。

enter image description here

我理解 JWT 令牌身份验证,但是我对如何使用刷新令牌有点困惑。

我希望我的 JWT 身份验证具有以下属性:

  1. JWT Token 的有效期为2小时。

  2. 客户端每小时刷新一次令牌。

  3. 如果用户令牌没有刷新(用户处于非活动状态,应用程序没有打开)并过期,他们将需要随时登录才能恢复。

我看到很多人声称使用刷新令牌的概念可以使这种体验变得更好,然而,我并不认为这有什么好处。管理它似乎更加复杂。

我的问题如下:

  1. 如果我要使用刷新令牌,那么在该令牌上进行良好实践的长期过期不是也是有益的吗?
  2. 如果我要使用刷新令牌,该令牌是否会与 userId 和/或 JWT 令牌一起保持?
  3. 当我每1小时更新一次令牌时,它是如何工作的?我想要创建一个接受 JWT 令牌或刷新令牌的端点吗?这将更新原始 JWT 令牌的过期日期,还是创建一个新的令牌?
  4. 考虑到这些细节,是否需要刷新令牌?看起来,如果用户只是使用 JWT 令牌来获取新的令牌(按照上面的链接) ,那么刷新令牌就是过时的。
41959 次浏览

Let me come to your questions a little later down the line and start by actually discussing the whole purpose of a refresh token.

So the situation is:

The user opens the app and provides his login credentials. Now, most probably the app is interacting with a REST backend service. REST is stateless, there isn't a way to authorize access to the APIs. Hence, so far in the discussion, there is no way to check if an authorized user is accessing the APIs or is just some random requests coming through.

Now to be able to solve this problem, we need a way to know that the requests are coming from an authorized user. So, what we did was to introduce something called an access token. So now once the user is authenticated successfully, he is issued an access token. This token is supposed to be a long and highly random token (to ensure that it can not be guessed). This is where the JWT comes into the picture. Now you may/may not want to store any user-specific details in a JWT token. Ideally, you would want to just store very simple, extremely non-sensitive details in the JWT. The manipulation of the JWT hash to retrieve other user's details (IDOR etc.) is taken care of by JWT (the library being used) itself.

So, for now, our problem with authorized access is solved.

Now we talk of an attack scenario. Let's say using all of the above user Alice, using the app, has the authorized access token and now her app can make requests to all the APIs and retrieve the data as per her authorization.

Assume that SOMEHOW Alice loses the Access Token or put another way, an adversary, Bob, gets access to Alice's access token. Now Bob, despite being unauthorized, can make requests to all the APIs that Alice was authorized to.

SOMETHING WE IDEALLY DON'T WANT.

Now the solution to this problem is :

  1. Either detect that there is something of this sort happening.
  2. Reduce the attack window itself.

Using just the access token alone, it is hard to achieve condition 1 above, because be it Alice or Bob, it's the same authorized token being used and hence requests form the two users are not distinguishable.

So we try achieving 2 above and hence we add an expiration to the validity of the access token, say the access token is valid for 't' (short-lived) time.

How does it help? Well, even if Bob has the access token, he can use it only while it is valid. As soon as it expires, he will have to retrieve it again. Now, of course, you could say that he can get it the same way he got it the first time. But then again there's nothing like 100% security!

The above approach still has a problem and in some cases an unacceptable one. When the access token expires, it would require the user to enter his login credentials and obtain an authorized access token again, which at least in case of mobile apps, is a bad (not acceptable) user experience.

Solution: This is where the refresh token comes in. It is again a random unpredictable token that is also issued to the app along with the access token in the first place. This refresh token is a very long-lived special token, which makes sure that as soon as the access token expires, it requests the server for a new access token, thus removing the need for the user to re-enter his login credentials to retrieve a new authorized access token, once an existing one has expired.

Now you may ask, Bob can have access to the refresh token as well, similar to the way he compromised the access token. YES. He can. However, now it becomes easy to identify such an incidence, which was not possible in the case of an access token alone, and take the necessary action to reduce the damage done.

How?

For every authenticated user (in case of a mobile app, generally), a one to one mapped refresh token and access token pair is issued to the app. So at any given point in time, for a single authenticated user, there will be only one access token corresponding to a refresh token. Now assume that if Bob has compromised the refresh token, he would be using it to generate an access token (because access token is the only thing which is authorized to access resources through the APIs). As soon as Bob (attacker) requests with the newly generated access token because Alice's (genuine user) access token is still valid, the server would see this as an anomaly, because for a single refresh token there can be only one authorized access token at a time. Identifying the anomaly, the server would destroy the refresh token in question and along with it all, it's associated access tokens will also get invalidated. Thus preventing any further access, genuine or malicious, to any authorization requiring resources. The user, Alice, would be required to once again authenticate with her credentials and fetch a valid pair of a refresh and access tokens.

Of course, you could still argue that Bob could once again get access to both refresh and access tokens and repeat the entire story above, potentially leading to a DoS on Alice, the actual genuine customer, but then again there is nothing like 100% security.

Also as a good practice, the refresh token should have an expiry, although a pretty long one.

I believe for this scenario you could work with the access token alone, making life easier for your clients but keeping the security benefits of a refresh token.

This is how it would work:

  1. When your user logs in with credentials (username/password) you return a short-lived JWT. You also create a db record where you store:

    • JWT id
    • user id
    • IP address
    • user agent
    • a valid flag (defaults to TRUE)
    • createdAt
    • updatedAt
  2. Your client submits the JWT in every request. As long as the JWT hasn't expired, it has access to the resources. If the JWT expired, you refresh it behind the scenes and return both the resource and an additional X-JWT header with the new JWT.

  3. When the client receives a response with an X-JWT header, it discards the old JWT and uses the new one for future requests.

How refreshing the JWT works on the server

  1. Look for the matching db record using the JWT id.
  2. Check if the valid flag is still true, otherwise reject.
  3. Optionally, you can compare the request IP address and user agent against the stored IP address and user agent, and decide to reject if something looks fishy.
  4. Optionally, you can check the db record's createdAt or updatedAt fields, and decide not to refresh if too much time has passed.
  5. Update the updatedAt field in the db record.
  6. Return the new JWT (which is basically a copy of the expired JWT, but with an extended expiration time).

This design would also give you the option to revoke all tokens for a user (for example, if the user loses his phone or updates his password).

Benefits:

  • Your client never has to check expiration times or make refresh token requests, all it does is check for an X-JWT header on responses.
  • You can add custom refresh logic based on IP address, user agent, max-token age, or a combination of those.
  • You can revoke some or all tokens for a user.

If I WERE to use a refresh token, wouldn't it still be beneficial to have a long term expiration for good practice on that token as well?

Refresh Tokens are long-lived, Access Tokens are short-lived.

If I WERE to use a refresh token, would that token be persisted with the userId and/or JWT token?

It would be persisted as a separate token on the client, alongside JWT but not inside JWT. UserID/UID can be stored inside the JWT token itself.

When I update my token every 1 hour, how does this work? Will I want to create an endpoint that takes in my JWT token or my refresh token? Will this update the expiration date of my original JWT token, or create a new token?

Yes, you need a separate service that issues and refreshes token. It won't update the expiration of the existing JWT Token. A token is simply JSON field-value pairs that are base64 encoded. So changing the data, changes the output. The token also has the issue date, which will at the very least change on every fresh issue (refresh). So every token will be unique and new. The old tokens will auto-expire, hence you need expiration on all Access Tokens, otherwise they will linger around forever.

The other answer here states that old tokens get destroyed when you issue a new token. That's simply not the case. Tokens cannot be destroyed. In fact, you can harvest hundreds of tokens by constantly contacting the auth server and asking for new fresh tokens using your Refresh Token. Each of those Access Tokens will be valid till their expiry. So expiry is imperative, and it should be short.

Is there really the need for a refresh token given these details? It seems that If the user is just using a JWT token to grab a new token (per the link above) then the refresh token is obsolete.

JWT tokens have client claims. For example is_manager:true claim on a JWT token might allow access to manager-level features. Now if you decide to demote the user from manager to contractor, that won't take effect immediately. The user may still be using the old token. Finally when that expires, he hits the auth server to refresh his token. The auth server issues a new token without the managerial claim and the user won't be able to access managerial features any more. This creates a window during which the user's claims are not in sync with the server. This again explains why Access Tokens should be short-lived so sync'ing can happen often.

Essentially you are updating the authorization checks every 15 minutes, instead of checking them on every single request (which is how typical session-based auth works). If you want real-time permissions instead of every-15-minute refreshes, then JWT may not be a good fit.