A complete teaching codebase for:
- HTTP Basic Authentication
- Opaque bearer-token authentication
- JSON Web Token authentication
- Server-side session authentication
- OAuth 2.0 Authorization Code flow with PKCE
- OpenID Connect login and Single Sign-On with Keycloak
| Username | Password | Role |
|---|---|---|
alice |
alice123 |
student |
admin |
admin123 |
admin |
| Username | Password | Role |
|---|---|---|
alice |
alice123 |
student |
adminuser |
admin123 |
student, admin |
The passwords are intentionally simple for a controlled classroom environment. Do not reuse them elsewhere.
python -m venv .venvActivate it:
Windows PowerShell
.venv\Scripts\Activate.ps1macOS or Linux
source .venv/bin/activateInstall dependencies:
python -m pip install -r requirements.txtOptional environment file:
cp .env.example .envOn Windows, copy .env.example to .env using File Explorer or:
Copy-Item .env.example .envpython run_core.pyOpen http://localhost:5000.
Import postman/Authentication_Labs.postman_collection.json into Postman.
Docker Desktop must be running.
docker compose up -dWait until the Keycloak login page is available at http://localhost:8080.
Keycloak admin console:
- Username:
admin - Password:
admin
The auth-lab realm, users, roles, and clients are imported automatically the first time the container starts.
Open two additional terminals with the virtual environment activated.
Terminal 1:
python run_sso_app_one.pyTerminal 2:
python run_sso_app_two.pyOpen:
- University Portal:
http://localhost:5001 - Library Portal:
http://localhost:5002
Log into one application with alice / alice123. Then open the other application and click login. Keycloak should reuse the central login session.
In Postman's OAuth 2.0 configuration, use:
- Grant type: Authorization Code with PKCE
- Callback URL:
https://oauth.pstmn.io/v1/callback - Authorization URL:
http://localhost:8080/realms/auth-lab/protocol/openid-connect/auth - Access Token URL:
http://localhost:8080/realms/auth-lab/protocol/openid-connect/token - Client ID:
postman-client - Client authentication: Send client credentials in body or no client secret (public client)
- Scope:
openid profile email - Code challenge method:
SHA-256
pytest -qThe tests cover Basic authentication, opaque-token issuance and revocation, JWT validation and role authorization, and session login/logout.
The realm is imported at startup. To recreate the container from the supplied realm file:
docker compose down
docker compose up -dThe current compose file does not attach a persistent data volume, so recreating the container returns it to the imported classroom configuration.
This project is intentionally designed for local education. Before production use, add TLS, a real database, secret management, CSRF protection for state-changing browser routes, rate limiting, audit logging, hardened cookie settings, secure key rotation, and deployment behind a production WSGI server.