authorization.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """
  2. oauthlib.oauth2.rfc6749
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. This module is an implementation of various logic needed
  5. for consuming and providing OAuth 2.0 RFC6749.
  6. """
  7. import logging
  8. from oauthlib.common import Request
  9. from oauthlib.oauth2.rfc6749 import utils
  10. from .base import BaseEndpoint, catch_errors_and_unavailability
  11. log = logging.getLogger(__name__)
  12. class AuthorizationEndpoint(BaseEndpoint):
  13. """Authorization endpoint - used by the client to obtain authorization
  14. from the resource owner via user-agent redirection.
  15. The authorization endpoint is used to interact with the resource
  16. owner and obtain an authorization grant. The authorization server
  17. MUST first verify the identity of the resource owner. The way in
  18. which the authorization server authenticates the resource owner (e.g.
  19. username and password login, session cookies) is beyond the scope of
  20. this specification.
  21. The endpoint URI MAY include an "application/x-www-form-urlencoded"
  22. formatted (per `Appendix B`_) query component,
  23. which MUST be retained when adding additional query parameters. The
  24. endpoint URI MUST NOT include a fragment component::
  25. https://example.com/path?query=component # OK
  26. https://example.com/path?query=component#fragment # Not OK
  27. Since requests to the authorization endpoint result in user
  28. authentication and the transmission of clear-text credentials (in the
  29. HTTP response), the authorization server MUST require the use of TLS
  30. as described in Section 1.6 when sending requests to the
  31. authorization endpoint::
  32. # We will deny any request which URI schema is not with https
  33. The authorization server MUST support the use of the HTTP "GET"
  34. method [RFC2616] for the authorization endpoint, and MAY support the
  35. use of the "POST" method as well::
  36. # HTTP method is currently not enforced
  37. Parameters sent without a value MUST be treated as if they were
  38. omitted from the request. The authorization server MUST ignore
  39. unrecognized request parameters. Request and response parameters
  40. MUST NOT be included more than once::
  41. # Enforced through the design of oauthlib.common.Request
  42. .. _`Appendix B`: https://tools.ietf.org/html/rfc6749#appendix-B
  43. """
  44. def __init__(self, default_response_type, default_token_type,
  45. response_types):
  46. BaseEndpoint.__init__(self)
  47. self._response_types = response_types
  48. self._default_response_type = default_response_type
  49. self._default_token_type = default_token_type
  50. @property
  51. def response_types(self):
  52. return self._response_types
  53. @property
  54. def default_response_type(self):
  55. return self._default_response_type
  56. @property
  57. def default_response_type_handler(self):
  58. return self.response_types.get(self.default_response_type)
  59. @property
  60. def default_token_type(self):
  61. return self._default_token_type
  62. @catch_errors_and_unavailability
  63. def create_authorization_response(self, uri, http_method='GET', body=None,
  64. headers=None, scopes=None, credentials=None):
  65. """Extract response_type and route to the designated handler."""
  66. request = Request(
  67. uri, http_method=http_method, body=body, headers=headers)
  68. request.scopes = scopes
  69. # TODO: decide whether this should be a required argument
  70. request.user = None # TODO: explain this in docs
  71. for k, v in (credentials or {}).items():
  72. setattr(request, k, v)
  73. response_type_handler = self.response_types.get(
  74. request.response_type, self.default_response_type_handler)
  75. log.debug('Dispatching response_type %s request to %r.',
  76. request.response_type, response_type_handler)
  77. return response_type_handler.create_authorization_response(
  78. request, self.default_token_type)
  79. @catch_errors_and_unavailability
  80. def validate_authorization_request(self, uri, http_method='GET', body=None,
  81. headers=None):
  82. """Extract response_type and route to the designated handler."""
  83. request = Request(
  84. uri, http_method=http_method, body=body, headers=headers)
  85. request.scopes = utils.scope_to_list(request.scope)
  86. response_type_handler = self.response_types.get(
  87. request.response_type, self.default_response_type_handler)
  88. return response_type_handler.validate_authorization_request(request)