Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_urldecode(self):
self.assertItemsEqual(urldecode(''), [])
self.assertItemsEqual(urldecode('='), [('', '')])
self.assertItemsEqual(urldecode('%20'), [(' ', '')])
self.assertItemsEqual(urldecode('+'), [(' ', '')])
self.assertItemsEqual(urldecode('c2'), [('c2', '')])
self.assertItemsEqual(urldecode('c2='), [('c2', '')])
self.assertItemsEqual(urldecode('foo=bar'), [('foo', 'bar')])
self.assertItemsEqual(urldecode('foo_%20~=.bar-'),
[('foo_ ~', '.bar-')])
self.assertItemsEqual(urldecode('foo=1,2,3'), [('foo', '1,2,3')])
self.assertItemsEqual(urldecode('foo=(1,2,3)'), [('foo', '(1,2,3)')])
self.assertItemsEqual(urldecode('foo=bar.*'), [('foo', 'bar.*')])
self.assertItemsEqual(urldecode('foo=bar@spam'), [('foo', 'bar@spam')])
self.assertItemsEqual(urldecode('foo=bar/baz'), [('foo', 'bar/baz')])
self.assertItemsEqual(urldecode('foo=bar?baz'), [('foo', 'bar?baz')])
self.assertItemsEqual(urldecode('foo=bar\'s'), [('foo', 'bar\'s')])
self.assertItemsEqual(urldecode('foo=$'), [('foo', '$')])
self.assertRaises(ValueError, urldecode, 'foo bar')
self.assertRaises(ValueError, urldecode, '%R')
self.assertRaises(ValueError, urldecode, '%RA')
self.assertItemsEqual(urldecode('='), [('', '')])
self.assertItemsEqual(urldecode('%20'), [(' ', '')])
self.assertItemsEqual(urldecode('+'), [(' ', '')])
self.assertItemsEqual(urldecode('c2'), [('c2', '')])
self.assertItemsEqual(urldecode('c2='), [('c2', '')])
self.assertItemsEqual(urldecode('foo=bar'), [('foo', 'bar')])
self.assertItemsEqual(urldecode('foo_%20~=.bar-'),
[('foo_ ~', '.bar-')])
self.assertItemsEqual(urldecode('foo=1,2,3'), [('foo', '1,2,3')])
self.assertItemsEqual(urldecode('foo=(1,2,3)'), [('foo', '(1,2,3)')])
self.assertItemsEqual(urldecode('foo=bar.*'), [('foo', 'bar.*')])
self.assertItemsEqual(urldecode('foo=bar@spam'), [('foo', 'bar@spam')])
self.assertItemsEqual(urldecode('foo=bar/baz'), [('foo', 'bar/baz')])
self.assertItemsEqual(urldecode('foo=bar?baz'), [('foo', 'bar?baz')])
self.assertItemsEqual(urldecode('foo=bar\'s'), [('foo', 'bar\'s')])
self.assertItemsEqual(urldecode('foo=$'), [('foo', '$')])
self.assertRaises(ValueError, urldecode, 'foo bar')
self.assertRaises(ValueError, urldecode, '%R')
self.assertRaises(ValueError, urldecode, '%RA')
self.assertRaises(ValueError, urldecode, '%AR')
self.assertRaises(ValueError, urldecode, '%RR')
def test_urldecode(self):
self.assertItemsEqual(urldecode(''), [])
self.assertItemsEqual(urldecode('='), [('', '')])
self.assertItemsEqual(urldecode('%20'), [(' ', '')])
self.assertItemsEqual(urldecode('+'), [(' ', '')])
self.assertItemsEqual(urldecode('c2'), [('c2', '')])
self.assertItemsEqual(urldecode('c2='), [('c2', '')])
self.assertItemsEqual(urldecode('foo=bar'), [('foo', 'bar')])
self.assertItemsEqual(urldecode('foo_%20~=.bar-'),
[('foo_ ~', '.bar-')])
self.assertItemsEqual(urldecode('foo=1,2,3'), [('foo', '1,2,3')])
self.assertItemsEqual(urldecode('foo=(1,2,3)'), [('foo', '(1,2,3)')])
self.assertItemsEqual(urldecode('foo=bar.*'), [('foo', 'bar.*')])
self.assertItemsEqual(urldecode('foo=bar@spam'), [('foo', 'bar@spam')])
self.assertItemsEqual(urldecode('foo=bar/baz'), [('foo', 'bar/baz')])
self.assertItemsEqual(urldecode('foo=bar?baz'), [('foo', 'bar?baz')])
self.assertItemsEqual(urldecode('foo=bar\'s'), [('foo', 'bar\'s')])
self.assertItemsEqual(urldecode('foo=$'), [('foo', '$')])
self.assertRaises(ValueError, urldecode, 'foo bar')
self.assertRaises(ValueError, urldecode, '%R')
self.assertRaises(ValueError, urldecode, '%RA')
self.assertRaises(ValueError, urldecode, '%AR')
self.assertRaises(ValueError, urldecode, '%RR')
def test_urldecode(self):
self.assertItemsEqual(urldecode(''), [])
self.assertItemsEqual(urldecode('='), [('', '')])
self.assertItemsEqual(urldecode('%20'), [(' ', '')])
self.assertItemsEqual(urldecode('+'), [(' ', '')])
self.assertItemsEqual(urldecode('c2'), [('c2', '')])
self.assertItemsEqual(urldecode('c2='), [('c2', '')])
self.assertItemsEqual(urldecode('foo=bar'), [('foo', 'bar')])
self.assertItemsEqual(urldecode('foo_%20~=.bar-'),
[('foo_ ~', '.bar-')])
self.assertItemsEqual(urldecode('foo=1,2,3'), [('foo', '1,2,3')])
self.assertItemsEqual(urldecode('foo=(1,2,3)'), [('foo', '(1,2,3)')])
self.assertItemsEqual(urldecode('foo=bar.*'), [('foo', 'bar.*')])
self.assertItemsEqual(urldecode('foo=bar@spam'), [('foo', 'bar@spam')])
self.assertItemsEqual(urldecode('foo=bar/baz'), [('foo', 'bar/baz')])
self.assertItemsEqual(urldecode('foo=bar?baz'), [('foo', 'bar?baz')])
self.assertItemsEqual(urldecode('foo=bar\'s'), [('foo', 'bar\'s')])
self.assertItemsEqual(urldecode('foo=$'), [('foo', '$')])
self.assertRaises(ValueError, urldecode, 'foo bar')
self.assertRaises(ValueError, urldecode, '%R')
self.assertRaises(ValueError, urldecode, '%RA')
self.assertRaises(ValueError, urldecode, '%AR')
self.assertRaises(ValueError, urldecode, '%RR')
raise ValueError('No token endpoint set for auto_refresh.')
if not is_secure_transport(token_url):
raise InsecureTransportError()
# Need to nullify token to prevent it from being added to the request
refresh_token = refresh_token or self.token.get('refresh_token')
self.token = {}
log.debug('Adding auto refresh key word arguments %s.',
self.auto_refresh_kwargs)
kwargs.update(self.auto_refresh_kwargs)
body = self._client.prepare_refresh_body(body=body,
refresh_token=refresh_token, scope=self.scope, **kwargs)
log.debug('Prepared refresh token request body %s', body)
r = self.post(token_url, data=dict(urldecode(body)), auth=auth,
timeout=timeout, verify=verify)
log.debug('Request to refresh token completed with status %s.',
r.status_code)
log.debug('Response headers were %s and content %s.',
r.headers, r.text)
log.debug('Invoking %d token response hooks.',
len(self.compliance_hook['refresh_token_response']))
for hook in self.compliance_hook['refresh_token_response']:
log.debug('Invoking hook %s.', hook)
r = hook(r)
self.token = self._client.parse_request_body_response(r.text, scope=self.scope)
if not 'refresh_token' in self.token:
log.debug('No new refresh token given. Re-using old.')
self.token['refresh_token'] = refresh_token
return self.token
log.debug('Adding auto refresh key word arguments %s.',
self.auto_refresh_kwargs)
kwargs.update(self.auto_refresh_kwargs)
body = self._client.prepare_refresh_body(body=body,
refresh_token=refresh_token, scope=self.scope, **kwargs)
log.debug('Prepared refresh token request body %s', body)
if headers is None:
headers = {
'Accept': 'application/json',
'Content-Type': (
'application/x-www-form-urlencoded;charset=UTF-8'
),
}
r = self.post(token_url, data=dict(urldecode(body)), auth=auth,
timeout=timeout, headers=headers, verify=verify, withhold_token=True, proxies=proxies)
log.debug('Request to refresh token completed with status %s.',
r.status_code)
log.debug('Response headers were %s and content %s.',
r.headers, r.text)
log.debug('Invoking %d token response hooks.',
len(self.compliance_hook['refresh_token_response']))
for hook in self.compliance_hook['refresh_token_response']:
log.debug('Invoking hook %s.', hook)
r = hook(r)
self.token = self._client.parse_request_body_response(r.text, scope=self.scope)
if not 'refresh_token' in self.token:
log.debug('No new refresh token given. Re-using old.')
self.token['refresh_token'] = refresh_token
return self.token
def urldecode(body):
"""Parse query or json to python dictionary"""
try:
return _urldecode(body)
except:
import json
return json.loads(body)
# this was pulled out of the params
# it needs to be passed into prepare_request_body
if client_secret is not None:
kwargs['client_secret'] = client_secret
body = self._client.prepare_request_body(code=code, body=body,
redirect_uri=self.redirect_uri,
include_client_id=include_client_id, **kwargs)
headers = headers or {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
}
self.token = {}
if method.upper() == 'POST':
r = self.post(token_url, data=dict(urldecode(body)),
timeout=timeout, headers=headers, auth=auth,
verify=verify, proxies=proxies)
log.debug('Prepared fetch token request body %s', body)
elif method.upper() == 'GET':
# if method is not 'POST', switch body to querystring and GET
r = self.get(token_url, params=dict(urldecode(body)),
timeout=timeout, headers=headers, auth=auth,
verify=verify, proxies=proxies)
log.debug('Prepared fetch token request querystring %s', body)
else:
raise ValueError('The method kwarg must be POST or GET.')
log.debug('Request to fetch token completed with status %s.',
r.status_code)
log.debug('Request headers were %s', r.request.headers)
log.debug('Request body was %s', r.request.body)
params = []
# The parameters from the following sources are collected into a single
# list of name/value pairs:
# * The query component of the HTTP request URI as defined by
# `RFC3986, Section 3.4`_. The query component is parsed into a list
# of name/value pairs by treating it as an
# "application/x-www-form-urlencoded" string, separating the names
# and values and decoding them as defined by
# `W3C.REC-html40-19980424`_, Section 17.13.4.
#
# .. _`RFC3986, Section 3.4`: https://tools.ietf.org/html/rfc3986#section-3.4
# .. _`W3C.REC-html40-19980424`: https://tools.ietf.org/html/rfc5849#ref-W3C.REC-html40-19980424
if uri_query:
params.extend(urldecode(uri_query))
# * The OAuth HTTP "Authorization" header field (`Section 3.5.1`_) if
# present. The header's content is parsed into a list of name/value
# pairs excluding the "realm" parameter if present. The parameter
# values are decoded as defined by `Section 3.5.1`_.
#
# .. _`Section 3.5.1`: https://tools.ietf.org/html/rfc5849#section-3.5.1
if headers:
headers_lower = dict((k.lower(), v) for k, v in headers.items())
authorization_header = headers_lower.get('authorization')
if authorization_header is not None:
params.extend([i for i in utils.parse_authorization_header(
authorization_header) if with_realm or i[0] != 'realm'])
# * The HTTP request entity-body, but only if all of the following
# conditions are met: