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
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
- JWT Generation: The example creates a test JWT token with user claims
- Middleware Setup: JWT middleware extracts claims from the
Authorization: Bearer <token>header - Parameter Injection: The middleware automatically injects:
user_idfrom thesubclaimsession_idfrom thesession_idclaimdependenciesdict with name, email, and roles
- Agent Tools: The agent can access user details through the injected dependencies