Skip to main content
AuthMiddleware validates the JWT in the Authorization header and injects its claims into endpoint parameters.
AuthMiddleware was named JWTMiddleware before v2.7. JWTMiddleware remains as an alias.

Code

jwt_middleware.py

Usage

1

Set up your virtual environment

2

Set Environment Variables

3

Install dependencies

4

Setup PostgreSQL Database

5

Run Example

The server will start and print a test JWT token to the console.
6

Test JWT Authentication

Test with the generated token:
Test without a token:
This returns 401 with {"detail": "Authorization header missing"}. validate=False skips signature verification, but a token is still required.Check the AgentOS API docs: Visit http://localhost:7777/docs to see all available endpoints.

How It Works

  1. JWT Generation: The example creates a test JWT token with user claims
  2. Middleware Setup: JWT middleware extracts claims from the Authorization: Bearer <token> header
  3. Parameter Injection: The middleware automatically injects:
    • user_id from the sub claim
    • session_id from the session_id claim
    • dependencies dict with name, email, and roles
  4. Agent Tools: The agent can access user details through the injected dependencies

Next Steps