使用JWT令牌认证时,真的需要刷新令牌吗? [英] Is a Refresh Token really necessary when using JWT token authentication?

查看:172
本文介绍了使用JWT令牌认证时,真的需要刷新令牌吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am referencing another SO post that discusses using refresh tokens with JWT.

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

我有一个具有非常通用的体系结构的应用程序,其中我的客户端(Web和移动设备)与REST API进行通信,然后与服务层和数据层进行通信.

I have an application with a very common architecture where my clients (web and mobile) talk to a REST API which then talks to a service layer and data layer.

我了解JWT令牌身份验证,但是对于如何使用刷新令牌我有些困惑.

I understand JWT token authentication, but I am a little confused at how I should use refresh tokens.

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

I want my JWT authentication to have the following properties:

  1. JWT令牌的有效期为2小时.

  1. JWT Token has an expiration of 2 hours.

客户端每小时都会刷新一次令牌.

The token is refreshed every hour by the client.

如果用户令牌未刷新(用户处于非活动状态且应用未打开)并且到期,则他们需要恢复登录时都需要登录.

If the user token is not refreshed (user is inactive and the app is not open) and expires, they will need to log in whenever they want to resume.

我看到很多人声称使用刷新令牌的概念来使它成为更好的体验,但是,我看不到这样做的好处.似乎必须管理它才增加了复杂性.

I see a lot of people claiming to make this a better experience using the concept of a refresh token, however, I don't see the benefit of this. It seems like an added complexity having to manage it.

我的问题如下:

  1. 如果我要使用刷新令牌,那么对于该令牌的良好实践也要长期到期是否仍然有益呢?
  2. 如果我要使用刷新令牌,该令牌是否会与userId和/或JWT令牌一起保留?
  3. 每1小时更新一次令牌时,该如何工作?我是否要创建一个接受我的JWT令牌或刷新令牌的终结点?这会更新我的原始JWT令牌的到期日期,还是创建一个新的令牌?
  4. 鉴于这些详细信息,是否需要刷新令牌?看来,如果用户只是使用JWT令牌(按上面的链接)来获取新令牌,则刷新令牌已过时.

推荐答案

稍后,让我提出您的问题,并从实际讨论刷新令牌的整个目的开始.

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

所以情况是:

用户打开应用程序并提供其登录凭据.现在,很可能该应用程序正在与REST后端服务进行交互. REST是无状态的,没有办法授权对API的访问.因此,到目前为止,在讨论中,还没有办法检查授权用户是否正在访问API或只是一些随机请求通过.

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.

现在能够解决此问题,我们需要一种方法来知道请求来自授权用户.因此,我们要做的是引入一种称为访问令牌的东西.因此,一旦用户成功通过身份验证,便会获得一个访问令牌.该令牌应该是一个长且高度随机的令牌(以确保不能被猜中).这就是JWT出现的地方.现在,您可能/不希望在JWT令牌中存储任何特定于用户的详细信息.理想情况下,您只想在JWT中存储非常简单,非常不敏感的细节. JWT(正在使用的库)本身负责处理JWT哈希以检索其他用户的详细信息(IDOR等).

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.

现在我们来谈谈攻击情形.假设使用上述所有用户Alice来使用该应用程序,都拥有授权的访问令牌,现在她的应用程序可以向所有API发出请求并根据其授权来检索数据.

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.

假设 SOMEHOW ,爱丽丝丢失了访问令牌,或者换句话说,对手Bob可以访问爱丽丝的访问令牌.现在,尽管未经授权,Bob仍可以向Alice被授权的所有API发出请求.

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. 都无法检测到正在发生这种情况.
  2. 减少攻击窗口本身.

仅使用访问令牌,很难达到上述条件1,因为无论是Alice还是Bob,它都是使用相同的授权令牌,因此对两个用户的请求是不可区分的.

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.

因此,我们尝试实现上述2,因此在访问令牌的有效性上添加了一个有效期,例如访问令牌在"t"(短时间)时间内有效.

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.

它有什么帮助?好吧,即使Bob拥有访问令牌,他也只能在有效时使用它.一旦过期,他将不得不再次检索它.现在,当然,您可以说他可以像第一次一样获得它.但是话又说回来,没有什么比100%安全!

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.

现在您可能会问,Bob也可以访问刷新令牌,类似于他破坏访问令牌的方式.是的.他可以.但是,现在变得很容易识别这种事件,这在单独使用访问令牌的情况下是不可能的,并采取必要的措施来减少所造成的损害.

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.

如何?

对于每个经过身份验证的用户(通常在移动应用程序中),将一对一映射的刷新令牌和访问令牌对发布给该应用程序.因此,在任何给定的时间点,对于一个经过身份验证的用户,将只存在一个与刷新令牌相对应的访问令牌.现在假设Bob破坏了刷新令牌,他将使用它来生成访问令牌(因为只有访问令牌才被授权通过API访问资源).一旦Bob(攻击者)由于Alice(原始用户)的访问令牌仍然有效而请求使用新生成的访问令牌,则服务器会将此视为异常,因为对于单个刷新令牌,在该位置只能有一个授权的访问令牌.一个时间.识别异常后,服务器将销毁相关的刷新令牌,并且与之相关的所有关联的访问令牌也将失效.因此,可以防止对需要资源的任何授权进行任何进一步的访问,无论是真实的还是恶意的. 用户Alice将被要求再次使用她的凭据进行身份验证,并获取一对有效的刷新和访问令牌.

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.

当然,您仍然可以说Bob可以再次获得刷新和访问令牌的访问权,并重复上述整个过程,有可能导致对真正的真正客户Alice进行DoS,但是再没有像100%的安全性.

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.

这篇关于使用JWT令牌认证时,真的需要刷新令牌吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆