common.py 13 KB

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