From 1933d3c70768db0e8ed509f683f9b830b6bfc44c Mon Sep 17 00:00:00 2001 From: Idan Gazit Date: Sat, 19 Nov 2011 23:59:27 +0200 Subject: [PATCH] Added support for HTTP Basic Auth credentials in 2-tuple --- docs/user/quickstart.rst | 19 +++++++++++++++---- requests/models.py | 8 ++++++-- test_requests.py | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index cf8d1a7..3a4a91f 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -239,11 +239,15 @@ Making requests with Basic Auth is extremely simple:: >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass')) -OAuth Authentication --------------------- +Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for +this authentication method:: + + >>> requests.get('https://api.github.com/user', auth=('user', 'pass')) + + +Providing the credentials as a tuple in this fashion is functionally equivalent +to the ``HTTPBasicAuth`` example above. -Miguel Araujo's `requests-oauth `_ project provides a simple interface for -establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository `_. Digest Authentication --------------------- @@ -256,6 +260,13 @@ Another popular form of web service protection is Digest Authentication:: +OAuth Authentication +-------------------- + +Miguel Araujo's `requests-oauth `_ project provides a simple interface for +establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository `_. + + Redirection and History ----------------------- diff --git a/requests/models.py b/requests/models.py index 9c4d133..0053c08 100644 --- a/requests/models.py +++ b/requests/models.py @@ -17,6 +17,7 @@ from datetime import datetime from .hooks import dispatch_hook from .structures import CaseInsensitiveDict from .status_codes import codes +from .auth import HTTPBasicAuth from .packages.urllib3.exceptions import MaxRetryError from .packages.urllib3.exceptions import SSLError as _SSLError from .packages.urllib3.exceptions import HTTPError as _HTTPError @@ -97,7 +98,7 @@ class Request(object): #: content and metadata of HTTP Response, once :attr:`sent `. self.response = Response() - #: Authentication tuple to attach to :class:`Request `. + #: Authentication tuple or object to attach to :class:`Request `. self.auth = auth #: CookieJar to attach to :class:`Request `. @@ -388,8 +389,11 @@ class Request(object): if (content_type) and (not 'content-type' in self.headers): self.headers['Content-Type'] = content_type - if self.auth: + if isinstance(self.auth, tuple) and len(self.auth) == 2: + # special-case basic HTTP auth + self.auth = HTTPBasicAuth(*self.auth) + # Allow auth to make its changes. r = self.auth(self) diff --git a/test_requests.py b/test_requests.py index 1cd73cf..9ac6b51 100755 --- a/test_requests.py +++ b/test_requests.py @@ -141,6 +141,25 @@ class RequestsTestSuite(unittest.TestCase): self.assertEqual(r.status_code, 200) + def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self): + + for service in SERVICES: + + auth = ('user', 'pass') + url = service('basic-auth', 'user', 'pass') + + r = requests.get(url, auth=auth) + self.assertEqual(r.status_code, 200) + + r = requests.get(url) + self.assertEqual(r.status_code, 401) + + + s = requests.session(auth=auth) + r = s.get(url) + self.assertEqual(r.status_code, 200) + + def test_BASICAUTH_HTTP_200_OK_GET(self): for service in SERVICES: -- 2.34.1