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 draft standard in Python.

Installation

pip3 install http-message-signatures

Synopsis

from http_message_signatures import HTTPMessageSigner, HTTPMessageVerifier, HTTPSignatureKeyResolver, algorithms
import requests, base64, hashlib, http_sfv

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

License

Licensed under the terms of the Apache License, Version 2.0.

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-0.5.0.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

http_message_signatures-0.5.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file http-message-signatures-0.5.0.tar.gz.

File metadata

File hashes

Hashes for http-message-signatures-0.5.0.tar.gz
Algorithm Hash digest
SHA256 5a59de19b90dce0eaf62021ee776d6562e5a166c96e4107db36f9c01f25552a3
MD5 a7be4ec54c31c3b9b4fcc3bf12661947
BLAKE2b-256 dcc99396cc932aaf2e79d9cbced8826138751de89e4c65c5b04a431e7f5e5032

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for http_message_signatures-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 719933cba48943b5e148fe7bbbf520927573f72c6ca00855cb2c79fdecee2cb2
MD5 8e65eeab79ef4f134a196b246051abd6
BLAKE2b-256 21db353399e056ec284c64ed6c3baa818bcd248f81029f98cc5dcdb0a7f12fa9

See more details on using hashes here.

Supported by

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