A Python-based microservice for user authentication and authorization built with FastAPI, following Test-Driven Development (TDD) principles.
- User Registration & Login - Secure user authentication with bcrypt password hashing
- JWT Token Management - Access and refresh token generation and validation
- Role-Based Access Control (RBAC) - Admin, User, and Moderator roles
- RESTful API - Clean, documented API endpoints
- Database Integration - SQLAlchemy ORM with PostgreSQL/SQLite support
- Comprehensive Testing - Full test coverage with pytest
- Docker Support - Containerized deployment ready
This microservice follows similar patterns to Laravel but uses Python/FastAPI:
| Laravel Component | Python/FastAPI Equivalent | Purpose |
|---|---|---|
app/Models/ |
app/models.py |
Database models (Eloquent → SQLAlchemy) |
app/Http/Controllers/ |
app/routers/ |
API route handlers |
app/Http/Requests/ |
app/schemas.py |
Request/Response validation (Form Requests → Pydantic) |
config/ |
app/config.py |
Application configuration |
database/migrations/ |
alembic/versions/ |
Database migrations |
routes/api.php |
app/main.py |
Route definitions |
tests/ |
tests/ |
Test files (PHPUnit → pytest) |
.env |
.env |
Environment variables |
microservice/
├── app/ # Main application directory
│ ├── __init__.py # Python package marker
│ ├── config.py # Configuration settings (like config/app.php)
│ ├── database.py # Database connection (like config/database.php)
│ ├── models.py # Database models (like app/Models/)
│ ├── schemas.py # Request/Response schemas (like Form Requests)
│ ├── auth.py # Authentication utilities (like Auth facade)
│ ├── routers/ # API route handlers (like Controllers)
│ │ ├── __init__.py
│ │ └── auth.py # Authentication routes
│ └── main.py # FastAPI app instance (like routes/api.php)
├── tests/ # Test files (like tests/)
│ ├── __init__.py
│ ├── conftest.py # Test configuration (like TestCase)
│ └── test_auth.py # Authentication tests
├── alembic/ # Database migrations (like database/migrations/)
├── docs/ # Documentation files
├── requirements.txt # Dependencies (like composer.json)
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker services
└── README.md # This file
- Python 3.8+
- PostgreSQL (or SQLite for development)
- Docker (optional)
- Clone and setup environment:
git clone <repository-url>
cd microservice
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Environment configuration:
cp env.example .env
# Edit .env with your database credentials- Database setup:
# Run migrations (like php artisan migrate)
alembic upgrade head- Run the application:
# Development server (like php artisan serve)
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000# Build and run with Docker Compose (like Laravel Sail)
docker-compose up --buildRun tests using pytest (similar to php artisan test):
# Run all tests
pytest
# Run with coverage
pytest --cov=app
# Run specific test file
pytest tests/test_auth.py
# Run with verbose output
pytest -vOnce the server is running, visit:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
| Method | Endpoint | Description | Laravel Equivalent |
|---|---|---|---|
| POST | /auth/register |
User registration | POST /api/register |
| POST | /auth/login |
User login | POST /api/login |
| GET | /auth/me |
Get current user | GET /api/user |
| POST | /auth/refresh |
Refresh token | POST /api/refresh |
| GET | /auth/admin/users |
List users (Admin) | GET /api/admin/users |
Key environment variables (similar to Laravel's .env):
DATABASE_URL=postgresql://user:pass@localhost:5432/auth_service
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7- Environment Variables: Set production values in
.env - Database: Use PostgreSQL in production
- Security: Use strong SECRET_KEY and HTTPS
- Monitoring: Add logging and health checks
- Scaling: Use load balancer for multiple instances
# Build production image
docker build -t auth-service .
# Run with production settings
docker run -p 8000:8000 --env-file .env auth-serviceFor Laravel developers transitioning to this Python microservice, see the detailed documentation in the docs/ folder:
- Laravel to FastAPI Mapping
- Database Models Guide
- Authentication System
- Testing Guide
- API Documentation
- Fork the repository
- Create a feature branch
- Write tests for new functionality
- Implement the feature
- Run tests and ensure they pass
- Submit a pull request
This project is licensed under the MIT License.