tokens.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. """
  2. oauthlib.oauth2.rfc6749.tokens
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. This module contains methods for adding two types of access tokens to requests.
  5. - Bearer http://tools.ietf.org/html/rfc6750
  6. - MAC http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01
  7. """
  8. from __future__ import absolute_import, unicode_literals
  9. from binascii import b2a_base64
  10. import hashlib
  11. import hmac
  12. try:
  13. from urlparse import urlparse
  14. except ImportError:
  15. from urllib.parse import urlparse
  16. from oauthlib.common import add_params_to_uri, add_params_to_qs, unicode_type
  17. from oauthlib import common
  18. from . import utils
  19. class OAuth2Token(dict):
  20. def __init__(self, params, old_scope=None):
  21. super(OAuth2Token, self).__init__(params)
  22. self._new_scope = None
  23. if 'scope' in params and params['scope']:
  24. self._new_scope = set(utils.scope_to_list(params['scope']))
  25. if old_scope is not None:
  26. self._old_scope = set(utils.scope_to_list(old_scope))
  27. if self._new_scope is None:
  28. # the rfc says that if the scope hasn't changed, it's optional
  29. # in params so set the new scope to the old scope
  30. self._new_scope = self._old_scope
  31. else:
  32. self._old_scope = self._new_scope
  33. @property
  34. def scope_changed(self):
  35. return self._new_scope != self._old_scope
  36. @property
  37. def old_scope(self):
  38. return utils.list_to_scope(self._old_scope)
  39. @property
  40. def old_scopes(self):
  41. return list(self._old_scope)
  42. @property
  43. def scope(self):
  44. return utils.list_to_scope(self._new_scope)
  45. @property
  46. def scopes(self):
  47. return list(self._new_scope)
  48. @property
  49. def missing_scopes(self):
  50. return list(self._old_scope - self._new_scope)
  51. @property
  52. def additional_scopes(self):
  53. return list(self._new_scope - self._old_scope)
  54. def prepare_mac_header(token, uri, key, http_method,
  55. nonce=None,
  56. headers=None,
  57. body=None,
  58. ext='',
  59. hash_algorithm='hmac-sha-1',
  60. issue_time=None,
  61. draft=0):
  62. """Add an `MAC Access Authentication`_ signature to headers.
  63. Unlike OAuth 1, this HMAC signature does not require inclusion of the
  64. request payload/body, neither does it use a combination of client_secret
  65. and token_secret but rather a mac_key provided together with the access
  66. token.
  67. Currently two algorithms are supported, "hmac-sha-1" and "hmac-sha-256",
  68. `extension algorithms`_ are not supported.
  69. Example MAC Authorization header, linebreaks added for clarity
  70. Authorization: MAC id="h480djs93hd8",
  71. nonce="1336363200:dj83hs9s",
  72. mac="bhCQXTVyfj5cmA9uKkPFx1zeOXM="
  73. .. _`MAC Access Authentication`: http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01
  74. .. _`extension algorithms`: http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-7.1
  75. :param uri: Request URI.
  76. :param headers: Request headers as a dictionary.
  77. :param http_method: HTTP Request method.
  78. :param key: MAC given provided by token endpoint.
  79. :param hash_algorithm: HMAC algorithm provided by token endpoint.
  80. :param issue_time: Time when the MAC credentials were issued (datetime).
  81. :param draft: MAC authentication specification version.
  82. :return: headers dictionary with the authorization field added.
  83. """
  84. http_method = http_method.upper()
  85. host, port = utils.host_from_uri(uri)
  86. if hash_algorithm.lower() == 'hmac-sha-1':
  87. h = hashlib.sha1
  88. elif hash_algorithm.lower() == 'hmac-sha-256':
  89. h = hashlib.sha256
  90. else:
  91. raise ValueError('unknown hash algorithm')
  92. if draft == 0:
  93. nonce = nonce or '{0}:{1}'.format(utils.generate_age(issue_time),
  94. common.generate_nonce())
  95. else:
  96. ts = common.generate_timestamp()
  97. nonce = common.generate_nonce()
  98. sch, net, path, par, query, fra = urlparse(uri)
  99. if query:
  100. request_uri = path + '?' + query
  101. else:
  102. request_uri = path
  103. # Hash the body/payload
  104. if body is not None and draft == 0:
  105. body = body.encode('utf-8')
  106. bodyhash = b2a_base64(h(body).digest())[:-1].decode('utf-8')
  107. else:
  108. bodyhash = ''
  109. # Create the normalized base string
  110. base = []
  111. if draft == 0:
  112. base.append(nonce)
  113. else:
  114. base.append(ts)
  115. base.append(nonce)
  116. base.append(http_method.upper())
  117. base.append(request_uri)
  118. base.append(host)
  119. base.append(port)
  120. if draft == 0:
  121. base.append(bodyhash)
  122. base.append(ext or '')
  123. base_string = '\n'.join(base) + '\n'
  124. # hmac struggles with unicode strings - http://bugs.python.org/issue5285
  125. if isinstance(key, unicode_type):
  126. key = key.encode('utf-8')
  127. sign = hmac.new(key, base_string.encode('utf-8'), h)
  128. sign = b2a_base64(sign.digest())[:-1].decode('utf-8')
  129. header = []
  130. header.append('MAC id="%s"' % token)
  131. if draft != 0:
  132. header.append('ts="%s"' % ts)
  133. header.append('nonce="%s"' % nonce)
  134. if bodyhash:
  135. header.append('bodyhash="%s"' % bodyhash)
  136. if ext:
  137. header.append('ext="%s"' % ext)
  138. header.append('mac="%s"' % sign)
  139. headers = headers or {}
  140. headers['Authorization'] = ', '.join(header)
  141. return headers
  142. def prepare_bearer_uri(token, uri):
  143. """Add a `Bearer Token`_ to the request URI.
  144. Not recommended, use only if client can't use authorization header or body.
  145. http://www.example.com/path?access_token=h480djs93hd8
  146. .. _`Bearer Token`: http://tools.ietf.org/html/rfc6750
  147. """
  148. return add_params_to_uri(uri, [(('access_token', token))])
  149. def prepare_bearer_headers(token, headers=None):
  150. """Add a `Bearer Token`_ to the request URI.
  151. Recommended method of passing bearer tokens.
  152. Authorization: Bearer h480djs93hd8
  153. .. _`Bearer Token`: http://tools.ietf.org/html/rfc6750
  154. """
  155. headers = headers or {}
  156. headers['Authorization'] = 'Bearer %s' % token
  157. return headers
  158. def prepare_bearer_body(token, body=''):
  159. """Add a `Bearer Token`_ to the request body.
  160. access_token=h480djs93hd8
  161. .. _`Bearer Token`: http://tools.ietf.org/html/rfc6750
  162. """
  163. return add_params_to_qs(body, [(('access_token', token))])
  164. def random_token_generator(request, refresh_token=False):
  165. return common.generate_token()
  166. def signed_token_generator(private_pem, **kwargs):
  167. def signed_token_generator(request):
  168. request.claims = kwargs
  169. return common.generate_signed_token(private_pem, request)
  170. return signed_token_generator
  171. class TokenBase(object):
  172. def __call__(self, request, refresh_token=False):
  173. raise NotImplementedError('Subclasses must implement this method.')
  174. def validate_request(self, request):
  175. raise NotImplementedError('Subclasses must implement this method.')
  176. def estimate_type(self, request):
  177. raise NotImplementedError('Subclasses must implement this method.')
  178. class BearerToken(TokenBase):
  179. __slots__ = (
  180. 'request_validator', 'token_generator',
  181. 'refresh_token_generator', 'expires_in'
  182. )
  183. def __init__(self, request_validator=None, token_generator=None,
  184. expires_in=None, refresh_token_generator=None):
  185. self.request_validator = request_validator
  186. self.token_generator = token_generator or random_token_generator
  187. self.refresh_token_generator = (
  188. refresh_token_generator or self.token_generator
  189. )
  190. self.expires_in = expires_in or 3600
  191. def create_token(self, request, refresh_token=False, save_token=True):
  192. """Create a BearerToken, by default without refresh token."""
  193. if callable(self.expires_in):
  194. expires_in = self.expires_in(request)
  195. else:
  196. expires_in = self.expires_in
  197. request.expires_in = expires_in
  198. token = {
  199. 'access_token': self.token_generator(request),
  200. 'expires_in': expires_in,
  201. 'token_type': 'Bearer',
  202. }
  203. # If provided, include - this is optional in some cases https://tools.ietf.org/html/rfc6749#section-3.3 but
  204. # there is currently no mechanism to coordinate issuing a token for only a subset of the requested scopes so
  205. # all tokens issued are for the entire set of requested scopes.
  206. if request.scopes is not None:
  207. token['scope'] = ' '.join(request.scopes)
  208. if request.state is not None:
  209. token['state'] = request.state
  210. if refresh_token:
  211. if (request.refresh_token and
  212. not self.request_validator.rotate_refresh_token(request)):
  213. token['refresh_token'] = request.refresh_token
  214. else:
  215. token['refresh_token'] = self.refresh_token_generator(request)
  216. token.update(request.extra_credentials or {})
  217. token = OAuth2Token(token)
  218. if save_token:
  219. self.request_validator.save_bearer_token(token, request)
  220. return token
  221. def validate_request(self, request):
  222. token = None
  223. if 'Authorization' in request.headers:
  224. token = request.headers.get('Authorization')[7:]
  225. else:
  226. token = request.access_token
  227. return self.request_validator.validate_bearer_token(
  228. token, request.scopes, request)
  229. def estimate_type(self, request):
  230. if request.headers.get('Authorization', '').startswith('Bearer'):
  231. return 9
  232. elif request.access_token is not None:
  233. return 5
  234. else:
  235. return 0