Skip to content

Verify company SSO email domains individually - #204

Merged
tnkuehne merged 2 commits into
mainfrom
feat/sso-multiple-domains
Nov 18, 2026
Merged

tnkuehne merged 2 commits into
mainfrom
feat/sso-multiple-domains

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A company SSO provider holds exactly one email domain. The session hook verifies an SSO address only when its domain matches that domain. Identity providers can return addresses on other domains of the same company, for example a Google Workspace with secondary domains. Those users get unverified accounts. A later personal "Sign in with Google" finds the account by email and cannot link it, so it fails with email_verification_required.

Change

Every company email domain is proven separately by DNS. Domains have their own list and status, as in WorkOS organization domains and Linear's approved email domains.

Data

  • New table sso_domain, one row per domain with its own TXT token and verified_at.
  • Verified domains are mirrored into sso_provider.domain, the comma-separated value Better Auth already reads. Before any domain is verified, the pending ones are mirrored instead. Better Auth's trust and linking decisions, email-first discovery and the session hook therefore accept every verified domain.
  • Adding a domain leaves verified domains and required SSO untouched.
  • Rules:
    • The last domain can't be removed.
    • The last verified domain can't be removed while SSO is required.
    • A settings import is checked against these rules before the connection is saved.
  • Every domain write locks the provider row, so concurrent verifications mirror the complete list.
  • Organizations that control the same DNS can still verify a shared domain, as before. Email-first discovery then asks for the organization link.
  • Providers verified through Better Auth before this change list their domains only in sso_provider. Those domains are adopted as verified, so tracking domains never revokes an existing proof.

Migration 0033_sso_domains

  • Seeds domains from existing providers.
  • Keeps a pending Better Auth token valid, so already published records still verify. The record name _better-auth-token-<provider>.<domain> is unchanged, and the identifier=value form is accepted too.
  • Accounts created earlier on a newly verified domain become verified on their next company sign-in.

API and CLI

  • POST /settings/domains and DELETE /settings/domains/{domain} add and remove a domain.
  • POST /settings/verify-domain takes an optional { domain }. Without a body it verifies every pending domain, which is what released CLIs send.
  • GET /settings adds ssoDomains with status and the DNS record per pending domain.
  • domain-verification returns the pending domains in records; the top-level fields stay for older clients.
  • In settings files, sso.domain stays a comma-separated list. It is required to register a connection; later it replaces the list when present. CLI export and apply keep working unchanged.

UI

  • New Email domains tab: one row per domain with status and Verify / Remove.
  • Add domain and Verify open a dialog showing the TXT record (type, name, value, each copyable).
  • The company SSO form asks for a domain only on first setup. Without a verified domain, Company sign-in links to the new tab.
  • Errors appear in the dialog or the row they concern. Status messages reset when switching tabs.
  • CopyValue now uses a new CopyButton, which the record rows reuse.

Google Workspace app identity keeps a single domain. App sign-in sends one hd hint and checks the hd claim against it.

Tests

  • Contracts: domain list parsing and its limits.
  • company-sso-plugin.integration.test.ts:
    • per-domain tokens and partial verification;
    • required SSO staying active while domains are added;
    • removal rules, the legacy record format, and shared domains across organizations;
    • concurrent verification of two domains;
    • a rejected import that leaves the connection unchanged;
    • CLI-style verification without a domain.
  • sso-membership.integration.test.ts: an alias user is verified on their next SSO sign-in.
  • sign-in-discovery.integration.test.ts: exact routing of every listed domain.
  • organization-settings.integration.test.ts: a Google app identity with several domains is rejected.
  • E2E:
    • onboarding.spec.ts: add a domain with validation, show the record dialog, remove a domain, and re-create the connection.
    • setup.spec.ts: a failed then successful verification in the dialog; a migrated connection without a domain; and API verification without a body.
  • Local runs: pnpm check, full vitest run (1056 passed), and test:e2e without the CLI and source suites (75 passed).

Visual review

  • Goal: manage several domains without a long page. Each domain shows its own status, and the DNS record appears only when it is needed.
  • Views checked at 1280 px and 390 px, in light and dark themes (Playwright screenshots from the E2E fixtures):
    • pending list
    • verified list with success message
    • record dialog
    • add dialog with validation error
    • Company sign-in tab without a verified domain
  • Fixed during review:
    • domain names truncated at 390 px; the domain now gets its own line;
    • the stale "Settings saved." banner after switching tabs;
    • a duplicate close action in the dialog.
  • Not verified: keyboard-only use of the dialog. It uses the shared shadcn Dialog with focus handling and a submit form.

Screenshots

Before → after: Company sign-in before a domain is verified. The DNS record moves out of this tab.

Before After
Before: verification card with record After: link to Email domains

Email domains tab (1280 px)

Pending Verified
Pending domains, light Verified domain, light
Pending domains, dark Verified domain, dark

Dialogs (1280 px)

DNS record Validation error
Record dialog, light Add dialog with error, light
Record dialog, dark Add dialog with error, dark

Mobile (390 px)

Pending, light Pending, dark Record, light Record, dark
Mobile pending, light Mobile pending, dark Mobile record, light Mobile record, dark

Documentation: #205 (draft, publish after release).

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 07ea243 Nov 18 2026, 02:32 PM

@tnkuehne
tnkuehne force-pushed the feat/sso-multiple-domains branch 2 times, most recently from 142fee5 to 531f3cf Compare November 18, 2026 14:20
A company SSO provider held exactly one email domain. Organizations whose
identity provider issues addresses on several domains, such as a Google
Workspace with secondary domains, got unverified accounts for every address
outside that domain. Those accounts could not link a personal Google or
Microsoft sign-in later.

Track each company email domain in sso_domain with its own DNS token and
verification state. Verified domains are mirrored into the Better Auth
provider, so SSO trust, email-first discovery and the session hook accept
any of them. A new domain stays pending without affecting verified ones, so
adding a domain never interrupts required company SSO. The last verified
domain cannot be removed while SSO is required, and a domain verified by
another organization is refused.

Settings gain an Email domains section where administrators add, verify
and remove domains; the DNS record is shown in a dialog. The API adds
POST /settings/domains and DELETE /settings/domains/{domain}, and
verify-domain accepts an optional domain. Settings files keep `sso.domain`
as a comma-separated list for existing CLI export and apply.

The migration seeds domains from existing providers and keeps a pending
Better Auth token valid. Accounts created earlier on a newly verified domain
become verified on their next company sign-in. Google Workspace app
identity keeps a single domain because app sign-in checks one hd claim.
@tnkuehne tnkuehne changed the title Support multiple verified email domains for company SSO Verify company SSO email domains individually Nov 18, 2026
Lock the provider row for every domain write so concurrent verifications
mirror the complete verified list. Check a settings import's domain list
before Better Auth saves the connection, so a rejected import changes
nothing. Keep allowing organizations that control the same DNS to verify a
shared domain, as before; email-first discovery already asks for the
organization link in that case.
@tnkuehne
tnkuehne merged commit 94b46c6 into main Nov 18, 2026
22 of 27 checks passed
@tnkuehne
tnkuehne deleted the feat/sso-multiple-domains branch November 18, 2026 15:13
tnkuehne added a commit that referenced this pull request Nov 23, 2026
## Problem

An organization with company SSO proves its email domains through DNS.
Since #291, its SSO session also reaches the person's other memberships
for an address on those domains. The company still did not own sign-in
for these addresses:

- An email code sent through an organization link or the API signed them
in.
- Google and Microsoft signed them in.

The resulting personal session reached every membership without passing
the company's sign-in. Required SSO protected only the organization
itself.

## Change

Organizations with active company SSO now own sign-in for their verified
domains, as WorkOS domain policies do.

**Setting**
- New column `organization_settings.domain_sso_required` (migration
`0040_domain_sso`).
- Activating company SSO turns it on; removing the connection turns it
off. The migration copies `sso_required`, so organizations that already
require SSO keep that requirement for their domains.
- Administrators switch it on the **Company sign-in** page, with `PUT
/api/v1/settings/domain-sso` or with `widefleet settings domain-sso
true|false`. Turning it on requires active SSO and a verified domain.
`GET /settings` adds `domainSsoRequired`.
- The rule applies only to DNS-verified domains of an organization the
installation admits. An operator-trusted provider without DNS proof does
not claim addresses.

**Enforcement**
- **Session creation** rejects a personal session for such an address
with `COMPANY_SSO_REQUIRED` and the provider ID. Every personal sign-in
method passes this check. Company SSO, another organization's SSO for a
guest, and recovery stay available.
- **Email codes:** `send-verification-otp` and `sign-in/email-otp`
return the same error, so no code is sent. The sign-in form continues
with company SSO, as the email field already does.
- **Google and Microsoft:** `validateUserInfo` rejects the address
before any user, account or session is created. The callback returns to
`/sign-in/connect` with `error=company_sso_required` and an encrypted,
10-minute hint (`symmetricEncodeJWT` with the auth secret). The page
decrypts it and starts company SSO with the address as login hint and
the original `next`, so OAuth and device continuations survive. A loop
guard falls back to a button.
- **One organization per domain:** verifying a domain that another
organization has already verified fails with a conflict. Earlier,
organizations that controlled the same DNS could both verify a domain;
that was kept in #204 to avoid a failing unique-index migration. The
check runs at verification and needs no index. With shared domains, an
address would have had two company sign-ins.
- Existing sessions and CLI grants stay valid until they end.
- **Joining by email domain (#300):** these addresses can no longer
prove their domain through Google or Microsoft. A linked company SSO
account of the organization that verified the domain by DNS now proves
it instead, so its administrators can still open organizations to the
domain.

**UI**
- New section **Company addresses use SSO only** with a switch on the
Company sign-in page. The switch is optimistic: a failed change returns
to the previous state and explains why in a toast. The shared `Toaster`
and `toast` export are the same lines as in #288.
- `/sign-in/connect` gets a **Continue with company SSO** state.

## Tests

- `social-sign-in.test.ts`:
- Google and Microsoft, for existing and new users, return the encrypted
hint without the address in the URL and create nothing.
  - Email code send and verify return `COMPANY_SSO_REQUIRED`.
- `organization-sso.integration.test.ts`:
  - A personal session is rejected.
  - An address on another domain still signs in.
  - Company SSO, another organization's SSO and recovery still work.
- A non-admitted organization, inactive SSO and a disabled rule each
lift the requirement.
- `organization-settings.integration.test.ts`:
- Activation turns the rule on and administrators can turn it off and on
again.
  - Removing SSO clears it; enabling without SSO conflicts.
- `company-sso-plugin.integration.test.ts`:
  - Activation through settings enables the rule.
  - A domain verified by another organization is rejected.
- `cli.test.ts`: `settings domain-sso`.
- **Local runs:**
  - `vitest run`: 1255 passed, 667 skipped.
  - `RUN_CLI_TESTS=1` CLI tests: 60 passed.
  - Control-plane `svelte-check`, oxlint and oxfmt on the changed files.
  - `cargo fmt --check` and `cargo clippy -p platform-cli -D warnings`.
- Playwright E2E was not run locally; CI covers it.

## Visual review

Goal: administrators see which addresses the rule covers and can switch
it. A person who picks Google is taken to their company's sign-in.

Checked at 1280 and 390 px in light and dark themes, rendered with the
built server and synthetic fixtures. No horizontal overflow, the switch
stays aligned with the title at 390 px, and the domain list wraps.

**Company sign-in, before → after (1280, light)**

| Before | After |
| --- | --- |
| <img width="420"
src="https://gist.githubusercontent.com/tnkuehne/ee0d49f9b80b16f92500a55f2e180ccd/raw/20afa5d25570a5f6b2f7301c306a5a72b4e7a031/before-sso-1280-light.png">
| <img width="420"
src="https://gist.githubusercontent.com/tnkuehne/ee0d49f9b80b16f92500a55f2e180ccd/raw/20afa5d25570a5f6b2f7301c306a5a72b4e7a031/after-sso-1280-light.png">
|

**Switched off at once (390, dark) and narrow layout (390, light)**

| Off (optimistic) | On |
| --- | --- |
| <img width="300"
src="https://gist.githubusercontent.com/tnkuehne/ee0d49f9b80b16f92500a55f2e180ccd/raw/20afa5d25570a5f6b2f7301c306a5a72b4e7a031/after-sso-off-390-dark.png">
| <img width="300"
src="https://gist.githubusercontent.com/tnkuehne/ee0d49f9b80b16f92500a55f2e180ccd/raw/20afa5d25570a5f6b2f7301c306a5a72b4e7a031/after-sso-390-light.png">
|

**Google or Microsoft with a company address**: shown only when the
automatic start is skipped by the loop guard; otherwise the browser goes
straight to company SSO.

| 390, light | 1280, dark |
| --- | --- |
| <img width="300"
src="https://gist.githubusercontent.com/tnkuehne/ee0d49f9b80b16f92500a55f2e180ccd/raw/20afa5d25570a5f6b2f7301c306a5a72b4e7a031/after-connect-390-light.png">
| <img width="420"
src="https://gist.githubusercontent.com/tnkuehne/ee0d49f9b80b16f92500a55f2e180ccd/raw/20afa5d25570a5f6b2f7301c306a5a72b4e7a031/after-connect-1280-dark.png">
|

Not checked visually: the toast after a failed switch (same component
and pattern as #288), and the expired-hint state, which shows the text
without the button.

Documentation: #307 (draft until a release includes this change).
















<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><a
href="https://app.greptile.com/api/retrigger?id=115870316&amp;sig=775908ce03283199d8ab76b3b29d4e0b6b28c1a226cf27f9b5ecb19862b1fdfb"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F306&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier:
apex" title="Review tier: apex"
src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-apex.svg?v=3"
height="12"></sup></h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge with
#307’s immediate switch handler included.

<details open><summary><h3>Summary</h3></summary>

Requires company SSO when a personal sign-in uses an address on an
admitted organization’s verified domains.

- Administrators can require company SSO for addresses on verified
domains.
- Personal sign-ins with covered addresses continue through company SSO.
- Each DNS-verified company domain belongs to one organization.
- Linked company SSO can prove its verified domain for organization
joining.

Tests and browser checks were not rerun.
</details>

<details open><summary><h3>Diagram</h3></summary>

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Email code, Google, or Microsoft"] --> B{"Company SSO required for this domain?"}
  B -->|No| C["Create personal session"]
  B -->|Yes| D["Continue with company SSO"]
  D --> E["Create company session"]
  F["Other organization SSO or recovery"] --> E
  G["Other personal sign-in methods"] --> H["Check domain rule before creating session"]
  H --> B
```
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (8) · Last reviewed commit: ["Accept company SSO on a
verified domain
..."](e687348)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant