资源授权

资源端点验证提供的令牌是否有效,并授予对与相关资源相关联的作用域的访问权限。

Request Verification

每个视图可以设置其绑定的特定范围。只有提供绑定到正确作用域的访问令牌的请求才能访问该视图。访问令牌通常嵌入在授权头中,但也可能出现在查询或正文中。

# Initial setup
from your_validator import your_validator
server = WebApplicationServer(your_validator)

# Per view scopes
required_scopes = ['https://example.com/userProfile']

# Validate request
uri = 'https://example.com/userProfile?access_token=sldafh309sdf'
headers, body, http_method = {}, '', 'GET'

valid, oauthlib_request = server.verify_request(
    uri, http_method, body, headers, required_scopes)

# oauthlib_request has a few convenient attributes set such as
# oauthlib_request.client = the client associated with the token
# oauthlib_request.user = the user associated with the token
# oauthlib_request.scopes = the scopes bound to this token

if valid:
    # return the protected resource / view
else:
    # return an http forbidden 403
class oauthlib.oauth2.ResourceEndpoint(default_token, token_types)[源代码]

授权访问受保护的资源。

客户端通过向资源服务器呈现访问令牌来访问受保护的资源。资源服务器必须验证访问令牌,并确保其未过期,并且其范围涵盖所请求的资源。资源服务器用于验证访问令牌(以及任何错误响应)的方法超出了本规范的范围,但通常涉及资源服务器和授权服务器之间的交互或协调:

# For most cases, returning a 403 should suffice.

客户端利用访问令牌向资源服务器进行身份验证的方法取决于授权服务器发布的访问令牌的类型。通常,它涉及使用HTTP“Authorization”请求标头字段 [RFC2617] 使用由所使用的访问令牌类型的规范定义的身份验证方案,例如 [RFC6750] **

# Access tokens may also be provided in query and body
https://example.com/protected?access_token=kjfch2345sdf   # Query
access_token=sdf23409df   # Body
find_token_type(request)[源代码]

令牌类型标识。

RFC 6749不提供在受保护资源访问期间轻松区分不同令牌类型的方法。我们通过要求每个已知的令牌类型根据请求给出估计来估计最可能的令牌类型(如果有的话)。

verify_request(uri, http_method='GET', body=None, headers=None, scopes=None)[源代码]

验证客户端、代码等,返回正文+标题