刷新令牌 - 多个客户端的服务器端存储和撤销
Refresh Tokens - Server Side Storage And Revoking For Multiple Clients
我开始使用 ASOS (AspNet.Security.OpenIdConnect.Server) 框架进行基于令牌的身份验证。
我已经完成了访问令牌的生成和检索,现在要继续刷新令牌位。
我的问题是:
- 我应该如何存储刷新令牌服务器端?
- 我是否应该只将 clientID 和经过散列和加盐处理的刷新令牌存储在数据库中(连同实用字段,例如到期日期)?
- 如果我的 API 的用户只有一个 clientID 和密码,但同时执行许多调用,那么预期的行为是什么(假设他们想在多台机器上横向扩展客户端以获得更好的性能吞吐量为例)。
- 具体来说,我的意思是如果 1 个客户端的访问令牌已过期,但他们的刷新令牌也已过期怎么办?
当然,他们可以去令牌端点同时获取新的访问令牌和刷新令牌,但是那个 clientID 的其他实例呢?假设它们的代码相同(即它们不共享刷新令牌的知识),每个实例还将继续请求新的访问和刷新令牌。
- 如果您为一个 clientID 存储单个刷新令牌,您最终会过度请求刷新令牌,可能每次访问令牌过期时,这是不可取的。
- 如果您为一个客户端存储多个刷新令牌,多少个是合理的?
另外,撤销刷新令牌的一般流程是什么?
是不是就像从存储它的地方删除它一样简单?
谢谢。
Should I just store the clientID and the hashed and salted refresh token in a database (Along with utility fields, such as an expiration date)?
我推荐的方法是使用 ASOS 附加到它创建的所有令牌的票证标识符。您可以通过 context.Ticket.GetTokenId()
和 context.Ticket.ExpiresUtc
.
从 SerializeRefreshToken
事件中检索刷新令牌标识符和到期日期
注意:默认标识符是 GUID,但您可以使用 context.Ticket.SetTokenId("token identifier")
.
替换它
Specifically, I mean what if 1 of the client's access tokens expires, but their refresh token has also expired? Of course they can go to the token endpoint to get a new access token and refresh token at the same time, but then what about the other instances for that clientID?
这实际上取决于您的应用程序要求以及您如何实现它。您可以自由地将刷新令牌视为完全独立的,或者相反,相互依赖的。这种逻辑通常会发生在 HandleTokenRequest
.
Also, what is the common process of revoking the refresh tokens? Is it as simple as just deleting it from wherever you're storing it?
如果您使用默认令牌格式(超过推荐的格式),刷新令牌将被视为有效,直到它们过期。您可以通过数据库查找来检查令牌是否已从 HandleTokenRequest
中撤销(您可以使用 context.Ticket.GetTokenId()
获取刷新令牌标识符)
我开始使用 ASOS (AspNet.Security.OpenIdConnect.Server) 框架进行基于令牌的身份验证。
我已经完成了访问令牌的生成和检索,现在要继续刷新令牌位。
我的问题是:
- 我应该如何存储刷新令牌服务器端?
- 我是否应该只将 clientID 和经过散列和加盐处理的刷新令牌存储在数据库中(连同实用字段,例如到期日期)?
- 如果我的 API 的用户只有一个 clientID 和密码,但同时执行许多调用,那么预期的行为是什么(假设他们想在多台机器上横向扩展客户端以获得更好的性能吞吐量为例)。
- 具体来说,我的意思是如果 1 个客户端的访问令牌已过期,但他们的刷新令牌也已过期怎么办? 当然,他们可以去令牌端点同时获取新的访问令牌和刷新令牌,但是那个 clientID 的其他实例呢?假设它们的代码相同(即它们不共享刷新令牌的知识),每个实例还将继续请求新的访问和刷新令牌。
- 如果您为一个 clientID 存储单个刷新令牌,您最终会过度请求刷新令牌,可能每次访问令牌过期时,这是不可取的。
- 如果您为一个客户端存储多个刷新令牌,多少个是合理的?
另外,撤销刷新令牌的一般流程是什么? 是不是就像从存储它的地方删除它一样简单?
谢谢。
Should I just store the clientID and the hashed and salted refresh token in a database (Along with utility fields, such as an expiration date)?
我推荐的方法是使用 ASOS 附加到它创建的所有令牌的票证标识符。您可以通过 context.Ticket.GetTokenId()
和 context.Ticket.ExpiresUtc
.
SerializeRefreshToken
事件中检索刷新令牌标识符和到期日期
注意:默认标识符是 GUID,但您可以使用 context.Ticket.SetTokenId("token identifier")
.
Specifically, I mean what if 1 of the client's access tokens expires, but their refresh token has also expired? Of course they can go to the token endpoint to get a new access token and refresh token at the same time, but then what about the other instances for that clientID?
这实际上取决于您的应用程序要求以及您如何实现它。您可以自由地将刷新令牌视为完全独立的,或者相反,相互依赖的。这种逻辑通常会发生在 HandleTokenRequest
.
Also, what is the common process of revoking the refresh tokens? Is it as simple as just deleting it from wherever you're storing it?
如果您使用默认令牌格式(超过推荐的格式),刷新令牌将被视为有效,直到它们过期。您可以通过数据库查找来检查令牌是否已从 HandleTokenRequest
中撤销(您可以使用 context.Ticket.GetTokenId()
获取刷新令牌标识符)