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)

Download files

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

Source Distribution

ustudio_hmac_tornado-0.4.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

ustudio_hmac_tornado-0.4.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file ustudio_hmac_tornado-0.4.0.tar.gz.

File metadata

  • Download URL: ustudio_hmac_tornado-0.4.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.6 Linux/7.0.0-1009-aws

File hashes

Hashes for ustudio_hmac_tornado-0.4.0.tar.gz
Algorithm Hash digest
SHA256 75b3235686d3151bc7e3429048dfdce04dfe111cc3c342f87e618d443d1f3ca6
MD5 e9132a0409dee7c070db87b034507d97
BLAKE2b-256 5135203d5ddc6d6757266f549c19f313bacb6327a7fd1203ba4fef2aeb603b3d

See more details on using hashes here.

File details

Details for the file ustudio_hmac_tornado-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ustudio_hmac_tornado-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fa0397b4d63a0f7df347a20df142c43f5849e162bbb7daf14429232f9b0b0be
MD5 ab1dd1bd4e6c57c6ad83ea6186829335
BLAKE2b-256 098031cf9ca90c21bda3565aec21a873fce9ebb10f26516c42584098696fa39c

See more details on using hashes here.

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