Skip to main content

A library that provides signing middlewares for any ASGI application.

Project description

Pipeline Status Coverage Report PyPI Version PyPI Python Versions License Follow Me on Twitter

ASGI Signing Middleware

The goal of this project is to provide a simple and straightforward way to securely sign data by providing ready-to-use middlewares, using blake2signer as signing backend.

Why would I need to use it?

If you need to store some data (state, generally), and want to avoid using a trusted DB for performance reasons, it is usually advisable to sign said data. This package will help you achieve that with ease, ready-to-use middlewares, without you needing to think on the details: just provide a signing secret and let this package handle the rest.

Why would I want to use it?

Because it is a relatively small (around 100 logical lines of code), simple (usage is very straight-forward) yet very customizable and fast middleware data signer. My idea is to keep it as uncomplicated as possible without much room to become a footgun. All defaults are very sane (secure) and everything just works out of the box.

Goals

  • Be safe and secure.
  • Be simple and straightforward.
  • Follow semver.
  • Be always typed.
  • 100% coverage.

Secondary goals

  • If possible, maintain active Python versions (3.8+).

Installing

This package is hosted on PyPi so just:

  • python3 -m pip install asgi-signing-middleware
  • poetry add asgi-signing-middleware
  • pipenv install asgi-signing-middleware

You can check the releases' page for package hashes and signatures.

Requirements

Versions currently tested (check the pipelines):

  • CPython 3.8
  • CPython 3.9
  • CPython 3.10
  • CPython 3.11
  • PyPy 3.8
  • Stackless 3.8

Tl; Dr Example

"""Tl;dr example."""

import typing

from fastapi import FastAPI
from fastapi import Request

from asgi_signing_middleware import SerializedSignedCookieMiddleware

app = FastAPI()
app.add_middleware(
    SerializedSignedCookieMiddleware,  # Any of the middlewares
    # Follows the middleware parameters
    secret=b'a very, very secret thing',  # This should probably come from some configs
    state_attribute_name='messages',  # Use in a request handler as `request.state.messages`
    cookie_name='my_cookie',
    cookie_ttl=60 * 5,  # 5 minutes, in seconds
    # You can also set extra signer kwargs and cookie properties, check the middleware
    # init for more info.
)

@app.get('/cookie')
def cookie_endpoint(request: Request) -> None:
    # This will only have data that was correctly signed, or None
    cookie_data: typing.Optional[typing.Dict[str, str]] = request.state.messages

    # ...

    # This will be signed and written into the cookie
    request.state.messages = {'A Title': 'The message', 'Another title': 'With another msg'}

Tip: all modules, classes, methods and functions are documented so don't doubt asking for help().

Documentation

Check out this project docs online or locally with inv docs. Alternatively, build them locally using inv docs --build.

Notice

I'm not a cryptoexpert, so this project needs a security review. If you are one and can do it, please contact me.

License

ASGI Signing Middleware is made by HacKan under MPL v2.0. You are free to use, share, modify and share modifications under the terms of that license. Derived works may link back to the canonical repository: https://gitlab.com/hackancuba/asgi-signing-middleware.

Copyright (C) 2022 HacKan (https://hackan.net)
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/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

asgi-signing-middleware-0.1.1.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

asgi_signing_middleware-0.1.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file asgi-signing-middleware-0.1.1.tar.gz.

File metadata

  • Download URL: asgi-signing-middleware-0.1.1.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/3.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2

File hashes

Hashes for asgi-signing-middleware-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0d45f96d4d34d154c82bf47f81ba141c3aae4b4b8461e95dac464030782866f2
MD5 715d3d372f52fdf9f4aaf816a5bafee4
BLAKE2b-256 4aaf6410f57bcfcfd8e6060e08e56ef54b88635d424dbea680c15500934eaf90

See more details on using hashes here.

File details

Details for the file asgi_signing_middleware-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: asgi_signing_middleware-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/3.10.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.2

File hashes

Hashes for asgi_signing_middleware-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 685c10c398b660a66dd37630af5a8d62cdf3957d3bcc09ddf7358dd48e49b028
MD5 e2300f1cd7ff47606f3ef30772a6183f
BLAKE2b-256 7cc00f58b03ca283cc1ed6d259a4680f8ea9701b8a56d468341acec3ddf0a8a5

See more details on using hashes here.

Supported by

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