Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def fake_send(r, **kwargs):
signature = to_unicode(getter(r))
self.assertIn('oauth_signature', signature)
resp = mock.MagicMock(spec=requests.Response)
resp.cookies = []
return resp
return fake_send
def fake_send(r, **kwargs):
auth_header = to_unicode(r.headers['Authorization'])
self.assertEqual(auth_header, signature)
resp = mock.MagicMock(spec=requests.Response)
resp.cookies = []
return resp
return fake_send
auth_header = (
'OAuth oauth_consumer_key="client",'
'oauth_signature_method="PLAINTEXT",'
'oauth_token="valid-token",'
'oauth_signature="secret&valid-token-secret"'
)
request = self.factory.get(url, HTTP_AUTHORIZATION=auth_header)
resp = handle(request)
data = json.loads(to_unicode(resp.content))
self.assertIn('username', data)
# case 2: invalid signature
auth_header = auth_header.replace('valid-token-secret', 'invalid')
request = self.factory.get(url, HTTP_AUTHORIZATION=auth_header)
resp = handle(request)
data = json.loads(to_unicode(resp.content))
self.assertEqual(data['error'], 'invalid_signature')
def scope_to_list(scope):
"""Convert a space separated string to a list of scopes."""
if isinstance(scope, (tuple, list, set)):
return [to_unicode(s) for s in scope]
elif scope is None:
return None
return scope.strip().split()
def url_encode(params):
encoded = []
for k, v in params:
encoded.append((to_bytes(k), to_bytes(v)))
return to_unicode(_urlencode(encoded))
"""
params = [
('response_type', response_type),
('client_id', client_id)
]
if redirect_uri:
params.append(('redirect_uri', redirect_uri))
if scope:
params.append(('scope', list_to_scope(scope)))
if state:
params.append(('state', state))
for k in kwargs:
if kwargs[k]:
params.append((to_unicode(k), kwargs[k]))
return add_params_to_uri(uri, params)
grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
.. _`Section 4.1.1`: http://tools.ietf.org/html/rfc6749#section-4.1.1
"""
params = [('grant_type', grant_type)]
if redirect_uri:
params.append(('redirect_uri', redirect_uri))
if 'scope' in kwargs:
kwargs['scope'] = list_to_scope(kwargs['scope'])
for k in kwargs:
if kwargs[k]:
params.append((to_unicode(k), kwargs[k]))
return add_params_to_qs(body, params)
def _sign(jws_header):
self._validate_header(jws_header)
_alg, _key = prepare_algorithm_key(
self._algorithms, jws_header, payload, key, private=True)
protected_segment = json_b64encode(jws_header.protected)
signing_input = b'.'.join([protected_segment, payload_segment])
signature = urlsafe_b64encode(_alg.sign(signing_input, _key))
rv = {
'protected': to_unicode(protected_segment),
'signature': to_unicode(signature)
}
if jws_header.header is not None:
rv['header'] = jws_header.header
return rv
def _unicode_value(value):
return to_unicode(value, 'latin-1')