diff --git a/Keycloak-installation.md b/Keycloak-installation.md new file mode 100644 index 0000000..4f8b508 --- /dev/null +++ b/Keycloak-installation.md @@ -0,0 +1,176 @@ +# Keycloak SSO Integration + +This document describes how Keycloak SSO was integrated into labhelper, replacing the built-in Django username/password authentication. + +## Overview + +Authentication is handled via OpenID Connect (OIDC) using the `mozilla-django-oidc` library. When a user visits any protected page, they are redirected to Keycloak to authenticate. On return, Keycloak group memberships are synced to Django groups, controlling what the user can do in the app. + +--- + +## Keycloak Setup + +### 1. Create a client + +In the Keycloak admin console, go to **Clients → Create client**. + +| Field | Value | Why | +|---|---|---| +| Client type | OpenID Connect | The protocol mozilla-django-oidc speaks | +| Client ID | `labhelper` | Must match `OIDC_RP_CLIENT_ID` in the app | +| Client authentication | On (confidential) | Server-side apps use a client secret; this is more secure than a public client | +| Authentication flow | Standard flow only | labhelper uses the standard authorisation code flow | + +After saving, go to the **Credentials** tab and copy the **Client secret** — this is `OIDC_RP_CLIENT_SECRET`. + +### 2. Set the redirect URI + +In the client **Settings** tab: + +| Field | Value | Why | +|---|---|---| +| Valid redirect URIs | `https://your-app/oidc/callback/` | Keycloak will only redirect back to whitelisted URIs after authentication. The trailing slash is required — without it Keycloak rejects the request. For local dev also add `http://127.0.0.1:8000/oidc/callback/` | + +### 3. Set the PKCE code challenge method + +In the client **Advanced** tab, find **"Proof Key for Code Exchange Code Challenge Method"** and set it to **S256**. + +Why: Keycloak 26 configures new clients with PKCE enforced. `mozilla-django-oidc` sends `S256` as the challenge method (the more secure option). If Keycloak is set to `plain`, the two sides don't agree and authentication fails with `code challenge method is not matching the configured one`. + +### 4. Add a Groups mapper + +This makes Keycloak include the user's group memberships in the token so the app can sync them to Django groups. + +Go to **Clients → labhelper → Client scopes** tab → click the dedicated scope (named `labhelper-dedicated`) → **Add mapper → By configuration → Group Membership**. + +| Field | Value | Why | +|---|---|---| +| Name | `groups` | Label for this mapper | +| Token Claim Name | `groups` | The claim name the app reads from the token | +| Full group path | Off | Sends `Lab Administrators` instead of `/Lab Administrators`. The app strips leading slashes anyway, but this is cleaner | +| Add to ID token | On | | +| Add to access token | On | | +| Add to userinfo | On | The app fetches userinfo after the token exchange | + +### 5. Create groups + +Go to **Groups** (left sidebar) and create these three groups with exactly these names — they map to the existing Django groups: + +- `Lab Administrators` — gets `is_staff=True` in Django (admin access) +- `Lab Staff` +- `Lab Viewers` + +### 6. Ensure users have an email address + +`mozilla-django-oidc` requires the `email` claim to be present in the token. Every Keycloak user who will log into labhelper must have: + +- An **Email** address set (Users → select user → Details tab) +- **Email verified** ticked + +Without an email, authentication fails silently with `Claims verification failed` in the Django logs. + +--- + +## App Configuration + +The app is configured entirely via environment variables. + +### Required variables + +```bash +OIDC_OP_BASE_URL=https://keycloak.example.com/realms/your-realm +OIDC_RP_CLIENT_ID=labhelper +OIDC_RP_CLIENT_SECRET= +``` + +`OIDC_OP_BASE_URL` is the realm URL. All OIDC endpoints (authorisation, token, userinfo, JWKS, logout) are derived from it automatically in `settings.py`. + +### Other relevant variables + +```bash +ALLOWED_HOSTS=your-app-hostname +CSRF_TRUSTED_ORIGINS=https://your-app +``` + +`CSRF_TRUSTED_ORIGINS` must include the app's origin. The OIDC callback goes through Django's CSRF middleware, and requests from untrusted origins are rejected. + +--- + +## How the App Side Works + +### Library + +`mozilla-django-oidc` handles the full OIDC flow: redirecting to Keycloak, validating the returned token (RS256 signature verified against Keycloak's JWKS endpoint), exchanging the authorisation code, and fetching userinfo. + +### Key settings + +| Setting | Value | Why | +|---|---|---| +| `OIDC_RP_SIGN_ALGO` | `RS256` | Keycloak signs tokens with RS256 by default | +| `OIDC_RP_SCOPES` | `openid email profile` | `profile` is needed to get `preferred_username`, `given_name`, `family_name` from Keycloak | +| `OIDC_USE_PKCE` | `True` | Required because Keycloak enforces PKCE on this client | +| `OIDC_STORE_ID_TOKEN` | `True` | The ID token is stored in the session and passed as `id_token_hint` when logging out, so Keycloak also ends its own session | +| `OIDC_EXEMPT_URLS` | `['search_api']` | The search endpoint is called via AJAX. The `SessionRefresh` middleware would return a redirect instead of JSON for unauthenticated AJAX calls, breaking the UI | +| `LOGIN_URL` | `oidc_authentication_init` | When `@login_required` intercepts an unauthenticated request, it redirects directly to the OIDC flow rather than a local login form | + +### Authentication backend (`labhelper/auth_backend.py`) + +Overrides `OIDCAuthenticationBackend` to: + +- Use `preferred_username` from Keycloak as the Django username +- Set `first_name` and `last_name` from `given_name` / `family_name` claims +- Sync group memberships on every login — if a user is added to or removed from a Keycloak group, it takes effect at their next login +- Set `is_staff=True` for members of `Lab Administrators` (grants Django admin access) + +`django.contrib.auth.backends.ModelBackend` is kept as a fallback so the Django admin login form still works with a local username/password (useful for emergency superuser access without Keycloak). + +### Session refresh middleware + +`mozilla_django_oidc.middleware.SessionRefresh` is added after `AuthenticationMiddleware`. It periodically checks whether the user's OIDC session is still valid and forces re-authentication if the token has expired, rather than keeping a stale Django session alive indefinitely. + +### URLs + +All OIDC routes are mounted under `/oidc/`: + +| URL | Purpose | +|---|---| +| `/oidc/authenticate/` | Initiates the OIDC flow, redirects to Keycloak | +| `/oidc/callback/` | Keycloak redirects here after authentication | +| `/oidc/logout/` | Logs out of Django and ends the Keycloak session | + +`/login/` is kept as a static landing page with a "Login with SSO" button, used when users navigate to it manually or are redirected there after logout. + +--- + +## Auth Flow + +``` +User visits protected page + ↓ +@login_required → redirect to /oidc/authenticate/?next=/original/url/ + ↓ +Redirect to Keycloak (with code_challenge for PKCE) + ↓ +User authenticates in Keycloak + ↓ +Keycloak redirects to /oidc/callback/?code=... + ↓ +App exchanges code for tokens, verifies RS256 signature + ↓ +App fetches userinfo, syncs groups and attributes + ↓ +User redirected to original URL +``` + +--- + +## Troubleshooting + +| Error | Cause | Fix | +|---|---|---| +| `Invalid parameter: redirect_uri` | Redirect URI not in Keycloak whitelist, or trailing slash missing | Add exact URI including trailing slash to Keycloak client Valid Redirect URIs | +| `Missing parameter: code_challenge_method` | Keycloak requires PKCE but app wasn't sending it | Set `OIDC_USE_PKCE = True` in settings | +| `code challenge method is not matching the configured one` | Keycloak client set to `plain`, app sends `S256` | Set PKCE method to `S256` in Keycloak client Advanced settings | +| `Claims verification failed` | User has no email set in Keycloak | Set email address and tick Email Verified on the Keycloak user | +| `NoReverseMatch` for `OIDC_EXEMPT_URLS` | Regex pattern used instead of URL name | Use the Django URL name (`'search_api'`), not a regex | +| Login loops without showing Keycloak | Existing Keycloak session auto-authenticates | Expected behaviour — Keycloak reuses its session. Log out of Keycloak admin console to test a clean login | diff --git a/labhelper/auth_backend.py b/labhelper/auth_backend.py new file mode 100644 index 0000000..f72c422 --- /dev/null +++ b/labhelper/auth_backend.py @@ -0,0 +1,54 @@ +from mozilla_django_oidc.auth import OIDCAuthenticationBackend +from django.contrib.auth.models import Group + +# Keycloak group name → Django group name mapping. +# Keycloak may send group paths with a leading slash (e.g. "/Lab Administrators"); +# these are stripped before comparison. +KEYCLOAK_GROUP_MAP = { + 'Lab Administrators': 'Lab Administrators', + 'Lab Staff': 'Lab Staff', + 'Lab Viewers': 'Lab Viewers', +} + +# Members of these groups receive is_staff=True (Django admin access) +STAFF_GROUPS = {'Lab Administrators'} + + +class KeycloakOIDCBackend(OIDCAuthenticationBackend): + """OIDC backend that maps Keycloak groups to Django groups on every login.""" + + def get_username(self, claims): + return claims.get('preferred_username') or super().get_username(claims) + + def create_user(self, claims): + user = super().create_user(claims) + self._sync_from_claims(user, claims) + return user + + def update_user(self, user, claims): + user = super().update_user(user, claims) + self._sync_from_claims(user, claims) + return user + + def _sync_from_claims(self, user, claims): + """Sync user attributes and group memberships from Keycloak token claims.""" + user.first_name = claims.get('given_name', user.first_name) + user.last_name = claims.get('family_name', user.last_name) + + # Keycloak sends group paths like "/Lab Administrators"; normalise them. + raw_groups = claims.get('groups', []) + keycloak_groups = {g.lstrip('/') for g in raw_groups} + + user.is_staff = bool(keycloak_groups & STAFF_GROUPS) + user.save() + + # Add/remove the user from each managed Django group to match Keycloak. + for kc_group, django_group_name in KEYCLOAK_GROUP_MAP.items(): + try: + group = Group.objects.get(name=django_group_name) + except Group.DoesNotExist: + continue + if kc_group in keycloak_groups: + user.groups.add(group) + else: + user.groups.remove(group) diff --git a/labhelper/settings.py b/labhelper/settings.py index 8ba9855..3ccd0ff 100644 --- a/labhelper/settings.py +++ b/labhelper/settings.py @@ -39,6 +39,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'mozilla_django_oidc', 'mptt', 'django_mptt_admin', 'sorl.thumbnail', @@ -52,6 +53,7 @@ MIDDLEWARE = [ 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'mozilla_django_oidc.middleware.SessionRefresh', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] @@ -137,8 +139,44 @@ MEDIA_ROOT = BASE_DIR / 'data' / 'media' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' -CSRF_TRUSTED_ORIGINS=os.environ.get('CSRF_TRUSTED_ORIGINS', 'https://labhelper.adebaumann.com').split(',') +CSRF_TRUSTED_ORIGINS=os.environ.get('CSRF_TRUSTED_ORIGINS', 'https://labhelper.adebaumann.com,http://127.0.0.1:8000').split(',') -LOGIN_URL = os.environ.get('LOGIN_URL', 'login') +LOGIN_URL = os.environ.get('LOGIN_URL', 'oidc_authentication_init') LOGIN_REDIRECT_URL = os.environ.get('LOGIN_REDIRECT_URL', 'index') LOGOUT_REDIRECT_URL = os.environ.get('LOGOUT_REDIRECT_URL', 'login') + +AUTHENTICATION_BACKENDS = [ + 'labhelper.auth_backend.KeycloakOIDCBackend', + # ModelBackend kept as fallback for Django admin emergency access + 'django.contrib.auth.backends.ModelBackend', +] + +# --------------------------------------------------------------------------- +# Keycloak / OIDC configuration +# +# Set OIDC_OP_BASE_URL to your realm URL, e.g.: +# https://keycloak.example.com/realms/myrealm +# +# All individual endpoints are derived from OIDC_OP_BASE_URL automatically. +# You can override any individual endpoint with its own env var. +# --------------------------------------------------------------------------- +_oidc_base = "http://127.0.0.1:8080/realms/master" +_oidc_connect = f'{_oidc_base}/protocol/openid-connect' if _oidc_base else '' + +OIDC_RP_CLIENT_ID = "labhelper" +OIDC_RP_CLIENT_SECRET = "NnDDaJfbQlBSHV1z1H2cCiaubLyuQcgY" +OIDC_RP_SIGN_ALGO = 'RS256' +OIDC_RP_SCOPES = 'openid email profile' +OIDC_USE_PKCE = True + +OIDC_OP_AUTHORIZATION_ENDPOINT = os.environ.get('OIDC_OP_AUTHORIZATION_ENDPOINT', f'{_oidc_connect}/auth') +OIDC_OP_TOKEN_ENDPOINT = os.environ.get('OIDC_OP_TOKEN_ENDPOINT', f'{_oidc_connect}/token') +OIDC_OP_USER_ENDPOINT = os.environ.get('OIDC_OP_USER_ENDPOINT', f'{_oidc_connect}/userinfo') +OIDC_OP_JWKS_ENDPOINT = os.environ.get('OIDC_OP_JWKS_ENDPOINT', f'{_oidc_connect}/certs') +OIDC_OP_LOGOUT_ENDPOINT = os.environ.get('OIDC_OP_LOGOUT_ENDPOINT', f'{_oidc_connect}/logout') + +# Store the ID token in the session so Keycloak logout can use id_token_hint +OIDC_STORE_ID_TOKEN = True + +# Exempt AJAX endpoints from the session-refresh middleware redirect +OIDC_EXEMPT_URLS = ['search_api'] diff --git a/labhelper/templates/base.html b/labhelper/templates/base.html index 52425fa..4c02860 100644 --- a/labhelper/templates/base.html +++ b/labhelper/templates/base.html @@ -33,7 +33,7 @@ Resources Fixme Admin -
+ {% csrf_token %}
diff --git a/labhelper/templates/login.html b/labhelper/templates/login.html index 011dc9d..c19eae8 100644 --- a/labhelper/templates/login.html +++ b/labhelper/templates/login.html @@ -9,70 +9,24 @@ {% endblock %} {% block content %} -
- {% if form.errors %} -
- Your username and password didn't match. Please try again. +
+ {% if request.GET.next and user.is_authenticated %} +
+ Your account doesn't have access to this page.
- {% endif %} - - {% if next %} - {% if user.is_authenticated %} -
- Your account doesn't have access to this page. To proceed, - please login with an account that has access. -
- {% else %} -
{% endblock %} - -{% block extra_js %} - -{% endblock %} diff --git a/labhelper/urls.py b/labhelper/urls.py index ed99266..f9c6ecc 100644 --- a/labhelper/urls.py +++ b/labhelper/urls.py @@ -17,8 +17,8 @@ Including another URLconf from django.conf import settings from django.conf.urls.static import static from django.contrib import admin -from django.urls import path -from django.contrib.auth import views as auth_views +from django.urls import include, path +from django.views.generic import TemplateView from boxes.views import ( add_box, @@ -40,8 +40,8 @@ from boxes.views import ( ) urlpatterns = [ - path('login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'), - path('logout/', auth_views.LogoutView.as_view(), name='logout'), + path('oidc/', include('mozilla_django_oidc.urls')), + path('login/', TemplateView.as_view(template_name='login.html'), name='login'), path('', index, name='index'), path('box-management/', box_management, name='box_management'), path('box-type/add/', add_box_type, name='add_box_type'), diff --git a/requirements.txt b/requirements.txt index 16deb09..02f606a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,3 +37,4 @@ sorl-thumbnail==12.11.0 bleach==6.1.0 coverage==7.6.1 whitenoise==6.8.2 +mozilla-django-oidc==4.0.1