Added support for HTTP Basic Auth credentials in 2-tuple
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Idan Gazit · committed by Idan Gazit · parent 7fdb09b · Browse files · 3 files·+40 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+40 added−6 removed
- other:+40 added−6 removed
Provenance
- Commit
1933d3c- Tree
a4b53c7- Author
Idan Gazit <idan@gazit.me>- Committer
Idan Gazit <idan@gazit.me>- Parent
7fdb09b
3 files changed+40 added−6 removed
modified:docs/user/quickstart.rst+15 added-4 removed
237 unmodified lines238 >>> from requests.auth import HTTPBasicAuth239 >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))240 <Response [200]>241 242-OAuth Authentication243---------------------242+Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for243+this authentication method::244 245-Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for246-establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.245+ >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))246+ <Response [200]>247 248+Providing the credentials as a tuple in this fashion is functionally equivalent249+to the ``HTTPBasicAuth`` example above.250+251+252 Digest Authentication253 ---------------------254 255 Another popular form of web service protection is Digest Authentication::3 unmodified lines259 >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))260 <Response [200]>261 262 263+OAuth Authentication264+--------------------265+266+Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for267+establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.268+269+270 Redirection and History271 -----------------------272 273 Requests will automatically perform location redirection while using impodotent methods.66 unmodified linesmodified:requests/models.py+6 added-2 removed
15 unmodified lines16 17 from .hooks import dispatch_hook18 from .structures import CaseInsensitiveDict19 from .status_codes import codes20+from .auth import HTTPBasicAuth21 from .packages.urllib3.exceptions import MaxRetryError22 from .packages.urllib3.exceptions import SSLError as _SSLError23 from .packages.urllib3.exceptions import HTTPError as _HTTPError24 from .packages.urllib3 import connectionpool, poolmanager72 unmodified lines97 #: :class:`Response <Response>` instance, containing98 #: content and metadata of HTTP Response, once :attr:`sent <send>`.99 self.response = Response()100 100- #: Authentication tuple to attach to :class:`Request <Request>`.101+ #: Authentication tuple or object to attach to :class:`Request <Request>`.102 self.auth = auth103 104 #: CookieJar to attach to :class:`Request <Request>`.105 self.cookies = dict(cookies or [])282 unmodified lines388 # Add content-type if it wasn't explicitly provided.389 if (content_type) and (not 'content-type' in self.headers):390 self.headers['Content-Type'] = content_type391 391-392 if self.auth:393+ if isinstance(self.auth, tuple) and len(self.auth) == 2:394+ # special-case basic HTTP auth395+ self.auth = HTTPBasicAuth(*self.auth)396+397 # Allow auth to make its changes.398 r = self.auth(self)399 400 # Update self to reflect the auth changes.218 unmodified linesmodified:test_requests.py+19 added
139 unmodified lines140 r = requests.patch(httpbin('patch'))141 self.assertEqual(r.status_code, 200)142 143 144+ def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):145+146+ for service in SERVICES:147+148+ auth = ('user', 'pass')149+ url = service('basic-auth', 'user', 'pass')150+151+ r = requests.get(url, auth=auth)152+ self.assertEqual(r.status_code, 200)153+154+ r = requests.get(url)155+ self.assertEqual(r.status_code, 401)156+157+158+ s = requests.session(auth=auth)159+ r = s.get(url)160+ self.assertEqual(r.status_code, 200)161+162+163 def test_BASICAUTH_HTTP_200_OK_GET(self):164 165 for service in SERVICES:166 412 unmodified lines