Skip to content

A Python-based microservice for user authentication and authorization built with FastAPI, following Test-Driven Development (TDD) principles.

Notifications You must be signed in to change notification settings

sakshamgorey/python-microservice

Repository files navigation

User Authentication & Authorization Microservice

A Python-based microservice for user authentication and authorization built with FastAPI, following Test-Driven Development (TDD) principles.

🚀 Features

  • 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

🏗️ Architecture Overview (For Laravel Developers)

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

📁 Project Structure

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

🛠️ Installation & Setup

Prerequisites

  • Python 3.8+
  • PostgreSQL (or SQLite for development)
  • Docker (optional)

Local Development

  1. Clone and setup environment:
git clone <repository-url>
cd microservice
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Environment configuration:
cp env.example .env
# Edit .env with your database credentials
  1. Database setup:
# Run migrations (like php artisan migrate)
alembic upgrade head
  1. Run the application:
# Development server (like php artisan serve)
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

Docker Setup

# Build and run with Docker Compose (like Laravel Sail)
docker-compose up --build

🧪 Testing

Run 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 -v

📚 API Documentation

Once the server is running, visit:

🔐 API Endpoints

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

🔧 Configuration

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

🚀 Deployment

Production Considerations

  1. Environment Variables: Set production values in .env
  2. Database: Use PostgreSQL in production
  3. Security: Use strong SECRET_KEY and HTTPS
  4. Monitoring: Add logging and health checks
  5. Scaling: Use load balancer for multiple instances

Docker Production

# Build production image
docker build -t auth-service .

# Run with production settings
docker run -p 8000:8000 --env-file .env auth-service

📖 Laravel Developer Guide

For Laravel developers transitioning to this Python microservice, see the detailed documentation in the docs/ folder:

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Implement the feature
  5. Run tests and ensure they pass
  6. Submit a pull request

📄 License

This project is licensed under the MIT License.

About

A Python-based microservice for user authentication and authorization built with FastAPI, following Test-Driven Development (TDD) principles.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published