This project demonstrates how to integrate FastAPI with Microsoft Authentication Library (MSAL) to enable Azure AD SSO authentication.
fastapi-msal/
├── app/
│ ├── api/
│ │ └── v1/
│ │ ├── __init__.py
│ │ └── api.py
│ ├── auth/
│ │ └── __init__.py
│ ├── core/
│ │ ├── __init__.py
│ │ └── config.py
│ ├── db/
│ │ └── __init__.py
│ ├── models/
│ │ └── __init__.py
│ ├── schemas/
│ │ └── __init__.py
│ ├── tests/
│ │ ├── __init__.py
│ │ ├── conftest.py
│ │ └── test_setup.py
│ ├── __init__.py
│ └── main.py
├── .env.example
├── .gitignore
├── requirements.txt
└── README.md
- Python 3.9 or higher
- Azure AD tenant with admin access (for creating application registration)
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Copy the
.env.examplefile to.env:cp .env.example .env
-
Update the
.envfile with your Azure AD application details and other settings.
Start the development server:
uvicorn app.main:app --reloadThe API will be available at http://localhost:8000
API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Run the tests using pytest:
pytest- FastAPI application with API versioning
- Azure AD SSO authentication via MSAL
- CORS configuration
- Configuration using Pydantic v2 Settings
- Logging setup
- Health check endpoints