1 # -*- coding: utf-8 -*-
3 oauthlib.oauth1.rfc5849.endpoints.access_token
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 This module is an implementation of the access token provider logic of
7 OAuth 1.0 RFC 5849. It validates the correctness of access token requests,
8 creates and persists tokens as well as create the proper response to be
9 returned to the client.
11 from __future__ import absolute_import, unicode_literals
15 from oauthlib.common import urlencode
17 from .base import BaseEndpoint
20 log = logging.getLogger(__name__)
23 class AccessTokenEndpoint(BaseEndpoint):
25 """An endpoint responsible for providing OAuth 1 access tokens.
27 Typical use is to instantiate with a request validator and invoke the
28 ``create_access_token_response`` from a view function. The tuple returned
29 has all information necessary (body, status, headers) to quickly form
30 and return a proper response. See :doc:`/oauth1/validator` for details on which
31 validator methods to implement for this endpoint.
34 def create_access_token(self, request, credentials):
35 """Create and save a new access token.
37 Similar to OAuth 2, indication of granted scopes will be included as a
38 space separated list in ``oauth_authorized_realms``.
40 :param request: An oauthlib.common.Request object.
41 :returns: The token as an urlencoded string.
43 request.realms = self.request_validator.get_realms(
44 request.resource_owner_key, request)
46 'oauth_token': self.token_generator(),
47 'oauth_token_secret': self.token_generator(),
48 # Backport the authorized scopes indication used in OAuth2
49 'oauth_authorized_realms': ' '.join(request.realms)
51 token.update(credentials)
52 self.request_validator.save_access_token(token, request)
53 return urlencode(token.items())
55 def create_access_token_response(self, uri, http_method='GET', body=None,
56 headers=None, credentials=None):
57 """Create an access token response, with a new request token if valid.
59 :param uri: The full URI of the token request.
60 :param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
61 :param body: The request body as a string.
62 :param headers: The request headers as a dict.
63 :param credentials: A list of extra credentials to include in the token.
64 :returns: A tuple of 3 elements.
65 1. A dict of headers to set on the response.
66 2. The response body as a string.
67 3. The response status code as an integer.
69 An example of a valid request::
71 >>> from your_validator import your_validator
72 >>> from oauthlib.oauth1 import AccessTokenEndpoint
73 >>> endpoint = AccessTokenEndpoint(your_validator)
74 >>> h, b, s = endpoint.create_access_token_response(
75 ... 'https://your.provider/access_token?foo=bar',
77 ... 'Authorization': 'OAuth oauth_token=234lsdkf....'
80 ... 'my_specific': 'argument',
83 {'Content-Type': 'application/x-www-form-urlencoded'}
85 'oauth_token=lsdkfol23w54jlksdef&oauth_token_secret=qwe089234lkjsdf&oauth_authorized_realms=movies+pics&my_specific=argument'
89 An response to invalid request would have a different body and status::
92 'error=invalid_request&description=missing+resource+owner+key'
96 The same goes for an an unauthorized request:
103 resp_headers = {'Content-Type': 'application/x-www-form-urlencoded'}
105 request = self._create_request(uri, http_method, body, headers)
106 valid, processed_request = self.validate_access_token_request(
109 token = self.create_access_token(request, credentials or {})
110 self.request_validator.invalidate_request_token(
112 request.resource_owner_key,
114 return resp_headers, token, 200
117 except errors.OAuth1Error as e:
118 return resp_headers, e.urlencoded, e.status_code
120 def validate_access_token_request(self, request):
121 """Validate an access token request.
123 :param request: An oauthlib.common.Request object.
124 :raises: OAuth1Error if the request is invalid.
125 :returns: A tuple of 2 elements.
126 1. The validation result (True or False).
127 2. The request object.
129 self._check_transport_security(request)
130 self._check_mandatory_parameters(request)
132 if not request.resource_owner_key:
133 raise errors.InvalidRequestError(
134 description='Missing resource owner.')
136 if not self.request_validator.check_request_token(
137 request.resource_owner_key):
138 raise errors.InvalidRequestError(
139 description='Invalid resource owner key format.')
141 if not request.verifier:
142 raise errors.InvalidRequestError(
143 description='Missing verifier.')
145 if not self.request_validator.check_verifier(request.verifier):
146 raise errors.InvalidRequestError(
147 description='Invalid verifier format.')
149 if not self.request_validator.validate_timestamp_and_nonce(
150 request.client_key, request.timestamp, request.nonce, request,
151 request_token=request.resource_owner_key):
152 return False, request
154 # The server SHOULD return a 401 (Unauthorized) status code when
155 # receiving a request with invalid client credentials.
156 # Note: This is postponed in order to avoid timing attacks, instead
157 # a dummy client is assigned and used to maintain near constant
158 # time request verification.
160 # Note that early exit would enable client enumeration
161 valid_client = self.request_validator.validate_client_key(
162 request.client_key, request)
164 request.client_key = self.request_validator.dummy_client
166 # The server SHOULD return a 401 (Unauthorized) status code when
167 # receiving a request with invalid or expired token.
168 # Note: This is postponed in order to avoid timing attacks, instead
169 # a dummy token is assigned and used to maintain near constant
170 # time request verification.
172 # Note that early exit would enable resource owner enumeration
173 valid_resource_owner = self.request_validator.validate_request_token(
174 request.client_key, request.resource_owner_key, request)
175 if not valid_resource_owner:
176 request.resource_owner_key = self.request_validator.dummy_request_token
178 # The server MUST verify (Section 3.2) the validity of the request,
179 # ensure that the resource owner has authorized the provisioning of
180 # token credentials to the client, and ensure that the temporary
181 # credentials have not expired or been used before. The server MUST
182 # also verify the verification code received from the client.
183 # .. _`Section 3.2`: http://tools.ietf.org/html/rfc5849#section-3.2
185 # Note that early exit would enable resource owner authorization
186 # verifier enumertion.
187 valid_verifier = self.request_validator.validate_verifier(
189 request.resource_owner_key,
193 valid_signature = self._check_signature(request, is_token_request=True)
195 # We delay checking validity until the very end, using dummy values for
196 # calculations and fetching secrets/keys to ensure the flow of every
197 # request remains almost identical regardless of whether valid values
198 # have been supplied. This ensures near constant time execution and
199 # prevents malicious users from guessing sensitive information
200 v = all((valid_client, valid_resource_owner, valid_verifier,
203 log.info("[Failure] request verification failed.")
204 log.info("Valid client:, %s", valid_client)
205 log.info("Valid token:, %s", valid_resource_owner)
206 log.info("Valid verifier:, %s", valid_verifier)
207 log.info("Valid signature:, %s", valid_signature)