为什么不通过重新发送授权 grant/code 而不是发送刷新令牌来获取新的访问令牌?

why not obtain a new access token by resending the authorization grant/code instead of sending refresh token?

我知道访问令牌是短期的,因为它们是在不访问数据库的情况下进行验证的,而刷新令牌是长期的,并且是针对数据库进行验证的。

我不明白为什么最初通过发送授权授予获取访问令牌与后来通过发送刷新令牌获取访问令牌之间存在差异。

查看来自 RFC 6749 的这张图,为什么客户端不简单地在步骤 (G) 中重新发送授权授予?为什么需要刷新令牌?

  +--------+                                           +---------------+
  |        |--(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        +---------------+

我认为刷新令牌比初始代码更安全是有原因的。

  • 代码从认证服务器传输到资源所有者的浏览器,然后再传输到客户端。刷新令牌不通过浏览器。所以代码更容易被破坏,应该是短暂的,只能使用一次。
  • OAuth 2 specification 不需要(只是建议)客户端重定向端点的安全传输层:

This specification does not mandate the use of TLS because at the time of this writing, requiring clients to deploy TLS is a significant hurdle for many client developers.

token endpoint 需要 TLS,用于获取刷新令牌:

Since requests to the token endpoint result in the transmission of clear-text credentials (in the HTTP request and response), the authorization server MUST require the use of TLS

这使得初始代码不安全,应尽快或在检测到多次使用时使其失效。要获取令牌,您仍然需要客户端密钥,但刷新令牌更安全并且可以重复使用。