A FastAPI project demonstrating user authentication, JWT token management, dynamic rate limiting with SlowAPI, and user plans, using Redis for state and token revocation.
- User authentication with JWT tokens
- Dynamic rate limiting per user plan (SlowAPI)
- User plans: free, premium, admin (with different rate limits)
- Token revocation and logout
- Redis for token and rate limit storage
- Scheduler for periodic cleanup
- Comprehensive API with endpoints for login, refresh, logout, plan-limited access, and more
- Testing with pytest
- Dockerized for easy setup
- Python 3.10+
- Docker & Docker Compose (for containerized setup)
docker compose up --build- The API will be available at http://localhost:8000
- Redis will be available at port 6379
- Docs: http://localhost:8000/docs
- Install uv:
pip install uv
- Install dependencies:
uv sync
- Start Redis locally (or use Docker):
docker run -p 6379:6379 redis:latest
- Run the app:
uvicorn app.main:app --reload
This project uses a fake in-memory user database for demonstration:
john_doe/password123(plan: premium)jane_doe/password456(plan: free)
Plans and rate limits:
free: 2 requests/minutepremium: 10 requests/minuteadmin: 9999 requests/minute
| Method | Path | Description |
|---|---|---|
| POST | /token |
Login, get JWT access token |
| POST | /refresh |
Refresh JWT token |
| POST | /logout |
Logout (revoke token) |
| POST | /revoke |
Revoke a token manually |
| GET | /is_token_revoked |
Check if a token is revoked |
| GET | /plan-limited-endpoint |
Rate-limited endpoint (plan-based) |
| GET | /premium-only-endpoint |
Premium/admin-only endpoint |
| GET | /users/me |
Get current user info |
| GET | /rate-limit-status |
Check rate limit status |
| GET | /health |
Health check |
curl -X POST http://localhost:8000/token \
-H 'Content-Type: application/json' \
-d '{"username": "john_doe", "password": "password123"}'curl -H "Authorization: Bearer <access_token>" http://localhost:8000/users/me- Uses SlowAPI for per-plan rate limits
- Limits are enforced per endpoint and user plan
- Exceeding the limit returns HTTP 429 with a retry time
Run tests with:
just testOr directly:
pytestjust build— Build Docker imagejust up— Start servicesjust down— Stop servicesjust restart— Rebuild and restartjust test— Run testsjust cov— Run tests with coveragejust clean— Clean Docker resources
- Configure via
.envfile (seeSettingsinapp/main.pyfor options) - Example variables:
SECRET_KEY,REDIS_URI, etc.
Pull requests and issues are welcome! For major changes, please open an issue first to discuss what you would like to change.
License: MIT