An implementation of the IETF HTTP Message Signatures draft standard
Project description
http-message-signatures is an implementation of the IETF RFC 9421 HTTP Message Signatures standard in Python.
Installation
pip3 install http-message-signatures
Synopsis
from http_message_signatures import HTTPMessageSigner, HTTPMessageVerifier, HTTPSignatureKeyResolver, algorithms, http_sfv
import requests, base64, hashlib
class MyHTTPSignatureKeyResolver(HTTPSignatureKeyResolver):
keys = {"my-key": b"top-secret-key"}
def resolve_public_key(self, key_id: str):
return self.keys[key_id]
def resolve_private_key(self, key_id: str):
return self.keys[key_id]
request = requests.Request('POST', 'https://example.com/foo?param=Value&Pet=dog', json={"hello": "world"})
request = request.prepare()
request.headers["Content-Digest"] = str(http_sfv.Dictionary({"sha-256": hashlib.sha256(request.body).digest()}))
signer = HTTPMessageSigner(signature_algorithm=algorithms.HMAC_SHA256, key_resolver=MyHTTPSignatureKeyResolver())
signer.sign(request, key_id="my-key", covered_component_ids=("@method", "@authority", "@target-uri", "content-digest"))
verifier = HTTPMessageVerifier(signature_algorithm=algorithms.HMAC_SHA256, key_resolver=MyHTTPSignatureKeyResolver())
verifier.verify(request)
Note that verifying the body content-digest is outside the scope of this package’s functionality, so it remains the caller’s responsibility. The requests-http-signature library builds upon this package to provide integrated signing and validation of the request body.
Multiple signatures
An HTTP request can potentially have multiple signatures. By default, http-message-signatures overwrites any existing signature when signing, and requires exactly one signature to be present when verifying. Before enabling multi-signature support, please read section 7.2.6 of the RFC, Multiple Signature Confusion.
To append a signature to a message that might already carry an existing signature without overwriting it, use:
signer.sign(request, append_if_signature_exists=True, ...)
To identify a signature using a tag parameter and verify it on a message possibly carrying multiple signatures, use:
verifier.verify(request, expect_tag="my_app_tag")
This will filter all signatures down to only those that set the tag to the expected value, verify each of them, and return a list of VerifyResult s. Verifying multiple signatures without prior knowledge of the application tag is not supported.
Error handling
If multiple signatures are found in the request but expect_tag=... was not passed, then InvalidSignature is raised. Also, the verify() method raises HTTPMessageSignaturesException or an exception derived from this class in case an error occurs (unable to load PEM key, unsupported algorithm specified in signature input, signature doesn’t match digest etc.)
Links
Bugs
Please report bugs, issues, feature requests, etc. on GitHub.
License
Copyright 2017-2024, Andrey Kislyuk and http-message-signatures contributors. Licensed under the terms of the Apache License, Version 2.0. Distribution of attribution information, LICENSE and NOTICE files with source copies of this package and derivative works is REQUIRED as specified by the Apache License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file http_message_signatures-2.0.1.tar.gz.
File metadata
- Download URL: http_message_signatures-2.0.1.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
394545b0cc296a4fd45166f57056e7e029dcd5b91d9bf549e108c96c23aa1cba
|
|
| MD5 |
21315415e4d34d74a7b9601b76e7ff17
|
|
| BLAKE2b-256 |
8095083707b15af3bdbb6c9e54ae7f448046f7ab55f9eaa5cccdc5dd23791d15
|
Provenance
The following attestation bundles were made for http_message_signatures-2.0.1.tar.gz:
Publisher:
release.yml on pyauth/http-message-signatures
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
http_message_signatures-2.0.1.tar.gz -
Subject digest:
394545b0cc296a4fd45166f57056e7e029dcd5b91d9bf549e108c96c23aa1cba - Sigstore transparency entry: 834282665
- Sigstore integration time:
-
Permalink:
pyauth/http-message-signatures@40be7e490b3ba227263d3f6381f4097f42097e2d -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/pyauth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@40be7e490b3ba227263d3f6381f4097f42097e2d -
Trigger Event:
push
-
Statement type:
File details
Details for the file http_message_signatures-2.0.1-py3-none-any.whl.
File metadata
- Download URL: http_message_signatures-2.0.1-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b2c463f5d077d3081f27770e8017762d5969a1f17e1dc89e8b96a57c44e8a5e
|
|
| MD5 |
f04fb792d61e4b61a244b8cbc37b7f2c
|
|
| BLAKE2b-256 |
8a4ac778016e3a9c18f283b401319346a980f3ff9556c674afa89a22b9e756bb
|
Provenance
The following attestation bundles were made for http_message_signatures-2.0.1-py3-none-any.whl:
Publisher:
release.yml on pyauth/http-message-signatures
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
http_message_signatures-2.0.1-py3-none-any.whl -
Subject digest:
2b2c463f5d077d3081f27770e8017762d5969a1f17e1dc89e8b96a57c44e8a5e - Sigstore transparency entry: 834282667
- Sigstore integration time:
-
Permalink:
pyauth/http-message-signatures@40be7e490b3ba227263d3f6381f4097f42097e2d -
Branch / Tag:
refs/tags/v2.0.1 - Owner: https://github.com/pyauth
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@40be7e490b3ba227263d3f6381f4097f42097e2d -
Trigger Event:
push
-
Statement type: