Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_jwt Discord

Swarmauri Middleware JWT

JWTMiddleware validates JSON Web Tokens (JWT) issued to FastAPI-based Swarmauri services. The middleware expects a Bearer token in the Authorization header, decodes it with the configured secret and algorithm, and stores the decoded payload on request.state.jwt_payload. Requests with missing or invalid tokens receive an HTTP 401 response.

Installation

pip

pip install swarmauri_middleware_jwt

Poetry

poetry add swarmauri_middleware_jwt

uv

# Install uv (see https://docs.astral.sh/uv/) if it is not already available
curl -LsSf https://astral.sh/uv/install.sh | sh

# Use uv to add the middleware to your environment
uv pip install swarmauri_middleware_jwt

Example

The example below attaches JWTMiddleware to a FastAPI application, issues a token using PyJWT, and performs a request that reads the decoded payload from request.state.

from fastapi import FastAPI, Request
from starlette.testclient import TestClient
import jwt

from swarmauri_middleware_jwt import JWTMiddleware


SECRET_KEY = "change-me"

app = FastAPI()

# Register the middleware with the FastAPI app
jwt_middleware = JWTMiddleware(secret_key=SECRET_KEY)
app.middleware("http")(jwt_middleware.dispatch)


@app.get("/protected")
async def protected_route(request: Request):
    return {"subject": request.state.jwt_payload["sub"]}


def run_example() -> dict:
    token = jwt.encode({"sub": "demo-user"}, SECRET_KEY, algorithm="HS256")
    client = TestClient(app)
    response = client.get(
        "/protected", headers={"Authorization": f"Bearer {token}"}
    )
    response.raise_for_status()
    return response.json()


if __name__ == "__main__":
    print(run_example())

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

Metadata

Release files for swarmauri_middleware_jwt 0.11.0.dev1

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

Source distribution (sdist)

Source distribution for swarmauri_middleware_jwt 0.11.0.dev1
File Size Uploaded
swarmauri_middleware_jwt-0.11.0.dev1.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for swarmauri_middleware_jwt 0.11.0.dev1
File Interpreter ABI Platform
swarmauri_middleware_jwt-0.11.0.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / swarmauri_middleware_jwt-0.11.0.dev1.tar.gz

Download URL swarmauri_middleware_jwt-0.11.0.dev1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0479e75d664e662351a6672d2bc363493c3214dccf598ae3444b2fdc00c605ad
BLAKE2b-256 checksum
How to use checksums
1c858fc2ca36add2d3fb4d9aaaf6ef2c826a5387ca092008430fb5caf4cc6d98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / swarmauri_middleware_jwt-0.11.0.dev1-py3-none-any.whl

Download URL swarmauri_middleware_jwt-0.11.0.dev1-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56f9806f8eea70231c3b0e968af6ce9fa7b0a6ab58586cb0e60dd8e8476413c8
BLAKE2b-256 checksum
How to use checksums
d2fa71368ce9ed55dfc72074cd39fe12a8dd942d993c019468c620296e908cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
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