authorization.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """
  3. oauthlib.oauth1.rfc5849.endpoints.authorization
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This module is an implementation of various logic needed
  6. for signing and checking OAuth 1.0 RFC 5849 requests.
  7. """
  8. from urllib.parse import urlencode
  9. from oauthlib.common import add_params_to_uri
  10. from .. import errors
  11. from .base import BaseEndpoint
  12. class AuthorizationEndpoint(BaseEndpoint):
  13. """An endpoint responsible for letting authenticated users authorize access
  14. to their protected resources to a client.
  15. Typical use would be to have two views, one for displaying the authorization
  16. form and one to process said form on submission.
  17. The first view will want to utilize ``get_realms_and_credentials`` to fetch
  18. requested realms and useful client credentials, such as name and
  19. description, to be used when creating the authorization form.
  20. During form processing you can use ``create_authorization_response`` to
  21. validate the request, create a verifier as well as prepare the final
  22. redirection URI used to send the user back to the client.
  23. See :doc:`/oauth1/validator` for details on which validator methods to implement
  24. for this endpoint.
  25. """
  26. def create_verifier(self, request, credentials):
  27. """Create and save a new request token.
  28. :param request: OAuthlib request.
  29. :type request: oauthlib.common.Request
  30. :param credentials: A dict of extra token credentials.
  31. :returns: The verifier as a dict.
  32. """
  33. verifier = {
  34. 'oauth_token': request.resource_owner_key,
  35. 'oauth_verifier': self.token_generator(),
  36. }
  37. verifier.update(credentials)
  38. self.request_validator.save_verifier(
  39. request.resource_owner_key, verifier, request)
  40. return verifier
  41. def create_authorization_response(self, uri, http_method='GET', body=None,
  42. headers=None, realms=None, credentials=None):
  43. """Create an authorization response, with a new request token if valid.
  44. :param uri: The full URI of the token request.
  45. :param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
  46. :param body: The request body as a string.
  47. :param headers: The request headers as a dict.
  48. :param credentials: A list of credentials to include in the verifier.
  49. :returns: A tuple of 3 elements.
  50. 1. A dict of headers to set on the response.
  51. 2. The response body as a string.
  52. 3. The response status code as an integer.
  53. If the callback URI tied to the current token is "oob", a response with
  54. a 200 status code will be returned. In this case, it may be desirable to
  55. modify the response to better display the verifier to the client.
  56. An example of an authorization request::
  57. >>> from your_validator import your_validator
  58. >>> from oauthlib.oauth1 import AuthorizationEndpoint
  59. >>> endpoint = AuthorizationEndpoint(your_validator)
  60. >>> h, b, s = endpoint.create_authorization_response(
  61. ... 'https://your.provider/authorize?oauth_token=...',
  62. ... credentials={
  63. ... 'extra': 'argument',
  64. ... })
  65. >>> h
  66. {'Location': 'https://the.client/callback?oauth_verifier=...&extra=argument'}
  67. >>> b
  68. None
  69. >>> s
  70. 302
  71. An example of a request with an "oob" callback::
  72. >>> from your_validator import your_validator
  73. >>> from oauthlib.oauth1 import AuthorizationEndpoint
  74. >>> endpoint = AuthorizationEndpoint(your_validator)
  75. >>> h, b, s = endpoint.create_authorization_response(
  76. ... 'https://your.provider/authorize?foo=bar',
  77. ... credentials={
  78. ... 'extra': 'argument',
  79. ... })
  80. >>> h
  81. {'Content-Type': 'application/x-www-form-urlencoded'}
  82. >>> b
  83. 'oauth_verifier=...&extra=argument'
  84. >>> s
  85. 200
  86. """
  87. request = self._create_request(uri, http_method=http_method, body=body,
  88. headers=headers)
  89. if not request.resource_owner_key:
  90. raise errors.InvalidRequestError(
  91. 'Missing mandatory parameter oauth_token.')
  92. if not self.request_validator.verify_request_token(
  93. request.resource_owner_key, request):
  94. raise errors.InvalidClientError()
  95. request.realms = realms
  96. if (request.realms and not self.request_validator.verify_realms(
  97. request.resource_owner_key, request.realms, request)):
  98. raise errors.InvalidRequestError(
  99. description=('User granted access to realms outside of '
  100. 'what the client may request.'))
  101. verifier = self.create_verifier(request, credentials or {})
  102. redirect_uri = self.request_validator.get_redirect_uri(
  103. request.resource_owner_key, request)
  104. if redirect_uri == 'oob':
  105. response_headers = {
  106. 'Content-Type': 'application/x-www-form-urlencoded'}
  107. response_body = urlencode(verifier)
  108. return response_headers, response_body, 200
  109. else:
  110. populated_redirect = add_params_to_uri(
  111. redirect_uri, verifier.items())
  112. return {'Location': populated_redirect}, None, 302
  113. def get_realms_and_credentials(self, uri, http_method='GET', body=None,
  114. headers=None):
  115. """Fetch realms and credentials for the presented request token.
  116. :param uri: The full URI of the token request.
  117. :param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
  118. :param body: The request body as a string.
  119. :param headers: The request headers as a dict.
  120. :returns: A tuple of 2 elements.
  121. 1. A list of request realms.
  122. 2. A dict of credentials which may be useful in creating the
  123. authorization form.
  124. """
  125. request = self._create_request(uri, http_method=http_method, body=body,
  126. headers=headers)
  127. if not self.request_validator.verify_request_token(
  128. request.resource_owner_key, request):
  129. raise errors.InvalidClientError()
  130. realms = self.request_validator.get_realms(
  131. request.resource_owner_key, request)
  132. return realms, {'resource_owner_key': request.resource_owner_key}