Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 5.1 KB

deploying.md

File metadata and controls

59 lines (40 loc) · 5.1 KB

Deploying and authentication

For a first deploy of your own instance, see README → Deploy your own.

Authentication

Two modes, chosen by the AUTH_MODE var:

  • open (default): no sign-in. Every request runs as a single implicit owner (sub = local, admin). Use it behind your own gate, or for local development.
  • oidc: "Sign in with " using OpenID Connect (Authorization Code + PKCE, state and nonce, discovery from OIDC_ISSUER) via openid-client. Sessions are HS256-signed cookies keyed on the sub claim, never on email. Access is limited to accounts with the admin role plus verified emails listed in ACCESS_ALLOWED_EMAILS; the policy is checked at sign-in and on every request, so revoking someone locks existing cookies out immediately. Destructive routes additionally require the admin role. Scopes: openid profile email roles.

The id_token's signature is checked against the provider's JWKS (openid-client's enableNonRepudiationChecks; the library skips it by default), alongside its issuer, audience, expiry and nonce.

Config for oidc mode: vars OIDC_ISSUER, OIDC_CLIENT_ID, AUTH_PROVIDER_LABEL, ACCESS_ALLOWED_EMAILS; secrets OIDC_CLIENT_SECRET, SESSION_SECRET. Redirect URI: https://<host>/api/auth/callback.

In open mode behind Cloudflare Access, the app probes /cdn-cgi/access/get-identity, which Cloudflare only serves on hostnames covered by an Access application; when that answers, the Account page drops the open-access warning and names the identity that let you in instead. Access visitors still share the single owner account, so the policy is what decides who gets in.

Sign-out clears the session and, when the provider's discovery document advertises an end_session_endpoint, sends the browser there (RP-initiated logout) with the id_token from sign-in as id_token_hint and post_logout_redirect_uri set to the instance's signed-out page, https://<host>/signed-out, which shows the sign-in card with a "You're signed out" note. Register that exact string as a post-logout redirect URI for the client, or the provider will refuse it. Providers without an end-session endpoint get a local sign-out that lands on the same page.

The author's instance (mode prod in cloudflare.config.ts) signs in through cubityfir.st (https://auth.cubityfir.st, the cubityfirst-auth Worker), a private provider on the same Cloudflare zone. Because a Worker's fetch to a same-zone hostname skips that hostname's Worker, its discovery, token, JWKS and userinfo calls go through the IDP service binding (services/oidc.ts); that binding is optional and only used when present. The binding names the Worker directly and ignores routes, so if the provider ever moves to another Worker the binding has to move with it. Its client has https://opsec.cubityfir.st/signed-out and https://opsec.nexus/signed-out registered as post-logout redirect URIs (auth.cubityfir.st/admin/clients), plus the bare origins, which builds from before the signed-out page still send.

The author's instance

One-time: the D1 database nexus-db and R2 bucket nexus-files already exist and are referenced in cloudflare.config.ts (Cloudflare cannot rename them; they keep the project's old name). The Worker is opsec. The custom domains opsec.cubityfir.st, opsec.nexus, and the legacy nexus.cubityfir.st (which redirects) are created from the domains list on first deploy (the zone must be on the same Cloudflare account).

npm run deploy:prod        # cf build --mode prod, apply migrations, cf deploy --prebuilt --mode prod
curl https://opsec.cubityfir.st/api/auth/login -o /dev/null -w "%{redirect_url}\n"   # must point at the identity provider

npm run deploy (no :prod) deploys the default-mode template: open access, a fresh opsec-db, no IDP binding. Because the template and mode prod share the Worker name opsec, running it against this account silently switches production to open access and an empty database (sign-in then fails with "Internal error"). Only use it on a self-hosted account.

Workers Builds (GitHub → Cloudflare)

The same trap applies to the dashboard's automatic builds, whose defaults are npm run build and npx wrangler deploy. In Workers & Pages → opsec → Settings → Builds, set:

Setting Value
Build command npm run ci:build (runs build:prod)
Deploy command npm run ci:deploy (applies D1 migrations to nexus-db with cf d1 migrations apply, then cf deploy --prebuilt --mode prod)

The build writes .cloudflare/output and records mode prod in it; the deploy uploads that build and refuses one recorded in any other mode, so a template build can never reach this Worker that way. Secrets (SESSION_SECRET, OIDC_CLIENT_SECRET, AI_API_KEY) live on the Worker and survive deploys.

Schema changes

  1. Edit src/worker/db/schema.ts
  2. npm run db:generate and review the SQL in drizzle/
  3. npm run db:migrate:local, run tests, commit the migration
  4. npm run db:migrate:remote before deploying