#142 feat: OIDC single sign-on and browser-approved CLI login
Originally created by: thomijasir
Originally owned by: thomijasir
Summary
Adds OpenID Connect single sign-on for the Admin UI and a browser-approved login for the CLI, as designed in [#141].
Closes [#141], closes [#121], closes [#30]. [#31] (OIDC for CI) was closed as not planned, since runner tokens already cover machines.
Single sign-on
- Root configures one provider on a new Single sign-on page. There are presets for Google, Microsoft Entra ID, Okta, Auth0, Amazon Cognito, Clerk, Supabase and Keycloak, plus a generic option for Authentik, Zitadel, Rauthy and others. The presets live in
app/src/modules/sso/presets.toml, including each provider's quirks (which claim proves the email, whetherprompt=loginis sent, which issuer paths are refused), so adding a provider is one[[preset]]block with no database or UI change.CONTRIBUTING.mdexplains the fields. Saving reads the discovery document. Single sign-on only turns on after a test sign-in, which also links the root account. - Admins still create accounts. A first sign-in links to an admin or member account only when the provider marks the email as verified, and later sign-ins match the provider's subject. Root is never linked by email, and unknown emails are refused.
- Sign-ins use PKCE, a nonce, and a
statebound to the starting browser by a cookie. The callback hands off through a one-time code in the URL fragment, and the session is issued same-origin through the same code as password login. - Passwords become optional per account while single sign-on is on. Root can require it for everyone except root, who keeps a break-glass password, and can hide the password form on the sign-in page while passwords keep working in the CLI and API.
- Sensitive actions can be confirmed at the provider in a popup (
max_age=0,auth_timechecked), so accounts without a password can still reveal secrets, manage users and create agent tokens. - Setup needs an HTTPS public URL (localhost excepted). The page stays locked and says how to fix it.
- A new issuer removes the old links and turns single sign-on off until the next test sign-in. People then link again by verified email.
- Master key rotation, backup, restore and factory reset cover the encrypted client secret and the identities.
Browser CLI login
dopbase login --browserprints the approval link and an 8-character code, then waits.--browser-openalso opens the link, and--passwordforces the prompt. On a server with single sign-on, plaindopbase loginuses the browser./cli/approveis a standalone page. It uses the browser's current session and shows which account it will sign in, with a Switch button. With no session, or one that ends while the page is open, it goes to sign-in and comes back to the code without the "session expired" dialog. Codes are entered one character per box. Approving asks for a recent confirmation if the last one is older than 10 minutes, and the tab counts down and closes after approval (or says to close it when the browser refuses).- Codes expire after 10 minutes and work once. Polling honours
slow_down, wrong codes are rate limited per session, one client can have at most 20 requests waiting, and Ctrl+C cancels the request. - Revealing secrets from an account without a password asks for a browser approval instead of a password.
GET /api/v1/auth/sessionnow returns a CSRF token for the tab, derived on the server from the session's own token. A tab opened from a link, which includes every approval tab, can therefore make changes. This also fixes Admin UI tabs opened in a new window.
Fixes along the way
- The invalid email error code is spelled
EMAIL_INVALID(wasEMAIL_INVAILD). DbSwitchnever had an aria-label, because the binding always evaluated toundefined.isSafeRedirectrefuses backslashes and control characters, which browsers treat as//.- The confirm dialog clears the typed password whenever it closes.
app/build.rsrebuilds the binary when a migration file changes, so a new migration is never missing from local builds.- The SSO callback consumes its flow with two let-else steps instead of a match with early returns. CodeQL read those returns as values of the flow and reported the Admin UI page paths as hard-coded nonces. No hard-coded value ever reached the nonce, and behaviour is unchanged.
Review follow-ups
- The SQL for SSO, sign-in, CLI login and users moved from the services into each module's
repository.rs, as in the other modules. - Presets moved from a database CHECK, a Rust enum and a TypeScript union into
presets.toml. The settings response lists them aspresets, an unknown preset is refused withSSO_PRESET_INVALID, and a saved provider whose preset was later removed falls back to the generic one. - Code that hard-codes one of the fixed roles carries a
TODO(rbac):comment, sorg "TODO\(rbac\)"lists everything the RBAC work has to change. - Tutorials for Google and for Keycloak in Docker (with screenshots), and documentation images open full size when clicked.
Commit layout
The 41 commits are grouped by area for review, in this order:
- The error code rename.
- Build, migrations and dependencies.
- Server auth, device flow, OIDC client and SSO.
- Crypto, users and CLI.
- Server and CLI tests.
- UI building blocks, then pages and routes.
- Docs and changelog.
- Review follow-ups: repositories, presets in TOML,
TODO(rbac)markers, the image viewer, the tutorials and their changelog entries.
The whole series is built and tested at the head. The commits in between are grouped by area, so not every one of them builds on its own.
Compatibility
- Breaking for API clients that match on
EMAIL_INVAILD: the code is nowEMAIL_INVALID. - Seven new timestamped migrations, each with a down file.
sso_providers.presetis plain text with no CHECK list. That migration was edited in place during review, so a local database created from an earlier version of this branch has to be recreated. They only add tables (cli_device_requests,sso_providers,admin_identities,sso_flows) and columns on those new tables.adminskeeps its schema: an account without a password stores an empty hash that never verifies, so no table rebuild and no cascade intosessions. - Additive API changes:
GET /api/v1/auth/methods, the/api/v1/auth/cli/*and/api/v1/auth/sso/*endpoints,/api/v1/sso/provider*,DELETE /api/v1/users/{id}/sso,csrfTokenin the session response, andhasPassword/ssoLinkedon users. - With single sign-on off, nothing changes for existing users: password login works as before, and
POST /api/v1/usersstill requires a password. dopbase loginkeeps the password prompt unless the server has single sign-on on or password login off. A new CLI talking to an older server falls back to the password prompt.- New dependencies:
openidconnect4 (on the existing reqwest client, redirects disabled),webbrowser, thehostnamefeature ofnix, andp256for tests only.
Verification
bun run test:ui: 41 files, 305 tests passedbun run typecheck:ui,bun run lint:ui: cleanbunx prettier --check .: clean (apart from an untracked local.claude/settings.local.json)bun run test:docs,bun run build:docs: pass, no dead linksbun run test:github: 19 passedcargo fmt --check,cargo clippy --all-targets --all-features -- -D warnings: cleancargo test --all-targets --all-features: 418 passed, 0 failed, 1 ignored (already ignored on main)app/Dockerfile.teston Linux (scripts/verify-backend.sh): fmt, clippy, 414 tests passed with--test-threads=1, release build, and the server start and stop smoke test (run before the review follow-ups, not repeated since)
The server tests run against an in-process mock provider that signs ES256 ID tokens. They cover:
- linking rules;
- browser binding and handoff reuse;
- forged, replayed and wrong-audience tokens, and nonce and algorithm checks;
- the HTTPS requirement, issuer slash handling and switching providers;
- Require SSO and the hidden password form;
- SSO confirmation and the device flow edge cases;
- the derived tab CSRF token;
- backups and key rotation;
- the preset file: quirks, a broken file refused, unknown presets refused, and a removed preset still signing in.
The CLI tests drive dopbase login --browser and a passwordless --reveal in a pseudo-terminal.
Checked by hand in Chrome against a local mock provider, a server on an insecure public URL, and Keycloak in Docker earlier on:
- Setup locked on a non-HTTPS public URL, presets, test sign-in, turning on, Require SSO with its confirm dialog, hiding the password form, and
/login?password. dopbase login --browserapproved from a browser that was already signed in (new tab, no sign-in), from a signed-out browser (sign-in, then back to the code), with an older confirmation (confirm dialog, then SSO popup), and denied.- The session ending before opening the link, on the review page, and inside the confirm dialog. Each one goes to sign-in, with no "session expired" dialog.
- Phone width (390px) for the approve and login pages.
- After the review changes: a preset added only to
presets.toml(withprompt_login = false) showed up in the UI, saved, and changed the confirmation URL. After it was removed again, the saved provider still signed people in and approveddopbase login --browser.
Demo
dopbase login --browser against a local server:
Dopbase login
Server: http://localhost:18840
Approve this login in the Admin UI. Copy this link into a browser where you can sign in to Dopbase:
http://localhost:18840/cli/approve?code=JTVR-ZHPT
Then check that the page shows this code:
JTVR-ZHPT
Waiting for approval. Press Ctrl+C to cancel.
Logged in to http://localhost:18840.
Screenshots (sign-in from the approval link, the review page, the countdown after approval, code entry, the Single sign-on settings page, and the Users page):
Checklist
- [x] I kept this pull request focused on one problem.
- [x] I added or updated tests for behavior changes, or explained why tests are not needed.
- [x] I updated the documentation and
CHANGELOG.mdwhen public behavior changed, or explained why they are not needed. - [x] I added a screenshot or video demo for a feature or improvement, or explained why one is not needed.
- [x] I updated relevan documentation inside
/docs
Ticket changed by: dopbase