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.
Enterprise SAML SSO — implementation plan
Spec: .ai/specs/2026-08-21-enterprise-saml-sso.md Research: .ai/research/sso.md Branch: victoria
HISTORICAL RECORD — several planned flows were later superseded. This plan describes what was built at execution time; the spec's Changelog is the source of truth for the final design. In particular: the archival/reassignment flow (Task 6) was replaced by link-instead-of-archive (nothing is deactivated;
reassignUserReferences/REASSIGNABLE_USER_COLUMNSwere deleted as dead code); the "Users to re-invite" card (Task 9 step 5) became "Covered Users" and was later removed entirely (coverage is documented on the Identity Provider card instead); and the controlled-environment TOTP exception (Task 10) was removed — SSO sessions skip the MFA gate in EVERY environment. Do not reintroduce these flows from this document.
Progress
- Task 1: Migration —
ssoConnectiontable + crash-freecreate_public_user(20260820215433_sso-connection.sql) - Task 2: Apply migration + regenerate types (applied via
pnpm db:migrate; ssoConnection in types.ts, 4 RLS policies confirmed in DB) - Task 3: Add
"sso"auth provider to env (typecheck green) - Task 4:
@carbon/auth/sso.server— GoTrue admin wrappers + session helper (typecheck green) - Task 5: SSO connection validator + service functions. Validator barreled via
~/modules/settings; service fns insettings.server.ts(NOT barreled — deliberate, service-role imports must not leak to browser; import~/modules/settings/settings.serverdirectly); MES copy atapps/mes/app/services/sso.service.ts. Typecheck green. - Task 6:
reassignUserReferences+migrateUserToSsotransaction (users.sso.server.ts). Deviations (schema-forced, documented in-file): list is 39 entries not 43 (part.assignee/service.assigneemoved toitemby 20240601210618;nonConformanceInvestigationTaskdropped by 20251120214020;approvalRequest.approverIddropped by 20260204204700) —satisfiesguard makes the list compile-time-checked against Kysely schema; fresh-user-without-old-account edge throws "re-invite" instead of guessing an employeeType. Typecheck green. - Task 7: Callback SSO branch (ERP + MES). ERP: provider→connection→company binding, domain enforcement w/ orphan cleanup, invite lookup (ilike), migrateUserToSso w/ multi-company + generic error flashes, company binding + group re-resolve, AccountLockout reset, CONTROLLED_ENVIRONMENT TOTP gate vs
mfaVerified: true, company cookie always set. MES: same enforcement, no migration (flash "complete first SSO sign-in in Carbon ERP");getSsoConnectionByProviderIdadded to MES sso.service. All 5 packages typecheck green; biome clean. - Task 8: Login "Continue with SSO" (ERP + MES) + public domain-check API route. Public
/api/sso/checkin BOTH apps (assertIsPost,sso-check:{ip}sliding-window 20/1h, boolean-only{ enabled }reply; MES has its own api+ route +path.to.api.ssoCheck); "Continue with SSO" button + email-first handler on both login pages;?email=prefill via ValidatedForm defaultValues. Typecheck green (erp + mes). - Task 9: Settings → SSO screen + nav + paths.
x+/settings+/sso.tsx(Enterprise redirect, settings perms, intent-branched action, SP-details Card withCopyfrom @carbon/react, ValidatedForm, deactivate confirm modal inlined from ConfirmDelete pattern, re-invite card via existinggetEmployees); nav entry Enterprise-gated viauseFlags().isEnterprise. Note:defaultValues.domainscarries anas unknown as string[]cast (zfd transform output type vs form input string — commented in-file). Typecheck + biome green. - Task 10: MFA exemption for SSO sessions.
ssoProviderId?on AuthSession (types.ts), preserved in refreshAuthSession beside mfaVerified; ERP + MES shellrequireMfagates exempt SSO sessions unless CONTROLLED_ENVIRONMENT.requireAuthSessionbounce verified to honormfaVerifiedat mint (session.server.ts:346) — no change needed there.path.to.sso+path.to.api.ssoCheckpre-staged in path.ts. - Task 11: SSO-aware invite emails.
getInviteLink(serviceRole, email, code)in users.server.ts (falls back to code link on lookup error); wired into employees.new / customers.new / suppliers.new / resend-invite (single path); bulk path inlined in jobs user-admin.ts (cannot import ERP modules). Typecheck green (erp + @carbon/jobs). - Task 12: GoTrue SAML config (dev + prod compose, .env.example). Routing verified: kong.yml has NO key-auth plugin anywhere and the
auth-v1catch-all proxies/auth/v1/→ GoTrue with CORS only; prod bind-mounts the same kong.yml behind Caddy's wholesalereverse_proxy kong:8000— SAML ACS/metadata endpoints pass through in dev and prod with no change. - Task 13: Unit tests.
users.sso.server.test.ts(19 tests: archived email, multi-company predicate, permission merge/remove round-trips incl. "0" wildcard, REASSIGNABLE_USER_COLUMNS audit-exclusion invariants) +packages/auth/src/services/sso.server.test.ts(5 tests: provider-id extraction). All pass; full @carbon/auth suite still green (36 tests). Note: ERP has no package.jsontestscript — invokepnpm --filter erp exec vitest run <pattern>. DB-backed transactional test skipped (no Kysely test harness exists; per plan, not built). - Task 14 (automated gates): typecheck green across @carbon/env, @carbon/auth, @carbon/jobs, erp, mes; biome clean on all SSO files; unit tests pass. Kong fix discovered during verification: GoTrue self-declares entityID/ACS WITHOUT /auth/v1 (from API_EXTERNAL_URL) and validates assertion Destination against it — added
auth-v1-ssoservice to kong.yml routing/sso/→ gotrue (fixes dev AND prod, same bind-mounted file); settings screen now displays the canonical un-prefixed URLs. Verified live:/sso/saml/metadatareturns 200 with real SP metadata. Browser walkthrough vs a mock IdP: pending user go-ahead. - Gotcha (dev ops):
crbn reloadpasses only--env-file .env.localto compose — root.envvars referenced by docker-compose (SAML_ENABLED, SAML_PRIVATE_KEY) must be exported in the shell when reloading gotrue, or it comes back with SAML off.crbn up(run by the user normally) loads them fine.
Dependencies
- Task 2 needs Task 1. Tasks 4–6 need Task 2 (generated types) and Task 3.
- Task 7 needs Tasks 4, 5, 6. Task 8 needs Tasks 3, 5. Task 9 needs Tasks 4, 5.
- Task 10 needs Task 7. Task 11 needs Task 5. Task 12 independent (can run anytime).
- Task 13 needs Tasks 6, 7. Task 14 last.
- Independent groups after Task 3: {4}, {5}, {12} may run in parallel; then {6}, {8 (api route part)}.
Design refinements forced by the schema sweep (surfaced for user review)
SUPERSEDED — see the spec changelog (
.ai/specs/2026-08-21-enterprise-saml-sso.md): SSO is now self-hosted-only and existing active accounts are LINKED (never archived), so the multi-company block, archival, and reassignment machinery below were deleted.
user.emailUNIQUE index (index_user_email_key,20230123004116_users-and-companies.sql:16) +create_public_user()conflicting only on(id)means a duplicate-email SSO signup crashes the trigger inside GoTrue's own transaction — the SAML login would fail opaquely before Carbon code runs. Fix: the trigger skips thepublic."user"insert when the email belongs to a DIFFERENT id (no mutation of the old row — a rogue IdP must not be able to touch it). The callback migration transaction then owns creating the new row after domain+invite verification.- Multi-company old accounts are BLOCKED from SSO migration in v1. If the same-email old user has
userToCompanyrows in companies other than the SSO company, archiving their email/account would break their other-company logins. The callback rejects with a clear flash ("this account belongs to multiple companies — contact support"). Single-company accounts (the normal case) migrate automatically. This is a documented v1 limitation, echoed in the spec changelog. - Old-account archival: within the migration transaction the old
public."user"row getsactive = falseand its email rewritten tosso-archived+{oldId}+{originalEmail}(frees the unique index; theemployeesview already filtersu.active = TRUEso the mangled email is never displayed). Post-commit, the oldauth.usersaccount is deleted via the admin API (revokes its sessions; auth-level email freed).fullNameis untouched, so audit references still render the person's name. - Reassignment column list (mutable, forward-looking, company-scoped — from the FK sweep):
assigneeon:changeOrder,changeOrderActionTask,customer,item,job,jobOperation,maintenanceDispatch,nonConformance,nonConformanceActionTask,nonConformanceApprovalTask,nonConformanceInvestigationTask,nonConformanceReviewer,part,pickingList,procedure,purchaseInvoice,purchaseOrder,purchasingRfq,qualityDocument,quote,receipt,riskRegister,salesOrder,salesOrderShipment,salesRfq,service,shipment,stockTransfer,supplier,supplierQuote,trainingquote.salesPersonId,quote.estimatorId,quoteLine.estimatorId,salesRfq.salesPersonId,salesOrder.salesPersonId,approvalRequest.approverId,employeeJob.managerId,costCenter.ownerId,purchaseOrderLine.ownerId,purchaseInvoiceLine.ownerId,supplierQuoteLine.ownerId,workflow.ownerId- Excluded by policy: all audit actors (
createdBy,updatedBy,closedBy,postedBy),workflowRun.ownerId(historical record),companyGroup.ownerId(group-scoped, not company-scoped).
Task 1: Migration — ssoConnection table + crash-free create_public_user
Depends on: none Files:
- Create:
packages/database/supabase/migrations/<timestamp>_sso-connection.sql(viapnpm db:migrate:new sso-connection; verify HHMMSS ≠000000and timestamp is newer than the newest existing migration) - Copy from (precedent):
packages/database/supabase/migrations/20260810100100_workflows-foundation.sql(table + RLS block),20260319000000_console-mode.sql(trigger body being replaced)
Steps:
pnpm db:migrate:new sso-connection- Write exactly:
CREATE TABLE IF NOT EXISTS "ssoConnection" (
"id" TEXT NOT NULL DEFAULT id('sso'),
"companyId" TEXT NOT NULL,
"providerId" TEXT NOT NULL,
"domains" TEXT[] NOT NULL,
"metadataUrl" TEXT,
"metadataXml" TEXT,
"active" BOOLEAN NOT NULL DEFAULT TRUE,
"createdBy" TEXT NOT NULL REFERENCES "user"("id"),
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
"updatedBy" TEXT REFERENCES "user"("id"),
"updatedAt" TIMESTAMP WITH TIME ZONE,
PRIMARY KEY ("id", "companyId"),
FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE,
CONSTRAINT "ssoConnection_providerId_key" UNIQUE ("providerId"),
CONSTRAINT "ssoConnection_metadata_check" CHECK (num_nonnulls("metadataUrl", "metadataXml") = 1)
);
CREATE INDEX IF NOT EXISTS "ssoConnection_companyId_idx" ON "ssoConnection" ("companyId");
CREATE INDEX IF NOT EXISTS "ssoConnection_createdBy_idx" ON "ssoConnection" ("createdBy");
CREATE INDEX IF NOT EXISTS "ssoConnection_updatedBy_idx" ON "ssoConnection" ("updatedBy");
CREATE INDEX IF NOT EXISTS "ssoConnection_domains_idx" ON "ssoConnection" USING GIN ("domains");
ALTER TABLE "ssoConnection" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "SELECT" ON "public"."ssoConnection"
FOR SELECT USING (
"companyId" = ANY ((SELECT get_companies_with_employee_role())::text[])
);
CREATE POLICY "INSERT" ON "public"."ssoConnection"
FOR INSERT WITH CHECK (
"companyId" = ANY ((SELECT get_companies_with_employee_permission('settings_update'))::text[])
);
CREATE POLICY "UPDATE" ON "public"."ssoConnection"
FOR UPDATE USING (
"companyId" = ANY ((SELECT get_companies_with_employee_permission('settings_update'))::text[])
);
CREATE POLICY "DELETE" ON "public"."ssoConnection"
FOR DELETE USING (
"companyId" = ANY ((SELECT get_companies_with_employee_permission('settings_update'))::text[])
);
-- Crash-free public-user creation: an SSO signup whose email already belongs to a
-- DIFFERENT public."user" row must not violate index_user_email_key inside GoTrue's
-- transaction. The row is skipped here; the SSO callback's migration transaction owns
-- creating it after domain + invite verification. The existing row is never mutated
-- from this trigger (a rogue IdP must not be able to touch it).
CREATE OR REPLACE FUNCTION public.create_public_user()
RETURNS TRIGGER AS $$
DECLARE
full_name TEXT;
name_parts TEXT[];
email_owner TEXT;
BEGIN
SELECT "id" INTO email_owner FROM public."user" WHERE "email" = NEW.email;
IF email_owner IS NOT NULL AND email_owner <> NEW.id::text THEN
INSERT INTO public."userPermission" ("id") VALUES (NEW.id) ON CONFLICT DO NOTHING;
RETURN NEW;
END IF;
full_name := NEW.raw_user_meta_data->>'name';
IF full_name IS NOT NULL THEN
name_parts := regexp_split_to_array(full_name, '\s+');
INSERT INTO public."user" ("id","email","active","firstName","lastName","about")
VALUES (NEW.id, NEW.email, true,
COALESCE(name_parts[1], ''),
COALESCE(array_to_string(name_parts[2:], ' '), ''), '')
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email;
ELSE
INSERT INTO public."user" ("id","email","active","firstName","lastName","about")
VALUES (NEW.id, NEW.email, true, '', '', '')
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email;
END IF;
INSERT INTO public."userPermission" ("id") VALUES (NEW.id) ON CONFLICT DO NOTHING;
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
- Keep the rest of the trigger definition identical to
20260319000000_console-mode.sql(do not re-create the trigger itself —CREATE OR REPLACE FUNCTIONsuffices; theon_auth_user_createdtrigger already points at this function). If the console-mode migration's function body has drifted from what is shown above (compare before writing), STOP and report — do not merge blindly.
Verify:
grep -c "CREATE POLICY" packages/database/supabase/migrations/*_sso-connection.sql
# Expected: 4
Out of scope: No companySettings columns; no seed data; no RPCs (so no PostgREST reload concern).
Task 2: Apply migration + regenerate types
Depends on: Task 1 Files:
- Modify:
packages/database/src/types.ts— regenerated, never hand-edited
Steps:
pnpm db:migrate(applies pending migrations to the worktree's local DB and regenerates types + swagger). If the local DB at127.0.0.1:$PORT_DBis unreachable, STOP and report — do not attemptdb:build(does not exist) and never rebuild the DB.
Verify:
grep -n "ssoConnection" packages/database/src/types.ts | head -3
# Expected: ssoConnection table types present (Row/Insert/Update entries)
Out of scope: any manual edit to types.ts.
Task 3: Add "sso" auth provider to env
Depends on: none Files:
- Modify:
packages/env/src/index.ts—AuthProviderunion (L116) gains"sso"; no change to theAUTH_PROVIDERSdefault string
Steps:
- Change
export type AuthProvider = "email" | "google" | "azure" | "passkey";to include| "sso". - Do NOT add
"sso"to the default"email,google,azure"— it is opt-in via env.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/env
# Expected: exit 0
Out of scope: browser-exposed env keys (getBrowserEnv) — AUTH_PROVIDERS is already exposed.
Task 4: @carbon/auth/sso.server — GoTrue admin wrappers + session helper
Depends on: Tasks 2, 3 Files:
- Create:
packages/auth/src/services/sso.server.ts - Modify:
packages/auth/package.json— add"./sso.server"export subpath (mirror the existing"./mfa.server"entry exactly, including types/import/require shape) - Copy from (precedent):
packages/auth/src/services/mfa.server.ts(server-only service file shape),packages/auth/src/lib/supabase/client.server.ts(env constant imports)
Steps:
- Implement, using
SUPABASE_URL+SUPABASE_SERVICE_ROLE_KEYfrom@carbon/envand plainfetchagainst GoTrue admin REST (${SUPABASE_URL}/auth/v1/admin/sso/providers), headers{ apikey: SUPABASE_SERVICE_ROLE_KEY, Authorization: "Bearer " + SUPABASE_SERVICE_ROLE_KEY, "Content-Type": "application/json" }. All functions return{ data, error }, never throw:createGoTrueSsoProvider(args: { metadataUrl?: string; metadataXml?: string; domains: string[] })→ POST body{ type: "saml", metadata_url?, metadata_xml?, domains }; returns{ data: { id: string }, error: null }from the response's providerid.updateGoTrueSsoProvider(providerId: string, args: same)→ PUT/admin/sso/providers/{id}.deleteGoTrueSsoProvider(providerId: string)→ DELETE/admin/sso/providers/{id}.getGoTrueSsoProvider(providerId: string)→ GET/admin/sso/providers/{id}.- Non-2xx →
{ data: null, error: <message from body or statusText> }.
getSsoProviderIdFromUser(user: User): string | null— read the SSO provider id from the Supabase user object. In GoTrue v2.x SAML users carry an identity withprovider="sso:<providerId>"inuser.identities, anduser.app_metadata.providersimilarly prefixed. Implement: scanuser.identities ?? []for aproviderstarting with"sso:", return the suffix; fall back touser.app_metadata?.provider?.startsWith("sso:"). If a locally-run SAML login (Task 14) shows neither location carries the provider id, STOP and report the actual user JSON — do not guess a third location.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/auth
# Expected: exit 0
Out of scope: no calls from client code (server-only file); no retry/backoff logic.
Task 5: SSO connection validator + service functions (settings module)
Depends on: Tasks 2, 4 Files:
- Modify:
apps/erp/app/modules/settings/settings.models.ts— addssoConnectionValidator - Modify:
apps/erp/app/modules/settings/settings.server.ts(create this file if it does not exist; checkapps/erp/app/modules/settings/first and follow the module's existing server-file pattern if present) - Modify:
apps/erp/app/modules/settings/index.ts— barrel export if the module barrels server files (check first; if server files are not barreled, import routes directly from the file, matching how otherx+/settings+routes import server helpers) - Copy from (precedent):
apps/erp/app/modules/settings/settings.models.tsapiKeyValidator(L55) for validator shape;apps/erp/app/modules/settings/settings.service.tsfor{ data, error }service shape
Steps:
ssoConnectionValidator(zod, used withvalidator(...)from@carbon/form):metadataUrl:zfd.text(z.string().url().optional())metadataXml:zfd.text(z.string().optional())domains:zfd.text(z.string().min(1))— comma-separated in the form;.transformsplit/trim/lowercase;.refineevery entry matches/^[a-z0-9.-]+\.[a-z]{2,}$/and contains no@.refineexactly one ofmetadataUrl/metadataXmlpresent.
- Service functions in
settings.server.ts(service-role client first arg, return{ data, error }):getSsoConnection(client, companyId)—client.from("ssoConnection").select("*").eq("companyId", companyId).eq("active", true).maybeSingle()getSsoConnectionByDomain(serviceRole, domain)—.contains("domains", [domain.toLowerCase()]).eq("active", true).maybeSingle()getSsoConnectionByProviderId(serviceRole, providerId)—.eq("providerId", providerId).eq("active", true).maybeSingle()upsertSsoConnection(serviceRole, { companyId, metadataUrl, metadataXml, domains, userId }): a. For each domain callgetSsoConnectionByDomain; if a hit belongs to a DIFFERENT company → return{ data: null, error: "Domain <d> is already registered to another company" }. b. Load existing connection for the company. If exists →updateGoTrueSsoProvider(providerId, ...)then update the row (domains,metadataUrl,metadataXml,updatedBy: userId,updatedAt: now ISO). If not →createGoTrueSsoProvider(...), then insert the row (companyId,providerId,domains,metadataUrl,metadataXml,createdBy: userId); on insert failure calldeleteGoTrueSsoProvider(providerId)before returning the error (compensating action — GoTrue is outside the DB transaction).deactivateSsoConnection(serviceRole, { companyId, userId })— load connection;deleteGoTrueSsoProvider(providerId); update rowactive: false, updatedBy, updatedAt.
- Add the same
getSsoConnectionByDomainto MES:apps/mes/app/services/sso.service.ts(MES layout keeps services flat; same function body).
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=mes
# Expected: exit 0
Out of scope: no UI here; no callback logic; no Kysely (single-row writes).
Task 6: reassignUserReferences + migrateUserToSso transaction (users module)
Depends on: Tasks 2, 5 Files:
- Create:
apps/erp/app/modules/users/users.sso.server.ts - Copy from (precedent): grep
transaction().execute(underapps/erp/appand follow the existing Kysely-transaction import pattern from the first service hit (e.g. how the module obtains the Kysely client). If no ERP service uses a Kysely transaction, STOP and report — do not invent a new DB client path.
Steps:
REASSIGNABLE_USER_COLUMNS: { table: string; column: string }[]— exactly the list in "Design refinements" §4 above (31assigneetables + the 12 named columns). Include a comment block stating the inclusion contract: mutable forward-looking user references, company-scoped tables only; audit actors (createdBy/updatedBy/closedBy/postedBy),workflowRun.ownerId,companyGroup.ownerIdare excluded by policy; new assignee-type columns must be added here.reassignUserReferences(trx, { companyId, fromUserId, toUserId })— for each entry:UPDATE <table> SET <column> = toUserId WHERE <column> = fromUserId AND "companyId" = companyId, via Kyselytrx.updateTable(...). Return per-table update counts for logging.migrateUserToSso(serviceRole, { newUserId, email, companyId, invite })— orchestrates: a. Pre-checks OUTSIDE the transaction: load old user by email (public."user"whereemail = <email> AND id <> newUserId AND active = true,.maybeSingle()). If found, load itsuserToCompanyrows; if any row hascompanyId <> <companyId>→ return{ data: null, error: "multi-company" }(v1 limitation — caller flashes the support message). b. Kysely transaction:- Insert
public."user"row fornewUserIdif absent (the trigger skipped it when the old row held the email): if an old user exists, FIRST update the old rowemail = 'sso-archived+' || <oldId> || '+' || <email>,active = false; THEN insert the new row (id: newUserId,email,active: true,firstName/lastNamecopied from the old row when present, else from the invite email local part,about: '') withON CONFLICT (id) DO NOTHINGsemantics (onConflictin Kysely:.onConflict((oc) => oc.column("id").doNothing())). - Accept the invite for
newUserId— replicateacceptInviteinternals transactionally (do NOT callacceptInvite, which uses supabase-client writes):UPDATE "employee" SET "active" = true WHERE "id" = newUserId AND "companyId" = ...is wrong for a NEW user — instead RE-KEY the invite-time rows (CONFIRMED via.claude/rules/user-employee-job-relationships.md: the invite flow pre-createsemployee— PK(id, companyId),id → user.id— keyed to an auth user created at invite time, i.e. the OLD id):UPDATE "employee" SET "id" = newUserId, "active" = true WHERE "id" = oldId AND "companyId" = <companyId>(the newpublic."user"row must be inserted FIRST for the FK), and the same re-key foremployeeJobif rows exist (instead of deleting them — they carry title/department/manager placement worth keeping). For a fresh SSO user with an invite but NO old user (possible only if the invite-time auth account was already deleted), insert theemployeerow followingcreateEmployeeAccount(apps/erp/app/modules/users/users.server.ts:376). InsertuserToCompany(userId: newUserId, companyId, role: invite.role), mergeinvite.permissionsintouserPermissionfornewUserId(followsetUserPermissionssemantics fromapps/erp/app/modules/users/users.server.ts), and setinvite.acceptedAt = now()guarded byacceptedAt IS NULL(row-lock viaFOR UPDATEselect first to make concurrent accepts idempotent). - If old user existed:
reassignUserReferences(trx, { companyId, fromUserId: oldId, toUserId: newUserId }); delete olduserToCompanyrow for this company;UPDATE "employee" SET "active" = false WHERE "id" = oldId AND "companyId" = companyId; delete oldemployeeJobrows (id + companyId); remove old groupmembershiprows (mirrordeactivateEmployeeinpackages/auth/src/services/users.server.ts:199-295). c. Post-commit (idempotent, failure-tolerant — log, don't throw):redis.del(getPermissionCacheKey(oldId))andredis.del(getPermissionCacheKey(newUserId)); delete the old auth user viaserviceRole.auth.admin.deleteUser(oldId)(revokes its sessions and frees the auth-level email).
- Insert
- Export from the module barrel only if other
.server.tsfiles are barreled (checkapps/erp/app/modules/users/index.ts).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: callback wiring (Task 7); no changes to acceptInvite itself; no reassignment of audit columns under any circumstance.
(Superseded: this whole reassignment/deactivation design was later replaced by link-instead-of-archive;
reassignUserReferencesandREASSIGNABLE_USER_COLUMNSno longer exist. See the spec Changelog.)
Task 7: Callback SSO branch (ERP + MES)
Depends on: Tasks 4, 5, 6 Files:
- Modify:
apps/erp/app/routes/_public+/callback.tsx— SSO branch in the action (before the existing company-resolution logic, afterrefreshAccessToken) - Modify:
apps/mes/app/routes/_public+/callback.tsx— same branch; MES has no invite-accept flow, so on the invite path redirect to the ERP URL (getAppUrl()) with a flash instead of running the migration (SSO users onboard via ERP once, then use MES normally) - Copy from (precedent): the existing action structure in each file (service-role usage,
destroyAuthSession, flash + redirect patterns)
Steps (ERP):
- After the session is obtained, get the Supabase user for the token (the action already has
userId+ service role; fetch the auth user viaserviceRole.auth.admin.getUserById(userId)), thenproviderId = getSsoProviderIdFromUser(user). If null → existing non-SSO path unchanged (byte-identical behavior). - SSO path:
a.
getSsoConnectionByProviderId(serviceRole, providerId). Miss/inactive →destroyAuthSessionwith flash "SSO connection is not active. Contact your administrator." b. Domain check:email.split("@")[1].toLowerCase()∈connection.domains, else → cleanup + reject (step d's cleanup). c. Membership check:userToCompanyrow for (userId, connection.companyId)? → proceed to the normal session-minting path, but scope company resolution toconnection.companyId(set the company cookie to it). d. No membership: load pending invite (invitewhereemail= user email ANDcompanyId= connection.companyId ANDacceptedAt IS NULLANDrevokedAt IS NULL). No invite → delete the orphan auth user IF it has zerouserToCompanyrows (serviceRole.auth.admin.deleteUser(userId)),destroyAuthSession, flash "SSO sign-in succeeded but no invite exists for {email}. Contact your administrator." With invite →migrateUserToSso(...). Onerror === "multi-company"→ flash "This account belongs to multiple companies and cannot be migrated automatically. Contact support." (do NOT delete the auth user in this case). On success → proceed to session minting withcompanyId = connection.companyId. - Session minting for the SSO path sets
mfaVerified: truein theAuthSessionunlessCONTROLLED_ENVIRONMENTis true (import both from@carbon/auth; the TOTP park-and-challenge check is skipped on this branch when not controlled).
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=mes
# Expected: exit 0
Out of scope: magic-link/OAuth/passkey callback behavior — every non-SSO session must take the exact existing code path; no changes to the callback's default-export component.
Task 8: Login "Continue with SSO" (ERP + MES) + public domain-check API route
Depends on: Tasks 3, 5 Files:
- Create:
apps/erp/app/routes/api+/sso.check.ts - Modify:
apps/erp/app/routes/_public+/login.tsx— loader boolean + button + handler - Modify:
apps/mes/app/routes/_public+/login.tsx— same - Copy from (precedent):
apps/erp/app/routes/_public+/login.tsx:94-110(IP rate limiting withRatelimitfrom@carbon/kv), L388-417 (onSignInWithGoogle/onSignInWithAzurehandler shape), L493-533 (gated button rendering)
Steps:
api+/sso.check.ts— public action (norequirePermissions):assertIsPost; IP rate limitRatelimit.slidingWindow(20, "1 h")keyedsso-check:{ip}; body{ email }; extract domain;getSsoConnectionByDomain(getCarbonServiceRole(), domain); returndata({ enabled: Boolean(connection) })— boolean only, no connection details.- ERP login loader:
const hasSsoAuth = isAuthProviderEnabled("sso") && CarbonEdition === Edition.Enterprise;(both already imported or importable from@carbon/auth). Return it with the existing booleans. - Button (rendered with the other provider buttons, same
Buttoncomponent + separator handling): label "Continue with SSO". HandleronSignInWithSSO: require the email field to be non-empty (inline form error "Enter your email first" if empty); POST topath.to.api.ssoCheck(add the path key — Task 9 step 4 coverspath.to; addssoCheckunder the api section here if Task 9 hasn't run:ssoCheck: \${api}/sso/check`); if{ enabled: false }→ inline error "SSO is not configured for your email domain."; elseconst { data, error } = await carbonClient.auth.signInWithSSO({ domain, options: { redirectTo: `${window.location.origin}/callback` } })and on successwindow.location.href = data.url`. - MES login: same loader boolean + button; the check endpoint is the ERP one — MES calls its own copy: create
apps/mes/app/routes/api+/sso.check.tswith the same body using the MES service from Task 5 step 3 (MES routes/services are separate apps; do not cross-import ERP modules).
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=mes
# Expected: exit 0
Out of scope: academy/starter login pages; no domain enumeration beyond the boolean; existing provider buttons untouched.
Task 9: Settings → SSO screen + nav + paths
Depends on: Tasks 4, 5 Files:
- Create:
apps/erp/app/routes/x+/settings+/sso.tsx - Modify:
apps/erp/app/utils/path.ts— addsso: \${x}/settings/sso`besidesecurity(L1967) andapi.ssoCheck` if not added in Task 8 - Modify:
apps/erp/app/modules/settings/ui/useSettingsSubmodules.tsx— add the entry to the System group next to Security (L200-205) - Copy from (precedent):
apps/erp/app/routes/x+/settings+/sequences.$tableId.tsx(loader/action/validator/flash/redirect shape),apps/erp/app/routes/x+/settings+/accounting.tsx(ValidatedForm layout with Card sections),apps/erp/app/routes/x+/settings+/security.tsx(permission pattern + CONTROLLED_ENVIRONMENT read)
Steps:
- Loader:
requirePermissions(request, { view: "settings", role: "employee" }); ifCarbonEdition !== Edition.Enterprise→throw redirect(path.to.settings)(spec: route hidden outside Enterprise). ReturngetSsoConnection(client, companyId)plus the SP URLs:acsUrl: \${SUPABASE_URL}/auth/v1/sso/saml/acs`,metadataUrl: `${SUPABASE_URL}/auth/v1/sso/saml/metadata`(importSUPABASE_URLfrom@carbon/auth`). - Action:
assertIsPost;requirePermissions(request, { update: "settings" }); branch on aintentform field:intent === "upsert"→validator(ssoConnectionValidator).validate(formData)→validationErroron error →upsertSsoConnection(getCarbonServiceRole(), { ...parsed, companyId, userId })→ error:data({}, await flash(request, error(...))); success:throw redirect(path.to.sso, await flash(request, success("SSO connection saved"))).intent === "deactivate"→deactivateSsoConnection(...)with the same flash/redirect pattern.
- Component: Card "Service provider details" with the two read-only URLs and copy buttons (grep
packages/react/src/for an existing copy-button component — useCopyfrom@carbon/reactif present; if no copy component exists, render the URLs in<Input isReadOnly>fields without a copy button — do not build a new component); Card "Identity provider" with theValidatedForm(fields:InputnamemetadataUrl,TextAreanamemetadataXml,Inputnamedomainscomma-separated with helper text,Submit); when a connection exists show registered domains + active state + a deactivate button inside a confirmModal(precedent: any settings delete modal, e.g.api-keys.delete.$id.tsx). - Nav entry in
useSettingsSubmodules.tsxSystem group:{ name: t\Single Sign-On`, to: path.to.sso, role: "employee", icon: }(importLuKeyRoundfromreact-icons/lu). Nav has no per-edition flag — the route's loader redirect handles non-Enterprise access; ALSO hide the entry by adding it to the component's existing gating only if an edition gate exists inisRouteVisible— it does not, so instead conditionally include the route object usinguseFlags().isEnterprise(hook atapps/erp/app/hooks/useFlags.tsx:15) inside theuseSettingsSubmodules` body. - (Superseded: this card shipped, was renamed "Covered Users", and was later removed entirely.) The "Users to re-invite" migration-aid list from the spec: render a third Card listing active company users whose email domain ∈
domains(loader: queryemployeesview via existing people/employees service — grepgetEmployeesinapps/erp/app/modules/peopleorusersand reuse; select email + name only) with a note "These users will be migrated on their first SSO sign-in. Re-invite them to enable SSO access." and a Link to the existing employees page (path.to.employeeAccountsor equivalent — confirm the key exists in path.ts; bulk re-invite itself is the existing resend-invite flow, not rebuilt here).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exit 0
Out of scope: no new bulk-invite machinery; no MES settings surface; no i18n catalog fill (handled by /translate at commit time).
Task 10: MFA exemption for SSO sessions
Depends on: Task 7 Files:
- Modify:
apps/erp/app/routes/x+/_layout.tsx+apps/mes/app/routes/x+/_layout.tsx— theMfaEnrollmentRequiredblocking-screen condition - Copy from (precedent): the existing
requireMfagate logic in those loaders (grepMfaEnrollmentRequiredfor exact locations)
Steps:
- Task 7 already mints SSO sessions with
mfaVerified: true(non-controlled), which satisfiesrequireAuthSession's re-check — no session.server.ts change needed. Verify this by readingrequireAuthSession's MFA bounce condition inpackages/auth/src/services/session.server.ts; if it re-derives fromuserHasVerifiedTotpFactorREGARDLESS ofmfaVerified, STOP and report. - (Superseded: the
!CONTROLLED_ENVIRONMENTcondition below was later removed — SSO sessions are exempt in every environment.) TherequireMfacompany-enforcement blocking screen: where the shell loader computes the gate (greprequireMfain both_layout.tsxfiles), exempt sessions whose auth user is SSO-sourced when!CONTROLLED_ENVIRONMENT: the loader has the auth session — carry anisSsoboolean on theAuthSession(add optional fieldssoProviderId?: stringto theAuthSessiontype inpackages/auth/src/types.tsor whereverAuthSessionis defined — grepmfaVerifiedinpackages/auth/srcto find it — set in Task 7's minting, preserved byrefreshAuthSessionexactly likemfaVerified— grepmfaVerifiedinsession.server.tsand mirror every propagation site). Gate becomes:requireMfa && !mfaVerified-satisfying-enrollment && !(authSession.ssoProviderId && !CONTROLLED_ENVIRONMENT).
Verify:
pnpm exec turbo run typecheck --filter=@carbon/auth --filter=erp --filter=mes
# Expected: exit 0
Out of scope: TOTP enrollment UI; /mfa routes; API-key/OAuth machine paths (never challenged today).
Task 11: SSO-aware invite emails
Depends on: Task 5 Files:
- Modify:
apps/erp/app/routes/x+/users+/employees.new.tsx(invite email block L124-144),customers.new.tsx(L89-95),suppliers.new.tsx(L89-95),resend-invite.tsx(L72-121) - Modify:
packages/jobs/src/inngest/functions/tasks/user-admin.ts(bulk resend, L100-114) - Copy from (precedent): the existing
sendEmail(... InviteEmail({ inviteLink }))blocks in those files
Steps:
- Add a tiny helper in the ERP users module (
apps/erp/app/modules/users/users.server.ts):getInviteLink(serviceRole, email, code): Promise<string>— ifgetSsoConnectionByDomain(import from the settings server file) hits an active connection for the email's domain, return${getAppUrl()}/login?email=${encodeURIComponent(email)}(login page prefill; the SSO callback consumes the pending invite — the code is not needed in the URL); else return the existing${getAppUrl()}/invite/${code}. - Replace the hardcoded
inviteLinkin the three route actions andresend-invite.tsxwith the helper. user-admin.ts(jobs package cannot import ERP modules): inline the same two-step lookup there using its existing service-role client — queryssoConnection.contains("domains", [domain]).eq("active", true).maybeSingle()directly, then pick the URL. Keep the existing link as the fallback.- Login page: read
?email=from the URL and prefill the email field if not already supported (checklogin.tsxfor an existingredirectTo/searchParams pattern; if prefill via?email=doesn't exist, adddefaultValuefromsearchParams.get("email")).
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/jobs
# Expected: exit 0
Out of scope: invite.$code.tsx route behavior (still works for already-authenticated SSO users); InviteEmail template content.
Task 12: GoTrue SAML config (dev + prod compose, .env.example)
Depends on: none Files:
- Modify:
packages/dev/docker/docker-compose.dev.yml— GoTrue service env (L79-113 region) - Modify:
contrib/deploying/simple-docker-caddy/docker-compose.prod.yml— GoTrue service env (L252-292 region) - Modify:
.env.exampleandcontrib/deploying/simple-docker-caddy/.env.example— document the new vars
Steps:
- Both compose files, in the GoTrue service
environmentblock:GOTRUE_SAML_ENABLED: ${SAML_ENABLED:-false} GOTRUE_SAML_PRIVATE_KEY: ${SAML_PRIVATE_KEY:-} .env.exampleentries:# Enterprise SAML SSO (Enterprise edition). Generate the key with: # openssl genpkey -algorithm RSA -out /tmp/pk.pem -quiet && \ # openssl pkey -in /tmp/pk.pem -out /tmp/pk.der -outform DER -traditional && \ # base64 -i /tmp/pk.der # (-w 0 on Linux) # The key MUST be base64-encoded PKCS#1 DER (the -traditional flag), min 2048-bit. SAML_ENABLED=false SAML_PRIVATE_KEY=- Check how the dev proxy / prod Caddy route
/auth/v1/*: grep the Caddyfile incontrib/deploying/simple-docker-caddy/and the dev proxy config inpackages/dev/for auth routing. If/auth/v1/is already proxied wholesale to GoTrue with no per-path auth plugin, no change is needed (note this in the task's completion evidence). If SSO paths are excluded or an auth gate exists on them, add explicit pass-through for/auth/v1/sso/saml/acsand/auth/v1/sso/saml/metadata. If the routing cannot be determined from the files, STOP and report what was found.
Verify:
grep -n "GOTRUE_SAML" packages/dev/docker/docker-compose.dev.yml contrib/deploying/simple-docker-caddy/docker-compose.prod.yml
# Expected: 2 lines per file (ENABLED + PRIVATE_KEY)
Out of scope: config.toml (local runtime is docker-compose; hosted parity is a doc comment only if a natural place exists); no MockSAML container in this task (Task 14 decides if needed).
Task 13: Unit tests — migration transaction, domain enforcement, trigger behavior
Depends on: Tasks 6, 7 Files:
- Create:
apps/erp/app/modules/users/users.sso.server.test.ts(vitest; follow the existing test setup — grep*.test.tsunderapps/erp/appfor the closest service-test precedent and mirror its mocking approach; if ERP has no service-level test precedent and tests live only in packages, put the pure-logic tests inpackages/authinstead and note it)
Steps:
- Test the pure decision logic (extract as pure functions in Task 6/7 code where needed so they are testable without a DB):
- domain-enforcement: provider→connection mismatch rejected; email domain not in
domainsrejected; case-insensitivity. - multi-company detection: old user with membership in another company →
"multi-company"error. REASSIGNABLE_USER_COLUMNScontains no audit columns: assert none ofcreatedBy,updatedBy,closedBy,postedBy, and notworkflowRun/companyGrouptables.- archived-email format is deterministic and preserves the original email as a suffix.
- domain-enforcement: provider→connection mismatch rejected; email domain not in
- If the codebase has a DB-backed test harness for Kysely (grep
harnesspackage usage in tests), add one transactional test: forced failure mid-transaction leaves invite unaccepted and old user untouched. If no such harness exists, skip DB-backed tests and note it — do not build a test DB harness in this task.
Verify:
pnpm --filter erp test -- users.sso
# Expected: all new tests pass (adjust the filter invocation to the app's vitest config if `--` filtering differs; expected output contains "passed")
Out of scope: e2e/browser tests (Task 14); testing GoTrue itself.
Task 14: Full gates + browser verification
Depends on: all Files: none (verification only)
Steps:
pnpm exec turbo run typecheck --filter=@carbon/env --filter=@carbon/auth --filter=@carbon/jobs --filter=erp --filter=mespnpm run lintpnpm --filter erp test(scoped to changed areas if the suite is slow)- Browser verification via
/test(with the user's permission, per repo policy): requires the dev stack withSAML_ENABLED=true, a generated dev key, and an IdP. Register mocksaml.com (public mock IdP, metadata athttps://mocksaml.com/api/saml/metadata) via the new settings screen against a dev company + a test domain, then walk: settings screen save → login button → IdP → callback → invited-user attach. If mocksaml.com is unusable from the dev environment, STOP and report; adding a local MockSAML container is a follow-up decision, not improvised here. - Spec bookkeeping: check acceptance criteria in the spec, add changelog entry (including the three design refinements from this plan's header), and record the v1 multi-company limitation.
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=mes
# Expected: exit 0 (final green)
Out of scope: committing — commits happen only via /check-and-commit on the user's explicit ask; moving the spec to implemented/ (needs deployment evidence).