Files
term_portal/dev/authentik/README.md
T

158 lines
11 KiB
Markdown

# Authentik — throwaway local OIDC Identity Provider
This directory holds a throwaway [Authentik](https://goauthentik.io/) instance, added to the dev
Docker Compose stack purely for **local testing of SSO/OIDC login**. It stands in for the single
real external identity federation the portal integrates with — the **University of Ljubljana**
(itself backed by Microsoft Entra ID) — which in the portal's schema is the one non-`local` value
of the `auth_provider` enum: `uni_lj`.
This is infrastructure/dev-setup only. For the bigger picture of running the portal locally, see
the main [README](../../README.md). **Wiring the express app itself up as an OIDC client against
this (or the real University of Ljubljana) is a separate, later task** — nothing here touches
`express/`.
> **Note:** This Authentik instance, its database, and every seeded user are throwaway. Nothing
> here is meant to be backed up, migrated, or taken into production.
## What's here
- `blueprints/uni-lj-stand-in.yaml` — a declarative [Authentik blueprint](https://docs.goauthentik.io/customize/blueprints/)
that, on every startup, seeds: one OIDC Provider + Application
(`Univerza v Ljubljani (test stand-in)`), two custom claim (scope) mappings restricted to the
business-relevant claims the University of Ljubljana/Entra actually sends — `email`, `name`,
`preferred_username` on the ID token, `given_name`, `family_name`, `picture` via `/userinfo`
(plus `sub`, which authentik always includes automatically) — and three test users (see below).
## Bringing it up
This is behind the `develop-idp` Docker Compose profile, so it's off by default:
docker compose --profile develop-idp up -d
(Combine with other profiles as needed, e.g. `docker compose --profile develop-idp --profile develop-concordancer up -d --build` if you're also working on the concordancer.)
First startup takes a little longer than the rest of the stack: Authentik runs its own database
migrations and then applies the blueprint in `blueprints/`. Watch it with:
docker compose logs -f authentik-server authentik-worker
> **Note:** the blueprint is re-applied automatically roughly every 60 minutes, on container
> restart, and whenever the blueprint file changes — so if you ever poke at a seeded provider,
> application, mapping, or user through the admin UI, expect your changes to be silently reverted.
> Treat that as a feature: it's the reset mechanism (see _Resetting test data_ below).
## Reaching the admin UI
Once up, the Authentik admin UI is at `http://localhost:${AUTHENTIK_LISTEN_PORT}` (default
`9000`, from the root `.env`) — e.g. `http://localhost:9000/if/admin/`.
Log in with the bootstrap admin account, created automatically on first startup from the root
`.env`:
| | value (from `.env`) |
| -------- | ------------------------------------------------------------------------ |
| Email | `AUTHENTIK_BOOTSTRAP_EMAIL` (default `admin@authentik.test`) |
| Password | `AUTHENTIK_BOOTSTRAP_PASSWORD` (default `weak_authentik_admin_password`) |
This account is Authentik's own superuser — unrelated to the portal's own
`PORTAL_ADMIN_INITIAL_EMAIL`/`PORTAL_ADMIN_INITIAL_PASSWORD` admin account, which is a completely
separate application.
## The registered test application
- **Application slug:** `uni-lj-test`
- **Redirect URI:** `http://localhost:${EXPRESS_LISTEN_PORT}/users/oidc/callback` (the portal's
planned generic OIDC callback route — provider selection is carried via session, not the URL)
- **Client ID / secret:** `AUTHENTIK_OIDC_CLIENT_ID` / `AUTHENTIK_OIDC_CLIENT_SECRET` in `.env`
(fixed, non-secret dev defaults — safe to commit, same pattern as `EXPRESS_SECRET`)
- **Scopes to request:** `openid email profile`
- **Discovery document:**
- from your host/browser: `http://localhost:${AUTHENTIK_LISTEN_PORT}/application/o/uni-lj-test/.well-known/openid-configuration`
- from inside another container on the compose network (e.g. the future express OIDC client):
`http://authentik-server:9000/application/o/uni-lj-test/.well-known/openid-configuration`
These are two different hosts on purpose: the authorization/redirect legs happen in the user's
browser (needs the `localhost` URL), while token/JWKS/userinfo calls happen server-to-server
(can use the internal `authentik-server` compose hostname). The future express-side OIDC client
task will need to account for this split.
Only one provider/application is registered, matching the single `uni_lj` value in the
portal's `auth_provider` enum — this is not meant to model multiple external IdPs.
## Test users
All seeded users share the password **`IdpTest123!`** (log in with it on Authentik's own login
page during the OIDC redirect flow — this is _not_ a portal password).
| Authentik username | Claims sent (ID token) / userinfo | Portal-side scenario it exercises |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ana.novak` | `name="Novak, Ana"`, `email=ana.novak@ff.uni-lj.si`, `preferred_username=novaka@ff.uni-lj.si` / userinfo: `given_name=Ana`, `family_name=Novak`, `picture=<Graph API constant>` | **(A) Brand new user, all claims present.** No pre-existing local account needed — log in via SSO, review the read-only data on `/users/create-profile`, and confirm to create the new portal account exactly as shown (nothing on that page is editable). |
| `berta.kolar.idp` | `name="Kolar, Berta"`, `email=berta.kolar@fri.uni-lj.si`, `preferred_username=kolarb@fri.uni-lj.si` / userinfo: `given_name=Berta`, `family_name=Kolar`, `picture=<Graph API constant>` | **(B) Email matches an existing LOCAL account → account conversion.** See below — you must register a matching local account first. Confirming with that account's password permanently overwrites its username/first/last name with the IdP data and removes its password — sign-in from then on is SSO-only. |
| `brez.imena` | `email=brez.imena@fri.uni-lj.si`, `preferred_username=brez.imena@fri.uni-lj.si` — **`given_name`/`family_name`/`name` absent** | **(C) Missing given_name/family_name claims → account creation/conversion is refused.** The claims are entirely absent from the ID token/userinfo response (not blank strings) — since there's nothing to review or persist, the portal shows a clear error and never renders a create/convert page. |
> **Note on claim placement:** the real University of Ljubljana/Entra ID token never carries
> `given_name`/`family_name`/`picture` — only the `/userinfo` endpoint does (`name` and
> `preferred_username`, by contrast, _are_ on the real ID token). Authentik has no per-field way to
> split claims between the ID token and userinfo response — a single provider-level toggle either
> includes everything in both, or restricts the ID token to bare-minimum standard fields (which
> would also strip `email`/`preferred_username`, breaking parity there). This blueprint leaves that
> toggle at its default (`include_claims_in_id_token: true`), so Authentik's ID token ends up a
> harmless superset of the real one — it also carries `given_name`/`family_name`/`picture`. This is
> intentional and documented, not a bug: the app code (`express/middleware/auth.js`) only ever reads
> `given_name`/`family_name` from the `/userinfo` response, never from ID-token claims, so the extra
> keys in Authentik's ID token are inert.
> **Note:** there is no seeded/default local user in this app's dev DB — registration is always
> manual, via the normal `/register` flow. So for scenario B, before testing SSO login as
> `berta.kolar.idp`, first manually register a local portal account with email exactly
> `berta.kolar@fri.uni-lj.si` (any other field can be anything you like), then log in via SSO as
> `berta.kolar.idp` and confirm the portal lands on `/users/convert-account`, showing that local
> account's current username alongside the (locked) IdP-derived data. Confirming with that local
> account's password converts it: username/first/last name are overwritten with the IdP data, its
> password is removed, and a subsequent local-password login attempt should fail with the usual
> "created via University of Ljubljana, no password" message.
## Troubleshooting
> **Note:** On a genuinely first-ever startup (empty `authentik-postgres-data` volume), the custom
> blueprint's very first apply attempt can race against Authentik's own built-in self-signed
> certificate being created (the blueprint's OIDC provider references that certificate as its
> signing key) — if the blueprint applies first, that one attempt fails and the provider ends up
> without a working configuration. Authentik retries blueprints roughly every 60 minutes on its
> own, but you don't need to wait: just re-apply it manually once, which always succeeds once the
> certificate exists (by then, well under a minute after startup):
>
> docker compose --profile develop-idp exec authentik-worker ak apply_blueprint /blueprints/custom/uni-lj-stand-in.yaml
>
> You can confirm whether this happened via the admin UI (System → Blueprints) or the API:
>
> curl -s -H "Authorization: Bearer ${AUTHENTIK_BOOTSTRAP_TOKEN}" \
> "http://localhost:${AUTHENTIK_LISTEN_PORT}/api/v3/managed/blueprints/?search=Ljubljana" \
> | grep -o '"status": *"[a-z]*"'
>
> A `"successful"` status means it's fine; `"error"` means run the manual re-apply above.
## Resetting test data
Because the blueprint uses `state: present`, Authentik reconciles all of the above back to these
exact values automatically (on restart, on blueprint file changes, and roughly every 60 minutes
regardless). So for most purposes, just wait or restart:
docker compose --profile develop-idp restart authentik-server authentik-worker
For a full wipe (new admin bootstrap, fresh database, blueprint re-applied from scratch):
docker compose --profile develop-idp down
docker volume rm terminoloski_portal_authentik-postgres-data terminoloski_portal_authentik-data
docker compose --profile develop-idp up -d
(Adjust the volume name prefix if your Compose project name differs from the directory name.)
## Tearing it down
docker compose --profile develop-idp down
This only stops/removes the `authentik-postgres`, `authentik-server` and `authentik-worker`
containers — it never touches the portal's own `postgres`/`express`/etc. services or data.