Skip to main content

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.

Given an HTTP request can potentially have multiple signatures the verify() method returns a list of VerifyResult s. However, the implementation currently supports just one signature, so the returned list currently contains just one element. If more signatures are found in the request then InvalidSignature is raised.

Additionally, 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.)

Authors

  • Andrey Kislyuk <https://kislyuk.com>

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

http_message_signatures-1.0.1.tar.gz (28.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

http_message_signatures-1.0.1-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file http_message_signatures-1.0.1.tar.gz.

File metadata

  • Download URL: http_message_signatures-1.0.1.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for http_message_signatures-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f86d32b2f889c6c271d4ae73cd6a6a2c8f746802b93ed940eade8536ebefec37
MD5 a0c839593070c9d7b0d707a49f0c528a
BLAKE2b-256 74150a118e74e8ada931f6c5dc934c37ce77e733e876205caaf611f907e520ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for http_message_signatures-1.0.1.tar.gz:

Publisher: release.yml on pyauth/http-message-signatures

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file http_message_signatures-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for http_message_signatures-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9fcce8d26bfb180537bb3b1215f1fa1e87fe3174bc7092cfd98da1f7fc3fbe4
MD5 b9d7db702049df5c1fcf108741fdc549
BLAKE2b-256 505681e189051ee6cffadc930a056d3f8f271b3fc68776bfd87cff2933fc2c65

See more details on using hashes here.

Provenance

The following attestation bundles were made for http_message_signatures-1.0.1-py3-none-any.whl:

Publisher: release.yml on pyauth/http-message-signatures

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page