Skip to content

Authorization for FastAPI using Microsoft Azure Active Directory

License

Notifications You must be signed in to change notification settings

alan-turing-institute/fastapimsal

 
 

Repository files navigation

FastAPI Auth

Authenticate users with FastAPI using the Microsoft MSAL Library and Azure Active Directory.

Getting started

Python dependencies

To get started install Poetry.

Then ensure all dependencies are installed:

poetry install

Pre-commit

Run to make CI-tests pass

poetry run pre-commit run --all-files

Note: SAFETY_API_KEY environment variable needs to be set to run the pre-commit hooks. Create an account with the Safety package and then navigate to Organization > API keys to fetch the key. Once the key has been obtained, set the variable in your terminal using:

export SAFETY_API_KEY=your-api-key

Examples

See examples/app.py for a simple example.

Create a .auth.env file:

echo "session_expire_time_minutes=1
session_secret=<Session-Cookie-Secret>
client_id=<Application-client-id>
client_secret=<Application-Client-secret>
tenant_id=<Tenant-id" > .auth.env

For the session_secret its a good idea to create a secret with openssl rand -hex 32

To run the example

poetry run uvicorn --reload --app-dir examples app:app --reload-dir examples

About

Authorization for FastAPI using Microsoft Azure Active Directory

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%