Code
jwt_middleware.py
Usage
1
Create a virtual environment
Open the
Terminal
and create a python virtual environment.2
Set Environment Variables
3
Install libraries
4
Setup PostgreSQL Database
5
Run Example
6
Test JWT Authentication
Test with the generated token:Test without token (should still work since validate=False):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_id
from thesub
claimsession_id
from thesession_id
claimdependencies
dict with name, email, and roles
- Agent Tools: The agent can access user details through the injected dependencies