# First-run onboarding: the bootstrap admin (PMS-676)

# First-run onboarding: the bootstrap admin (PMS-676)

How the very first administrator gets into a brand-new mokosh-server instance and configures it, including the case where email/SMTP is not yet set up.

## The chicken-and-egg, and whose it is

On a fresh instance email/SMTP is not configured, so no verification message can be sent. If admin access were gated on a verified email address, the first admin could never get in to configure email in the first place.

mokosh-server does not add that gate: the production bootstrap-admin login does not depend on email verification, and the two tests at the end of this document pin it. That is the whole of mokosh's part, and it is worth being precise about, because the trap a real deployment actually fell into was NOT here.

It was in bunyip, the OP. `bunyip-web`'s `needs_onboarding` pinned a named-but-unverified user to `/onboarding` whenever `setup_status.email_enabled == true`, and the onboarding allowlist did not include `/admin/email`, so a deployment with email ENABLED but undeliverable (bad credentials, a DMARC or SPF reject, a wrong `SMTP_FROM`, an unreachable relay) trapped the one party who could repair the relay: the verification mail never arrived, and the admin had no route to the page that would fix it. Bunyip already exempted `email_enabled == false`; the missing case was enabled-but-broken. That is BUNYIP-401, the chicken-and-egg behind PSA-1's "Verify the onboarding process for the admin", fixed in bunyip PR #393.

So read this document as mokosh's downstream posture and not as the fix for first-run onboarding. If a first admin cannot get in on a deployment where SMTP is set but mail is not arriving, the gate to look at is bunyip's, not one of mokosh's.

## Production: bootstrap admin via bunyip-as-OP

Production authenticates through bunyip-as-OP (the sole OP; see the "Auth" section of [`architecture.md`](../../architecture.md#auth-two-independent-mechanisms-pms-295)). The first admin signs in with bunyip, and bunyip's access token carries the platform-admin claim (`bunyip_role = "admin"`).

On that login mokosh runs `place_bunyip_user` (`src/modules/auth/middleware.rs`), which:

- Provisions the user's own personal tenant and JIT-creates the local shadow row (no pre-existing invite needed).
- Maps the `bunyip_role = "admin"` platform claim to tenant `Admin` via `effective_role_from_bunyip`. Since MAPPS-519 this claim no longer promotes to mokosh `super_admin`: that role was retired from `users.role` (migration 133), and cross-tenant, deployment-wide privilege now lives only in a separate `platform_admins` row, never in a bunyip claim.
- Returns an authenticated session regardless of `email_verified`. An unverified address only means no pending invite is consumed and the JIT row is stored against a placeholder address; it never blocks login or the tenant-admin grant.

So a fresh instance with no SMTP configured still lets the first bunyip admin sign in as the `Admin` of their own (newly provisioned) tenant. That is a different, narrower thing than the deployment-wide operator access steps 3-5 below need; see the `DeploymentOperator` note after the steps.

### Steps

1. Spin up the instance (see `docs/quickstart.md` for the dev stack; production is the same server behind bunyip-as-OP with `OIDC_ISSUER` + `OIDC_AUDIENCE` set to bunyip).
2. Sign in through the SPA with the bunyip account that holds the platform-admin role. You land as the tenant `Admin` of your own tenant, with no email configured and no verification step.
3. Configure email as the deployment operator: `PUT /api/v1/settings/email` (deployment-operator only, `DeploymentOperator`; PMS-638, narrowed by PMS-1280). The SMTP password is stored AES-256-GCM-encrypted and the live mailer is hot-swapped on write, so email starts working with no restart. Any field left unset falls back to the matching `SMTP_*` env var.
4. Name the deployment as the deployment operator: `PUT /api/v1/settings/app-name` (deployment-operator only, `DeploymentOperator`; PMS-789, narrowed by PMS-1280), body `{"app_name": "PSA Systems"}`. This is the product name recipients see in outbound mail, in the authenticator app when they enrol in MFA, and on the API's own landing page. It is one value for the whole deployment, not per tenant, and it takes effect immediately with no restart. Leave it unset and everything reads `Mokosh`; send an empty string to clear it back to that. It is deliberately not an environment variable, so changing it never needs a redeploy.
5. If this deployment offers Google Contacts import, set its Google OAuth client as the deployment operator: `PUT /api/v1/settings/google-contacts-client` (deployment-operator only, `DeploymentOperator`; PMS-1444), body `{"client_id": "...", "client_secret": "..."}`. Both halves, always: a deployment holding one refuses to boot, so the route refuses to store one. Which Google application the installation authenticates as is a property of the deployment and no tenant can set it (PMS-1430), and the pair is stored as two governed application-tier secrets in whichever provider `SECRET_BACKEND` names rather than in `tenant_settings`. It takes effect immediately, with no restart, the same way email does. The read returns whether each half is set and which provider serves them, never a value, not even the id. Google Cloud console steps: [google-contacts-verification.md](../../dev-docs/google-contacts-verification.md#registering-the-hosts-google-client-operator). Before anyone can sign in at all, `mokosh-server provider-set` does the same job from a shell (PMS-1441).

`DeploymentOperator` admits a caller through either of two paths: a platform-admin bearer (`/platform/login`, a `platform_admins` row), or an admin of the fixed system tenant (`SYSTEM_TENANT_ID`, the deployment's own organisation). The bunyip bootstrap-admin from step 2 is neither: `place_bunyip_user` provisions a personal tenant for every new sign-in, not the system tenant, and the bunyip claim no longer mints a platform-admin bearer (see above). So steps 3-5 need the separate platform-admin persona, bootstrapped below, signed in at `/platform/login` rather than through the SPA's bunyip session.

6. From here, invite the rest of the team. Later users go through normal verification: an invite to address X is only consumed by a bunyip login with a verified X (`place_bunyip_user`'s invite gate, PMS-248). Turning email on does not change or downgrade the bootstrap admin.

## Local development: the ADMIN_EMAIL / ADMIN_PASSWORD seed

For local dev without bunyip, `maybe_bootstrap_admin` (`src/modules/auth/bootstrap.rs`, called from `src/main.rs`) seeds the first platform admin at startup when BOTH `ADMIN_EMAIL` and `ADMIN_PASSWORD` are set AND the `platform_admins` table is empty. That seeds a `platform_admins` row (`status = 'active'`, already verified), not a `users` row; the tenant identity plane is untouched. Sign in at `/platform/login` with these credentials. Legacy password login (`/api/v1/auth/login`) has no email-verification gate either, so both paths work with SMTP unconfigured.

This path is DEV ONLY (it is labelled as such in code and `.env.example`); it is ignored once any platform admin exists. Production should provision its first platform admin through a real workflow (signup form, IaC, or manual SQL) instead of this seed, per `auth::bootstrap`'s own module doc.

## What "no verification gate" means precisely

`email_verified` decides two things, neither of which blocks bootstrap-admin login:

- Invite consumption: a pending invite is honored only for a verified address. This one rule is enforced in two spots, so a reader grepping for the column finds more hits than there are rules: `place_bunyip_user` consumes the invite, and `find_bunyip_principal` carries the same `email_verified_at IS NOT NULL` condition inside its `has_pending_invite` EXISTS, which is the SQL half PMS-777 folded in so the path resolves placement, principal and invite on one pool checkout.
- Email persistence: the real address is stored on the JIT insert only when verified; otherwise a `<sub>@unresolved.invalid` placeholder is used (MAPPS-335). The placeholder is repaired on the first request after bunyip reports the address verified (`repair_placeholder_email`, PMS-635): the JIT insert runs once, so until then the row kept an address in the reserved `.invalid` TLD that every outbound email bounced off, and the invite gate above could never open for it.

There is no `RequireVerified` extractor and no `email_verified_at`-based 403 anywhere in the request path.

## Tests

`tests/suites/bunyip_login.rs`:

- `bootstrap_admin_unverified_email_still_authenticates` - `email_verified = false` (SMTP unconfigured) plus the bunyip `admin` claim still yields an authenticated session and tenant `Admin` (MAPPS-519: not `super_admin`).
- `bootstrap_admin_verified_email_still_authenticates` - `email_verified = true` (SMTP configured) yields the same tenant `Admin`, proving enabling email does not change what role the bunyip claim mints.
