1 # -*- coding: utf-8 -*-
3 oauthlib.oauth2.rfc6749
4 ~~~~~~~~~~~~~~~~~~~~~~~
6 This module is an implementation of various logic needed
7 for consuming and providing OAuth 2.0 RFC6749.
9 from __future__ import absolute_import, unicode_literals
13 from oauthlib.common import Request
15 from .base import BaseEndpoint, catch_errors_and_unavailability
17 log = logging.getLogger(__name__)
20 class AuthorizationEndpoint(BaseEndpoint):
22 """Authorization endpoint - used by the client to obtain authorization
23 from the resource owner via user-agent redirection.
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
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::
37 https://example.com/path?query=component # OK
38 https://example.com/path?query=component#fragment # Not OK
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::
46 # We will deny any request which URI schema is not with https
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::
52 # HTTP method is currently not enforced
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::
59 # Enforced through the design of oauthlib.common.Request
61 .. _`Appendix B`: http://tools.ietf.org/html/rfc6749#appendix-B
64 def __init__(self, default_response_type, default_token_type,
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
72 def response_types(self):
73 return self._response_types
76 def default_response_type(self):
77 return self._default_response_type
80 def default_response_type_handler(self):
81 return self.response_types.get(self.default_response_type)
84 def default_token_type(self):
85 return self._default_token_type
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."""
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)
105 @catch_errors_and_unavailability
106 def validate_authorization_request(self, uri, http_method='GET', body=None,
108 """Extract response_type and route to the designated handler."""
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)