diff --git a/PLAN.md b/PLAN.md index 7d91192..f47335d 100644 --- a/PLAN.md +++ b/PLAN.md @@ -58,7 +58,10 @@ What we learned provisioning (2026-12-02/03): mail server also has no `SPOOF_PROTECTION`, so any account can send as any address. coreapps gives each app its own sending-only account instead (see Coreapps, step 4) - - Authelia's `groups` claim is always empty: its LDAP settings look + - Authelia's `groups` claim is `null` (not even `[]`, which breaks + apps that expect a list: Miniflux refuses every sign-in with group + mapping on), and the ID token carries no email or name (only + userinfo has them). The groups: its LDAP settings look for `(member={dn})` with names from `gidNumber`, but the Core's groups (`admins`, `everybody`) are `groupOfUniqueNames` with `uniqueMember`. `groups_filter: (uniqueMember={dn})` and @@ -467,6 +470,25 @@ Steps: - [ ] A mailbox for the test account (`coreapp.test`), so mail to it is delivered and `coreapp mail` could read it; needs the Core's mailbox conventions (uidNumber, homeDirectory) + - Skill shortened 2026-12-03 (577 -> ~450 lines): sign-in details in + `references/sign-in.md`, publishing in `references/publishing.md`, + read when SKILL.md says. Rehearsed with Miniflux (upstream image, + postgres, sso; a new agent, an empty repository): deployed, signed + in, data kept, and it used the reference file. Fixed from it: + `coreapp signin` called a quiet callback failure "Signed in" (the + app sent the browser back to its sign-in page); it now says "Not + signed in ... offers sign-in again". Every app's `.env` has + `CORE_PROXY` (traefik's address, to trust for `X-Forwarded-*`). The + skill: `groups` is `null` (don't turn on group mapping), claims + only in userinfo, `preferred_username` is the email; apps that can + make admins only from groups can't follow the admin rule yet + (Miniflux: no admin until groups work); wait in the entrypoint for + Authelia's discovery and the database; chain to the upstream + image's entrypoint; secure-cookie settings; a regular user is the + lowest role where people have only private things; the cookie jar + in one command + - [ ] David: Miniflux runs without an admin until the Core's + groups work (then map `admins`) - [ ] `[postgres] extensions = ["vector"]`: the installer enables extensions an app can't (when an app needs one) - [ ] PostgreSQL 18 on the Core (Federated's), or a minimum version diff --git a/README.md b/README.md index 3920707..29b5ba4 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,6 @@ stay installed. | `templates/app/` | What `coreapp new` starts from | | `templates/postgres/`, `templates/sso/` | Example apps laid over it by `--postgres` / `--sso`: their files replace the base's; `*.append` files are added to the base file, `.remove` lists base files to drop, `.needs` goes into `needs` | | `templates/postgres-sso/` | The example app when both are used | -| `skills/coreapp/` | A Claude Code skill for making apps; `update.sh` links it into `~/.claude/skills/` | +| `skills/coreapp/` | A Claude Code skill for making apps (`SKILL.md`, with `references/sign-in.md` and `references/publishing.md` read when needed); `update.sh` links it into `~/.claude/skills/` | | `uninstall.sh` | Removes what coreapp-dev set up | | [PLAN.md](PLAN.md), [TEST.md](TEST.md) | What's next, and how to test it | diff --git a/TEST.md b/TEST.md index c5c01ab..fbf03dd 100644 --- a/TEST.md +++ b/TEST.md @@ -193,8 +193,9 @@ The Core's own scripts trace secrets, so their output goes to `coreapp.toml` and `.coreapp` (`needs=`); `/federated/apps/hello/.env` is root's, mode 600, with `IMAGE_VERSION` equal to `version` and a random `APP_SECRET`, `CORE_DOMAIN` the Core's domain and - `CORE_ADMIN_EMAIL` `admin@` (an app installed before they - existed gets them on its next update). + `CORE_ADMIN_EMAIL` `admin@`, `CORE_PROXY` traefik's address (as + `INTERNAL_IP` in `/federated/services/traefik/service`) (an app installed + before they existed gets them on its next update). Superpanel's catalog (`/federated/apps/superpanel/data/catalog.json`) lists `hello`. 3. `coreapp check`: `OK: HTTP 200, valid certificate.` (the first @@ -401,6 +402,13 @@ has the `DB_*` variables, `.coreapp` says `needs=postgres`. /nope (its forms: /signin, /signin/core)". sso-demo's `/` (a link, not a form): "didn't lead to the Core's sign-in". A page with two hidden-only forms: "several forms ... pick one with --form". +11a. A callback that fails quietly: an app whose callback sends the browser + back to its sign-in page (Miniflux with group mapping on, or its sign-in + page replayed through `signin.main` with `follow` replaced): "Not + signed in: Authelia accepted the test account, but the app ended at + ..., which offers sign-in again", exit 1. A page that neither shows the + test account nor offers sign-in: "Signed in ..." and "Its page doesn't + show coreapp.test, but doesn't offer sign-in again either ...". 12. The Core's admin: a new `coreapp new --sso` app, deployed, `coreapp signin --cookies jar`: its page says "Signed in as coreapp.test ... groups: none." (not admin). With `CORE_ADMIN_EMAIL` in its `.env` set diff --git a/core/templates/env.in b/core/templates/env.in index 13e2fe5..c755b0b 100644 --- a/core/templates/env.in +++ b/core/templates/env.in @@ -7,4 +7,7 @@ CORE_DOMAIN=@DOMAIN@ # The Core's admin account: always an admin of the app, when signed in with # the Core (needs "sso") CORE_ADMIN_EMAIL=admin@@DOMAIN@ +# The Core's proxy (traefik), the only one in front of the app: trust its +# X-Forwarded-For and X-Forwarded-Proto +CORE_PROXY=@TRAEFIK_IP@ APP_SECRET=@APP_SECRET:RANDOM:LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL@ diff --git a/lib/signin.py b/lib/signin.py index fa1bcae..98aab1a 100644 --- a/lib/signin.py +++ b/lib/signin.py @@ -58,6 +58,8 @@ def errors(url): class Browser: def __init__(self, jar): self.jar = jar + # Where sign-in started (the path, and a pressed form's action) + self.signin_urls = [] self.opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(jar), NoRedirect) @@ -216,6 +218,7 @@ def follow(browser, url, username, password, stop_at=None, form=None): if picked: referer = url url, data, label = picked + browser.signin_urls.append(url) submitted = True continue return url, status, body, signed_in @@ -290,12 +293,28 @@ def main(args): if status >= 400: raise SystemExit(f"Not signed in: Authelia accepted the test account, but the app answered " f"HTTP {status} at {shown(last)} (coreapp logs says why).") - if cookies: - jar.save(ignore_discard=True, ignore_expires=True) text = re.sub(rb"<[^>]+>", b" ", body).decode(errors="replace") mentions = username.split("@")[0] in text + # Back at the app isn't signed in if the app's page offers sign-in again + # (its callback failed quietly and sent the browser to its sign-in page) + page = body.decode(errors="replace") + starts = {urllib.parse.urlsplit(u).path for u in [start] + browser.signin_urls} + targets = {urllib.parse.urlsplit(urllib.parse.urljoin(last, t)).path + for t in re.findall(r"""(?:href|action)\s*=\s*["']([^"'#]+)""", page)} + offers = (urllib.parse.urlsplit(last).path in starts or starts & targets + or re.search(r"""]*type\s*=\s*["']?password""", page, re.I)) + if offers and not mentions: + raise SystemExit( + f"Not signed in: Authelia accepted the test account, but the app ended at " + f"{shown(last)}, which offers sign-in again: its callback didn't sign the person in " + "(coreapp logs says why).") + if cookies: + jar.save(ignore_discard=True, ignore_expires=True) print(f"Signed in: back at the app, {shown(last)} answered HTTP {status}.") - print("Its page " + ("shows" if mentions else "doesn't show") + f" {username.split('@')[0]}.") + who = username.split("@")[0] + print(f"Its page shows {who}." if mentions else + f"Its page doesn't show {who}, but doesn't offer sign-in again either; to be sure, " + "look at a page only signed-in people get, with --cookies.") if cookies: print(f"Cookies saved in {cookies} (curl -b {cookies} https://{app.netloc}/...).") return 0 diff --git a/skills/coreapp/SKILL.md b/skills/coreapp/SKILL.md index e7eecb0..19cf9fa 100644 --- a/skills/coreapp/SKILL.md +++ b/skills/coreapp/SKILL.md @@ -55,6 +55,7 @@ What the Core gives every app: | `IMAGE_VERSION` | The installed version (`coreapp deploy` builds with plain `docker build .`, no build args, so read the version from here, not a build-time `VERSION`) | | `CORE_DOMAIN` | The Core's domain (``). Set only on a Core: an app that also runs elsewhere can use it to tell | | `CORE_ADMIN_EMAIL` | `admin@`, the Core's admin account: always an admin of the app (see `sso`) | + | `CORE_PROXY` | traefik's address, the only proxy in front of the app: the one to trust for `X-Forwarded-For` and `X-Forwarded-Proto` | `APP_SECRET` isn't hex or base64: an app that wants key bytes (e.g. 32 bytes for encryption) derives them, e.g. SHA-256 of `APP_SECRET`. It @@ -155,14 +156,11 @@ The Core makes a PostgreSQL user and database just for this app, and adds: | `DB_NAME`, `DB_USER` | `coreapp_` with `-` as `_` (e.g. `coreapp_my_app`) | | `DB_PASSWORD` | random | -The server is PostgreSQL 17 (17.6 today; `coreapp core` shows the Core's -version, and `coreapp status` once the app needs it), shared with the Core's other apps; the app owns its database and -nothing else. Check what version the app needs before choosing this (its -docs, its own compose file's `postgres:` image, its migrations): features -of a newer PostgreSQL fail only when the code that uses them runs, so a -deploy, `coreapp check` and a sign-in can all pass on the wrong version. -If it needs a newer one, see "Not available yet". There's no superuser access, so the app creates -its own tables (migrations at startup are the usual way). At boot the +The server is the Core's PostgreSQL (its version and extensions: "What the +Core offers"; check the app's needs against them first), shared with the +Core's other apps; the app owns its database and nothing else. There's no +superuser access, so the app creates its own tables (migrations at startup +are the usual way). At boot the database may come up after the app: retry the connection instead of exiting. Many apps want a URL; build it from these variables in the entrypoint or the app's config (e.g. @@ -198,85 +196,31 @@ and adds: | `OIDC_CLIENT_ID` | `coreapp-` | | `OIDC_CLIENT_SECRET` | random | -The client, by default: +By default the client's redirect URI is `$APP_URL/auth/callback`, PKCE +(S256) is required and the secret goes in the Authorization header +(`client_secret_basic`). Many apps have a fixed callback path or no PKCE: +then fit the client to the app with `[sso]` in `coreapp.toml` (`callback`, +`pkce`, `token_auth`), never with a workaround in the app. Use the app's +own OpenID Connect support ("generic OIDC" settings) with `OIDC_*`. -- Redirect URI: exactly `https://./auth/callback` - (`$APP_URL/auth/callback`). -- Authorization code flow, PKCE with S256 required, client authentication - `client_secret_basic` (secret in the Authorization header). +Two rules for who may do what, on every Core: -If the app's sign-in can't be configured to match (many have a fixed -callback path, and many don't send PKCE), change the client in -`coreapp.toml` instead of working around it in the app: +- **The Core's admin is the app's admin.** `CORE_ADMIN_EMAIL` + (`admin@`, the Core's own admin) is always an admin of the app: + made one (again) on each Core sign-in, trusted only from the email + Authelia gives, never from an address typed into the app. No setup link, + no first-come owner; it signs in only with the Core. +- **Everyone else gets the lowest role that can look around.** Any other + Core account may sign in, without an invitation, as a read-only member + (a viewer). The app's admins raise people from there. In an app where + each person has only their own private things (a feed reader, notes), + a regular user is the lowest role. -```toml -[sso] -callback = "/auth/openid/core" # the app's callback path (default "/auth/callback") -pkce = false # the app doesn't send PKCE (default true) -token_auth = "client_secret_post" # it sends the secret in the form (default client_secret_basic) -``` - -Set only what the app needs, and say in a comment why. Find out from the -app's documentation or source: the callback path it puts in `redirect_uri` -(sometimes built in the browser, e.g. `/auth/openid/`; then -pick the key so the path is known), whether it sends `code_challenge`, -and how it sends the secret. Authelia checks the redirect URI when the -sign-in starts (an error page instead of its login page if it's wrong), but -PKCE and the secret only after the person has signed in, so a curl of the -authorize URL can't show those are right: `coreapp signin` can (see -"Deploy and test"). Changing -`[sso]` and deploying registers the client again; Authelia restarts for it -(a few seconds of 404s from it, which an app checking discovery at startup -may log; it should retry). -- Scopes: `openid profile email groups`. Userinfo gives `sub`, - `preferred_username`, `name`, `email`, and `groups`, which is empty on - today's Cores: they have two groups, `admins` and `everybody`, but - Authelia's settings don't find them yet (for Federated to fix). Read `groups` - if the app can use it, but don't depend on it for roles. -- **The app's admin is the Core's admin.** On a Core, the account - `CORE_ADMIN_EMAIL` (`admin@`, the Core's own admin, as in - Federated's apps) is always an admin of the app, from its first sign-in: - no setup link, no first-come owner. It can then make other people admins - in the app. Two rules: - - Trust it only from the Core's sign-in: the `email` in Authelia's - userinfo or ID token, compared case-insensitively. Never make a local - account, a sign-up or an invitation with that address an admin; anyone - could type it. - - Every time, not only the first: if its admin role was taken away in - the app, its next Core sign-in gives it back. - - If the app has its own first-run setup (an owner to create, a setup - link), skip it on a Core: the Core's admin is the owner. If the app can't - run without an owner (its pages say "not set up" until there is one), - make `CORE_ADMIN_EMAIL` the owner at startup, without a password; it - signs in with the Core. That account signs in only with the Core: turn - off the app's password sign-in and password reset for it, or its - forgot-password page could give it a local password. The example app - (`coreapp new --sso`) shows the check in `oidc.py`. `coreapp signin` - signs in as the test account, not the admin, so ask the user to sign in - once as `admin@` in a browser and check it gets the admin pages. -- **Everyone else gets the lowest role.** Any other Core account (a person - at the company) may sign in, with no invitation, and gets the app's - lowest role that can still look around: read-only, seeing what the app - shows its members but changing nothing (a viewer). Not "nothing until an - admin grants access": they should find the app useful to look at. The - app's admins raise people from there in the app. Don't hand out more by default, and don't - require invitations for Core accounts. `coreapp signin` checks this: the - test account should land with that role. -- Discovery: `$OIDC_ISSUER/.well-known/openid-configuration`. The Core - routes `authelia.` from inside the app's container to the real - HTTPS endpoint, with a valid certificate, so normal TLS verification works. -- Sign-out in the app ends the app's session; the person stays signed in to - the Core. - -Use the app's own OpenID Connect support or its framework's library where it -has one: most self-hosted apps have "generic OIDC" settings that map onto -these variables. If the app has its own step for setting up its client (an -admin makes a secret in its settings, or pastes a client block into the -identity provider), skip it on the Core: fill its settings from `OIDC_*`, -so sign-in works with no admin step, and don't show those instructions when -the Core gave the client. For a standard-library Python example of the whole flow, -`coreapp new --sso example` makes one (`oidc.py`). +**Before writing sign-in code, read +[references/sign-in.md](references/sign-in.md):** how to find what the app +needs for `[sso]`, how to build the two rules into the app (first-run +setup, password reset), what Authelia tells the app (its `groups` are +empty today), and how to test sign-in with `coreapp signin`. ### `email`: sending mail @@ -352,8 +296,11 @@ and make the Dockerfile fit the Core: - **Listen on `0.0.0.0` and on `port`**, plain HTTP. No TLS in the app. - **Know its public address**: `APP_URL` (many apps need a "base URL" or - "trusted host" setting; it's behind a proxy that sends `X-Forwarded-Proto: - https` and `X-Forwarded-For`). + "trusted host" setting). It's behind traefik, which sends + `X-Forwarded-Proto: https` and `X-Forwarded-For`: set the app's "trusted + proxies" to `CORE_PROXY` (so it logs and rate-limits by the real client + address), and turn on its "behind HTTPS" or secure-cookie setting if it + has one, since it sees plain HTTP itself. - **Keep state in `/data`**: point upload folders, SQLite files and generated keys there. If the app needs a directory structure under `/data`, create it at startup (it starts empty). @@ -377,6 +324,18 @@ and make the Dockerfile fit the Core: otherwise, and on a Core nobody else sets them. (In the app's own code, as above, the Core's values are defaults that the app's explicit settings override: that's for its other editions, not upstream `ENV`.) + Before writing the entrypoint, look at how the upstream image starts + (`docker inspect -f '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' + `): end yours by running what it ran (`exec` its entrypoint with + `"$@"`, or `exec "$@"` when it only has a `CMD`). +- **Wait for what it needs at startup** if it only checks once: the + database may come up after the app on a reboot, and Authelia restarts + when the app's sign-in client is registered (on the first deploy, and + when `[sso]` changes). An app that fetches Authelia's discovery only at + startup then has no sign-in until restarted, so wait in the entrypoint, + with a time limit, e.g. + `until wget -qO /dev/null "$OIDC_ISSUER/.well-known/openid-configuration"; do sleep 2; done` + (and `nc -z "$DB_HOST" "$DB_PORT"` for the database). - **Images without a shell** (built `FROM scratch` or distroless) can't run an entrypoint script, and `coreapp shell` can't open one. Copy a static busybox in and run the script with it: @@ -437,42 +396,13 @@ or ask the user to use a browser), sign in if it has `sso`, and check that what it stores survives `coreapp deploy --recreate` (data in `/data` and the database). -Sign in with `coreapp signin`: it signs in to the app as a browser would, -on the real Authelia with the app's real client, as the Core's test account -(`coreapp.test@`, display name `coreapp.test`, in no groups; made on -the Core in the VM when first needed, with a new password each time). It -prints each step, with Authelia's error if the client doesn't fit, and says -whether it got back to the app signed in: - -```bash -coreapp signin # starts at /login -coreapp signin /auth/login # the app's own page that starts sign-in -coreapp signin /signin # a page whose button starts sign-in: pressed -coreapp signin --cookies jar /login && curl -b jar https://./api/me -``` - -Like the other commands, run it in the app's folder (it reads -`coreapp.toml`); the cookie jar can be anywhere, e.g. `--cookies -"$TMPDIR/jar"`. - -A page with a sign-in button (a form with only hidden fields, such as a -CSRF token) is submitted as a person pressing it would; with several such -forms, pick one with `--form `. Don't add routes to the app just for -the test. - -For apps whose sign-in starts in the browser's JavaScript (single-page -apps: the page builds the authorize URL itself), `coreapp signin --code` -starts at Authelia with the app's client and stops at the app's callback, -printing it with the code; give that to the app as its page would (e.g. -Vikunja's: `POST /api/v1/auth/openid//callback` with `code` and -`redirect_url`). Its `state` is not the app's, so a callback that checks -state (a server-side callback page) refuses it: use the path form for those. -Authelia bans an account for a few minutes after repeated failed sign-ins, -so don't loop on it. - -Still ask the user to sign in once in a browser before a release, as the -Core's admin (`admin@`): it checks what a script can't (the app's -pages, and that the admin gets the admin role). +For an app with `sso`, `coreapp signin [path]` signs in as a browser would, +with the Core's test account (`coreapp.test@`), and says whether it +got back to the app signed in; see +[references/sign-in.md](references/sign-in.md) for its options +(`--cookies`, `--form`, `--code` for single-page apps). The test account +isn't the admin: ask the user to sign in once in a browser as +`admin@` before a release. ### 4. When something fails @@ -506,11 +436,8 @@ Common causes: - `coreapp check` fails but deploy worked: a new certificate can take a minute; it retries for two. It also tries the VM directly, which tells the app apart from the way in. -- Sign-in fails (`coreapp signin` shows Authelia's error and hint): "The - 'redirect_uri' parameter does not match" means the app's callback isn't - the client's (`[sso] callback`); "invalid_client" means it sends the - secret another way (`[sso] token_auth`); a PKCE error means `[sso] pkce` - doesn't match what the app does. +- Sign-in fails: `coreapp signin` shows Authelia's error and hint; what + each means is at the end of [references/sign-in.md](references/sign-in.md). - Permission denied under `/data`: the app switches to another user after starting (the Core gives `/data` to the image's `USER`), or it writes somewhere else in the container (`coreapp shell 'id; ls -ld /data'`). @@ -520,48 +447,11 @@ The installer's own log (the Core's install scripts) is on the Core in look at the end of it with `coreapp-vm ssh tail -n 40 /federated/logs/coreapps/-install.log` only when a deploy fails in the installer itself. -### 5. Publishing (when the user asks for a release) +### 5. Publishing (only when the user asks for a release) -A published app is a tagged commit in its repository plus its image in a -registry, from which any Core can install it. Only publish when the user -asks: it pushes to their registry and tags their repository. - -1. Set `image` in `coreapp.toml` to the registry: for an app on a Gitea - server, `//` in lowercase (Gitea's container - registry is on the same host). `coreapp publish` suggests one from the - git remote. `coreapp deploy` keeps working with that name. -2. Raise `version` if this version was published before (published - versions are never replaced). -3. Commit everything; `coreapp publish` refuses uncommitted changes. -4. `coreapp publish` builds the image for linux/amd64 (add - `--platform linux/amd64,linux/arm64` for ARM Cores too), pushes - `image:version`, and tags the commit `v`. It needs - `docker login ` first; that's the user's to do (for Gitea, - with a token that has the `write:package` scope). -5. The user pushes the commit and tag: `git push && git push origin v`. - -New apps also have `.gitea/workflows/publish.yml`, which does the same on -the Gitea server when a `v*` tag is pushed, if the repository has an -Actions runner that can use Docker and the secrets `REGISTRY_USER` and -`REGISTRY_TOKEN`. When porting an existing app, copy it from a new app -(`coreapp new x`) if the user wants it. - -To check a release the way a Core's owner will get it: -`coreapp install https:////` installs it on the -Core in the VM from the repository (the newest `v*` tag; `@v1.2.0` for -another), pulling the published image, and `coreapp update ` moves it -to a newer release. Coreapps are often private: for a private repository -and image, the Core needs a token for the host first, `coreapp login -`, which asks for a user name and a read-only token -(repositories and packages). That's the user's to enter, not yours. The Core -only reads `coreapp.toml` and `icon.png` from the repository; `version` has -to match the tag. On the same Core, `coreapp deploy` from the folder and -`coreapp install` from the repository update the same app (same name, data -and database). A Core keeps running the image it installed, by digest: a -release can't be fixed by pushing its tag again (installing it again is -refused), so publish a new version. Going back to an older release needs -`coreapp install --downgrade @v`; the user decides that, since -the older version may not understand the newer one's data. +Publishing pushes the image to the user's registry and tags their +repository; installing a release from its repository is how a Core's owner +gets it. For both, read [references/publishing.md](references/publishing.md). ## Reporting back @@ -570,7 +460,7 @@ When you're done, tell the user: - what you changed in the repository (`coreapp.toml`, Dockerfile, config), - what you tested and how (deploy, check, which pages, a redeploy keeping data, `coreapp signin`), and what they still need to test themselves - (signing in in a browser with their own account), + (signing in in a browser as `admin@`), - anything the app needs that the Core doesn't offer yet, and how you handled it, - anything in coreapp-dev that got in the way or was unclear. coreapp-dev is diff --git a/skills/coreapp/references/publishing.md b/skills/coreapp/references/publishing.md new file mode 100644 index 0000000..05d6dfb --- /dev/null +++ b/skills/coreapp/references/publishing.md @@ -0,0 +1,53 @@ +# Publishing a release, and installing it from its repository + +Read this only when the user asks for a release, or to install an app from +its repository. Publishing pushes to the user's registry and tags their +repository, so don't do it on your own. + +## Publishing + +A published app is a tagged commit in its repository plus its image in a +registry, from which any Core can install it. + +1. Set `image` in `coreapp.toml` to the registry: for an app on a Gitea + server, `//` in lowercase (Gitea's container + registry is on the same host). `coreapp publish` suggests one from the + git remote. `coreapp deploy` keeps working with that name. +2. Raise `version` if this version was published before (published + versions are never replaced). +3. Commit everything; `coreapp publish` refuses uncommitted changes. +4. `coreapp publish` builds the image for linux/amd64 (add + `--platform linux/amd64,linux/arm64` for ARM Cores too), pushes + `image:version`, and tags the commit `v`. It needs + `docker login ` first; that's the user's to do (for Gitea, + with a token that has the `write:package` scope). +5. The user pushes the commit and tag: `git push && git push origin v`. + +New apps also have `.gitea/workflows/publish.yml`, which does the same on +the Gitea server when a `v*` tag is pushed, if the repository has an +Actions runner that can use Docker and the secrets `REGISTRY_USER` and +`REGISTRY_TOKEN`. When porting an existing app, copy it from a new app +(`coreapp new x`) if the user wants it. + +## Installing from the repository + +To check a release the way a Core's owner will get it: + +- `coreapp install https:////` installs it on the + Core in the VM from the repository (the newest `v*` tag; `@v1.2.0` for + another), pulling the published image. `coreapp update ` moves it + to a newer release. +- The Core reads only `coreapp.toml` and `icon.png` from the repository; + `version` has to match the tag. +- Coreapps are often private: for a private repository and image, the Core + needs a token for the host first, `coreapp login `, which asks + for a user name and a read-only token (repositories and packages). That's + the user's to enter, not yours. +- On the same Core, `coreapp deploy` from the folder and `coreapp install` + from the repository update the same app (same name, data and database). +- A Core keeps running the image it installed, by digest: a release can't + be fixed by pushing its tag again (installing it again is refused), so + publish a new version. +- Going back to an older release needs `coreapp install --downgrade + @v`; the user decides that, since the older version may not + understand the newer one's data. diff --git a/skills/coreapp/references/sign-in.md b/skills/coreapp/references/sign-in.md new file mode 100644 index 0000000..ef7773b --- /dev/null +++ b/skills/coreapp/references/sign-in.md @@ -0,0 +1,147 @@ +# Sign-in with Core accounts: the details + +Read this when the app has `needs = ["sso"]`: to fit the Core's client to +the app, to put the admin and role rules into the app, and to test sign-in. +SKILL.md has the summary. + +## Fitting the client to the app + +The Core's client, by default: redirect URI exactly +`https://./auth/callback` (`$APP_URL/auth/callback`), +authorization code flow, PKCE with S256 required, the secret sent with +`client_secret_basic` (in the Authorization header). + +If the app's sign-in can't be configured to match (many have a fixed +callback path, and many don't send PKCE), change the client in +`coreapp.toml` instead of working around it in the app: + +```toml +[sso] +callback = "/auth/openid/core" # the app's callback path (default "/auth/callback") +pkce = false # the app doesn't send PKCE (default true) +token_auth = "client_secret_post" # it sends the secret in the form (default client_secret_basic) +``` + +Set only what the app needs, and say in a comment why. Find out from the +app's documentation or source: + +- the callback path it puts in `redirect_uri` (sometimes built in the + browser, e.g. `/auth/openid/`; then pick the key so the + path is known), +- whether it sends `code_challenge`, +- how it sends the secret. + +Authelia checks the redirect URI when sign-in starts (an error page instead +of its login page if it's wrong), but PKCE and the secret only after the +person has signed in, so a curl of the authorize URL can't show those are +right; `coreapp signin` can. Changing `[sso]` and deploying registers the +client again; Authelia restarts for it (a few seconds of 404s from it). +Many apps fetch discovery only once at startup: wait for it in the +entrypoint (SKILL.md, "Wait for what it needs at startup"). + +Use the app's own OpenID Connect support or its framework's library: most +self-hosted apps have "generic OIDC" settings that map onto `OIDC_*`. If +the app has its own step for setting up its client (an admin makes a +secret in its settings, or pastes a client block into the identity +provider), skip it on the Core: fill its settings from `OIDC_*`, so +sign-in works with no admin step, and don't show those instructions when +the Core gave the client. `coreapp new --sso example` makes a +standard-library Python example of the whole flow (`oidc.py`). + +## What Authelia says about the person + +- Scopes: `openid profile email groups`. The claims come from the + userinfo endpoint: `sub`, `email` (with `email_verified: true`), `name` + (the account's uid, e.g. `coreapp.test`), `preferred_username` (the + email address, which is what people sign in with), `groups`. The ID token + has none of them, so an app that reads the person only from the ID token + gets no email or name: turn on its "use userinfo" setting. +- `groups` is `null` on today's Cores, not a list: they have two groups, + `admins` and `everybody`, but Authelia's settings don't find them yet + (for Federated to fix). Don't turn on an app's group-to-role mapping: + apps that expect a list fail every sign-in on `null` (Miniflux: "groups + must be a string or a list of strings"). +- An app that can make admins only from a group (no way to name one by + email) can't follow the admin rule yet: leave its admins off, say so in + your report, and note that once groups work, mapping the Core's + `admins` group would do it (the user decides). +- Discovery: `$OIDC_ISSUER/.well-known/openid-configuration`. The Core + routes `authelia.` from inside the app's container to the real + HTTPS endpoint, with a valid certificate, so normal TLS verification + works. +- Sign-out in the app ends the app's session; the person stays signed in + to the Core. + +## Putting the admin and role rules into the app + +The rules are in SKILL.md: `CORE_ADMIN_EMAIL` is always an admin, everyone +else gets the lowest role that can look around. How: + +- Check the admin on every Core sign-in, from the `email` Authelia gives + (compared case-insensitively), and make that account an admin (back) if + it isn't. Never from an address typed into the app: no local account, + sign-up or invitation with that address becomes an admin. +- If the app has its own first-run setup (an owner to create, a setup + link), skip it on a Core: the Core's admin is the owner. If the app + can't run without an owner (its pages say "not set up" until there is + one), make `CORE_ADMIN_EMAIL` the owner at startup, without a password. +- That account signs in only with the Core: turn off the app's password + sign-in and password reset for it, or its forgot-password page could + give it a local password. +- Other Core accounts join on their first Core sign-in, without an + invitation, as read-only members (a viewer: they see what members see, + change nothing). Not "nothing until an admin grants access". +- The example app (`coreapp new --sso`) shows the admin check in `oidc.py`. + +## Testing sign-in: coreapp signin + +`coreapp signin` signs in to the app as a browser would, on the real +Authelia with the app's real client, as the Core's test account +(`coreapp.test@`, display name `coreapp.test`, in no groups; made +on the Core in the VM when first needed, with a new password each time). +It prints each step, with Authelia's error if the client doesn't fit, and +says whether it got back to the app signed in. Run it in the app's folder +(it reads `coreapp.toml`). + +```bash +coreapp signin # starts at /login +coreapp signin /auth/login # the app's own page that starts sign-in +coreapp signin /signin # a page whose button starts sign-in: pressed +jar=$(mktemp) && coreapp signin --cookies "$jar" /login && curl -b "$jar" https://./api/me +``` + +- A page with a sign-in button (a form with only hidden fields, such as a + CSRF token) is submitted as a person pressing it would; with several such + forms, pick one with `--form `. Don't add routes to the app just + for the test. +- With `--cookies`, check what the test account may do: it should land as + a read-only member. Keep the jar's path in one command, as above + (`$TMPDIR` can differ between a sandboxed shell and `coreapp`'s). +- "Signed in" needs the app to have kept the person: if its page offers + sign-in again (its callback failed quietly), `coreapp signin` says "Not + signed in ... offers sign-in again"; `coreapp logs` has the app's reason. +- Apps whose sign-in starts in the browser's JavaScript (single-page apps: + the page builds the authorize URL itself): `coreapp signin --code` starts + at Authelia with the app's client and stops at the app's callback, + printing it with the code; give that to the app as its page would (e.g. + Vikunja's: `POST /api/v1/auth/openid//callback` with `code` and + `redirect_url`). Its `state` isn't the app's, so a server-side callback + that checks state refuses it: use the path form for those. +- Authelia bans an account for a few minutes after repeated failed + sign-ins, so don't loop on it. + +The test account isn't the admin, so ask the user to sign in once in a +browser as `admin@` before a release: it checks what a script +can't (the app's pages, and that the admin gets the admin role). + +## When sign-in fails + +`coreapp signin` shows Authelia's error and hint: + +- "The 'redirect_uri' parameter does not match": the app's callback isn't + the client's (`[sso] callback`). +- "invalid_client": it sends the secret another way (`[sso] token_auth`). +- A PKCE error: `[sso] pkce` doesn't match what the app does. +- "didn't lead to the Core's sign-in": the path doesn't start sign-in; give + the right one, `--form` for a page with several buttons, or `--code` for + a single-page app. diff --git a/templates/app/README.md b/templates/app/README.md index 223c5a8..d0978ff 100644 --- a/templates/app/README.md +++ b/templates/app/README.md @@ -41,6 +41,7 @@ keep its values and add new ones): | `APP_SECRET` | A random secret, for signing cookies and such | | `IMAGE_VERSION` | The version that's installed | | `CORE_DOMAIN` | The Core's domain (set only on a Core) | +| `CORE_PROXY` | traefik's address, the proxy in front of the app (trust its `X-Forwarded-*`) | | `CORE_ADMIN_EMAIL` | `admin@`, the Core's admin account: always an admin of the app, when it signs in with the Core | `needs = ["postgres"]` adds `DB_HOST`, `DB_PORT`, `DB_NAME`, `DB_USER` and