Hawk itself does not provide any mechanism for obtaining or transmitting the set of shared credentials required, but this project proposes a scheme we use across mozilla services projects.
Great, how can I use it?
First, you’ll need to install it:
.. code-block:: bash pip install requests-hawk
Then, in your project, if you know the id and key, you can use:
.. code-block:: python import requests from requests_hawk import HawkAuth hawk_auth = HawkAuth(id='my-hawk-id', key='my-hawk-secret-key') requests.post("https://example.com/url", auth=hawk_auth)
Or if you need to derive them from the hawk session token, instead use:
.. code-block:: python import requests from requests_hawk import HawkAuth hawk_auth = HawkAuth( hawk_session=resp.headers['hawk-session-token'], server_url=self.server_url ) requests.post("/url", auth=hawk_auth)
In the second example, the server_url parameter to HawkAuth was used to provide a default host name, to avoid having to repeat it for each request.
If you wish to override the default algorithm of sha256, pass the desired algorithm name using the optional algorithm parameter.
Note: The credentials parameter has been removed. Instead pass id and key separately (as above), or pass the existing dict as **credentials.
Integration with httpie
Httpie is a tool which lets you do requests to a distant server in a nice and easy way. Under the hood, httpie uses the requests library. We’ve made it simple for you to plug hawk with it.
If you know the id and key, use it like that:
.. code-block:: bash http POST localhost:5000/registration\ --auth-type=hawk --auth='id:key'
Or, if you want to use the hawk session token, you can do as follows:
.. code-block:: bash http POST localhost:5000/registration\ --auth-type=hawk --auth='c0d8cd2ec579a3599bef60f060412f01f5dc46f90465f42b5c47467481315f51:'
Take care, don’t forget to add the extra : at the end of the hawk session token for it to be considered like so.
To run test, you can use tox:
.. code-block:: bash tox
- Simplified API for using HawkAuth when the id and key are known. (#8)
- Added support for overriding the default algorithm (sha256) when deriving credentials from the hawk session token, via a new algorithm parameter.
See the README for migration advice if you use the credentials parameter.
- Make sure the requests json parameter is handled properly. (#7)
- Fix encoding error in setup.py with Python 3.4
- Add a configuration parameter in order to be able to set the timestamp to use.
- Add Python3 support
- First version
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|requests_hawk-1.0.0-py2.py3-none-any.whl (9.0 kB) Copy SHA256 Checksum SHA256||py2.py3||Wheel||Dec 15, 2015|
|requests-hawk-1.0.0.tar.gz (6.3 kB) Copy SHA256 Checksum SHA256||–||Source||Dec 15, 2015|