我正在构建一个移动应用程序,并使用JWT进行身份验证。
似乎最好的方法是将JWT访问令牌与刷新令牌配对,以便我可以随意地频繁过期访问令牌。
- 刷新令牌是什么样子的?它是一个随机字符串吗?那个字符串是否被加密?它是另一个JWT吗?
- 在这种情况下,刷新令牌应该存储在用户模型上的数据库中,是吗?看起来应该加密它。
- 用户登录后,我是否发送刷新令牌,并让客户端访问另一个路由来检索访问令牌?
我正在构建一个移动应用程序,并使用JWT进行身份验证。
似乎最好的方法是将JWT访问令牌与刷新令牌配对,以便我可以随意地频繁过期访问令牌。
假设这是关于OAuth 2.0的,因为它涉及JWT和刷新令牌...
就像访问令牌一样,原则上刷新令牌可以是任何东西,包括您描述的所有选项;当授权服务器想要无状态或希望对呈现它的客户端施加某种“持有证明”语义时,可以使用JWT;请注意,刷新令牌与访问令牌不同,因为它只提供给最初颁发它的授权服务器,而不是提供给资源服务器,所以JWT作为访问令牌的自包含验证优化不适用于刷新令牌
这取决于数据库的安全性/访问权限;如果其他方/服务器/应用程序/用户可以访问数据库,则可以(但是在何处以及如何存储加密密钥可能会因人而异...)
授权服务器可以同时颁发访问令牌和刷新令牌,具体取决于客户端用于获取它们的授权,规范包含每个标准授权的详细信息和选项。
+--------+ +---------------+
| |--(A)------- Authorization Grant --------->| |
| | | |
| |<-(B)----------- Access Token -------------| |
| | & Refresh Token | |
| | | |
| | +----------+ | |
| |--(C)---- Access Token ---->| | | |
| | | | | |
| |<-(D)- Protected Resource --| Resource | | Authorization |
| Client | | Server | | Server |
| |--(E)---- Access Token ---->| | | |
| | | | | |
| |<-(F)- Invalid Token Error -| | | |
| | +----------+ | |
| | | |
| |--(G)----------- Refresh Token ----------->| |
| | | |
| |<-(H)----------- Access Token -------------| |
+--------+ & Optional Refresh Token +---------------+
(A) The client requests an access token by authenticating with the
authorization server and presenting an authorization grant.
(B) The authorization server authenticates the client and validates
the authorization grant, and if valid, issues an access token
and a refresh token.
(C) The client makes a protected resource request to the resource
server by presenting the access token.
(D) The resource server validates the access token, and if valid,
serves the request.
(E) Steps (C) and (D) repeat until the access token expires. If the
client knows the access token expired, it skips to step (G);
otherwise, it makes another protected resource request.
(F) Since the access token is invalid, the resource server returns
an invalid token error.
(G) The client requests a new access token by authenticating with
the authorization server and presenting the refresh token. The
client authentication requirements are based on the client type
and on the authorization server policies.
(H) The authorization server authenticates the client and validates
the refresh token, and if valid, issues a new access token (and,
optionally, a new refresh token).
基于这个使用Node.js实现JWT与刷新令牌的实例:
在这种情况下,他们使用一个uid而不是JWT。当他们刷新令牌时,他们发送刷新令牌和用户。如果您将其实现为JWT,则不需要发送用户,因为它已经包含在JWT中了。
他们在单独的文档(表格)中实现此功能。对我来说很有意义,因为用户可以登录不同的客户端应用程序,并且每个应用程序可能都有一个刷新令牌。如果用户失去安装有某个应用的设备,则可以无效该设备的刷新令牌,而不影响其他登录的设备。
在此实现中,它用访问令牌和刷新令牌同时响应登录方法。对我来说看起来是正确的。
refresh_token
的JWT,@adonese是这个意思吗?如果是这样的话,OAuth RFC 6749明确规定不要将refresh_token
发送到资源服务器(而JWT将被发送到资源服务器):https://tools.ietf.org/html/rfc6749#section-1.5 - Brenno CostaJWT存在两个问题:
规范不好
很难撤销(用于身份验证时)
第一个问题可以通过使用自己的JWT实现来解决:将想要的任何内容放入JSON中,使用AES加密 - 没问题 - 用于身份验证(如果需要授权:在JSON中添加角色)。
超简洁的JWT {"id" : "<id>"}
第二个问题需要澄清。对于存储在服务器端的常规会话,不存在撤销问题:服务器随时可以使会话失效。但是,常规会话存在可扩展性和性能问题,因此使用JWT。
解决撤销问题的常见方法是使用刷新令牌。
以下是如何完成该操作: