| 1 |
.. _authentication: |
| 2 |
|
| 3 |
Authentication |
| 4 |
============== |
| 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, from |
| 10 |
the simple to the complex. |
| 11 |
|
| 12 |
|
| 13 |
Basic Authentication |
| 14 |
-------------------- |
| 15 |
|
| 16 |
Many web services that require authentication accept HTTP Basic Auth. This is |
| 17 |
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 HTTPBasicAuth |
| 22 |
>>> basic = HTTPBasicAuth('user', 'pass') |
| 23 |
>>> requests.get('https://httpbin.org/basic-auth/user/pass', auth=basic) |
| 24 |
<Response [200]> |
| 25 |
|
| 26 |
In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand |
| 27 |
for using it:: |
| 28 |
|
| 29 |
>>> requests.get('https://httpbin.org/basic-auth/user/pass', auth=('user', 'pass')) |
| 30 |
<Response [200]> |
| 31 |
|
| 32 |
Providing the credentials in a tuple like this is exactly the same as the |
| 33 |
``HTTPBasicAuth`` example above. |
| 34 |
|
| 35 |
|
| 36 |
netrc Authentication |
| 37 |
~~~~~~~~~~~~~~~~~~~~ |
| 38 |
|
| 39 |
If no authentication method is given with the ``auth`` argument, Requests will |
| 40 |
attempt to get the authentication credentials for the URL's hostname from the |
| 41 |
user's netrc file. The netrc file overrides raw HTTP authentication headers |
| 42 |
set with `headers=`. |
| 43 |
|
| 44 |
If credentials for the hostname are found, the request is sent with HTTP Basic |
| 45 |
Auth. |
| 46 |
|
| 47 |
Requests will search for the netrc file at `~/.netrc`, `~/_netrc`, or at the path |
| 48 |
specified by the `NETRC` environment variable. `~` denotes the user's home |
| 49 |
directory, which is `$HOME` on Unix based systems and `%USERPROFILE%` on Windows. |
| 50 |
|
| 51 |
Usage of netrc file can be disabled by setting `trust_env` to `False` in the |
| 52 |
Requests session:: |
| 53 |
|
| 54 |
>>> s = requests.Session() |
| 55 |
>>> s.trust_env = False |
| 56 |
>>> s.get('https://httpbin.org/basic-auth/user/pass') |
| 57 |
|
| 58 |
Digest Authentication |
| 59 |
--------------------- |
| 60 |
|
| 61 |
Another very popular form of HTTP Authentication is Digest Authentication, |
| 62 |
and Requests supports this out of the box as well:: |
| 63 |
|
| 64 |
>>> from requests.auth import HTTPDigestAuth |
| 65 |
>>> url = 'https://httpbin.org/digest-auth/auth/user/pass' |
| 66 |
>>> requests.get(url, auth=HTTPDigestAuth('user', 'pass')) |
| 67 |
<Response [200]> |
| 68 |
|
| 69 |
|
| 70 |
OAuth 1 Authentication |
| 71 |
---------------------- |
| 72 |
|
| 73 |
A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib`` |
| 74 |
library allows Requests users to easily make OAuth 1 authenticated requests:: |
| 75 |
|
| 76 |
>>> import requests |
| 77 |
>>> from requests_oauthlib import OAuth1 |
| 78 |
|
| 79 |
>>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json' |
| 80 |
>>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET', |
| 81 |
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET') |
| 82 |
|
| 83 |
>>> requests.get(url, auth=auth) |
| 84 |
<Response [200]> |
| 85 |
|
| 86 |
For more information on how to OAuth flow works, please see the official `OAuth`_ website. |
| 87 |
For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_ |
| 88 |
repository on GitHub |
| 89 |
|
| 90 |
OAuth 2 and OpenID Connect Authentication |
| 91 |
----------------------------------------- |
| 92 |
|
| 93 |
The ``requests-oauthlib`` library also handles OAuth 2, the authentication mechanism |
| 94 |
underpinning OpenID Connect. See the `requests-oauthlib OAuth2 documentation`_ for |
| 95 |
details of the various OAuth 2 credential management flows: |
| 96 |
|
| 97 |
* `Web Application Flow`_ |
| 98 |
* `Mobile Application Flow`_ |
| 99 |
* `Legacy Application Flow`_ |
| 100 |
* `Backend Application Flow`_ |
| 101 |
|
| 102 |
Other Authentication |
| 103 |
-------------------- |
| 104 |
|
| 105 |
Requests is designed to allow other forms of authentication to be easily and |
| 106 |
quickly plugged in. Members of the open-source community frequently write |
| 107 |
authentication handlers for more complicated or less commonly-used forms of |
| 108 |
authentication. Some of the best have been brought together under the |
| 109 |
`Requests organization`_, including: |
| 110 |
|
| 111 |
- Kerberos_ |
| 112 |
- NTLM_ |
| 113 |
|
| 114 |
If you want to use any of these forms of authentication, go straight to their |
| 115 |
GitHub page and follow the instructions. |
| 116 |
|
| 117 |
|
| 118 |
New Forms of Authentication |
| 119 |
--------------------------- |
| 120 |
|
| 121 |
If you can't find a good implementation of the form of authentication you |
| 122 |
want, you can implement it yourself. Requests makes it easy to add your own |
| 123 |
forms of authentication. |
| 124 |
|
| 125 |
To do so, subclass :class:`AuthBase <requests.auth.AuthBase>` and implement the |
| 126 |
``__call__()`` method:: |
| 127 |
|
| 128 |
>>> import requests |
| 129 |
>>> class MyAuth(requests.auth.AuthBase): |
| 130 |
... def __call__(self, r): |
| 131 |
... # Implement my authentication |
| 132 |
... return r |
| 133 |
... |
| 134 |
>>> url = 'https://httpbin.org/get' |
| 135 |
>>> requests.get(url, auth=MyAuth()) |
| 136 |
<Response [200]> |
| 137 |
|
| 138 |
When an authentication handler is attached to a request, |
| 139 |
it is called during request setup. The ``__call__`` method must therefore do |
| 140 |
whatever is required to make the authentication work. Some forms of |
| 141 |
authentication will additionally add hooks to provide further functionality. |
| 142 |
|
| 143 |
Further examples can be found under the `Requests organization`_ and in the |
| 144 |
``auth.py`` file. |
| 145 |
|
| 146 |
.. _OAuth: https://oauth.net/ |
| 147 |
.. _requests_oauthlib: https://github.com/requests/requests-oauthlib |
| 148 |
.. _requests-oauthlib OAuth2 documentation: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html |
| 149 |
.. _Web Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#web-application-flow |
| 150 |
.. _Mobile Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#mobile-application-flow |
| 151 |
.. _Legacy Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#legacy-application-flow |
| 152 |
.. _Backend Application Flow: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow |
| 153 |
.. _Kerberos: https://github.com/requests/requests-kerberos |
| 154 |
.. _NTLM: https://github.com/requests/requests-ntlm |
| 155 |
.. _Requests organization: https://github.com/requests |