aboutsummaryrefslogtreecommitdiff
path: root/requests/auth.py
diff options
context:
space:
mode:
Diffstat (limited to 'requests/auth.py')
-rw-r--r--requests/auth.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/requests/auth.py b/requests/auth.py
index 353180a..a20c545 100644
--- a/requests/auth.py
+++ b/requests/auth.py
@@ -11,10 +11,20 @@ import time
import hashlib
from base64 import b64encode
+
from .compat import urlparse, str
from .utils import randombytes, parse_dict_header
+try:
+ from oauthlib.oauth1.rfc5849 import (Client, SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER)
+ from oauthlib.common import extract_params
+ # hush pyflakes:
+ SIGNATURE_HMAC; SIGNATURE_TYPE_AUTH_HEADER
+except (ImportError, SyntaxError):
+ SIGNATURE_HMAC = None
+ SIGNATURE_TYPE_AUTH_HEADER = None
+CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'
def _basic_auth_str(username, password):
"""Returns a Basic Auth string."""
@@ -29,6 +39,37 @@ class AuthBase(object):
raise NotImplementedError('Auth hooks must be callable.')
+class OAuth1(AuthBase):
+ """Signs the request using OAuth 1 (RFC5849)"""
+ def __init__(self, client_key,
+ client_secret=None,
+ resource_owner_key=None,
+ resource_owner_secret=None,
+ callback_uri=None,
+ signature_method=SIGNATURE_HMAC,
+ signature_type=SIGNATURE_TYPE_AUTH_HEADER,
+ rsa_key=None, verifier=None):
+
+ try:
+ signature_type = signature_type.upper()
+ except AttributeError:
+ pass
+
+ self.client = Client(client_key, client_secret, resource_owner_key,
+ resource_owner_secret, callback_uri, signature_method,
+ signature_type, rsa_key, verifier)
+
+ def __call__(self, r):
+ contenttype = r.headers.get('Content-Type', None)
+ decoded_body = extract_params(r.data)
+ if contenttype == None and decoded_body != None:
+ r.headers['Content-Type'] = 'application/x-www-form-urlencoded'
+
+ r.url, r.headers, r.data = self.client.sign(
+ unicode(r.url), unicode(r.method), r.data, r.headers)
+ return r
+
+
class HTTPBasicAuth(AuthBase):
"""Attaches HTTP Basic Authentication to the given Request object."""
def __init__(self, username, password):