Skip to content
Myr

Initial implementation of OAuth1 request signing with OAuthLib

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Idan Gazit · committed by Donald Stufft · parent 632d989 · Browse files · 2 files+45 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+45 added−2 removed

  • other:+45 added−2 removed

2 files+45 added−2 removed

modified:requests/auth.py+44 added-1 removed
9 unmodified lines
10import time11import hashlib1213from base64 import b64encode14from oauthlib.oauth1.rfc5849 import (Client, SIGNATURE_HMAC, SIGNATURE_RSA,15 SIGNATURE_PLAINTEXT, SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_QUERY,16 SIGNATURE_TYPE_BODY)17from .compat import urlparse, str18from .utils import randombytes, parse_dict_header19201821def _basic_auth_str(username, password):22 """Returns a Basic Auth string."""2324 return 'Basic ' + b64encode(('%s:%s' % (username, password)).encode('latin1')).strip().decode('latin1')
5 unmodified lines
30 def __call__(self, r):31 raise NotImplementedError('Auth hooks must be callable.')323334class OAuth1(AuthBase):35 """Signs the request using OAuth 1 (RFC5849)"""36 def __init__(self, client_key,37 client_secret=None,38 resource_owner_key=None,39 resource_owner_secret=None,40 callback_uri=None,41 signature_method=SIGNATURE_HMAC,42 signature_type=SIGNATURE_TYPE_AUTH_HEADER,43 rsa_key=None, verifier=None):44 self.client = Client(client_key, client_secret, resource_owner_key,45 resource_owner_secret, callback_uri, signature_method,46 signature_type, rsa_key, verifier)4748 def __call__(self, r):49 if (r.files or50 r.headers['Content-Type'] != 'application/x-www-form-urlencoded'):5152 # XXX TODO can we use body signatures with a non formencoded body?53 if self.client.signature_type == SIGNATURE_TYPE_BODY:54 raise ValueError('Body signatures may not be used with non-form-urlencoded content')5556 # Spec only specifies signing of application/x-www-form-urlencoded57 # params. Files don't get signed either.58 body = u''59 alter_body = False # we shouldn't touch the body60 else:61 body = r.data # OAuthLib is cool with both strings and dicts.62 if isinstance(str, body):63 # XXX gross hack. We must pass unicode...64 body = unicode(body, 'utf-8')65 alter_body = True6667 full_url, new_body, headers = self.client.sign(r.url, unicode(r.method), body, r.headers)68 r.url = full_url69 if alter_body:70 r.data = new_body71 r.headers = headers72 return r737475class HTTPBasicAuth(AuthBase):76 """Attaches HTTP Basic Authentication to the given Request object."""77 def __init__(self, username, password):78 self.username = username
115 unmodified lines
modified:setup.py+1 added-1 removed
19 unmodified lines
20if sys.argv[-1] == 'test':21 os.system('python tests/test_requests.py')22 sys.exit()2324required = ['certifi>=0.0.7',]24required = ['certifi>=0.0.7','oauthlib']25packages = [26 'requests',27 'requests.packages',28 'requests.packages.urllib3',
39 unmodified lines