Skip to main content

JWT validation middleware for Swarmauri applications.

Project description

Swarmauri Logo

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


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.

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

swarmauri_middleware_jwt-0.8.0.dev34.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file swarmauri_middleware_jwt-0.8.0.dev34.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_jwt-0.8.0.dev34.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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}

File hashes

Hashes for swarmauri_middleware_jwt-0.8.0.dev34.tar.gz
Algorithm Hash digest
SHA256 b537f79f94a5aee7b3f251133f8137d4ce7bae004c54fd1578d5a08de7f5de88
MD5 3f91546fb726e41f271043f912054b64
BLAKE2b-256 7c2cd2d1b5d70771513abfe002c4ccff8f98e60ba78d3d32c6641d2558f85766

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_jwt-0.8.0.dev34-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_jwt-0.8.0.dev34-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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}

File hashes

Hashes for swarmauri_middleware_jwt-0.8.0.dev34-py3-none-any.whl
Algorithm Hash digest
SHA256 862a4f20600301d58932ae1f70f53da19cf8e2adab9c2ce25da4abf3f78032a7
MD5 ed1cc04aeffc219e159248d2a3ca4ebe
BLAKE2b-256 45185188360e20129e63cadf56879da2ee78f93048a44cdd250fe11a9dd8a0f2

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