Skip to content

mnaimfaizy/fastapi_rbac

Repository files navigation

FastAPI RBAC - User Management Microservice

FastAPI RBAC Logo

Backend CI React Frontend CI Docker Publish Documentation

License: MIT Python Version FastAPI React TypeScript

Docker Hub Backend Docker Hub Frontend Documentation Status Maintenance

A comprehensive Role-Based Access Control (RBAC) system with FastAPI backend and React frontend, designed to handle Authentication and Authorization for other services.

πŸš€ Quick Start

New to the project?

Start here: Getting Started

Want to run immediately?

git clone <repository-url>
cd fastapi_rbac
docker-compose up -d

# Access at:
# Frontend: http://localhost:80
# Backend API: http://localhost:8000/docs

πŸ“š Documentation

All documentation has been organized for easy navigation:

πŸ› οΈ Scripts & Utilities

Utility scripts are organized by purpose in the scripts/ directory:

✨ Key Features

πŸ”’ Enterprise Security

  • πŸ” JWT Authentication: Secure token-based auth with refresh tokens
  • πŸ›‘οΈ CSRF Protection: Complete protection against Cross-Site Request Forgery
  • 🧽 Input Sanitization: XSS prevention with comprehensive HTML cleaning
  • ⚑ Rate Limiting: DoS attack prevention on all critical endpoints
  • πŸ”’ Security Headers: Strict Content Security Policy and browser protections

πŸ‘₯ Access Control & Management

  • πŸ‘₯ Role-Based Access Control: Flexible permission system with role hierarchies
  • 🎯 User Management: Complete CRUD operations with admin oversight
  • 🏒 Organization Management: Role groups and permission groups for enterprise use

πŸ§ͺ Quality & Testing

  • πŸ§ͺ Comprehensive Testing: 90+ backend tests + 354 frontend tests across 16 files
  • All async DB queries in backend and tests use SQLModel’s .exec() idiom with AsyncSession (not .execute()).
  • See backend/test/README.md for full test/factory/fixture/optimization details.
  • ⚑ Frontend Testing: Complete coverage with Vitest, React Testing Library
  • πŸ” API Testing: Comprehensive service layer testing with mocking

πŸš€ Infrastructure & Integration

  • πŸ“§ Email Integration: Notifications and password reset functionality
  • ⚑ Background Tasks: Celery integration for async operations
  • 🐳 Docker Ready: Full containerization with production configs
  • πŸ“Š API Documentation: Auto-generated OpenAPI/Swagger docs

πŸ—οΈ Project Structure

fastapi_rbac/
β”œβ”€β”€ πŸ“ backend/              # FastAPI application
β”‚   β”œβ”€β”€ app/                 # Main application code
β”‚   β”œβ”€β”€ alembic/            # Database migrations
β”‚   └── tests/              # Backend tests
β”œβ”€β”€ πŸ“ react-frontend/       # React TypeScript app
β”‚   β”œβ”€β”€ src/                # Frontend source code
β”‚   └── public/             # Static assets
β”œβ”€β”€ πŸ“ docs/                # πŸ“š Organized documentation
β”‚   β”œβ”€β”€ getting-started/    # New developer guides
β”‚   β”œβ”€β”€ development/        # Development workflows
β”‚   β”œβ”€β”€ deployment/         # Production guides
β”‚   β”œβ”€β”€ troubleshooting/    # Problem solving
β”‚   └── reference/          # Technical references
β”œβ”€β”€ πŸ“ scripts/             # πŸ› οΈ Utility scripts
β”‚   β”œβ”€β”€ dev/               # Development helpers
β”‚   β”œβ”€β”€ docker/            # Container operations
β”‚   β”œβ”€β”€ deployment/        # Release scripts
β”‚   β”‚   β”œβ”€β”€ production/    # Production deployment
β”‚   β”‚   └── release/       # Release automation
β”‚   └── database/          # DB management
β”œβ”€β”€ πŸ“„ docker-compose.dev.yml      # Development environment
β”œβ”€β”€ πŸ“„ docker-compose.test.yml     # Testing environment
└── πŸ“„ docker-compose.prod-test.yml # Production testing environment

🎯 Development Workflow

1. First Time Setup

# Follow the comprehensive setup guide
# This covers IDE setup, dependencies, and configuration
.\docs\development\DEVELOPER_SETUP.md

2. Daily Development

# Start development environment
docker-compose up -d

# Run backend tests
.\scripts\dev\run-tests.ps1

# Run frontend tests
cd react-frontend
npm test

# Access services:
# - Frontend: http://localhost:80
# - Backend: http://localhost:8000
# - API Docs: http://localhost:8000/docs

3. Before Deployment

# Validate production setup
.\scripts\docker\validate-config.ps1 -Validate

# Test production configuration locally
.\scripts\docker\test-production.ps1

# Create a new release
.\scripts\deployment\release\Create-Release.ps1 -Version v0.2.0

πŸ”§ Common Operations

Task Command Documentation
Start development docker-compose up -d Getting Started
Run backend tests .οΏ½scripts\dev\run-tests.ps1 Testing Guide
Run frontend tests cd react-frontend && npm test Frontend Testing
Security validation python backend/test/test_csrf_implementation.py Security Features
Deploy to production .οΏ½scripts\deployment\push-to-dockerhub.ps1 Deployment
Troubleshoot CORS .οΏ½scripts\docker\diagnose-cors.ps1 CORS Guide
Database migration .οΏ½scripts\database\migrate-db.ps1 DB Reference

πŸ§ͺ Integration Testing Environments

This project supports both Docker Compose-based and local integration testing for the backend.

  • Docker Compose-based testing (recommended for CI and team consistency):

    • Uses backend/.env.test for environment variables.
    • Database and Redis hostnames:
      • DATABASE_HOST=fastapi_rbac_db_test
      • REDIS_HOST=fastapi_rbac_redis_test
      • REDIS_URL=redis://fastapi_rbac_redis_test:6379/0
    • Run with:
      docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
  • Local testing (if you want to run tests outside Docker):

    • Uses backend/.env.test.local for environment variables.
    • Database and Redis hostnames:
      • DATABASE_HOST=localhost
      • REDIS_HOST=localhost
      • REDIS_URL=redis://localhost:6379/0
    • Make sure Postgres and Redis are running locally.
    • Run tests with:
      pytest backend/test/integration/

See comments in backend/.env.example for more details.

Running Tests with test_runner.py

All backend test running is now managed through a single script: backend/test_runner.py.

  • Run all tests:
    python backend/test_runner.py all
  • Run unit tests only:
    python backend/test_runner.py unit
  • Run integration tests only:
    python backend/test_runner.py integration
  • Run a specific test file:
    python backend/test_runner.py specific --path backend/test/unit/test_crud_user.py
  • Run the comprehensive demo suite:
    python backend/test_runner.py demo
  • Other options: See Test Runner Help for more.

Note: All previous test scripts (run_tests.py, run_comprehensive_tests.py, test_all_units.py, run_final_tests.py) have been removed. Use only test_runner.py for all test operations.


πŸ†˜ Need Help?

  1. πŸ” Check documentation: Start with Getting Started
  2. πŸ”§ Browse troubleshooting: See Troubleshooting Guides
  3. πŸ’¬ Ask the team: Create an issue with detailed information
  4. πŸ“– Read the code: Well-documented with inline comments

🀝 Contributing

  1. Read the Development Setup
  2. Follow the Coding Standards
  3. Write tests for new features
  4. Update documentation as needed

πŸ€– Copilot MCP Server Integration

This project is pre-configured to use the GitHub MCP server for Copilot Chat automation and repository management.

  • Workspace Scope: The .vscode/mcp.json file ensures this configuration only applies to this repository.
  • Getting Started: Open Copilot Chat in VS Code, switch to Agent mode, and select the "github" MCP server.
  • Example Prompts:
    • List all open issues in this repository
    • Create a new issue titled "Test MCP server connection"
    • Show all collaborators
    • List open pull requests
  • Best Practices:
    • Use clear, action-oriented prompts (e.g., "Create", "List", "Show").
    • Reference issues, PRs, or resources by number or name when possible.
    • Check the Copilot Chat tools menu for available actions.

For advanced usage and troubleshooting, see docs/development/MCP_SERVER.md.

Ready to get started? πŸ‘‰ Click here to begin!

Run the backend service

uvicorn app.main:app --port 8001 --reload

Frontend

# Navigate to frontend directory
cd react-frontend

# Install dependencies
npm install

# Run the development server
npm run dev

Docker-based Development

Run the entire stack:

# From project root
docker-compose up -d

Run only specific components:

# Just the backend
cd backend
docker-compose up -d

# Just the frontend
cd react-frontend
docker-compose up -d

Documentation

License

This project is licensed under the terms of the MIT license.

About

A production-ready Role-Based Access Control (RBAC) microservice with FastAPI backend and React TypeScript frontend. Features JWT authentication, CSRF protection, rate limiting, role hierarchies, permission groups, and comprehensive security. Includes Celery workers, Redis caching, Docker deployment, and 90+ backend + 354 frontend tests.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors