cleanenergyexchange / fastapi-zitadel-auth

Python code example for FastAPI using Zitadel + Authorization Code Flow with PKCE and JWKS
MIT License
1 stars 0 forks source link

fastapi-zitadel-auth

Python example to protect FastAPI endpoints using Zitadel.

Details:

[!WARNING] This repo is a work in progress and should not be used in production just yet.

Zitadel setup

Project

App 1: API

App 2: User Agent

User creation

Service User creation

FastAPI setup

Copy the .env.example file to .env and fill in the values above.

Run with uv

uv run src/main.py

Alternatively, use classic venv:

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python src/main.py

Swagger UI

Open http://localhost:8001/docs in a new browser window, click on the "Authorize" button, log in, and then access the private endpoint in the Swagger UI.

Service User

While the server is running, in another terminal, run the src/service_user.py script to authenticate the service user. Make sure to have the SERVICE_USER_PRIVATE_KEY_FILE set in the .env file (see above).

uv run src/service_user.py

Credits:

Partly inspired by Intility/fastapi-azure-auth (as of v5).