A secure, plug-and-play authentication microservice built with Python, FastAPI, and Argon2id.
Clone, configure your environment variables, and you have a production-ready auth system.
- Secure password hashing with Argon2id + pepper
- JWT authentication with access token (15min) + refresh token (7 days)
- Refresh token rotation with reuse detection (revokes all tokens on suspicious activity)
- Email verification on signup via Resend
- Forgot/reset password flow with email link
- Change password (authenticated)
- Password breach detection via Have I Been Pwned API (k-Anonymity)
- Rate limiting on sensitive endpoints
- CORS configurable via environment variables
- Global error handling (no stack traces leaked)
- Environment validation on startup (fails fast if misconfigured)
- Python 3.12
- FastAPI — web framework
- SQLAlchemy — ORM
- PostgreSQL — database
- Argon2id — password hashing (argon2-cffi)
- PyJWT — JSON Web Tokens
- Resend — transactional emails
- SlowAPI — rate limiting
Vertical Slice Architecture (VSA) + CQRS pattern:
features/auth/
├── cadastro/ # POST /cadastro
├── login/ # POST /login
├── logout/ # POST /logout
├── refresh/ # POST /refresh
├── me/ # GET /me
├── change_password/ # POST /change-password
├── verify_email/ # GET /verify-email
├── forgot_password/ # POST /forgot-password
└── reset_password/ # POST /reset-password
Each feature contains its own dto.py, handler.py, and endpoint.py.
cp .env.example .env
# Edit .env with your values
docker-compose up# Prerequisites: Python 3.12, PostgreSQL
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Edit .env with your values
uvicorn main:app --reload| Variable | Description |
|---|---|
DB_HOST |
PostgreSQL host |
DB_PORT |
PostgreSQL port |
DB_NAME |
Database name |
DB_USER |
Database user |
DB_PASSWORD |
Database password |
SECRET_KEY |
JWT signing key (min 32 chars) |
REFRESH_SECRET_KEY |
Refresh token signing key (min 32 chars) |
HASH_PEPPER |
Password pepper (min 32 chars) |
APP_URL |
API base URL |
FRONTEND_URL |
Frontend base URL (for email links) |
CORS_ORIGINS |
Allowed origins, comma-separated |
RESEND_API_KEY |
Resend API key |
RESEND_FROM_EMAIL |
Sender email address |
See .env.example for a template.
| Method | Endpoint | Auth | Description |
|---|---|---|---|
| POST | /cadastro |
No | Create account |
| POST | /login |
No | Get tokens |
| POST | /refresh |
No | Rotate tokens |
| POST | /logout |
No | Revoke refresh token |
| GET | /me |
Bearer | Get current user |
| POST | /change-password |
Bearer | Change password |
| GET | /verify-email |
No | Verify email via token |
| POST | /forgot-password |
No | Request password reset |
| POST | /reset-password |
No | Reset password via token |
| GET | /health |
No | Health check |
Once running, access the interactive Swagger UI at:
http://localhost:8000/docs
- Argon2id with OWASP recommended parameters (time_cost=3, memory_cost=64MB, parallelism=4)
- Pepper stored as environment variable, never in the database
- Password breach check via Have I Been Pwned (k-Anonymity — your password never leaves the server)
- Refresh token rotation with automatic revocation on reuse detection
- Rate limiting on login (5/min), signup (3/min), forgot password (3/min)
- NIST-compliant password policy (length-based, no arbitrary complexity rules)