Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/oauthlib/oauth1/rfc5849/utils.py: 37%
35 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:22 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:22 +0000
1"""
2oauthlib.utils
3~~~~~~~~~~~~~~
5This module contains utility methods used by various parts of the OAuth
6spec.
7"""
8import urllib.request as urllib2
10from oauthlib.common import quote, unquote
12UNICODE_ASCII_CHARACTER_SET = ('abcdefghijklmnopqrstuvwxyz'
13 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
14 '0123456789')
17def filter_params(target):
18 """Decorator which filters params to remove non-oauth_* parameters
20 Assumes the decorated method takes a params dict or list of tuples as its
21 first argument.
22 """
23 def wrapper(params, *args, **kwargs):
24 params = filter_oauth_params(params)
25 return target(params, *args, **kwargs)
27 wrapper.__doc__ = target.__doc__
28 return wrapper
31def filter_oauth_params(params):
32 """Removes all non oauth parameters from a dict or a list of params."""
33 is_oauth = lambda kv: kv[0].startswith("oauth_")
34 if isinstance(params, dict):
35 return list(filter(is_oauth, list(params.items())))
36 else:
37 return list(filter(is_oauth, params))
40def escape(u):
41 """Escape a unicode string in an OAuth-compatible fashion.
43 Per `section 3.6`_ of the spec.
45 .. _`section 3.6`: https://tools.ietf.org/html/rfc5849#section-3.6
47 """
48 if not isinstance(u, str):
49 raise ValueError('Only unicode objects are escapable. ' +
50 'Got {!r} of type {}.'.format(u, type(u)))
51 # Letters, digits, and the characters '_.-' are already treated as safe
52 # by urllib.quote(). We need to add '~' to fully support rfc5849.
53 return quote(u, safe=b'~')
56def unescape(u):
57 if not isinstance(u, str):
58 raise ValueError('Only unicode objects are unescapable.')
59 return unquote(u)
62def parse_keqv_list(l):
63 """A unicode-safe version of urllib2.parse_keqv_list"""
64 # With Python 2.6, parse_http_list handles unicode fine
65 return urllib2.parse_keqv_list(l)
68def parse_http_list(u):
69 """A unicode-safe version of urllib2.parse_http_list"""
70 # With Python 2.6, parse_http_list handles unicode fine
71 return urllib2.parse_http_list(u)
74def parse_authorization_header(authorization_header):
75 """Parse an OAuth authorization header into a list of 2-tuples"""
76 auth_scheme = 'OAuth '.lower()
77 if authorization_header[:len(auth_scheme)].lower().startswith(auth_scheme):
78 items = parse_http_list(authorization_header[len(auth_scheme):])
79 try:
80 return list(parse_keqv_list(items).items())
81 except (IndexError, ValueError):
82 pass
83 raise ValueError('Malformed authorization header')