Split authentication out into a separate file.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Cory Benfield · committed by Cory Benfield · parent 54b77af · Browse files · 3 files·+85 added−56 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+85 added−56 removed
- other:+85 added−56 removed
Provenance
- Commit
a7c5d5e- Tree
f2b1d46- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent
54b77af
3 files changed+85 added−56 removed
added:docs/user/authentication.rst+84 added
1+.. _authentication:2+3+Authentication4+==============5+6+This document discusses using various kinds of authentication with Requests.7+8+Many web services require authentication, and there are many different types.9+Below, we outline various forms of authentication available in Requests, from10+the simple to the complex.11+12+13+Basic Authentication14+--------------------15+16+Many web services that require authentication accept HTTP Basic Auth. This is17+the simplest kind, and Requests supports it straight out of the box.18+19+Making requests with HTTP Basic Auth is very simple::20+21+ >>> from requests.auth import HTTPBasicAuth22+ >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))23+ <Response [200]>24+25+In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand26+for using it::27+28+ >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))29+ <Response [200]>30+31+Providing the credentials in a tuple like this is exactly the same as the32+``HTTPBasicAuth`` example above.33+34+35+Digest Authentication36+---------------------37+38+Another very popular form of HTTP Authentication is Digest Authentication,39+and Requests supports this out of the box as well::40+41+ >>> from requests.auth import HTTPDigestAuth42+ >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'43+ >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))44+ <Response [200]>45+46+47+Other Authentication48+--------------------49+50+Requests is designed to allow other forms of authentication to be easily and51+quickly plugged in. Members of the open-source community frequently write52+authentication handlers for more complicated or less commonly-used forms of53+authentication. Some of the best have been brought together under the54+`Requests organization`_, including:55+56+- OAuth_57+- Kerberos_58+- NTLM_59+60+If you want to use any of these forms of authentication, go straight to their61+Github page and follow the instructions.62+63+64+New Forms of Authentication65+---------------------------66+67+If you can't find a good implementation of the form of authentication you68+want, you can implement it yourself. Requests makes it easy to add your own69+forms of authentication.70+71+To do so, subclass :class:`requests.auth.AuthBase` and implement the72+``__call__()`` method. When an authentication handler is attached to a request,73+it is called during request setup. The ``__call__`` method must therefore do74+whatever is required to make the authentication work. Some forms of75+authentication will additionally add hooks to provide further functionality.76+77+Examples can be found under the `Requests organization`_ and in the78+``auth.py`` file.79+80+.. _OAuth: https://github.com/requests/requests-oauthlib81+.. _Kerberos: https://github.com/requests/requests-kerberos82+.. _NTLM: https://github.com/requests/requests-ntlm83+.. _Requests organization: https://github.com/requests84+modified:docs/user/quickstart.rst-56 removed
335 unmodified lines336 >>> r.text337 '{"cookies": {"cookies_are": "working"}}'338 339 340-Basic Authentication341---------------------342-343-Many web services require authentication. There are many different types of344-authentication, but the most common is HTTP Basic Auth.345-346-Making requests with Basic Auth is extremely simple::347-348- >>> from requests.auth import HTTPBasicAuth349- >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))350- <Response [200]>351-352-Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for353-this authentication method::354-355- >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))356- <Response [200]>357-358-Providing the credentials as a tuple in this fashion is functionally equivalent359-to the ``HTTPBasicAuth`` example above.360-361-362-Digest Authentication363----------------------364-365-Another popular form of web service protection is Digest Authentication::366-367- >>> from requests.auth import HTTPDigestAuth368- >>> url = 'http://httpbin.org/digest-auth/auth/user/pass'369- >>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))370- <Response [200]>371-372-373-Other Authentication374---------------------375-376-Requests is designed to allow other forms of authentication to be easily and377-quickly plugged in. Members of the open-source community frequently write378-authentication handlers for more complicated or less commonly-used forms of379-authentication. Some of the best have been brought together under a single380-`organization on Github`_, including:381-382-- OAuth_383-- Kerberos_384-- NTLM_385-386-If you want to use any of these forms of authentication, go straight to their387-Github page and follow the instructions. If you can't find the one you want,388-why not write one and submit it?389-390-.. _OAuth: https://github.com/requests/requests-oauthlib391-.. _Kerberos: https://github.com/requests/requests-kerberos392-.. _NTLM: https://github.com/requests/requests-ntlm393-.. _organization on Github: https://github.com/requests394-395-340 Redirection and History341 -----------------------342 343 Requests will automatically perform location redirection while using the GET78 unmodified linesmodified:docs/index.rst+1 added
88 unmodified lines89 user/intro90 user/install91 user/quickstart92 user/advanced93+ user/authentication94 95 96 Community Guide97 -----------------36 unmodified lines