Skip to main content

ustudio-hmac-tornado

Client, Server and Testing utilities for HMAC authentication between services using Tornado

How it Works

Clients and servers communicate using a shared secret, which is not transmitted as part of the protocol.

When a client makes an HTTP request, it generates a SHA-256 HMAC digest of the concatenated path, HTTP method and request body, and includes an Authorization HTTP header of the form:

USTUDIO-HMAC-V2 [KEY] [DIGEST]

On the server side, the server uses the provided key to look up a pre-shared secret; it then reproduces the digest signing algorithm against the request, and compares that the digest it generated matches that provided by the client.

If the key does not correspond to a known secret, if the secrets do not match between client and server, or if any of the signed parameters have been changed in the request, the digests will not match and the server will reject the request.

Installation

pip install ustudio-hmac-tornado

Usage

This library implements both client-side signing of requests, and server-side validating of signed requests.

Client Side Usage

The hmacauth.client.authenticated_request function can be used to generate tornado.httpclient.HTTPRequest objects which are authenticated against a key and secret.

It requires three arguments:

  • url - May be the first positional or a keyword argument
  • hmac_key - Must be a keyword argument
  • hmac_secret - Must be a keyword argument

hmac_key and hmac_secret will be removed from the arguments, and all other arguments will be passed to the HTTPRequest constructor. The headers will be modified to include the Authorization header with the provided hmac_key and generated digest.

from hmacauth.client import authenticated_request

response = await http_client.fetch(authenticated_request(
    "https://some.service/api/v1/resource",
    method="POST",
    body=json.dumps({
        "request": "body"
    }).encode("utf8")))

Server Side Usage

On the server side, the library provides a decorator hmacauth.server.hmac_authorized, which works similarly to tornado.web.authorized, but validates requests using HMAC authorization, rather than checking the current_user property.

When a request is made to a decorated method, the Authorization header is parsed, and the key is looked up by calling handler.get_hmac_secret(key) on the handler with the wrapped method.

handler.get_hmac_secret should return the correct secret for that key, or None if the key is invalid. The decorator will then use that key to validate the request and raise an HTTPError(401) if it is invalid, or invoke the method if it is valid.

from tornado.web import RequestHandler
from hmacauth.server import hmac_authorized


class SecureRoute(RequestHandler):
    @hmac_authorized
    def post(self, some, args):
        # Only called if the request is valid
        self.finish("You're OK!")

    def get_hmac_secret(self, key):
        return self.settings["database"].get_secret(key)

Release files for ustudio-hmac-tornado 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ustudio-hmac-tornado 0.4.0
File Size Uploaded
ustudio_hmac_tornado-0.4.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ustudio-hmac-tornado 0.4.0
File Interpreter ABI Platform
ustudio_hmac_tornado-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / ustudio_hmac_tornado-0.4.0.tar.gz

Download URL ustudio_hmac_tornado-0.4.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
75b3235686d3151bc7e3429048dfdce04dfe111cc3c342f87e618d443d1f3ca6
BLAKE2b-256 checksum
How to use checksums
5135203d5ddc6d6757266f549c19f313bacb6327a7fd1203ba4fef2aeb603b3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.6 Linux/7.0.0-1009-aws

Release files / ustudio_hmac_tornado-0.4.0-py3-none-any.whl

Download URL ustudio_hmac_tornado-0.4.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6fa0397b4d63a0f7df347a20df142c43f5849e162bbb7daf14429232f9b0b0be
BLAKE2b-256 checksum
How to use checksums
098031cf9ca90c21bda3565aec21a873fce9ebb10f26516c42584098696fa39c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.14.6 Linux/7.0.0-1009-aws

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page