authorization.py 4.6 KB

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