身份验证和资源服务器之间的 OAuth v2通信

我在理解 OAUTH-v2的工作原理时遇到了一些问题。

OAuth 版本2规范表示:

  1. 访问受保护的资源

    客户端访问受保护 资源,通过表示访问
    令牌到资源服务器 资源服务器必须验证
    访问令牌,并确保它没有 过期,其范围包括
    请求的资源。方法 资源服务器用于
    验证访问令牌(以及 任何错误响应)均 超出 此规范的范围 ,但是 一般 涉及交互作用或 资源之间的协调 服务器和授权
    服务器
    .

在实践中,资源服务器和授权服务器之间的这种交互是如何工作的?

  • 资源服务器如何运行 确定它的访问标记 收到是否有效?
  • 怎么样 资源服务器解压缩允许的 范围,以确定是否应该授予对特定资源的访问权?作用域是编码在访问令牌中,还是资源服务器必须首先与授权服务器联系?
  • 如何在资源服务器和授权服务器之间建立信任?

访问令牌属性和 用于访问受保护的 资源“强大”超出了这个范围 规范 并由 配套规格。

有人能举例说明令牌属性吗?

14560 次浏览

The reason this is out of scope for the specification is the wide range of ways to accomplish this connection between the two entities. The main question is how complex is your deployment.

For example, do you have one server managing authentication and access, and a set of discrete services each with its own servers serving the API calls? Or, do you have just one box with one web server which handles both authentication/authorization and the API calls?

In the case of a single box, not much is needed as the entity issuing tokens is the same as the one validating them. You can implement tokens to use a database table key and lookup the record in the database (or memory cache) on every request, or you can encode the scope, user id and other information directly into the token and encrypt it using a symmetric or asymmetric algorithm.

Things get a bit more complex when dealing with a distributed environment, but not by much. You still issue tokens at the authorization server, but the resource server needs a way to validate those. It can do it by making an internal API available to the resource server to ask the authorization server to "resolve" the token (which can be fast in a local environment), or the two can establish a public/private key pair or symmetric secret and use that to encrypt everything the resource server needs into the token.

Self contained tokens are longer but offer much better performance per-request. However, they come with a price - you can't really revoke them while they are still valid (not expired). For this reason, self contained tokens should be very short lived (whatever is acceptable to you to leave access open after it was revoked - e.g. many sites use one hour), with a refresh token good for a year or more to get new tokens.

One example of resource- to authorization server API is the one at Google Developers Website.
It doesn't specify the format of the access token though, but the response seems pretty universally useful.