A Flask web application demonstrating Identity Provider (IdP) integration with Keycloak for Single Sign-On (SSO) authentication to demonstrate a secure, centralised IdP platform designed for organisations (schools, government, businesses) to manage student and teacher identities. The platform will serve as a trusted authentication and authorisation system, allowing users to seamlessly access various educational tools and services while ensuring compliance with privacy regulations and data residency laws.
- Using environment variables for secrets
- Implementing proper SSL/TLS
- Setting up secure cookie configurations
- Adding CSRF protection
- User registration and login via Keycloak
- OpenID Connect (OIDC) integration
- Session-based authentication with JWT tokens
- Form validation with WTForms
- Dockerized development environment
- Flask Application: Web server running on port 5000
- Keycloak: Identity provider running on port 7080
- Docker Compose: Orchestrates both services with health checks
- Docker and Docker Compose
- Python 3.13+ (for local development)
-
Clone the repository:
git clone git@github.com:Siyavula/idp-demo.git cd idp-demo
-
Start the services:
docker-compose up --build
-
Wait for Keycloak to be healthy (check logs), then access:
- Flask app: http://localhost:5000
- Keycloak admin: http://localhost:7080 (admin/admin)
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
flask --app app run --debug
The application auto-configures the Keycloak client on startup. Key settings in app/__init__.py
:
- SERVER_URL: Keycloak server URL
- CLIENT_ID: OIDC client identifier
- CLIENT_SECRET: Client authentication secret
- REALM_NAME: Keycloak realm (default: master)
app/
├── __init__.py # Flask app factory
├── auth.py # Authentication blueprint
├── home.py # Home page blueprint
├── forms.py # WTForms validation
├── utils.py # Keycloak utility functions
├── models/
│ └── user.py # User model with Keycloak integration
├── templates/ # Jinja2 templates
├── static/ # CSS and static assets
└── config/
└── oid_client_config.json # OIDC client configuration
- Users register/login through Flask forms
- Credentials are validated against Keycloak
- Successful authentication returns JWT tokens
- Tokens are stored in session and cookies
- Protected routes check for valid sessions