我有一个程序,与YouTube直播API集成。它在计时器上运行,所以对我来说,每50分钟用刷新令牌获取一个新的访问令牌相对容易。我的问题是,为什么?

当我通过YouTube认证时,它给了我一个刷新令牌。然后,我大约每小时使用这个刷新令牌来获得一个新的访问令牌。如果我有刷新令牌,我总是可以使用它来获得一个新的访问令牌,因为它永远不会过期。因此,我不认为这比从一开始就给我一个访问令牌而不打扰整个刷新令牌系统更安全。


当前回答

@Teyam提到SO帖子为什么OAuth v2同时拥有访问和刷新令牌?但我更喜欢另一个答案:https://stackoverflow.com/a/12885823/254109

DR refresh_token不会增加安全性。它的目的是提高可伸缩性和性能。然后,access_token可以存储在一些快速的临时存储中(如内存)。它还允许授权和资源服务器分离。

其他回答

“所以我不认为这比从一开始就给我一个访问令牌,而不打扰整个刷新令牌系统更安全。” 我也纠结于同样的问题。简单的回答是,刷新令牌对于确保凭证没有过期是必要的。

An example may help: I have a database that stores your medical records. You consent to sharing your medical records with your spouse. Your spouse uses their Access Token to read your records from my database. Two weeks from now your spouse checks again on your medical records and the refresh token is used to ensure they still have permission (from the authentication server) to view your records. The refresh token bypasses the need for your spouse to re-enter their credentials (username and password) to the authentication server, but it does ensure they still have legitimacy to access the resource. A never expiring Access Token would not know if you had revoked your spouse's rights to access your medical records.

@Teyam提到SO帖子为什么OAuth v2同时拥有访问和刷新令牌?但我更喜欢另一个答案:https://stackoverflow.com/a/12885823/254109

DR refresh_token不会增加安全性。它的目的是提高可伸缩性和性能。然后,access_token可以存储在一些快速的临时存储中(如内存)。它还允许授权和资源服务器分离。

access_token使用得更频繁,撤消的能力不是很重要,因为它们的生命周期很短。

refresh_token使用的频率较低,撤消的能力至关重要,因为它们可以用来生成新的access_token。

验证已签名令牌的成本较低,但撤销令牌很困难。

验证存储在数据库中的令牌的成本很高,但很容易撤销。

因此,签名密钥可以用作access_token来提高性能。

Db存储的键可以作为refresh_token使用,以便于撤销它们。

如果没有refresh_token,就很难找到一种提供低成本验证和简单撤销能力的机制。因此refresh_token的存在是由于性能原因。

这是一次很好的学习经历,了解了令牌、刷新令牌和缓存它。然而,(我很好奇,我在这里不给出任何建议)我们可以使用用户登录后返回的代码,当使用微软身份平台时。我们是否可以只存储CodeIdToken,并在需要时使用它来获取新的访问令牌?因为我在想,我们用它来获得访问令牌,那么我们应该每次都用来重新生成访问令牌吗?

 ...
 ResponseType = OpenIdConnectResponseType.CodeIdToken,
...

and

private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
    {
        IConfidentialClientApplication clientApp = MsalAppBuilder.BuildConfidentialClientApplication();
        AuthenticationResult result = await clientApp.AcquireTokenByAuthorizationCode(new[] { "User.Read" }, context.Code)
          .ExecuteAsync();
    }
    

refresh_token模式使OAuth服务器处于控制状态,因此当发生不好的事情(如access_token和refresh_token泄露)时,服务器可以进行干预。

e.g.

如果access_token和refresh_token落入黑客手中,access_token将很快过期,黑客可能会尝试刷新令牌,但服务器现在有能力/控制不再发出access_token(考虑到服务器获得了泄漏信息)。