Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
SAML SSO account linking — implementation plan
Spec: .ai/specs/2026-08-29-saml-sso-account-linking.md (see §0 Design revision v2 — authoritative)
Branch: little-rock
Posture: staff-operated v1; app-layer (no DB triggers T1–T4); reuse ssoConnection/ssoDomain;
provider-agnostic linking keyed on the auth.identities.email column. Full code + unit tests +
migration rollback-validation; draft PR flagged e2e-unverified (needs a SAML env + the
crbnos/supabase fork's GOTRUE_DISABLE_SIGNUP=true + Kong/allow-list fixes — not in this repo).
Progress
- Task 1: Pre-seed helpers in
provisioning.server.ts(+ pure unit-tested builders) - Task 2: Wire backfill + audit into
verifySsoDomain; wire removal intoremoveSsoDomain - Task 3: Seed newly-invited users in the three account-creation flows
- Task 4: Migration —
auth.sso_domainsguard trigger +ssoReservedDomain(defense-in-depth) - Task 5: Unit tests + migration rollback-validation script
- Task 6: Docs (AGENTS.md, spec→implemented note) + draft PR
Dependencies
- Task 2, 3 need Task 1 (helpers). Task 4 is independent of 1–3. Task 5 needs 1–4. Task 6 last.
Task 1: Pre-seed helpers in provisioning.server.ts
Files:
- Modify:
packages/ee/src/sso/provisioning.server.ts— add three server helpers + two pure builders. - Copy from (precedent):
linkSsoIdentityToUserin the same file (rawsqloverauth.identities,{ data, error }return,try/catch+ logger).
Steps:
- Add a pure, exported, unit-testable builder:
// The provider column value for a GoTrue SSO provider id. export function ssoProviderColumn(providerId: string): string { return `sso:${providerId}`; } // The lowercased domain of an email, or null. Mirrors uncoveredSsoDomainError's split. export function emailDomain(email: string): string | null { return email.split("@")[1]?.trim().toLowerCase() ?? null; } seedSsoIdentityForUser(db, { userId, email, providerId })— insert ONE pre-seed row. The load-bearing match is the generatedemailcolumn (lower(identity_data->>'email'));provider_idis a readable placeholder. Provider-agnostic (works for any IdP via GoTrue's email fallback).
ReturnINSERT INTO auth.identities (provider_id, user_id, identity_data, provider, created_at, updated_at) VALUES (${lowerEmail}, ${userId}::uuid, jsonb_build_object('sub', ${lowerEmail}::text, 'email', ${lowerEmail}::text), ${'sso:' + providerId}, now(), now()) ON CONFLICT (provider_id, provider) DO NOTHING{ data: { seeded: boolean } | null, error }. Lowercase the email in TS before binding.backfillSsoIdentitiesForDomain(db, { providerId, domain })— set-based seed of every existing non-SSO user on the domain (adopted policy §4: verified owner controls the domain instance-wide, so NOT company-scoped). Return the linked user ids for audit.
ReturnINSERT INTO auth.identities (provider_id, user_id, identity_data, provider, created_at, updated_at) SELECT lower(u.email), u.id, jsonb_build_object('sub', lower(u.email), 'email', lower(u.email)), ${'sso:' + providerId}, now(), now() FROM auth.users u WHERE u.email IS NOT NULL AND u.is_sso_user = false AND lower(split_part(u.email, '@', 2)) = ${lowerDomain} ON CONFLICT (provider_id, provider) DO NOTHING RETURNING user_id{ data: { linkedUserIds: string[] } | null, error }.removeSsoIdentitiesForDomain(db, { providerId, domain })— delete this domain's SSO identities, keyed on theemailcolumn so it also catches GoTrue's self-healed opaque-NameID rows:
ReturnDELETE FROM auth.identities WHERE provider = ${'sso:' + providerId} AND lower(split_part(email, '@', 2)) = ${lowerDomain}{ data: { removed: number } | null, error }.- Match the existing file's error handling exactly (logger.error +
{ data: null, error }).
Verify:
pnpm --filter @carbon/ee typecheck
# Expected: no errors
Out of scope: touching linkSsoIdentityToUser / migrateUserToSso / deleteJitSsoUser (the
DISABLE_SIGNUP=false repair path — left intact as a fallback), is_sso_user.
Task 2: Wire backfill + audit into verifySsoDomain; removal into removeSsoDomain
Depends on: Task 1 Files:
- Modify:
packages/ee/src/sso/connections.server.ts— add adb: Kysely<KyselyDatabase>param toverifySsoDomainandremoveSsoDomain; call the Task-1 helpers after the existing success writes. - Modify:
apps/erp/app/routes/x+/settings+/sso.tsx— passgetDatabaseClient()at both call sites (intent === "verifyDomain",intent === "removeDomain"). - Audit: import
insertAuditLogEntriesfrom@carbon/database(seepackages/database/src/audit.ts:205).
Steps:
verifySsoDomain(serviceRole, db, args): after theupdateat line 345–356 succeeds (row flipped toverified), callbackfillSsoIdentitiesForDomain(db, { providerId: connection.data.providerId, domain: row.data.domain }). On its error, log and continue (verification already committed — do NOT fail the verify). Then best-effort audit (wrapped in try/catch, never throws):insertAuditLogEntries(serviceRole, args.companyId, [{ tableName: "auth.identities", entityType: "ssoIdentityBackfill", entityId: row.data.domain, recordId: row.data.domain, operation: "INSERT", actorId: args.userId, diff: null, metadata: { domain, providerId, linkedCount, requestId: null, ipAddress: null, userAgent: null, origin: null } }])— confirm the exactCreateAuditLogEntryshape inpackages/database/src/audit.types.tsand match it. If audit is disabled for the company the RPC errors — swallow + log (a privilege event we try to record, but must not block verify).removeSsoDomain(serviceRole, db, args): after the row is deleted and GoTrue re-synced (line 388+), ifwasVerified, callremoveSsoIdentitiesForDomain(db, { providerId: connection.providerId, domain: row.data.domain }). Needs the connection's providerId — read it viagetSsoConnectionas the existingwasVerifiedbranch already does (line 402). On error, log + continue.- Update
sso.tsx:import { getDatabaseClient } from "~/services/database.server";and pass it as the new 2nd arg in both calls.
Verify:
pnpm --filter @carbon/ee typecheck && pnpm exec turbo run typecheck --filter=erp
# Expected: no errors
Out of scope: changing the shipped GoTrue-sync ordering in verify/remove (its lockout-avoidance reasoning is load-bearing — the guard in Task 4 is designed around it).
Task 3: Seed newly-invited users in the three account-creation flows
Depends on: Task 1 Files:
- Modify:
apps/erp/app/modules/users/users.server.ts— after each ofcreateEmployeeAccount,createCustomerAccount,createSupplierAccountresolves the auth user id and creates the user, seed an SSO identity when the email's domain already has a verified connection. - Read first: the three flows (around L293/408/549 per the code map) to find the exact point after
resolveAuthUserId+createUserwhereuserId,email, a service-role client, and a db handle are all in scope.
Steps:
- Add a small server helper (co-located, e.g. in
users.server.tsor a thin wrapper over Task 1):maybeSeedSsoIdentity(serviceRole, db, { userId, email })that callsgetSsoConnectionByDomain(@carbon/ee/sso.server) foremailDomain(email); if a verified connection exists, callsseedSsoIdentityForUser(db, { userId, email, providerId: connection.providerId }). Best-effort: log on error, never throw (account creation must not fail because seeding failed — the user still works via the backfill path or the callback repair fallback). - Call it in all three flows after the account row + auth user exist. If any flow lacks a Kysely db
handle, thread
getDatabaseClient()from the caller — if a flow's shape makes this unsafe (e.g. it runs before the auth user is committed), STOP and report; do not reorder account creation. isSsoEnabled()self-gatesgetSsoConnectionByDomain, so off-Enterprise this is a no-op.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: no errors
Out of scope: the invite-acceptance / callback flow (already handled by migrateUserToSso).
Task 4: Migration — auth.sso_domains guard + ssoReservedDomain (defense-in-depth)
Depends on: none Files:
- Create:
packages/database/supabase/migrations/<ts>_sso-domain-guard.sqlviapnpm db:migrate:new sso-domain-guard(never000000HHMMSS).
Steps:
ssoReservedDomaintable:("domain" TEXT PRIMARY KEY), seeded with the same values asPUBLIC_EMAIL_DOMAINSinapps/erp/app/modules/settings/settings.models.tsPLUS Carbon-own domains (carbon.ms,carbon.us.org,carbonms.onmicrosoft.com— verify current list). Add a header comment cross-referencingPUBLIC_EMAIL_DOMAINSto keep them in sync. This table is NOT company-scoped (global reserved list) — RLS: enable + a permissiveSELECTfor authenticated, no write policy (staff/service-role only). UseINSERT ... ON CONFLICT DO NOTHINGfor idempotency.- Guard function + trigger on
auth.sso_domains(precedent:create_public_useronauth.users,SECURITY DEFINER SET search_path = publicper house convention):
NOTE: requires a claim row (any status), NOTCREATE OR REPLACE FUNCTION auth.enforce_sso_domain_claim() RETURNS TRIGGER LANGUAGE plpgsql SECURITY DEFINER SET search_path = public AS $$ BEGIN IF current_setting('app.sso_domain_override', true) = 'on' THEN RETURN NEW; END IF; IF EXISTS (SELECT 1 FROM public."ssoReservedDomain" WHERE lower("domain") = lower(NEW."domain")) THEN RAISE EXCEPTION 'auth.sso_domains insert blocked: % is a reserved domain', NEW."domain"; END IF; IF NOT EXISTS (SELECT 1 FROM public."ssoDomain" WHERE lower("domain") = lower(NEW."domain")) THEN RAISE EXCEPTION 'auth.sso_domains insert blocked: no ssoDomain claim for %', NEW."domain"; END IF; RETURN NEW; END $$; DROP TRIGGER IF EXISTS enforce_sso_domain_claim ON auth.sso_domains; CREATE TRIGGER enforce_sso_domain_claim BEFORE INSERT ON auth.sso_domains FOR EACH ROW EXECUTE FUNCTION auth.enforce_sso_domain_claim();verified, because the shippedverifySsoDomainsyncs GoTrue (which writesauth.sso_domains) while the row is stillpending— a verified-only guard would break the happy path or force a lockout-prone reorder. Reserved domains can never get a claim row via the app validator anyway; the reserved check makes that hold in-DB too. - Idempotent:
CREATE TABLE IF NOT EXISTS,CREATE OR REPLACE FUNCTION,DROP TRIGGER IF EXISTS. pnpm run generate:typesafter — only if the local DB is reachable and the migration applies.ssoReservedDomainis not referenced from TS (guard is SQL-only), so a types regen is not required to typecheck. If the DB is not reachable, document the regen as a deploy step. Do NOT rebuild the DB to test — wait for the user.
Verify:
# Rolled-back psql transaction (see Task 5). Do not run a full db:migrate rebuild.
Out of scope: touching auth.sso_providers, create_public_user, or the ssoDomain/ssoConnection
tables' definitions.
Task 5: Unit tests + migration rollback-validation
Depends on: Tasks 1–4 Files:
- Create:
packages/ee/src/sso/provisioning.test.ts(or extend an existing sibling test) — pure-fn tests foremailDomain,ssoProviderColumn(edge cases: no@, uppercase, whitespace, subdomains). - Create:
.ai/runs/2026-08-29-sso-domain-guard-rollback.sql— a BEGIN/…/ROLLBACK script (precedent: memoryreference_migration_rollback_validation) asserting: (a) insertingauth.sso_domainsfor a domain with assoDomainclaim succeeds; (b) without a claim → RAISES; (c) for a reserved domain → RAISES; (d) withset_config('app.sso_domain_override','on',true)→ succeeds. Run viasupabase_adminif the DB is reachable; otherwise leave it as a documented validation artifact.
Verify:
pnpm --filter @carbon/ee test
# Expected: new tests pass
Out of scope: e2e SAML flow (env-gated — documented in the PR, not tested here).
Task 6: Docs + draft PR
Depends on: Tasks 1–5 Files:
- Modify:
packages/ee/AGENTS.md— add the newprovisioning.server.tsexports + the pre-seed model to the SSO bullet. - Modify:
.claude/rules/authentication-system.md— if it documents the SSO linking flow, add the pre-seed/DISABLE_SIGNUP=true model (read first; only if it covers this). - Modify:
.ai/specs/2026-08-29-saml-sso-account-linking.md— leave in place; move tospecs/implemented/only after e2e verification (it is not verified here). - Commit via
/check-and-commit; open a draft PR ontomainwith the deployment prerequisites and the e2e-unverified flag spelled out.
Verify:
gh pr view --json isDraft,title
# Expected: isDraft = true