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
Provenance
- Commit
798dc4a- Tree
8c836b1- Author
Idan Gazit <idan@gazit.me>- Committer
Donald Stufft <donald.stufft@gmail.com>- Parent
632d989
2 files changed+45 added−2 removed
modified:requests/auth.py+44 added-1 removed
9 unmodified lines10 import time11 import hashlib12 13 from base64 import b64encode14+from oauthlib.oauth1.rfc5849 import (Client, SIGNATURE_HMAC, SIGNATURE_RSA,15+ SIGNATURE_PLAINTEXT, SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_QUERY,16+ SIGNATURE_TYPE_BODY)17 from .compat import urlparse, str18 from .utils import randombytes, parse_dict_header19 20 18-21 def _basic_auth_str(username, password):22 """Returns a Basic Auth string."""23 24 return 'Basic ' + b64encode(('%s:%s' % (username, password)).encode('latin1')).strip().decode('latin1')5 unmodified lines30 def __call__(self, r):31 raise NotImplementedError('Auth hooks must be callable.')32 33 34+class 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)47+48+ def __call__(self, r):49+ if (r.files or50+ r.headers['Content-Type'] != 'application/x-www-form-urlencoded'):51+52+ # 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')55+56+ # 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 = True66+67+ 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 r73+74+75 class HTTPBasicAuth(AuthBase):76 """Attaches HTTP Basic Authentication to the given Request object."""77 def __init__(self, username, password):78 self.username = username115 unmodified linesmodified:setup.py+1 added-1 removed
19 unmodified lines20 if sys.argv[-1] == 'test':21 os.system('python tests/test_requests.py')22 sys.exit()23 24-required = ['certifi>=0.0.7',]24+required = ['certifi>=0.0.7','oauthlib']25 packages = [26 'requests',27 'requests.packages',28 'requests.packages.urllib3',39 unmodified lines