Skip to content

Build your personal knowledge base with Trilium Notes

chore(oauth): add Authelia sample for local OpenID Connect testing

Elian Doran1a85e8e425fe
diff --git a/apps/server/docker/authelia/README.md b/apps/server/docker/authelia/README.md
new file mode 100644
index 0000000000..c4d517c0d6
--- /dev/null
+++ b/apps/server/docker/authelia/README.md
@@ -0,0 +1,101 @@
+# Authelia (for testing OpenID Connect / SSO login)
+
+A throwaway [Authelia](https://www.authelia.com/) OIDC provider for testing Trilium's
+OpenID Connect login locally — and for reproducing
+[issue #6387](https://github.com/TriliumNext/Trilium/issues/6387) (Trilium not reading
+`name`/`email` from the UserInfo endpoint when a spec-compliant provider keeps them out
+of the ID token).
+
+> **DEV ONLY.** Every secret in `config/` is a public throwaway. Never reuse this for anything real.
+
+## Why a hosts entry is needed
+
+Unlike a plain reverse proxy, OIDC shares one **issuer URL** between two callers: your
+**browser** (which gets redirected to Authelia to log in) and the **Trilium server**
+(which calls Authelia's token + userinfo endpoints over the back channel). Both must reach
+Authelia at the same name. Authelia also refuses a bare `localhost` session-cookie domain
+(it has no dot), so the portal runs on `auth.example.com`.
+
+Add this line to your hosts file (`C:\Windows\System32\drivers\etc\hosts` on Windows,
+`/etc/hosts` on Linux/macOS — needs admin):
+
+```
+127.0.0.1 auth.example.com
+```
+
+Trilium's own redirect target stays on `localhost:8080` (a loopback host, which is the only
+way Authelia will accept an `http://` redirect URI), so no entry is needed for it.
+
+Authelia 4.39 also **requires HTTPS** for its portal URL, so it's served with a self-signed
+cert for `auth.example.com` (in `config/certs/`). Your browser will warn the first time —
+click through. Trilium's back channel must trust it too; see step 2.
+
+## Quick start
+
+1. Start Authelia:
+ ```bash
+ docker compose up -d
+ # tail logs to confirm it booted: docker compose logs -f authelia
+ ```
+ The portal is at **http://auth.example.com:9091** (login: `testuser` / `authelia`).
+
+2. Start Trilium on the host (default port 8080) with these env vars set:
+ ```bash
+ TRILIUM_OAUTH_BASE_URL=http://localhost:8080
+ TRILIUM_OAUTH_CLIENT_ID=trilium
+ TRILIUM_OAUTH_CLIENT_SECRET=insecure_secret
+ TRILIUM_OAUTH_ISSUER_BASE_URL=https://auth.example.com:9091
+ TRILIUM_OAUTH_ISSUER_NAME=Authelia
+ TRILIUM_OAUTH_ISSUER_ICON=https://auth.example.com:9091/api/oidc/consent/logo
+ # So Node trusts the self-signed Authelia cert on the back channel (token/userinfo calls):
+ NODE_EXTRA_CA_CERTS=apps/server/docker/authelia/config/certs/public.crt
+ ```
+ For `pnpm run server:start`, export them first. PowerShell:
+ ```powershell
+ $env:TRILIUM_OAUTH_CLIENT_ID = 'trilium'
+ $env:NODE_EXTRA_CA_CERTS = "$PWD\apps\server\docker\authelia\config\certs\public.crt"
+ # ...etc
+ ```
+ (Quick-and-dirty alternative to `NODE_EXTRA_CA_CERTS`: `NODE_TLS_REJECT_UNAUTHORIZED=0` —
+ disables TLS verification globally, dev only.)
+
+3. In Trilium, open **Options → MFA**, choose **OpenID Connect** as the method, then enroll:
+ the owner must already be signed in (password) before binding the SSO identity — see
+ `afterCallback` in [apps/server/src/services/open_id.ts](../../src/services/open_id.ts).
+
+4. Log out and sign in via the **Authelia** button. The browser bounces through
+ `auth.example.com:9091` and back to `localhost:8080/callback`.
+
+## Reproducing issue #6387 vs. the workaround
+
+- **Reproduce the bug (default):** the shipped config has **no** `claims_policy`, so Authelia
+ returns `name`/`email` only from the UserInfo endpoint. On `main`/released builds this throws
+ `Cannot read properties of undefined (reading 'toString')`; on the `feature/oauth_improvements`
+ branch it no longer crashes but enrolls with a **blank name/email** in settings.
+
+- **Test the Authelia-side workaround:** in [config/configuration.yml](config/configuration.yml),
+ uncomment the `claims_policy: 'trilium'` line on the client and the `claims_policies:` block,
+ then `docker compose restart authelia`. Now `name`/`email` ride in the ID token and the fields
+ populate. (You may need to clear cookies for `auth.example.com` between runs.)
+
+## Cleanup
+
+```bash
+docker compose down -v
+```
+
+## Notes
+
+- The argon2id password hash and all secrets (session/storage/HMAC, the inline OIDC JWKS
+ key, and the self-signed TLS cert in `config/certs/`) are well-known Authelia dev samples /
+ freshly generated throwaways — fine to commit, useless to an attacker.
+- Regenerate the TLS cert with (run from this folder):
+ ```bash
+ openssl req -x509 -nodes -newkey rsa:2048 \
+ -keyout config/certs/private.key -out config/certs/public.crt \
+ -days 3650 -subj "/CN=auth.example.com" \
+ -addext "subjectAltName=DNS:auth.example.com"
+ ```
+ (Git Bash on Windows mangles `-subj`; prefix the command with `MSYS_NO_PATHCONV=1`.)
+- Two harmless startup warnings are expected: the plaintext `client_secret` deprecation
+ notice and "no access_control rules ... default_policy 'one_factor'".
diff --git a/apps/server/docker/authelia/config/certs/private.key b/apps/server/docker/authelia/config/certs/private.key
new file mode 100644
index 0000000000..3d37f8195b
--- /dev/null
+++ b/apps/server/docker/authelia/config/certs/private.key
@@ -0,0 +1,28 @@
+-----BEGIN PRIVATE KEY-----
+MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCnwl4qLcX7K+nT
+wifFFSnbo40YaGMYVh5BAAies/VzypoPXr07nHfQu7d6u8YOemczgLJa4OVSFnOL
+cVH2EK+ZaM5B/VdzEsUNDWfZuYNhoIKtDrYDFSS6NwBHHBHyvIff2KX5yvCGsA54
+bfTR1ReqSzmzYTh2SRCOzPtQsWgFv3NQXwZ5eXyuP96Db+siD+zIn2xZLOFv+VWp
+qVilDroKkfghcOSs1wZfUKhoE6MayRqa6SESDyE9cZcL2KzCd1/SuXcHjRANFGxx
+k3F+izntTyIpD5H72bXE9TlPYPpyn1Qwzv4tnSn9krzpEhvJDLiSxyGxmFBFR7Eo
+aqjqILuhAgMBAAECggEAAzr5coMvly3Qu/b2v7X6g0KzFh2hTglEqL8kEK9x0M84
+NVxaBTM8OtLuNlJWHsZ1qmgTnySqS8uX1t8phdp18YH99Jxjk26LXcw0rsx32qtY
+kCsqvx9TC0or+A09gqdlr9OkL2MXK7vJXTauU+mlhz4kDBJeypx5xvXXOiCpanC4
+h1/6pkeMzX5if4ejUm2xvVzzqkrRo4F4+fWh5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ia/config/certs/public.crt b/apps/server/docker/authelia/config/certs/public.crt
new file mode 100644
index 0000000000..435d4d6f1a
--- /dev/null
+++ b/apps/server/docker/authelia/config/certs/public.crt
@@ -0,0 +1,20 @@
+-----BEGIN CERTIFICATE-----
+MIIDNDCCAhygAwIBAgIUCqaB5GjOsgz/kJzXwC6L5XmiPT4wDQYJKoZIhvcNAQEL
+BQAwGzEZMBcGA1UEAwwQYXV0aC5leGFtcGxlLmNvbTAeFw0yNjA2MTgwNjQxMTha
+Fw0zNjA2MTUwNjQxMThaMBsxGTAXBgNVBAMMEGF1dGguZXhhbXBsZS5jb20wggEi
+MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCnwl4qLcX7K+nTwifFFSnbo40Y
+aGMYVh5BAAies/VzypoPXr07nHfQu7d6u8YOemczgLJa4OVSFnOLcVH2EK+ZaM5B
+/VdzEsUNDWfZuYNhoIKtDrYDFSS6NwBHHBHyvIff2KX5yvCGsA54bfTR1ReqSzmz
+YTh2SRCOzPtQsWgFv3NQXwZ5eXyuP96Db+siD+zIn2xZLOFv+VWpqVilDroKkfgh
+cOSs1wZfUKhoE6MayRqa6SESDyE9cZcL2KzCd1/SuXcHjRANFGxxk3F+izntTyIp
+D5H72bXE9TlPYPpyn1Qwzv4tnSn9krzpEhvJDLiSxyGxmFBFR7EoaqjqILuhAgMB
+AAGjcDBuMB0GA1UdDgQWBBQakcr/VJwTYi8u14uRcucuRyJOCTAfBgNVHSMEGDAW
+gBQakcr/VJwTYi8u14uRcucuRyJOCTAPBgNVHRMBAf8EBTADAQH/MBsGA1UdEQQU
+MBKCEGF1dGguZXhhbXBsZS5jb20wDQYJKoZIhvcNAQELBQADggEBAFofOluCwHOU
+J3mPUELYQsu4YqKZUHx2Idxs6OZ0LT0J/Su6C0yENB6npX5yrRa0g7/Zn94oFAwJ
+HdcVPSO9O+bGtMk8WzP5IyWkdpQrYKTCvGNDcg5FIujS1bYME5/Gs34og/vcce26
+21sXDoARJApOdh2h1Yn5o1dcHNX10UJBGcR7cuEGyg1eI9UCtJ1WyJx7wwoFZj4R
+qxx7Zl+InglGTjHzYKTxg9YkY2uohWBDqXUB401hxiWIPPrwmhWMbCJOFWIrLlT/
+bsWQjdqzf9pj/2d0+VrwK+Plyq6KV9jE7W5gLCATfjc1ZGyG4hgzSz70VMIfaGmy
+v+ri0c9QgFc=
+-----END CERTIFICATE-----
diff --git a/apps/server/docker/authelia/config/configuration.yml b/apps/server/docker/authelia/config/configuration.yml
new file mode 100644
index 0000000000..a0bcb4eacd
--- /dev/null
+++ b/apps/server/docker/authelia/config/configuration.yml
@@ -0,0 +1,116 @@
+# Minimal Authelia config for testing Trilium's OpenID Connect (OIDC) login.
+#
+# DEV ONLY — every secret in here is throwaway. Do not reuse for anything real.
+#
+# This config deliberately reproduces https://github.com/TriliumNext/Trilium/issues/6387:
+# a spec-compliant provider only returns `name`/`email` from the UserInfo endpoint, NOT in
+# the ID token. To test the Authelia-side workaround instead, uncomment the `claims_policies`
+# block at the bottom and the `claims_policy:` line on the client.
+
+theme: 'light'
+
+server:
+ address: 'tcp://0.0.0.0:9091'
+ # Authelia 4.39 requires https for authelia_url, so the portal is served over TLS with a
+ # self-signed cert for auth.example.com (see config/certs, regenerate via the README).
+ tls:
+ certificate: '/config/certs/public.crt'
+ key: '/config/certs/private.key'
+
+log:
+ level: 'info'
+
+# Required since 4.38; throwaway dev secret.
+identity_validation:
+ reset_password:
+ jwt_secret: 'insecure_dev_jwt_secret_change_me_0123456789'
+
+# Single test user. Password is "authelia" (the well-known Authelia example argon2id hash).
+authentication_backend:
+ file:
+ path: '/config/users_database.yml'
+
+access_control:
+ default_policy: 'one_factor'
+
+session:
+ secret: 'fb4KB+NJHQ948f5Ell9S9H23HIQE06OgdsFeD9EPqb0xX7xWOZN0aB63hVRGVAxS'
+ cookies:
+ # The portal lives on a subdomain of this registrable domain. Authelia rejects a bare
+ # `localhost` cookie domain (no dot), which is why we use auth.example.com + a hosts entry.
+ - domain: 'example.com'
+ authelia_url: 'https://auth.example.com:9091'
+
+storage:
+ encryption_key: 'w2uKzhx4Gt/aKYycoWGgQck+E/Onp8wCHqFYPTQL/xPSp6ZCSplsW8OEtWek5E50'
+ local:
+ path: '/data/db.sqlite3'
+
+notifier:
+ filesystem:
+ # Authelia writes registration / reset emails here instead of sending them.
+ filename: '/data/notification.txt'
+
+identity_providers:
+ oidc:
+ hmac_secret: 'PrK2/+SmOG79R1WvgUUgm6RSkHEgs6wIk2jE30B/kdxhBFVyz4bzai7USyzseBkH'
+ jwks:
+ - key_id: 'main'
+ algorithm: 'RS256'
+ use: 'sig'
+ key: |
+ -----BEGIN PRIVATE KEY-----
+ MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCucabx4DHA0nEg
+ FxLyXf+uWnUGmK8c0/2b969K2d+onVuNWbtzMMKksUYUdLJEUMzbyW6DX0WC3d7E
+ y5BMEZoo9xKrgW5k2w/PNA8oGyVsSlQbiQCIePJNGxhChTyXGS5/Ck0FqSOz5ZbP
+ Ver5i+Mfl/Hoa5cyg6eSQqIrOxnl2Z7UyGzDFJ36UhlM5AFB4nm53JhhGq5TRlb/
+ U+MmvgDqhep7KRNJwQ0azqvOAK1Gmp6M96hVY5EFQv24RUQ6X5OPTmag5ofFRCwm
+ ATE1OiNi/XSjE7sQe/rm2Uq9ha/fCnFnj98WsNMT1ZA993rUBYm/YJ4p+Jsp5oPv
+ RP2tP8s1AgMBAAECggEAGEWeh7mc2po9XMxxGbGMDZevAEm21dFGUJEEPis2BElO
+ z4pce2DduFolKNv64OgINQ2gN53GN3kFEcdMLtVlKI2GNsBV2qkjWD6DXF54lMp7
+ zPABV7vSZPmf5MOSpGh/0HPftymrWtDXbMZk4S9+XOtAoMj1mvT4GDD2zQKWbiVg
+ F/i1hhy3KytMMPmeu/9QBxsufsnHFu4Ztbs//usxwjZKySdFFf0sZVX3uSREnfLR
+ Qsi5zEirxy5W/Siu5zWg73d5UFrT6KQttFrBRUkFSc0IVFavqgX5e7jrZTgGHS/m
+ US3YEMOIHT1pU1p2XL9WiJ7RcdiZVU8YpTfPUam9KQKBgQDsVGiSlTs0riIQSv8J
+ WBoFDb0XKPFcKPoqA2gEEi1NCmzbzKDUP0uE9Zxr7aAJg4vJChxn+bQJtwuO/2S3
+ pb8TeSFGw5G86DGqvLQyCrxZOmqiPw/sJ8TSj23fiZa0G1oVDmpJpmyWGFySxxHm
+ C9aSDuw7EY73Bu8g+v0e+ke/aQKBgQC89p0cIxN+4DAUynQZEoYt5mfnol8vNQ01
+ cjLHrtOgShNttr/Te2TEkT41F82J4O44XSyp7FhOzb8aO8KrbEM7/1cxShCg2gYY
+ Fzhxa0rFJTBJDmGzpK5iXzoW4rtPlme2ScWSG9aVoeijfmmdQSKAdcbuDqJ40/YE
+ 6yE+/Jr/7QKBgHJuSJg5ZAqVCm9jMOxgWXD6/wUY0Q1z1fXD4nieKIs2MhGXmoRL
+ BZEisBPeYnBY3rWAjNKXqOJPXGxoI2d8/PE75K30Ajw7HdlzN7lHU33M+FcJO/d4
+ JihiRKxJXcxltg+eBD8XM1rV/vUfKP5/2uysBxxu2w+nruAER83ZGCaJAoGARmtF
+ G2I69rJHWMglwd40YNCXciwBes8zb/Jx2BZfyUExwlYzYHWAlPxniVOohjRrsleO
+ uHhaZGuqRvV0U3vSBn7emIC4d6koUjv9Dc9lGtkECIWyhUbescw5EqRGF5++YKFX
+ Xl6/dh3TW3JVm4PVfrflfXu4snyTFzwktJtgi/UCgYBWq0FsohJb1QlxV86HpvG7
+ cJZsuosFUD68iUIxDIFqSKyXIZ606kzCJrzwCGsAsqUgQ2x2W5Njx/tLLRhDG6XY
+ griRZhVXctF56SH6SeVK6XIpei9hdrEyyYXXIyD3wC7i8uu3Fcp0++xsExiy9exA
+ b3m+Gr4ZMDXg5/5HnEZTNw==
+ -----END PRIVATE KEY-----
+ clients:
+ - client_id: 'trilium'
+ client_name: 'Trilium'
+ # DEV ONLY plaintext secret; matches TRILIUM_OAUTH_CLIENT_SECRET below.
+ client_secret: '$plaintext$insecure_secret'
+ public: false
+ authorization_policy: 'one_factor'
+ require_pkce: false
+ redirect_uris:
+ # Loopback host lets Authelia accept an http (non-TLS) redirect URI.
+ - 'http://localhost:8080/callback'
+ scopes:
+ - 'openid'
+ - 'profile'
+ - 'email'
+ # Trilium sends credentials via HTTP Basic for non-Google issuers
+ # (see resolveClientAuthMethod() in apps/server/src/services/open_id.ts).
+ token_endpoint_auth_method: 'client_secret_basic'
+
+ # --- WORKAROUND (issue #6387) ---------------------------------------
+ # Uncomment the next line AND the claims_policies block below to force
+ # name/email into the ID token, sidestepping the missing-claims bug.
+ # claims_policy: 'trilium'
+
+ # claims_policies:
+ # trilium:
+ # id_token: ['email', 'name']
diff --git a/apps/server/docker/authelia/config/users_database.yml b/apps/server/docker/authelia/config/users_database.yml
new file mode 100644
index 0000000000..bb7b897565
--- /dev/null
+++ b/apps/server/docker/authelia/config/users_database.yml
@@ -0,0 +1,15 @@
+# DEV ONLY test users for the Authelia OIDC reproduction setup.
+#
+# Login: username testuser
+# password authelia
+#
+# The argon2id hash below is for the password "authelia". Regenerate with:
+# docker compose exec authelia authelia crypto hash generate argon2 --password 'yourpassword'
+users:
+ testuser:
+ disabled: false
+ displayname: 'Test User'
+ password: '$argon2id$v=19$m=65536,t=3,p=4$qQd8abEKDMNXIviIrwpiwg$JGj/Q1QkXJBhViqh7t4hy+QYt0y7rkh2+PuqZm0wcpc'
+ groups:
+ - 'admins'
diff --git a/apps/server/docker/authelia/docker-compose.yml b/apps/server/docker/authelia/docker-compose.yml
new file mode 100644
index 0000000000..a3beeee71f
--- /dev/null
+++ b/apps/server/docker/authelia/docker-compose.yml
@@ -0,0 +1,17 @@
+services:
+ authelia:
+ image: "authelia/authelia:4.39"
+ restart: unless-stopped
+ ports:
+ # Authelia portal + OIDC provider
+ - "9091:9091"
+ volumes:
+ # Not :ro — Authelia's entrypoint chowns /config on startup.
+ - ./config:/config
+ - authelia_data:/data
+ # Lets Authelia reach the Trilium dev server running on the host, if ever needed.
+ extra_hosts:
+ - "host.docker.internal:host-gateway"
+
+volumes:
+ authelia_data: