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

Benjamin Mako Hill || Want to submit a patch?