This repository demonstrates how to integrate OpenID Connect authentication into a Python Flask application using Keycloak as the identity provider.
It walks you through:
- Setting up a Keycloak server with Docker
- Registering an OpenID client
- Building and organizing a scalable Flask application
- Implementing a secure OpenID Connect login flow
- Retrieving user information
- Implementing logout and route protection
- Python 3.11+
- Docker + Docker Compose
-
Clone the repository:
git clone https://github.com/Ariovis-fr/open-id-tutorial.git cd open-id-tutorial
-
Start Keycloak using Docker:
docker-compose up
This launches a Keycloak instance accessible at
http://localhost:8080
. -
Set up your environment variables:
Create a
.env
file at the root of the project:OIDC_CLIENT_ID=your-client-id OIDC_CLIENT_SECRET=your-client-secret OIDC_AUTHORITY=http://localhost:8080/realms/your-realm REDIRECT_URI=http://localhost:8081/auth/signin SECRET_KEY=your-secret-key
-
Install dependencies:
pip install -r requirements.txt
flask --app app run --debug --port=8081
Visit http://localhost:8081 in your browser.
.
├── app
│ ├── auth
│ ├── extensions
│ ├── main
│ ├── static
│ └── templates
├── config.py
├── docker-compose.yaml
├── .env
└── README.md
- OpenID Connect authentication using Keycloak
- Session management with Flask-Session
- User information display
- Secure login & logout
- Route protection with decorators
- Modular Flask app structure
- Navigate to http://localhost:8081/auth/login
- Log in via Keycloak
- Return to your Flask app with authenticated user data
This repository accompanies the blog article:
How to Connect a Python Flask App to Keycloak with OpenID Connect