You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

468 lines
14 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. oauthlib.common
  4. ~~~~~~~~~~~~~~
  5. This module provides data structures and utilities common
  6. to all implementations of OAuth.
  7. """
  8. from __future__ import absolute_import, unicode_literals
  9. import collections
  10. import datetime
  11. import logging
  12. import re
  13. import sys
  14. import time
  15. from . import get_debug
  16. try:
  17. from secrets import randbits
  18. from secrets import SystemRandom
  19. except ImportError:
  20. from random import getrandbits as randbits
  21. from random import SystemRandom
  22. try:
  23. from urllib import quote as _quote
  24. from urllib import unquote as _unquote
  25. from urllib import urlencode as _urlencode
  26. except ImportError:
  27. from urllib.parse import quote as _quote
  28. from urllib.parse import unquote as _unquote
  29. from urllib.parse import urlencode as _urlencode
  30. try:
  31. import urlparse
  32. except ImportError:
  33. import urllib.parse as urlparse
  34. UNICODE_ASCII_CHARACTER_SET = ('abcdefghijklmnopqrstuvwxyz'
  35. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  36. '0123456789')
  37. CLIENT_ID_CHARACTER_SET = (r' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN'
  38. 'OPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}')
  39. SANITIZE_PATTERN = re.compile(r'([^&;]*(?:password|token)[^=]*=)[^&;]+', re.IGNORECASE)
  40. INVALID_HEX_PATTERN = re.compile(r'%[^0-9A-Fa-f]|%[0-9A-Fa-f][^0-9A-Fa-f]')
  41. always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  42. 'abcdefghijklmnopqrstuvwxyz'
  43. '0123456789' '_.-')
  44. log = logging.getLogger('oauthlib')
  45. PY3 = sys.version_info[0] == 3
  46. if PY3:
  47. unicode_type = str
  48. else:
  49. unicode_type = unicode
  50. # 'safe' must be bytes (Python 2.6 requires bytes, other versions allow either)
  51. def quote(s, safe=b'/'):
  52. s = s.encode('utf-8') if isinstance(s, unicode_type) else s
  53. s = _quote(s, safe)
  54. # PY3 always returns unicode. PY2 may return either, depending on whether
  55. # it had to modify the string.
  56. if isinstance(s, bytes):
  57. s = s.decode('utf-8')
  58. return s
  59. def unquote(s):
  60. s = _unquote(s)
  61. # PY3 always returns unicode. PY2 seems to always return what you give it,
  62. # which differs from quote's behavior. Just to be safe, make sure it is
  63. # unicode before we return.
  64. if isinstance(s, bytes):
  65. s = s.decode('utf-8')
  66. return s
  67. def urlencode(params):
  68. utf8_params = encode_params_utf8(params)
  69. urlencoded = _urlencode(utf8_params)
  70. if isinstance(urlencoded, unicode_type): # PY3 returns unicode
  71. return urlencoded
  72. else:
  73. return urlencoded.decode("utf-8")
  74. def encode_params_utf8(params):
  75. """Ensures that all parameters in a list of 2-element tuples are encoded to
  76. bytestrings using UTF-8
  77. """
  78. encoded = []
  79. for k, v in params:
  80. encoded.append((
  81. k.encode('utf-8') if isinstance(k, unicode_type) else k,
  82. v.encode('utf-8') if isinstance(v, unicode_type) else v))
  83. return encoded
  84. def decode_params_utf8(params):
  85. """Ensures that all parameters in a list of 2-element tuples are decoded to
  86. unicode using UTF-8.
  87. """
  88. decoded = []
  89. for k, v in params:
  90. decoded.append((
  91. k.decode('utf-8') if isinstance(k, bytes) else k,
  92. v.decode('utf-8') if isinstance(v, bytes) else v))
  93. return decoded
  94. urlencoded = set(always_safe) | set('=&;:%+~,*@!()/?\'$')
  95. def urldecode(query):
  96. """Decode a query string in x-www-form-urlencoded format into a sequence
  97. of two-element tuples.
  98. Unlike urlparse.parse_qsl(..., strict_parsing=True) urldecode will enforce
  99. correct formatting of the query string by validation. If validation fails
  100. a ValueError will be raised. urllib.parse_qsl will only raise errors if
  101. any of name-value pairs omits the equals sign.
  102. """
  103. # Check if query contains invalid characters
  104. if query and not set(query) <= urlencoded:
  105. error = ("Error trying to decode a non urlencoded string. "
  106. "Found invalid characters: %s "
  107. "in the string: '%s'. "
  108. "Please ensure the request/response body is "
  109. "x-www-form-urlencoded.")
  110. raise ValueError(error % (set(query) - urlencoded, query))
  111. # Check for correctly hex encoded values using a regular expression
  112. # All encoded values begin with % followed by two hex characters
  113. # correct = %00, %A0, %0A, %FF
  114. # invalid = %G0, %5H, %PO
  115. if INVALID_HEX_PATTERN.search(query):
  116. raise ValueError('Invalid hex encoding in query string.')
  117. # We encode to utf-8 prior to parsing because parse_qsl behaves
  118. # differently on unicode input in python 2 and 3.
  119. # Python 2.7
  120. # >>> urlparse.parse_qsl(u'%E5%95%A6%E5%95%A6')
  121. # u'\xe5\x95\xa6\xe5\x95\xa6'
  122. # Python 2.7, non unicode input gives the same
  123. # >>> urlparse.parse_qsl('%E5%95%A6%E5%95%A6')
  124. # '\xe5\x95\xa6\xe5\x95\xa6'
  125. # but now we can decode it to unicode
  126. # >>> urlparse.parse_qsl('%E5%95%A6%E5%95%A6').decode('utf-8')
  127. # u'\u5566\u5566'
  128. # Python 3.3 however
  129. # >>> urllib.parse.parse_qsl(u'%E5%95%A6%E5%95%A6')
  130. # u'\u5566\u5566'
  131. query = query.encode(
  132. 'utf-8') if not PY3 and isinstance(query, unicode_type) else query
  133. # We want to allow queries such as "c2" whereas urlparse.parse_qsl
  134. # with the strict_parsing flag will not.
  135. params = urlparse.parse_qsl(query, keep_blank_values=True)
  136. # unicode all the things
  137. return decode_params_utf8(params)
  138. def extract_params(raw):
  139. """Extract parameters and return them as a list of 2-tuples.
  140. Will successfully extract parameters from urlencoded query strings,
  141. dicts, or lists of 2-tuples. Empty strings/dicts/lists will return an
  142. empty list of parameters. Any other input will result in a return
  143. value of None.
  144. """
  145. if isinstance(raw, (bytes, unicode_type)):
  146. try:
  147. params = urldecode(raw)
  148. except ValueError:
  149. params = None
  150. elif hasattr(raw, '__iter__'):
  151. try:
  152. dict(raw)
  153. except ValueError:
  154. params = None
  155. except TypeError:
  156. params = None
  157. else:
  158. params = list(raw.items() if isinstance(raw, dict) else raw)
  159. params = decode_params_utf8(params)
  160. else:
  161. params = None
  162. return params
  163. def generate_nonce():
  164. """Generate pseudorandom nonce that is unlikely to repeat.
  165. Per `section 3.3`_ of the OAuth 1 RFC 5849 spec.
  166. Per `section 3.2.1`_ of the MAC Access Authentication spec.
  167. A random 64-bit number is appended to the epoch timestamp for both
  168. randomness and to decrease the likelihood of collisions.
  169. .. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1
  170. .. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
  171. """
  172. return unicode_type(unicode_type(randbits(64)) + generate_timestamp())
  173. def generate_timestamp():
  174. """Get seconds since epoch (UTC).
  175. Per `section 3.3`_ of the OAuth 1 RFC 5849 spec.
  176. Per `section 3.2.1`_ of the MAC Access Authentication spec.
  177. .. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1
  178. .. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
  179. """
  180. return unicode_type(int(time.time()))
  181. def generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET):
  182. """Generates a non-guessable OAuth token
  183. OAuth (1 and 2) does not specify the format of tokens except that they
  184. should be strings of random characters. Tokens should not be guessable
  185. and entropy when generating the random characters is important. Which is
  186. why SystemRandom is used instead of the default random.choice method.
  187. """
  188. rand = SystemRandom()
  189. return ''.join(rand.choice(chars) for x in range(length))
  190. def generate_signed_token(private_pem, request):
  191. import jwt
  192. now = datetime.datetime.utcnow()
  193. claims = {
  194. 'scope': request.scope,
  195. 'exp': now + datetime.timedelta(seconds=request.expires_in)
  196. }
  197. claims.update(request.claims)
  198. token = jwt.encode(claims, private_pem, 'RS256')
  199. token = to_unicode(token, "UTF-8")
  200. return token
  201. def verify_signed_token(public_pem, token):
  202. import jwt
  203. return jwt.decode(token, public_pem, algorithms=['RS256'])
  204. def generate_client_id(length=30, chars=CLIENT_ID_CHARACTER_SET):
  205. """Generates an OAuth client_id
  206. OAuth 2 specify the format of client_id in
  207. https://tools.ietf.org/html/rfc6749#appendix-A.
  208. """
  209. return generate_token(length, chars)
  210. def add_params_to_qs(query, params):
  211. """Extend a query with a list of two-tuples."""
  212. if isinstance(params, dict):
  213. params = params.items()
  214. queryparams = urlparse.parse_qsl(query, keep_blank_values=True)
  215. queryparams.extend(params)
  216. return urlencode(queryparams)
  217. def add_params_to_uri(uri, params, fragment=False):
  218. """Add a list of two-tuples to the uri query components."""
  219. sch, net, path, par, query, fra = urlparse.urlparse(uri)
  220. if fragment:
  221. fra = add_params_to_qs(fra, params)
  222. else:
  223. query = add_params_to_qs(query, params)
  224. return urlparse.urlunparse((sch, net, path, par, query, fra))
  225. def safe_string_equals(a, b):
  226. """ Near-constant time string comparison.
  227. Used in order to avoid timing attacks on sensitive information such
  228. as secret keys during request verification (`rootLabs`_).
  229. .. _`rootLabs`: http://rdist.root.org/2010/01/07/timing-independent-array-comparison/
  230. """
  231. if len(a) != len(b):
  232. return False
  233. result = 0
  234. for x, y in zip(a, b):
  235. result |= ord(x) ^ ord(y)
  236. return result == 0
  237. def to_unicode(data, encoding='UTF-8'):
  238. """Convert a number of different types of objects to unicode."""
  239. if isinstance(data, unicode_type):
  240. return data
  241. if isinstance(data, bytes):
  242. return unicode_type(data, encoding=encoding)
  243. if hasattr(data, '__iter__'):
  244. try:
  245. dict(data)
  246. except TypeError:
  247. pass
  248. except ValueError:
  249. # Assume it's a one dimensional data structure
  250. return (to_unicode(i, encoding) for i in data)
  251. else:
  252. # We support 2.6 which lacks dict comprehensions
  253. if hasattr(data, 'items'):
  254. data = data.items()
  255. return dict(((to_unicode(k, encoding), to_unicode(v, encoding)) for k, v in data))
  256. return data
  257. class CaseInsensitiveDict(dict):
  258. """Basic case insensitive dict with strings only keys."""
  259. proxy = {}
  260. def __init__(self, data):
  261. self.proxy = dict((k.lower(), k) for k in data)
  262. for k in data:
  263. self[k] = data[k]
  264. def __contains__(self, k):
  265. return k.lower() in self.proxy
  266. def __delitem__(self, k):
  267. key = self.proxy[k.lower()]
  268. super(CaseInsensitiveDict, self).__delitem__(key)
  269. del self.proxy[k.lower()]
  270. def __getitem__(self, k):
  271. key = self.proxy[k.lower()]
  272. return super(CaseInsensitiveDict, self).__getitem__(key)
  273. def get(self, k, default=None):
  274. return self[k] if k in self else default
  275. def __setitem__(self, k, v):
  276. super(CaseInsensitiveDict, self).__setitem__(k, v)
  277. self.proxy[k.lower()] = k
  278. def update(self, *args, **kwargs):
  279. super(CaseInsensitiveDict, self).update(*args, **kwargs)
  280. for k in dict(*args, **kwargs):
  281. self.proxy[k.lower()] = k
  282. class Request(object):
  283. """A malleable representation of a signable HTTP request.
  284. Body argument may contain any data, but parameters will only be decoded if
  285. they are one of:
  286. * urlencoded query string
  287. * dict
  288. * list of 2-tuples
  289. Anything else will be treated as raw body data to be passed through
  290. unmolested.
  291. """
  292. def __init__(self, uri, http_method='GET', body=None, headers=None,
  293. encoding='utf-8'):
  294. # Convert to unicode using encoding if given, else assume unicode
  295. encode = lambda x: to_unicode(x, encoding) if encoding else x
  296. self.uri = encode(uri)
  297. self.http_method = encode(http_method)
  298. self.headers = CaseInsensitiveDict(encode(headers or {}))
  299. self.body = encode(body)
  300. self.decoded_body = extract_params(self.body)
  301. self.oauth_params = []
  302. self.validator_log = {}
  303. self._params = {
  304. "access_token": None,
  305. "client": None,
  306. "client_id": None,
  307. "client_secret": None,
  308. "code": None,
  309. "code_challenge": None,
  310. "code_challenge_method": None,
  311. "code_verifier": None,
  312. "extra_credentials": None,
  313. "grant_type": None,
  314. "redirect_uri": None,
  315. "refresh_token": None,
  316. "request_token": None,
  317. "response_type": None,
  318. "scope": None,
  319. "scopes": None,
  320. "state": None,
  321. "token": None,
  322. "user": None,
  323. "token_type_hint": None,
  324. # OpenID Connect
  325. "response_mode": None,
  326. "nonce": None,
  327. "display": None,
  328. "prompt": None,
  329. "claims": None,
  330. "max_age": None,
  331. "ui_locales": None,
  332. "id_token_hint": None,
  333. "login_hint": None,
  334. "acr_values": None
  335. }
  336. self._params.update(dict(urldecode(self.uri_query)))
  337. self._params.update(dict(self.decoded_body or []))
  338. def __getattr__(self, name):
  339. if name in self._params:
  340. return self._params[name]
  341. else:
  342. raise AttributeError(name)
  343. def __repr__(self):
  344. if not get_debug():
  345. return "<oauthlib.Request SANITIZED>"
  346. body = self.body
  347. headers = self.headers.copy()
  348. if body:
  349. body = SANITIZE_PATTERN.sub('\1<SANITIZED>', str(body))
  350. if 'Authorization' in headers:
  351. headers['Authorization'] = '<SANITIZED>'
  352. return '<oauthlib.Request url="%s", http_method="%s", headers="%s", body="%s">' % (
  353. self.uri, self.http_method, headers, body)
  354. @property
  355. def uri_query(self):
  356. return urlparse.urlparse(self.uri).query
  357. @property
  358. def uri_query_params(self):
  359. if not self.uri_query:
  360. return []
  361. return urlparse.parse_qsl(self.uri_query, keep_blank_values=True,
  362. strict_parsing=True)
  363. @property
  364. def duplicate_params(self):
  365. seen_keys = collections.defaultdict(int)
  366. all_keys = (p[0]
  367. for p in (self.decoded_body or []) + self.uri_query_params)
  368. for k in all_keys:
  369. seen_keys[k] += 1
  370. return [k for k, c in seen_keys.items() if c > 1]