@@ -2,10 +2,7 @@ requests-http-signature: A Requests auth module for HTTP Signature
22==================================================================
33**requests-http-signature ** is a `Requests <https://github.com/requests/requests >`_ `authentication plugin
44<http://docs.python-requests.org/en/master/user/authentication/> `_ (``requests.auth.AuthBase `` subclass) implementing
5- the `IETF HTTP Signatures draft RFC <https://tools.ietf.org/html/draft-richanna-http-message-signatures >`_. It has no
6- required dependencies outside the standard library. If you wish to use algorithms other than HMAC (namely, RSA and
7- ECDSA algorithms specified in the RFC), there is an optional dependency on
8- `cryptography <https://pypi.python.org/pypi/cryptography >`_.
5+ the `IETF HTTP Message Signatures draft RFC <https://datatracker.ietf.org/doc/draft-ietf-httpbis-message-signatures/ >`_.
96
107Installation
118------------
@@ -19,65 +16,74 @@ Usage
1916.. code-block :: python
2017
2118 import requests
22- from requests_http_signature import HTTPSignatureAuth
19+ from requests_http_signature import HTTPSignatureAuth, algorithms
2320
2421 preshared_key_id = ' squirrel'
25- preshared_secret = ' monorail_cat'
22+ preshared_secret = b ' monorail_cat'
2623 url = ' http://example.com/path'
27-
28- requests.get(url, auth = HTTPSignatureAuth(key = preshared_secret, key_id = preshared_key_id))
2924
30- By default, only the ``Date `` header is signed (as per the RFC) for body-less requests such as GET. The ``Date `` header
31- is set if it is absent. In addition, for requests with bodies (such as POST), the ``Digest `` header is set to the SHA256
32- of the request body and signed (an example of this appears in the RFC). To add other headers to the signature, pass an
33- array of header names in the ``headers `` keyword argument.
25+ auth = HTTPSignatureAuth(key = preshared_secret, key_id = preshared_key_id, signature_algorithm = algorithms.HMAC_SHA256 )
26+ requests.get(url, auth = auth)
27+
28+ By default, only the ``Date `` header and the ``@method ``, ``@authority ``, and ``@target-uri `` derived component
29+ identifiers are signed for body-less requests such as GET. The ``Date `` header is set if it is absent. In addition, for
30+ requests with bodies (such as POST), the ``Content-Digest `` header is set to the SHA256 of the request body using the
31+ format described in the
32+ `IETF Digest Fields draft RFC <https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-digest-headers >`_ and signed.
33+ To add other headers to the signature, pass an array of header names in the ``covered_component_ids `` keyword argument.
3434
3535In addition to signing messages in the client, the class method ``HTTPSignatureAuth.verify() `` can be used to verify
3636incoming requests:
3737
3838.. code-block :: python
3939
40- def key_resolver (key_id , algorithm ):
41- return ' monorail_cat'
40+ class key_resolver :
41+ def resolve_public_key (self , key_id ):
42+ assert key_id == ' squirrel'
43+ return ' monorail_cat'
44+
45+ HTTPSignatureAuth.verify(request, signature_algorithm = algorithms.HMAC_SHA256 , key_resolver = key_resolver)
46+
4247
43- HTTPSignatureAuth.verify(request, key_resolver = key_resolver)
48+ Asymmetric key algorithms
49+ ~~~~~~~~~~~~~~~~~~~~~~~~~
50+ To sign or verify messages with an asymmetric key algorithm, set the ``signature_algorithm `` keyword argument to
51+ ``algorithms.ED25519 ``, ``algorithms.ECDSA_P256_SHA256 ``, ``algorithms.RSA_V1_5_SHA256 ``, or
52+ ``algorithms.RSA_PSS_SHA512 ``. Note that signing with rsa-pss-sha512 is not currently supported due to a limitation of
53+ the cryptography library.
4454
45- Asymmetric key algorithms (RSA and ECDSA)
46- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47- For asymmetric key algorithms, you should supply the private key as the ``key `` parameter to the ``HTTPSignatureAuth() ``
48- constructor as bytes in the PEM format:
55+ For asymmetric key algorithms, you can supply the private key as the ``key `` parameter to the ``HTTPSignatureAuth() ``
56+ constructor as bytes in the PEM format, or configure the key resolver as follows:
4957
5058.. code-block :: python
5159
5260 with open (' key.pem' , ' rb' ) as fh:
53- requests.get(url, auth = HTTPSignatureAuth(algorithm = " rsa-sha256" , key = fh.read(), key_id = preshared_key_id))
61+ auth = HTTPSignatureAuth(algorithm = algorithms.RSA_V1_5_SHA256 , key = fh.read(), key_id = preshared_key_id)
62+ requests.get(url, auth = auth)
5463
55- When verifying, the ``key_resolver() `` callback should provide the public key as bytes in the PEM format as well.
64+ class MyKeyResolver :
65+ def resolve_public_key (self , key_id : str ):
66+ return public_key_pem_bytes[key_id]
67+
68+ def resolve_private_key (self , key_id : str ):
69+ return private_key_pem_bytes[key_id]
70+
71+ auth = HTTPSignatureAuth(algorithm = algorithms.RSA_V1_5_SHA256 , key = fh.read(), key_resolver = MyKeyResolver())
72+ requests.get(url, auth = auth)
5673
5774 Links
5875-----
59- * `IETF HTTP Signatures draft <https://tools .ietf.org/html/draft-richanna-http -message-signatures >`_
60- * https://github.com/joyent/node- http-signature
61- * ` Project home page (GitHub) < https://github.com/kislyuk/requests-http-signature >`_
62- * `Documentation (Read the Docs ) <https://requests-http-signature.readthedocs.io/en/latest/ >`_
76+ * `IETF HTTP Signatures draft <https://datatracker .ietf.org/doc/ html/draft-ietf-httpbis -message-signatures >`_
77+ * ` http-message-signatures < https://github.com/pyauth/ http-message-signatures >`_ - a dependency of this library that
78+ handles much of the implementation
79+ * `Project home page (GitHub ) <https://github.com/pyauth/ requests-http-signature >`_
6380* `Package distribution (PyPI) <https://pypi.python.org/pypi/requests-http-signature >`_
64- * `Change log <https://github.com/kislyuk /requests-http-signature/blob/master/Changes.rst >`_
81+ * `Change log <https://github.com/pyauth /requests-http-signature/blob/master/Changes.rst >`_
6582
6683Bugs
6784~~~~
68- Please report bugs, issues, feature requests, etc. on `GitHub <https://github.com/kislyuk /requests-http-signature/issues >`_.
85+ Please report bugs, issues, feature requests, etc. on `GitHub <https://github.com/pyauth /requests-http-signature/issues >`_.
6986
7087License
7188-------
7289Licensed under the terms of the `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0 >`_.
73-
74- .. image :: https://github.com/pyauth/requests-http-signature/workflows/Python%20package/badge.svg
75- :target: https://github.com/pyauth/requests-http-signature/actions
76- .. image :: https://codecov.io/github/kislyuk/requests-http-signature/coverage.svg?branch=master
77- :target: https://codecov.io/github/kislyuk/requests-http-signature?branch=master
78- .. image :: https://img.shields.io/pypi/v/requests-http-signature.svg
79- :target: https://pypi.python.org/pypi/requests-http-signature
80- .. image :: https://img.shields.io/pypi/l/requests-http-signature.svg
81- :target: https://pypi.python.org/pypi/requests-http-signature
82- .. image :: https://readthedocs.org/projects/requests-http-signature/badge/?version=latest
83- :target: https://requests-http-signature.readthedocs.org/
0 commit comments