授权

class oauthlib.oauth1.AuthorizationEndpoint(request_validator, token_generator=None)[源代码]

负责让经过身份验证的用户授权客户端访问其受保护资源的端点。

典型的用法是有两个视图,一个用于显示授权表单,另一个用于在提交时处理该表单。

第一个视图将希望利用 get_realms_and_credentials 获取请求的领域和有用的客户端凭据,如名称和描述,以便在创建授权表单时使用。

在表单处理过程中,您可以使用 create_authorization_response 要验证请求,请创建一个验证器,并准备用于将用户发送回客户端的最终重定向URI。

看见 请求验证器 有关为此终结点实现哪些验证器方法的详细信息。

create_authorization_response(uri, http_method='GET', body=None, headers=None, realms=None, credentials=None)[源代码]

创建授权响应,如果有效,则使用新的请求令牌。

参数:
  • uri -- 令牌请求的完整URI。

  • http_method -- 有效的HTTP谓词,即GET、POST、PUT、HEAD等。

  • body -- 字符串形式的请求正文。

  • headers -- 请求标头作为字典。

  • credentials -- 要包括在验证器中的凭据列表。

返回:

由3个元素组成的元组。1.要在响应上设置的标头字典。2.字符串形式的响应Body。3.响应状态码,整型。

如果当前令牌绑定的回调URI为OOB,则返回200状态码的响应。在这种情况下,可能希望修改响应以更好地向客户端显示验证器。

授权请求示例:

>>> from your_validator import your_validator
>>> from oauthlib.oauth1 import AuthorizationEndpoint
>>> endpoint = AuthorizationEndpoint(your_validator)
>>> h, b, s = endpoint.create_authorization_response(
...     'https://your.provider/authorize?oauth_token=...',
...     credentials={
...         'extra': 'argument',
...     })
>>> h
{'Location': 'https://the.client/callback?oauth_verifier=...&extra=argument'}
>>> b
None
>>> s
302

带有“OOB”回调的请求示例::

>>> from your_validator import your_validator
>>> from oauthlib.oauth1 import AuthorizationEndpoint
>>> endpoint = AuthorizationEndpoint(your_validator)
>>> h, b, s = endpoint.create_authorization_response(
...     'https://your.provider/authorize?foo=bar',
...     credentials={
...         'extra': 'argument',
...     })
>>> h
{'Content-Type': 'application/x-www-form-urlencoded'}
>>> b
'oauth_verifier=...&extra=argument'
>>> s
200
create_verifier(request, credentials)[源代码]

创建并保存新的请求令牌。

参数:
  • request (oauthlib.common.Request) -- OAuthlib请求。

  • credentials -- 额外令牌凭据的字典。

返回:

作为判决书的验证者。

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

获取提供的请求令牌的域和凭据。

参数:
  • uri -- 令牌请求的完整URI。

  • http_method -- 有效的HTTP谓词,即GET、POST、PUT、HEAD等。

  • body -- 字符串形式的请求正文。

  • headers -- 请求标头作为字典。

返回:

由2个元素组成的元组。1.请求领域列表。2.在创建授权表时可能有用的凭证字典。