Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(organizations): trust only the linked WorkOS organization at sign-in
Petr Bauch1b99d0393a88
diff --git a/CLAUDE.md b/CLAUDE.mdindex 5542b3edd..fa270db9e 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -189,6 +189,10 @@ Tests live in `core/tests/switch_core/` mirroring the module structure. Uses pyt`AIRTABLE_CLIENT_CONFIG_PATH`, and switching it off- `docs/old/GATEWAY_OIDC_SETUP.md` — configuring the gateway's bring-your-ownOIDC browser sign-in (variables, redirect URI, WorkOS Connect setup)+- `docs/old/workos-directory.md` — keeping organizations in WorkOS+ (`ORGANIZATION_DIRECTORY=workos`): what it needs, linking organizations and+ existing accounts with `switch-link-workos`, what a sign-in brings in, and+ what is not done yet- `docs/old/google-workspace-setup.md` — offering Google Workspace on aself-hosted server: registering the Google app (Internal or External), itsredirect URI, saving it under Connectors or indiff --git a/core/pyproject.toml b/core/pyproject.tomlindex fe2aedb35..8df9db904 100644--- a/core/pyproject.toml+++ b/core/pyproject.toml@@ -54,6 +54,7 @@ Issues = "https://github.com/sandbox-quantum/switch/issues"[project.scripts]switch-core = "switch_core.main:main"switch-migrate = "switch_core.main:migrate"+switch-link-workos = "switch_core.workos_linking:main"[build-system]requires = ["hatchling"]diff --git a/core/switch_core/config.py b/core/switch_core/config.pyindex 5b7cd91a7..104344661 100644--- a/core/switch_core/config.py+++ b/core/switch_core/config.py@@ -1548,6 +1548,13 @@ class SwitchConfig(BaseSettings):"(GATEWAY_OIDC_*) set up against the same WorkOS environment: ""the sign-in is what names the person there.")+ if self.gateway_signup_mode != "invite_only":+ raise ValueError(+ "ORGANIZATION_DIRECTORY=workos needs GATEWAY_SIGNUP_MODE=invite_only: "+ "until organizations can be created in WorkOS from Switch, a "+ "customer's organization is linked by an operator "+ "(switch-link-workos), not created by whoever signs up."+ )return self@model_validator(mode="after")diff --git a/core/switch_core/db/audit.py b/core/switch_core/db/audit.pyindex d6ea6bf92..6e02b2d8f 100644--- a/core/switch_core/db/audit.py+++ b/core/switch_core/db/audit.py@@ -85,6 +85,9 @@ class OrganizationAuditAction(StrEnum):SERVICE_CLEARED = "organization.service_cleared"# Brought in from a directory kept outside Switch when they signed in.MEMBER_ADDED_AT_SIGN_IN = "organization.member_added_at_sign_in"+ MEMBER_PROMOTED_AT_SIGN_IN = "organization.member_promoted_at_sign_in"+ # An operator made the organization follow one in the directory.+ LINKED_TO_DIRECTORY = "organization.linked_to_directory"class ServerAuditAction(StrEnum):diff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.pyindex d222fa5b3..3c4bfe364 100644--- a/core/switch_core/db/stores/user_store.py+++ b/core/switch_core/db/stores/user_store.py@@ -391,6 +391,15 @@ class UserStore:"guess a winner a third time.")+ async def link_oidc_identity(+ self, session: AsyncSession, *, user: User, iss: str, sub: str+ ) -> None:+ """Link `(iss, sub)` to `user`, as an operator does for an account that+ signed in some other way before (`workos_linking.py`)."""+ await self._link_identity(+ session, user=user, iss=iss, sub=sub, join_tenant=False+ )+async def _link_identity(self,session: AsyncSession,diff --git a/core/switch_core/db/tenant_lookup.py b/core/switch_core/db/tenant_lookup.pyindex 8a0d9cada..f4c084630 100644--- a/core/switch_core/db/tenant_lookup.py+++ b/core/switch_core/db/tenant_lookup.py@@ -171,14 +171,17 @@ two. It discloses which workspaces a person belongs to or administersthrough an organization; the gateway asks it only about the signed-incaller.-**`organization_of_customer` is the fifteenth**, and the one that answers-with an organization rather than a workspace. Under the WorkOS directory-(`workos_directory.py`) a sign-in names the customers the person belongs to,-by the customer's id; which Switch organization each one is has to be read-before anything is bound, to bring the person's membership rows up to date.-It discloses whether a customer has a Switch organization, and its id; the-gateway asks it only about customers WorkOS says the signed-in person belongs-to.+**`directory_organizations_of_user` is the fifteenth**, and the one that+answers with organizations rather than workspaces. Under the WorkOS directory+(`workos_directory.py`) a sign-in names the WorkOS organizations the person+belongs to; which Switch organizations follow those, and which ones the+person is already in that follow WorkOS at all, has to be read before+anything is bound, to bring their membership rows up to date. Its second+argument is a JSON array of WorkOS organization ids, so one call answers for+all of them. It discloses which WorkOS organizations have a Switch+organization, and which linked organizations a person belongs to; the+gateway asks it only about the signed-in person and the organizations WorkOS+says they belong to.Why not the obvious alternatives is argued in`docs/old/multi-tenancy-phase1-db.md`, "The bootstrap problem"; the short@@ -216,6 +219,7 @@ on the runtime-state sweep is one extra round trip every five seconds.from __future__ import annotations+import jsonfrom collections.abc import Iterablefrom dataclasses import dataclass@@ -456,14 +460,22 @@ TENANT_LOOKUPS: tuple[TenantLookup, ...] = (),),TenantLookup(- name="organization_of_customer",- arguments=("customer_id",),- query="SELECT id FROM organizations WHERE customer_id = p_customer_id",+ name="directory_organizations_of_user",+ arguments=("user_id", "workos_organization_ids"),+ query=(+ "SELECT id FROM organizations "+ "WHERE workos_organization_id IN "+ "(SELECT jsonb_array_elements_text(p_workos_organization_ids::jsonb)) "+ "OR (workos_organization_id IS NOT NULL AND id IN "+ "(SELECT organization_id FROM organization_members "+ "WHERE user_id = p_user_id)) "+ "ORDER BY id"+ ),purpose=(- "Which organization is a customer's, from the customer's id a "- "WorkOS sign-in names. Read before anything is bound, to bring the "- "person's organization memberships up to date; unique by "- "constraint."+ "Which organizations follow a WorkOS organization a sign-in names "+ "(a JSON array of their ids), and which organizations following "+ "WorkOS the person is already in. Read before anything is bound, "+ "to bring the person's organization memberships up to date."),),TenantLookup(@@ -592,8 +604,9 @@ _LOOKUP_STATEMENTS: dict[str, TextClause] = {"tenant_of_sign_in_flow": text("SELECT tenant_id FROM tenant_of_sign_in_flow(:state) AS tenant_id"),- "organization_of_customer": text(- "SELECT tenant_id FROM organization_of_customer(:customer_id) AS tenant_id"+ "directory_organizations_of_user": text(+ "SELECT tenant_id FROM directory_organizations_of_user("+ ":user_id, :workos_organization_ids) AS tenant_id"),}@@ -752,8 +765,14 @@ async def tenant_of_sign_in_flow(return _at_most_one(lookup, await _call(session_factory, lookup, state))-async def organization_of_customer(- session_factory: async_sessionmaker[AsyncSession], customer_id: str-) -> str | None:- lookup = TENANT_LOOKUPS_BY_NAME["organization_of_customer"]- return _at_most_one(lookup, await _call(session_factory, lookup, customer_id))+async def directory_organizations_of_user(+ session_factory: async_sessionmaker[AsyncSession],+ user_id: str,+ workos_organization_ids: list[str],+) -> list[str]:+ return await _call(+ session_factory,+ TENANT_LOOKUPS_BY_NAME["directory_organizations_of_user"],+ user_id,+ json.dumps(sorted(workos_organization_ids)),+ )diff --git a/core/switch_core/gateway/oidc_routes.py b/core/switch_core/gateway/oidc_routes.pyindex 113c77fc3..8b2c1fb1e 100644--- a/core/switch_core/gateway/oidc_routes.py+++ b/core/switch_core/gateway/oidc_routes.py@@ -15,6 +15,7 @@ import httpxfrom authlib.integrations.starlette_client import OAuth, OAuthErrorfrom fastapi import APIRouter, Depends, HTTPException, Requestfrom pydantic import BaseModel+from sqlalchemy.exc import SQLAlchemyErrorfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom starlette.responses import JSONResponse, RedirectResponse@@ -234,9 +235,11 @@ async def _bring_in_organizations(subject: str,) -> None:"""Bring the person's organization memberships up to date with a directory- kept outside Switch (`organization_mirror.py`). One that can't be reached- doesn't stop the sign-in: authorization reads Switch's rows, which stay as- they were, and the failure is logged."""+ kept outside Switch (`organization_mirror.py`). Neither a directory that+ can't be read nor a failure writing Switch's rows stops the sign-in:+ authorization reads Switch's rows, each organization's change commits on+ its own, and an organization left behind is caught up at the next+ sign-in. Both are logged."""try:memberships = await organization_directory.memberships_at_sign_in(subject)except DirectoryUnavailable as error:@@ -247,8 +250,16 @@ async def _bring_in_organizations(error.message,)return- if memberships is not None:+ if memberships is None:+ return+ try:await bring_in_memberships(session_factory, user.id, memberships)+ except SQLAlchemyError:+ logger.exception(+ "Could not bring user %s's organizations up to date at sign-in; "+ "some of Switch's may be behind the directory",+ user.id,+ )@router.get("/auth/oidc/callback", name="oidc_callback")diff --git a/core/switch_core/main.py b/core/switch_core/main.pyindex 4c88cc6c4..07a904ba5 100644--- a/core/switch_core/main.py+++ b/core/switch_core/main.py@@ -527,8 +527,8 @@ def _organization_directory("""The directory `ORGANIZATION_DIRECTORY` names (`organization_directory.py`)."""if config.organization_directory == "local":return LocalDirectory()- if config.workos_api_key is None:- raise RuntimeError("ORGANIZATION_DIRECTORY=workos needs WORKOS_API_KEY")+ # `_validate_organization_directory` has refused a missing key already.+ assert config.workos_api_key is not Nonereturn WorkOSDirectory(http, config.workos_api_key, config.workos_api_url)diff --git a/core/switch_core/migrations/versions/f2b8d6c1a4e9_directory_organizations_of_user.py b/core/switch_core/migrations/versions/f2b8d6c1a4e9_directory_organizations_of_user.pynew file mode 100644index 000000000..8e8e5f9c2--- /dev/null+++ b/core/switch_core/migrations/versions/f2b8d6c1a4e9_directory_organizations_of_user.py@@ -0,0 +1,47 @@+"""the lookup that finds the organizations following WorkOS for a sign-in++Under the WorkOS directory a sign-in names the WorkOS organizations the+person belongs to, and which Switch organizations follow those, and which+ones following WorkOS the person is already in, has to be read before+anything is bound. This adds the fifteenth lookup to the exemption from+row-level security. See `db/tenant_lookup.py` for what it discloses.++The DDL below is a verbatim copy of `switch_core/db/tenant_lookup.py` as it+stood when this migration was written, copied rather than imported so a later+edit cannot change what this revision means.++Revision ID: f2b8d6c1a4e9+Revises: 10f6d02ac5bc+Create Date: 2026-11-02 00:00:00.000000++"""++from collections.abc import Sequence++from alembic import op++revision: str = "f2b8d6c1a4e9"+down_revision: str | Sequence[str] | None = "10f6d02ac5bc"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+expand_only: bool = True++SECURE_SEARCH_PATH = "pg_catalog, public, pg_temp"++CREATE_DIRECTORY_ORGANIZATIONS_OF_USER = f"""CREATE OR REPLACE FUNCTION directory_organizations_of_user(p_user_id text, p_workos_organization_ids text)+ RETURNS SETOF text+ LANGUAGE sql STABLE SECURITY DEFINER+ SET search_path = {SECURE_SEARCH_PATH}+AS $$SELECT id FROM organizations WHERE workos_organization_id IN (SELECT jsonb_array_elements_text(p_workos_organization_ids::jsonb)) OR (workos_organization_id IS NOT NULL AND id IN (SELECT organization_id FROM organization_members WHERE user_id = p_user_id)) ORDER BY id$$"""++DROP_DIRECTORY_ORGANIZATIONS_OF_USER = (+ "DROP FUNCTION IF EXISTS directory_organizations_of_user(text, text)"+)+++def upgrade() -> None:+ op.execute(CREATE_DIRECTORY_ORGANIZATIONS_OF_USER)+++def downgrade() -> None:+ op.execute(DROP_DIRECTORY_ORGANIZATIONS_OF_USER)diff --git a/core/switch_core/migrations/versions/f2b8d6c1a4e9_organization_of_customer.py b/core/switch_core/migrations/versions/f2b8d6c1a4e9_organization_of_customer.pydeleted file mode 100644index 11bfcbe8e..000000000--- a/core/switch_core/migrations/versions/f2b8d6c1a4e9_organization_of_customer.py+++ /dev/null@@ -1,45 +0,0 @@-"""the lookup that finds a customer's organization--Under the WorkOS directory a sign-in names the customers the person belongs-to by the customer's id, and which Switch organization each one is has to be-read before anything is bound. This adds the fifteenth lookup to the-exemption from row-level security. See `db/tenant_lookup.py` for what it-discloses.--The DDL below is a verbatim copy of `switch_core/db/tenant_lookup.py` as it-stood when this migration was written, copied rather than imported so a later-edit cannot change what this revision means.--Revision ID: f2b8d6c1a4e9-Revises: 10f6d02ac5bc-Create Date: 2026-11-02 00:00:00.000000--"""--from collections.abc import Sequence--from alembic import op--revision: str = "f2b8d6c1a4e9"-down_revision: str | Sequence[str] | None = "10f6d02ac5bc"-branch_labels: str | Sequence[str] | None = None-depends_on: str | Sequence[str] | None = None-expand_only: bool = True--SECURE_SEARCH_PATH = "pg_catalog, public, pg_temp"--CREATE_ORGANIZATION_OF_CUSTOMER = f"""CREATE OR REPLACE FUNCTION organization_of_customer(p_customer_id text)- RETURNS SETOF text- LANGUAGE sql STABLE SECURITY DEFINER- SET search_path = {SECURE_SEARCH_PATH}-AS $$SELECT id FROM organizations WHERE customer_id = p_customer_id$$"""--DROP_ORGANIZATION_OF_CUSTOMER = "DROP FUNCTION IF EXISTS organization_of_customer(text)"---def upgrade() -> None:- op.execute(CREATE_ORGANIZATION_OF_CUSTOMER)---def downgrade() -> None:- op.execute(DROP_ORGANIZATION_OF_CUSTOMER)diff --git a/core/switch_core/organization_directory.py b/core/switch_core/organization_directory.pyindex 9d7efbbd8..fa1df4f23 100644--- a/core/switch_core/organization_directory.py+++ b/core/switch_core/organization_directory.py@@ -37,10 +37,12 @@ class DirectoryOrganization:@dataclass(frozen=True)class DirectoryMembership:- """Someone's membership of a customer's organization, as the directory- has it: the customer, and whether they are its `org_admin`."""+ """Someone's membership of an organization, as the directory has it: the+ directory's id for the organization (`organizations.workos_organization_id`+ for the Switch organization linked to it), and whether they are its+ `org_admin`."""- customer_id: str+ workos_organization_id: strrole: strdiff --git a/core/switch_core/organization_mirror.py b/core/switch_core/organization_mirror.pyindex 73225a28b..469e7bd57 100644--- a/core/switch_core/organization_mirror.py+++ b/core/switch_core/organization_mirror.py@@ -1,30 +1,38 @@"""Switch's organization rows brought up to date with a directory keptoutside Switch (`organization_directory.py`), for one person at sign-in.-Only customers that already have a Switch organization are touched: a-customer starts using Switch by creating its organization here, not by one of-its people signing in. Someone the directory lists in such an organization-and Switch does not is added, with the directory's role, and the addition is-recorded in the organization's audit log. A role that differs, or a-membership the directory no longer lists, is left as it is and logged: taking-a role away also takes away what it reached, which is the directory mirror's-to do, not a sign-in's.+Only organizations an operator has linked to one in the directory are+touched (`organizations.workos_organization_id`), and only by memberships of+exactly that one: a customer starts using Switch by having its organization+linked, not by one of its people signing in.++- Someone the directory lists and Switch does not is added, with the+ directory's role.+- A member the directory lists as `org_admin` is made one.++Both are recorded in the organization's audit log. Neither takes anything+away. A demotion, or a membership the directory no longer lists, is logged at+`error` and left as it is: taking a role away also takes away the workspaces+it reached, which is for the directory mirror to do (not built yet), not a+sign-in."""from __future__ import annotationsimport logging+from sqlalchemy import select, updatefrom sqlalchemy.dialects.postgresql import insertfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker+from switch_core.authz import ORGANIZATION_ADMIN_ROLEfrom switch_core.db.audit import (OrganizationAuditAction,record_organization_audit_event,)-from switch_core.db.models import OrganizationMember+from switch_core.db.models import Organization, OrganizationMemberfrom switch_core.db.session_scope import organization_session-from switch_core.db.tenant_lookup import organization_of_customer+from switch_core.db.tenant_lookup import directory_organizations_of_userfrom switch_core.organization_directory import DirectoryMembershiplogger = logging.getLogger(__name__)@@ -35,45 +43,91 @@ async def bring_in_memberships(user_id: str,memberships: list[DirectoryMembership],) -> None:- """Add `user_id` to each Switch organization of a customer in- `memberships` that they are not in yet, one organization at a time."""- for membership in memberships:- organization_id = await organization_of_customer(- session_factory, membership.customer_id- )- if organization_id is None:- continue+ """Bring `user_id`'s rows in each linked organization up to date with+ `memberships`, one organization at a time, each committed on its own."""+ roles = {+ membership.workos_organization_id: membership.role for membership in memberships+ }+ for organization_id in await directory_organizations_of_user(+ session_factory, user_id, list(roles)+ ):async with organization_session(session_factory, organization_id) as session:- member = await session.get(OrganizationMember, (organization_id, user_id))- if member is not None:- if member.role != membership.role:- logger.warning(- "User %s is %s in organization %s and %s in its "- "directory; the sign-in leaves Switch's role as it is",- user_id,- member.role,- organization_id,- membership.role,- )- continue- added = await session.execute(- insert(OrganizationMember)- .values(- organization_id=organization_id,- user_id=user_id,- role=membership.role,- )- .on_conflict_do_nothing(index_elements=["organization_id", "user_id"])- .returning(OrganizationMember.user_id)+ await _bring_in_one(session, organization_id, user_id, roles)+++async def _bring_in_one(+ session: AsyncSession,+ organization_id: str,+ user_id: str,+ roles: dict[str, str],+) -> None:+ workos_organization_id, current = (+ await session.execute(+ select(Organization.workos_organization_id, OrganizationMember.role)+ .outerjoin(+ OrganizationMember,+ (OrganizationMember.organization_id == Organization.id)+ & (OrganizationMember.user_id == user_id),+ )+ .where(Organization.id == organization_id)+ )+ ).one()+ wanted = roles.get(workos_organization_id) if workos_organization_id else None+ if wanted is None:+ if current is not None:+ logger.error(+ "User %s is %s in organization %s, which WorkOS organization %s "+ "no longer lists them in; Switch keeps their membership until "+ "the directory mirror can end it",+ user_id,+ current,+ organization_id,+ workos_organization_id,+ )+ return+ if current is None:+ added = await session.execute(+ insert(OrganizationMember)+ .values(organization_id=organization_id, user_id=user_id, role=wanted)+ .on_conflict_do_nothing(index_elements=["organization_id", "user_id"])+ .returning(OrganizationMember.user_id)+ )+ if added.first() is None:+ # A concurrent sign-in or invitation added them first.+ return+ action = OrganizationAuditAction.MEMBER_ADDED_AT_SIGN_IN+ details = {"role": wanted}+ elif current != wanted and wanted == ORGANIZATION_ADMIN_ROLE:+ await session.execute(+ update(OrganizationMember)+ .where(+ OrganizationMember.organization_id == organization_id,+ OrganizationMember.user_id == user_id,+ )+ .values(role=wanted)+ )+ action = OrganizationAuditAction.MEMBER_PROMOTED_AT_SIGN_IN+ details = {"from_role": current, "role": wanted}+ else:+ if current != wanted:+ logger.error(+ "User %s is %s in organization %s and %s in WorkOS organization "+ "%s; Switch keeps their role until the directory mirror can "+ "change it",+ user_id,+ current,+ organization_id,+ wanted,+ workos_organization_id,)- if added.first() is not None:- await record_organization_audit_event(- session,- organization_id=organization_id,- actor_user_id=None,- action=OrganizationAuditAction.MEMBER_ADDED_AT_SIGN_IN,- target_type="user",- target_id=user_id,- details={"role": membership.role},- )- await session.commit()+ return+ await record_organization_audit_event(+ session,+ organization_id=organization_id,+ actor_user_id=None,+ action=action,+ target_type="user",+ target_id=user_id,+ details=details,+ )+ await session.commit()diff --git a/core/switch_core/workos_directory.py b/core/switch_core/workos_directory.pyindex e402ffa66..0feaf3ad5 100644--- a/core/switch_core/workos_directory.py+++ b/core/switch_core/workos_directory.py@@ -2,19 +2,26 @@The hosted service shares one WorkOS environment with other products. Acustomer is one organization there, and a product may give it organizations-of its own beneath that one; every one of them carries the customer's id in-its metadata (`tenant_id`, the name the first product gave the customer), and-that id, not any one organization's WorkOS id, is what tells customers apart.-A Switch organization is one customer (`organizations.customer_id`).--WorkOS holds who belongs to a customer and who is its `org_admin`; Switch-holds which workspaces, and the roles there. At sign-in the person's+of its own beneath that one. A Switch organization follows exactly one WorkOS+organization, the one its `workos_organization_id` names, and an operator+sets that link (`workos_linking.py`); nothing a WorkOS organization says about+itself, its metadata included, links it to Switch, because any product in the+environment may write that. So only memberships of that one organization count+here: an `org_admin` of some other organization of the same customer is+nothing in Switch until they are made one in the linked organization.++WorkOS holds who belongs to a linked organization and who is its `org_admin`;+Switch holds which workspaces, and the roles there. At sign-in the person'smemberships are read from WorkOS (`memberships_at_sign_in`), by their WorkOSuser id, which the sign-in's `sub` is.-Changes made from Switch go through the service that owns the customer-record, not through this module's API key, so each product's rules apply in-one place; until that is wired in, this directory refuses them.+Changes made from Switch to a linked organization go through the service that+owns the customer record, not through this module's API key, so each+product's rules apply in one place; until that is wired in, this directory+refuses those that grant (adding someone, making them Org Admin). Those that+take away (a removal, a demotion) change only Switch, so an Org Admin can end+access WorkOS has already ended. An organization nobody has linked is Switch's own, as under the+local directory, and that is what a new organization is until then."""from __future__ import annotations@@ -39,7 +46,9 @@ logger = logging.getLogger(__name__)WORKOS_TIMEOUT_SECONDS = 10.0# WorkOS's largest page._PAGE = 100-# The metadata key every organization of a customer carries its id under.+# The metadata key a customer's organizations carry its id under. Recorded as+# `organizations.customer_id` when an organization is linked, never trusted+# to link one.CUSTOMER_ID_KEY = "tenant_id"NOT_FROM_SWITCH_YET = (@@ -49,10 +58,10 @@ NOT_FROM_SWITCH_YET = (def customer_id_of(organization: dict[str, Any]) -> str | None:- """The customer a WorkOS organization belongs to, or None if it names- none. Root and child organizations of one customer answer alike."""- metadata = organization.get("metadata") or {}- value = metadata.get(CUSTOMER_ID_KEY)+ """The customer a WorkOS organization says it belongs to, or None if it+ names none."""+ metadata = organization.get("metadata")+ value = metadata.get(CUSTOMER_ID_KEY) if isinstance(metadata, dict) else Nonereturn value if isinstance(value, str) and value else None@@ -64,6 +73,10 @@ def switch_role(workos_role: str) -> str:)+def _unreadable(what: str) -> DirectoryUnavailable:+ return DirectoryUnavailable(f"WorkOS answered with {what} Switch cannot read.")++class WorkOSDirectory:def __init__(self, http: httpx.AsyncClient, api_key: str, api_url: str) -> None:self._http = http@@ -86,85 +99,97 @@ class WorkOSDirectory:raise DirectoryUnavailable(f"WorkOS answered {response.status_code} for {path}.")- body = response.json()+ try:+ body = response.json()+ except ValueError as error:+ raise DirectoryUnavailable(+ f"WorkOS answered {path} with no JSON."+ ) from errorif not isinstance(body, dict):raise DirectoryUnavailable(f"WorkOS answered {path} with no object.")return body- async def memberships_at_sign_in(- self, subject: str- ) -> list[DirectoryMembership] | None:- """The active memberships of WorkOS user `subject`, one per customer.-- Someone in several organizations of one customer holds the highest of- their roles there. An organization that names no customer is skipped,- and logged: Switch cannot tell which customer it is part of.- """- roles: dict[str, str] = {}- for membership in await self._memberships(subject):- organization_id = membership.get("organization_id")- role = (membership.get("role") or {}).get("slug")- if not isinstance(organization_id, str) or not isinstance(role, str):- raise DirectoryUnavailable(- "WorkOS answered with a membership Switch cannot read."- )- organization = await self._get(f"/organizations/{organization_id}", {})- customer_id = customer_id_of(organization)- if customer_id is None:- logger.warning(- "WorkOS organization %s names no customer (metadata.%s); "- "user %s's membership of it is not brought into Switch",- organization_id,- CUSTOMER_ID_KEY,- subject,- )- continue- if roles.get(customer_id) != ORGANIZATION_ADMIN_ROLE:- roles[customer_id] = switch_role(role)- return [- DirectoryMembership(customer_id=customer_id, role=role)- for customer_id, role in sorted(roles.items())- ]-- async def _memberships(self, user_id: str) -> list[dict[str, Any]]:+ async def _list(self, path: str, params: dict[str, str]) -> list[dict[str, Any]]:+ """Every item of a paginated list, page by page."""found: list[dict[str, Any]] = []after: str | None = Nonewhile True:- params = {"user_id": user_id, "statuses": "active", "limit": str(_PAGE)}+ page_params = {**params, "limit": str(_PAGE)}if after is not None:- params["after"] = after- page = await self._get("/user_management/organization_memberships", params)+ page_params["after"] = after+ page = await self._get(path, page_params)data = page.get("data")- if not isinstance(data, list):- raise DirectoryUnavailable(- "WorkOS answered the memberships with no list."- )- found.extend(item for item in data if isinstance(item, dict))- after = (page.get("list_metadata") or {}).get("after")+ list_metadata = page.get("list_metadata") or {}+ if not isinstance(data, list) or not isinstance(list_metadata, dict):+ raise _unreadable(f"a list for {path}")+ if not all(isinstance(item, dict) for item in data):+ raise _unreadable(f"an item of {path}")+ found.extend(data)+ after = list_metadata.get("after")if not after:return found+ if not isinstance(after, str):+ raise _unreadable(f"a page cursor for {path}")++ async def memberships_at_sign_in(+ self, subject: str+ ) -> list[DirectoryMembership] | None:+ """The active memberships of WorkOS user `subject`, one per WorkOS+ organization. Which of those organizations Switch follows is for the+ caller to read from its own rows."""+ memberships: list[DirectoryMembership] = []+ for membership in await self._list(+ "/user_management/organization_memberships",+ {"user_id": subject, "statuses": "active"},+ ):+ organization_id = membership.get("organization_id")+ role = membership.get("role")+ slug = role.get("slug") if isinstance(role, dict) else None+ if not isinstance(organization_id, str) or not isinstance(slug, str):+ raise _unreadable("a membership")+ memberships.append(+ DirectoryMembership(+ workos_organization_id=organization_id, role=switch_role(slug)+ )+ )+ return memberships++ async def organization(self, workos_organization_id: str) -> dict[str, Any]:+ """The WorkOS organization `workos_organization_id`, for an operator+ linking a Switch organization to it."""+ return await self._get(f"/organizations/{workos_organization_id}", {})++ async def users_with_email(self, email: str) -> list[dict[str, Any]]:+ """The WorkOS users whose address is `email`, for an operator linking+ existing accounts."""+ return await self._list("/user_management/users", {"email": email})async def create_organization(self, creator: User, name: str) -> DirectoryOrganization:- raise DirectoryRefusal(NOT_FROM_SWITCH_YET)+ return DirectoryOrganization(workos_organization_id=None, customer_id=None)async def discard_organization(self, organization: DirectoryOrganization) -> None:- raise DirectoryRefusal(NOT_FROM_SWITCH_YET)+ return Noneasync def join(self, organization: Organization, user_id: str, role: str) -> None:- if organization.customer_id is None:+ if organization.workos_organization_id is None:return Noneraise DirectoryRefusal(NOT_FROM_SWITCH_YET)async def set_role(self, organization: Organization, user_id: str, role: str) -> None:- if organization.customer_id is None:+ """Taking Org Admin away in Switch needs nothing from WorkOS: it is how+ Switch catches up with a demotion there, and a sign-in makes them Org+ Admin again for as long as WorkOS still has them as one."""+ if organization.workos_organization_id is None:return None- raise DirectoryRefusal(NOT_FROM_SWITCH_YET)+ if role == ORGANIZATION_ADMIN_ROLE:+ raise DirectoryRefusal(NOT_FROM_SWITCH_YET)+ return Noneasync def remove(self, organization: Organization, user_id: str) -> None:- if organization.customer_id is None:- return None- raise DirectoryRefusal(NOT_FROM_SWITCH_YET)+ """As with a demotion: Switch catching up with a removal in WorkOS,+ undone at the next sign-in for as long as WorkOS still lists them."""+ return Nonediff --git a/core/switch_core/workos_linking.py b/core/switch_core/workos_linking.pynew file mode 100644index 000000000..d8f62b1ac--- /dev/null+++ b/core/switch_core/workos_linking.py@@ -0,0 +1,263 @@+"""`switch-link-workos`: what an operator links by hand when a server moves to+the WorkOS directory (`workos_directory.py`).++- `switch-link-workos organization ORGANIZATION_ID WORKOS_ORGANIZATION_ID`+ makes a Switch organization follow one WorkOS organization. From then on its+ members are brought in from that organization when they sign in, and its+ `org_admin`s are its Org Admins. Nothing a WorkOS organization says about+ itself links it, so this is the only way one is linked.+- `switch-link-workos accounts [--apply]` links existing accounts to their+ WorkOS users, by address. Signing in through WorkOS never joins an existing+ account by address (`UserStore.get_or_create_oidc_user`), so without this an+ account that signed in some other way before is refused. Each account is+ linked only when exactly one WorkOS user has its address, verified, and that+ user is linked to no other account. Every account is reported, and without+ `--apply` nothing is written.++Both need `WORKOS_API_KEY` and the gateway's OIDC sign-in configured against+the WorkOS environment, as the server does.+"""++from __future__ import annotations++import argparse+import asyncio+import logging+import sys+from dataclasses import dataclass++from sqlalchemy import exists, select+from sqlalchemy.exc import IntegrityError+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker++from switch_core.config import SwitchConfig+from switch_core.db.audit import (+ OrganizationAuditAction,+ record_organization_audit_event,+)+from switch_core.db.engine import create_engine_from_config, create_session_factory+from switch_core.db.models import OidcIdentity, Organization, User+from switch_core.db.session_scope import organization_session+from switch_core.db.stores.user_store import UserStore+from switch_core.organization_directory import DirectoryError+from switch_core.outbound import guarded_async_client+from switch_core.tenant_context import no_tenant+from switch_core.workos_directory import WorkOSDirectory, customer_id_of++logger = logging.getLogger(__name__)+++class LinkingError(Exception):+ """A link refused; the message says why, for the operator."""+++async def link_organization(+ session_factory: async_sessionmaker[AsyncSession],+ directory: WorkOSDirectory,+ organization_id: str,+ workos_organization_id: str,+) -> str:+ """Make `organization_id` follow `workos_organization_id`, and say what+ was done. Records the WorkOS organization's customer as+ `customer_id` where it names one."""+ workos_organization = await directory.organization(workos_organization_id)+ customer_id = customer_id_of(workos_organization)+ async with organization_session(session_factory, organization_id) as session:+ organization = await session.get(Organization, organization_id)+ if organization is None:+ raise LinkingError(f"No organization {organization_id} here.")+ if organization.workos_organization_id == workos_organization_id:+ return (+ f"Organization {organization_id} already follows "+ f"{workos_organization_id}."+ )+ if organization.workos_organization_id is not None:+ raise LinkingError(+ f"Organization {organization_id} already follows "+ f"{organization.workos_organization_id}."+ )+ organization.workos_organization_id = workos_organization_id+ organization.customer_id = customer_id+ try:+ await session.flush()+ except IntegrityError as error:+ raise LinkingError(+ f"Another organization already follows {workos_organization_id}"+ + (f" or customer {customer_id}" if customer_id else "")+ + "."+ ) from error+ await record_organization_audit_event(+ session,+ organization_id=organization_id,+ actor_user_id=None,+ action=OrganizationAuditAction.LINKED_TO_DIRECTORY,+ target_type="organization",+ target_id=organization_id,+ details={+ "workos_organization_id": workos_organization_id,+ "customer_id": customer_id,+ },+ )+ await session.commit()+ logger.warning(+ "Linked organization %s to WorkOS organization %s",+ organization_id,+ workos_organization_id,+ )+ return (+ f"Organization {organization_id} now follows {workos_organization_id} "+ f"(customer {customer_id or 'not named'})."+ )+++@dataclass(frozen=True)+class AccountLink:+ """What linking one account did, or would do."""++ user_id: str+ email: str+ outcome: str+ linked: bool+++async def link_accounts(+ session_factory: async_sessionmaker[AsyncSession],+ directory: WorkOSDirectory,+ user_store: UserStore,+ issuer: str,+ *,+ apply: bool,+) -> list[AccountLink]:+ """Link every account with no identity from `issuer` to the WorkOS user+ with its address, where exactly one has it verified and is linked to no+ other account. Writes only when `apply`."""+ with no_tenant():+ async with session_factory() as session:+ users = list(+ await session.scalars(+ select(User)+ .where(+ ~exists().where(+ OidcIdentity.user_id == User.id, OidcIdentity.iss == issuer+ )+ )+ .order_by(User.created_at, User.id)+ )+ )+ results: list[AccountLink] = []+ for user in users:+ results.append(+ await _link_account(+ session, directory, user_store, issuer, user, apply=apply+ )+ )+ if apply:+ await session.commit()+ return results+++async def _link_account(+ session: AsyncSession,+ directory: WorkOSDirectory,+ user_store: UserStore,+ issuer: str,+ user: User,+ *,+ apply: bool,+) -> AccountLink:+ def result(outcome: str, linked: bool = False) -> AccountLink:+ return AccountLink(user.id, user.email, outcome, linked)++ candidates = [+ workos_user+ for workos_user in await directory.users_with_email(user.email)+ if str(workos_user.get("email", "")).lower() == user.email.lower()+ ]+ if not candidates:+ return result("no WorkOS user has this address")+ if len(candidates) > 1:+ return result(f"{len(candidates)} WorkOS users have this address")+ workos_user = candidates[0]+ subject = workos_user.get("id")+ if not isinstance(subject, str) or not subject:+ return result("the WorkOS user has no id Switch can read")+ if workos_user.get("email_verified") is not True:+ return result(f"WorkOS user {subject} has not verified this address")+ holder = await user_store.get_by_oidc_identity(session, iss=issuer, sub=subject)+ if holder is not None:+ return result(f"WorkOS user {subject} is linked to account {holder.id}")+ if not apply:+ return result(f"would link WorkOS user {subject}")+ await user_store.link_oidc_identity(session, user=user, iss=issuer, sub=subject)+ return result(f"linked WorkOS user {subject}", linked=True)+++def _parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ prog="switch-link-workos",+ description="Link organizations and existing accounts to WorkOS.",+ )+ commands = parser.add_subparsers(dest="command", required=True)+ organization = commands.add_parser(+ "organization", help="make a Switch organization follow a WorkOS one"+ )+ organization.add_argument("organization_id")+ organization.add_argument("workos_organization_id")+ accounts = commands.add_parser(+ "accounts", help="link existing accounts to their WorkOS users by address"+ )+ accounts.add_argument(+ "--apply", action="store_true", help="write the links (default: report only)"+ )+ return parser+++async def _run(config: SwitchConfig, arguments: argparse.Namespace) -> int:+ if not config.workos_api_key or not config.gateway_oidc_issuer_url:+ raise SystemExit(+ "switch-link-workos needs WORKOS_API_KEY and the gateway's OIDC "+ "sign-in (GATEWAY_OIDC_*) configured against the WorkOS environment."+ )+ engine = create_engine_from_config(config)+ try:+ async with guarded_async_client(config.outbound_policy) as http:+ directory = WorkOSDirectory(+ http, config.workos_api_key, config.workos_api_url+ )+ session_factory = create_session_factory(engine)+ try:+ if arguments.command == "organization":+ print(+ await link_organization(+ session_factory,+ directory,+ arguments.organization_id,+ arguments.workos_organization_id,+ )+ )+ return 0+ links = await link_accounts(+ session_factory,+ directory,+ UserStore(),+ config.gateway_oidc_issuer_url,+ apply=arguments.apply,+ )+ except (LinkingError, DirectoryError) as error:+ print(error, file=sys.stderr)+ return 1+ for link in links:+ print(f"{link.user_id}\t{link.email}\t{link.outcome}")+ print(+ f"{sum(link.linked for link in links)} linked, "+ f"{len(links)} accounts without a WorkOS identity"+ + ("" if arguments.apply else " (report only: pass --apply to link)")+ )+ return 0+ finally:+ await engine.dispose()+++def main() -> None:+ config = SwitchConfig()+ raise SystemExit(asyncio.run(_run(config, _parser().parse_args())))diff --git a/core/tests/switch_core/db/test_tenant_exemption_allowlist.py b/core/tests/switch_core/db/test_tenant_exemption_allowlist.pyindex 496e71b3b..ee076e104 100644--- a/core/tests/switch_core/db/test_tenant_exemption_allowlist.py+++ b/core/tests/switch_core/db/test_tenant_exemption_allowlist.py@@ -238,6 +238,11 @@ _RAW_SESSION_FACTORY_MODULES = {# (`rls_ddl.GLOBAL_TABLES`): one guardrails policy covers the whole# deployment, so there is no tenant for a policy to narrow on."switch_core.trust.client",+ # The operator's account linking reads `users` and writes+ # `oidc_identities`, both global (`rls_ddl.GLOBAL_TABLES`): a person and+ # their sign-in identities belong to no tenant. Its organization linking+ # binds the organization (`organization_session`).+ "switch_core.workos_linking",## ── The OAuth client Core registers for itself at a vendor that offers# no other (`service_oauth_clients`, in `rls_ddl.GLOBAL_TABLES`). Thediff --git a/core/tests/switch_core/db/test_tenant_lookup.py b/core/tests/switch_core/db/test_tenant_lookup.pyindex 4c8a4830b..f6b16052c 100644--- a/core/tests/switch_core/db/test_tenant_lookup.py+++ b/core/tests/switch_core/db/test_tenant_lookup.py@@ -111,7 +111,7 @@ _ADDED_SINCE = {"tenant_of_sign_in_flow": "e5956cbd3bdc","tenants_bindable_by_user": "5c2e8f17a9d4","all_organization_ids": "10f6d02ac5bc",- "organization_of_customer": "f2b8d6c1a4e9",+ "directory_organizations_of_user": "f2b8d6c1a4e9",}# A third kind of change, and the quietest: a lookup whose body a laterdiff --git a/core/tests/switch_core/gateway/test_oidc_routes.py b/core/tests/switch_core/gateway/test_oidc_routes.pyindex b4d747480..25c5bb90a 100644--- a/core/tests/switch_core/gateway/test_oidc_routes.py+++ b/core/tests/switch_core/gateway/test_oidc_routes.py@@ -9,6 +9,7 @@ import httpximport pytestfrom fastapi import FastAPI, HTTPExceptionfrom sqlalchemy import select+from sqlalchemy.exc import OperationalErrorfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom starlette.middleware.sessions import SessionMiddlewarefrom starlette.responses import RedirectResponse@@ -118,7 +119,7 @@ class TestSigningInWithADirectory:return _config(organization_directory="workos",workos_api_key="sk_test_example",- gateway_signup_mode="open",+ gateway_signup_mode="invite_only",)async def test_their_organizations_are_brought_in(@@ -126,7 +127,7 @@ class TestSigningInWithADirectory:session_factory: async_sessionmaker[AsyncSession],monkeypatch: pytest.MonkeyPatch,) -> None:- customer = f"ten_{uuid.uuid4().hex[:8]}"+ linked = f"org_workos_{uuid.uuid4().hex[:8]}"organization_id = f"org-{uuid.uuid4().hex[:8]}"async with session_factory() as session:session.add(@@ -134,11 +135,11 @@ class TestSigningInWithADirectory:id=organization_id,slug=organization_id,name="Acme",- customer_id=customer,+ workos_organization_id=linked,))await session.commit()- directory = _Directory([DirectoryMembership(customer, "org_admin")])+ directory = _Directory([DirectoryMembership(linked, "org_admin")])monkeypatch.setattr(oidc_routes, "_client", lambda: _FakeClient(self._TOKEN))async with session_factory() as session:@@ -181,6 +182,32 @@ class TestSigningInWithADirectory:assert response.status_code == 303assert "WorkOS answered 500." in caplog.text+ async def test_a_failure_writing_switchs_rows_does_not_stop_the_sign_in(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ monkeypatch: pytest.MonkeyPatch,+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ async def failing(*_args: object) -> None:+ raise OperationalError("UPDATE organization_members", {}, Exception())++ monkeypatch.setattr(oidc_routes, "bring_in_memberships", failing)+ monkeypatch.setattr(oidc_routes, "_client", lambda: _FakeClient(self._TOKEN))++ async with session_factory() as session:+ response = await oidc_routes.oidc_callback(+ request=SimpleNamespace(session={}), # type: ignore[arg-type]+ config=self._config(),+ session=session,+ session_factory=session_factory,+ user_store=UserStore(),+ organization_directory=_Directory([]), # type: ignore[arg-type]+ )++ assert response.status_code == 303+ [record] = [r for r in caplog.records if "may be behind" in r.message]+ assert record.levelname == "ERROR"+async def test_an_account_with_the_same_email_is_not_joined(self,session_factory: async_sessionmaker[AsyncSession],diff --git a/core/tests/switch_core/test_config_organization_directory.py b/core/tests/switch_core/test_config_organization_directory.pyindex 460de4d66..f687d5b12 100644--- a/core/tests/switch_core/test_config_organization_directory.py+++ b/core/tests/switch_core/test_config_organization_directory.py@@ -9,6 +9,7 @@ OIDC = dict(gateway_oidc_client_id="client_example",gateway_oidc_client_secret="placeholder", # gitleaks:allowgateway_oidc_scopes="openid email profile",+ gateway_signup_mode="invite_only",)@@ -36,3 +37,13 @@ def test_workos_without_the_sign_in_is_refused() -> None:organization_directory="workos",workos_api_key="sk_test_example", # gitleaks:allow)++[email protected]("mode", ["open", "default_tenant"])+def test_workos_with_self_service_sign_up_is_refused(mode: str) -> None:+ with pytest.raises(ValidationError, match="invite_only"):+ _config(+ organization_directory="workos",+ workos_api_key="sk_test_example", # gitleaks:allow+ **{**OIDC, "gateway_signup_mode": mode},+ )diff --git a/core/tests/switch_core/test_organization_mirror.py b/core/tests/switch_core/test_organization_mirror.pyindex e687cdb15..ed2e4a205 100644--- a/core/tests/switch_core/test_organization_mirror.py+++ b/core/tests/switch_core/test_organization_mirror.py@@ -28,7 +28,7 @@ def _id(prefix: str) -> str:return f"{prefix}-{uuid.uuid4().hex[:8]}"-async def _organization(session_factory, customer_id: str | None) -> str:+async def _organization(session_factory, workos_organization_id: str | None) -> str:organization_id = _id("org")async with session_factory() as session:session.add(@@ -36,7 +36,7 @@ async def _organization(session_factory, customer_id: str | None) -> str:id=organization_id,slug=organization_id,name="Acme",- customer_id=customer_id,+ workos_organization_id=workos_organization_id,))await session.commit()@@ -64,33 +64,52 @@ async def _role(session_factory, organization_id: str, user_id: str) -> str | Noreturn None if member is None else member.role-async def _added(session_factory, organization_id: str) -> list[OrganizationAuditEvent]:+async def _member(session_factory, organization_id: str, user_id: str, role: str):+ async with session_factory() as session:+ session.add(+ OrganizationMember(+ organization_id=organization_id, user_id=user_id, role=role+ )+ )+ await session.commit()+++async def _recorded(+ session_factory, organization_id: str, action: OrganizationAuditAction+) -> list[OrganizationAuditEvent]:async with session_factory() as session:return list(await session.scalars(select(OrganizationAuditEvent).where(OrganizationAuditEvent.organization_id == organization_id,- OrganizationAuditEvent.action- == OrganizationAuditAction.MEMBER_ADDED_AT_SIGN_IN,+ OrganizationAuditEvent.action == action,)))+async def _added(session_factory, organization_id: str) -> list[OrganizationAuditEvent]:+ return await _recorded(+ session_factory,+ organization_id,+ OrganizationAuditAction.MEMBER_ADDED_AT_SIGN_IN,+ )++class TestBringingInMemberships:async def test_adds_them_with_the_directorys_role_and_records_it(self, rls_harness: RLSHarness) -> None:session_factory, restricted = rls_harness.owner, rls_harness.restricted- customer = _id("ten")- organization_id = await _organization(session_factory, customer)+ linked = _id("org_workos")+ organization_id = await _organization(session_factory, linked)ada = await _person(session_factory)await bring_in_memberships(- restricted, ada, [DirectoryMembership(customer, "org_admin")]+ restricted, ada, [DirectoryMembership(linked, "org_admin")])await bring_in_memberships(- restricted, ada, [DirectoryMembership(customer, "org_admin")]+ restricted, ada, [DirectoryMembership(linked, "org_admin")])assert await _role(session_factory, organization_id, ada) == "org_admin"@@ -99,44 +118,88 @@ class TestBringingInMemberships:assert event.target_id == adaassert event.details == {"role": "org_admin"}- async def test_a_customer_with_no_switch_organization_is_left_alone(+ async def test_only_the_linked_organization_counts(self, rls_harness: RLSHarness) -> None:+ """An `org_admin` of some other WorkOS organization (another product's,+ beneath the same customer, or one claiming the customer in its+ metadata) is nothing in the linked organization."""session_factory, restricted = rls_harness.owner, rls_harness.restricted+ organization_id = await _organization(session_factory, _id("org_workos"))ada = await _person(session_factory)await bring_in_memberships(- restricted, ada, [DirectoryMembership(_id("ten"), "member")]+ restricted, ada, [DirectoryMembership(_id("org_elsewhere"), "org_admin")])- async with session_factory() as session:- assert (- await session.scalars(- select(OrganizationMember).where(OrganizationMember.user_id == ada)- )- ).all() == []+ assert await _role(session_factory, organization_id, ada) is None++ async def test_an_organization_nobody_linked_is_left_alone(+ self, rls_harness: RLSHarness+ ) -> None:+ session_factory, restricted = rls_harness.owner, rls_harness.restricted+ organization_id = await _organization(session_factory, None)+ ada = await _person(session_factory)+ await _member(session_factory, organization_id, ada, "member")- async def test_a_role_that_differs_is_left_and_logged(+ await bring_in_memberships(+ restricted, ada, [DirectoryMembership(_id("org_workos"), "org_admin")]+ )++ assert await _role(session_factory, organization_id, ada) == "member"++ async def test_a_member_made_org_admin_is_promoted_and_it_is_recorded(+ self, rls_harness: RLSHarness+ ) -> None:+ session_factory, restricted = rls_harness.owner, rls_harness.restricted+ linked = _id("org_workos")+ organization_id = await _organization(session_factory, linked)+ ada = await _person(session_factory)+ await _member(session_factory, organization_id, ada, "member")++ await bring_in_memberships(+ restricted, ada, [DirectoryMembership(linked, "org_admin")]+ )++ assert await _role(session_factory, organization_id, ada) == "org_admin"+ [event] = await _recorded(+ session_factory,+ organization_id,+ OrganizationAuditAction.MEMBER_PROMOTED_AT_SIGN_IN,+ )+ assert event.target_id == ada+ assert event.details == {"from_role": "member", "role": "org_admin"}++ async def test_a_demotion_is_left_and_logged_as_an_error(self, rls_harness: RLSHarness, caplog: pytest.LogCaptureFixture) -> None:"""Taking a role away takes away what it reached, which is not asign-in's to do."""session_factory, restricted = rls_harness.owner, rls_harness.restricted- customer = _id("ten")- organization_id = await _organization(session_factory, customer)+ linked = _id("org_workos")+ organization_id = await _organization(session_factory, linked)ada = await _person(session_factory)- async with session_factory() as session:- session.add(- OrganizationMember(- organization_id=organization_id, user_id=ada, role="org_admin"- )- )- await session.commit()+ await _member(session_factory, organization_id, ada, "org_admin")await bring_in_memberships(- restricted, ada, [DirectoryMembership(customer, "member")]+ restricted, ada, [DirectoryMembership(linked, "member")])assert await _role(session_factory, organization_id, ada) == "org_admin"assert await _added(session_factory, organization_id) == []- assert "leaves Switch's role as it is" in caplog.text+ [record] = [r for r in caplog.records if "keeps their role" in r.message]+ assert record.levelname == "ERROR"++ async def test_a_membership_no_longer_listed_is_left_and_logged_as_an_error(+ self, rls_harness: RLSHarness, caplog: pytest.LogCaptureFixture+ ) -> None:+ session_factory, restricted = rls_harness.owner, rls_harness.restricted+ organization_id = await _organization(session_factory, _id("org_workos"))+ ada = await _person(session_factory)+ await _member(session_factory, organization_id, ada, "org_admin")++ await bring_in_memberships(restricted, ada, [])++ assert await _role(session_factory, organization_id, ada) == "org_admin"+ [record] = [r for r in caplog.records if "no longer lists them" in r.message]+ assert record.levelname == "ERROR"diff --git a/core/tests/switch_core/test_workos_directory.py b/core/tests/switch_core/test_workos_directory.pyindex ca2759eae..0dbaebcba 100644--- a/core/tests/switch_core/test_workos_directory.py+++ b/core/tests/switch_core/test_workos_directory.py@@ -11,10 +11,11 @@ import pytestfrom switch_core.organization_directory import (DirectoryMembership,+ DirectoryOrganization,DirectoryRefusal,DirectoryUnavailable,)-from switch_core.workos_directory import WorkOSDirectory+from switch_core.workos_directory import WorkOSDirectory, customer_id_ofAPI = "https://workos.example.test"KEY = "sk_test_example"@@ -83,15 +84,12 @@ def _directory(handler) -> WorkOSDirectory:class TestMembershipsAtSignIn:- async def test_one_membership_per_customer_with_the_highest_role(self) -> None:- """A customer's own organization and one a product made beneath it- name the same customer; an `org_admin` of either is its Org Admin."""+ async def test_one_membership_per_organization_in_switchs_roles(self) -> None:+ """Every organization is answered by its WorkOS id, whatever its+ metadata says: which ones a Switch organization follows is Switch's+ own record, not anything WorkOS holds."""workos = _WorkOS(- organizations={- "org_root": {"tenant_id": "ten_a"},- "org_child": {"tenant_id": "ten_a"},- "org_other": {"tenant_id": "ten_b"},- },+ organizations={},memberships=[("org_root", "member"),("org_child", "org_admin"),@@ -102,36 +100,28 @@ class TestMembershipsAtSignIn:memberships = await _directory(workos).memberships_at_sign_in("user_EXAMPLE")assert memberships == [- DirectoryMembership(customer_id="ten_a", role="org_admin"),- DirectoryMembership(customer_id="ten_b", role="member"),+ DirectoryMembership(workos_organization_id="org_root", role="member"),+ DirectoryMembership(workos_organization_id="org_child", role="org_admin"),+ DirectoryMembership(workos_organization_id="org_other", role="member"),]+ assert all(+ not request.url.path.startswith("/organizations/")+ for request in workos.requests+ )async def test_every_page_is_read(self) -> None:workos = _WorkOS(- organizations={f"org_{i}": {"tenant_id": f"ten_{i}"} for i in range(5)},+ organizations={},memberships=[(f"org_{i}", "member") for i in range(5)],page=2,)memberships = await _directory(workos).memberships_at_sign_in("user_EXAMPLE")- assert [membership.customer_id for membership in memberships] == [- f"ten_{i}" for i in range(5)+ assert [membership.workos_organization_id for membership in memberships] == [+ f"org_{i}" for i in range(5)]- async def test_an_organization_naming_no_customer_is_skipped(- self, caplog: pytest.LogCaptureFixture- ) -> None:- workos = _WorkOS(- organizations={"org_bare": {}, "org_a": {"tenant_id": "ten_a"}},- memberships=[("org_bare", "org_admin"), ("org_a", "member")],- )-- memberships = await _directory(workos).memberships_at_sign_in("user_EXAMPLE")-- assert memberships == [DirectoryMembership(customer_id="ten_a", role="member")]- assert "org_bare names no customer" in caplog.text-@pytest.mark.parametrize("answer",[@@ -139,6 +129,16 @@ class TestMembershipsAtSignIn:httpx.Response(401),httpx.Response(200, json={"data": "not a list"}),httpx.Response(200, content=json.dumps([1, 2])),+ httpx.Response(200, content=b"<html>not JSON</html>"),+ httpx.Response(200, json={"data": [], "list_metadata": "not a dict"}),+ httpx.Response(200, json={"data": ["not a dict"]}),+ httpx.Response(+ 200,+ json={"data": [{"organization_id": "org_a", "role": "org_admin"}]},+ ),+ httpx.Response(+ 200, json={"data": [], "list_metadata": {"after": {"not": "text"}}}+ ),],)async def test_an_answer_switch_cannot_use_is_unavailable(@@ -157,27 +157,72 @@ class TestMembershipsAtSignIn:await _directory(unreachable).memberships_at_sign_in("user_EXAMPLE")+class TestWhatAnOperatorLinks:+ async def test_an_organization_is_read_by_its_id(self) -> None:+ workos = _WorkOS(+ organizations={"org_a": {"tenant_id": "ten_a"}}, memberships=[]+ )++ organization = await _directory(workos).organization("org_a")++ assert customer_id_of(organization) == "ten_a"++ async def test_users_are_found_by_address(self) -> None:+ def handler(request: httpx.Request) -> httpx.Response:+ assert request.url.path == "/user_management/users"+ assert request.url.params["email"] == "[email protected]"+ return httpx.Response(+ 200,+ json={+ "data": [{"id": "user_A", "email": "[email protected]"}],+ "list_metadata": {"after": None},+ },+ )++ users = await _directory(handler).users_with_email("[email protected]")++ assert [user["id"] for user in users] == ["user_A"]++class TestChangesFromSwitch:- """Changes made from Switch go through the service that owns the customer- record; until that is wired in, they are refused for a customer's- organization, and an organization Switch keeps itself stays its own."""+ """Changes made from Switch to a linked organization go through the+ service that owns the customer record; until that is wired in, those that+ grant are refused, and an organization nobody linked stays Switch's own."""- async def test_a_customers_organization_refuses_them(self) -> None:+ async def test_a_linked_organization_refuses_those_that_grant(self) -> None:directory = _directory(lambda request: httpx.Response(500))- customer = SimpleNamespace(customer_id="ten_a")+ linked = SimpleNamespace(workos_organization_id="org_a")for change in (- directory.join(customer, "u", "member"), # type: ignore[arg-type]- directory.set_role(customer, "u", "org_admin"), # type: ignore[arg-type]- directory.remove(customer, "u"), # type: ignore[arg-type]+ directory.join(linked, "u", "member"), # type: ignore[arg-type]+ directory.set_role(linked, "u", "org_admin"), # type: ignore[arg-type]):with pytest.raises(DirectoryRefusal):await change+ async def test_a_linked_organization_lets_switch_take_away(self) -> None:+ """So an Org Admin can end in Switch what WorkOS has ended; a sign-in+ brings back whatever WorkOS still grants."""+ directory = _directory(lambda request: httpx.Response(500))+ linked = SimpleNamespace(workos_organization_id="org_a")++ await directory.set_role(linked, "u", "member") # type: ignore[arg-type]+ await directory.remove(linked, "u") # type: ignore[arg-type]+async def test_an_organization_switch_keeps_itself_needs_nothing(self) -> None:directory = _directory(lambda request: httpx.Response(500))- local = SimpleNamespace(customer_id=None)+ local = SimpleNamespace(workos_organization_id=None)await directory.join(local, "u", "member") # type: ignore[arg-type]await directory.set_role(local, "u", "org_admin") # type: ignore[arg-type]await directory.remove(local, "u") # type: ignore[arg-type]++ async def test_a_new_organization_is_switchs_own_until_linked(self) -> None:+ directory = _directory(lambda request: httpx.Response(500))++ created = await directory.create_organization(SimpleNamespace(), "Example") # type: ignore[arg-type]++ assert created == DirectoryOrganization(+ workos_organization_id=None, customer_id=None+ )+ await directory.discard_organization(created)diff --git a/core/tests/switch_core/test_workos_linking.py b/core/tests/switch_core/test_workos_linking.pynew file mode 100644index 000000000..cdd01bfa0--- /dev/null+++ b/core/tests/switch_core/test_workos_linking.py@@ -0,0 +1,290 @@+"""What an operator links by hand when a server moves to the WorkOS+directory, against Postgres, on the restricted role row-level security+applies to; `owner` only arranges fixtures and reads results."""++from __future__ import annotations++import uuid++import pytest+from sqlalchemy import select++from switch_core.db.audit import OrganizationAuditAction+from switch_core.db.models import (+ OidcIdentity,+ Organization,+ OrganizationAuditEvent,+ User,+)+from switch_core.db.stores.user_store import UserStore+from switch_core.workos_linking import LinkingError, link_accounts, link_organization+from tests.conftest import RLSHarness++pytestmark = pytest.mark.no_ambient_tenant+++def _id(prefix: str) -> str:+ return f"{prefix}-{uuid.uuid4().hex[:8]}"+++class _WorkOS:+ """Answers the two reads linking makes: organizations by id, users by+ address."""++ def __init__(+ self,+ organizations: dict[str, dict] | None = None,+ users: dict[str, list[dict]] | None = None,+ ) -> None:+ self.organizations = organizations or {}+ self.users = users or {}++ async def organization(self, workos_organization_id: str) -> dict:+ return self.organizations[workos_organization_id]++ async def users_with_email(self, email: str) -> list[dict]:+ return self.users.get(email, [])+++async def _organization(session_factory, workos_organization_id: str | None) -> str:+ organization_id = _id("org")+ async with session_factory() as session:+ session.add(+ Organization(+ id=organization_id,+ slug=organization_id,+ name="Acme",+ workos_organization_id=workos_organization_id,+ )+ )+ await session.commit()+ return organization_id+++async def _person(session_factory) -> User:+ user_id = _id("user")+ user = User(id=user_id, name=user_id, email=f"{user_id}@example.test", role="user")+ async with session_factory() as session:+ session.add(user)+ await session.commit()+ return user+++async def _identities(session_factory, user_id: str) -> list[tuple[str | None, str]]:+ async with session_factory() as session:+ rows = await session.execute(+ select(OidcIdentity.iss, OidcIdentity.sub).where(+ OidcIdentity.user_id == user_id+ )+ )+ return [(iss, sub) for iss, sub in rows]+++class TestLinkingAnOrganization:+ async def test_links_it_records_the_customer_and_audits_it(+ self, rls_harness: RLSHarness+ ) -> None:+ owner, restricted = rls_harness.owner, rls_harness.restricted+ organization_id = await _organization(owner, None)+ workos_id, customer = _id("org_workos"), _id("ten")+ workos = _WorkOS(+ organizations={workos_id: {"metadata": {"tenant_id": customer}}}+ )++ said = await link_organization(+ restricted,+ workos, # type: ignore[arg-type]+ organization_id,+ workos_id,+ )++ assert "now follows" in said+ async with owner() as session:+ organization = await session.get(Organization, organization_id)+ [event] = await session.scalars(+ select(OrganizationAuditEvent).where(+ OrganizationAuditEvent.organization_id == organization_id,+ OrganizationAuditEvent.action+ == OrganizationAuditAction.LINKED_TO_DIRECTORY,+ )+ )+ assert organization is not None+ assert organization.workos_organization_id == workos_id+ assert organization.customer_id == customer+ assert event.details == {+ "workos_organization_id": workos_id,+ "customer_id": customer,+ }++ async def test_linking_it_again_to_the_same_one_changes_nothing(+ self, rls_harness: RLSHarness+ ) -> None:+ workos_id = _id("org_workos")+ organization_id = await _organization(rls_harness.owner, workos_id)++ said = await link_organization(+ rls_harness.restricted,+ _WorkOS(organizations={workos_id: {}}), # type: ignore[arg-type]+ organization_id,+ workos_id,+ )++ assert "already follows" in said++ async def test_an_organization_linked_elsewhere_is_refused(+ self, rls_harness: RLSHarness+ ) -> None:+ organization_id = await _organization(rls_harness.owner, _id("org_workos"))+ other = _id("org_workos")++ with pytest.raises(LinkingError, match="already follows"):+ await link_organization(+ rls_harness.restricted,+ _WorkOS(organizations={other: {}}), # type: ignore[arg-type]+ organization_id,+ other,+ )++ async def test_a_workos_organization_another_follows_is_refused(+ self, rls_harness: RLSHarness+ ) -> None:+ workos_id = _id("org_workos")+ await _organization(rls_harness.owner, workos_id)+ organization_id = await _organization(rls_harness.owner, None)++ with pytest.raises(LinkingError, match="Another organization"):+ await link_organization(+ rls_harness.restricted,+ _WorkOS(organizations={workos_id: {}}), # type: ignore[arg-type]+ organization_id,+ workos_id,+ )++ async def test_an_unknown_organization_is_refused(+ self, rls_harness: RLSHarness+ ) -> None:+ workos_id = _id("org_workos")++ with pytest.raises(LinkingError, match="No organization"):+ await link_organization(+ rls_harness.restricted,+ _WorkOS(organizations={workos_id: {}}), # type: ignore[arg-type]+ _id("org"),+ workos_id,+ )+++class TestLinkingAccounts:+ async def test_reports_without_writing_then_links_with_apply(+ self, rls_harness: RLSHarness+ ) -> None:+ owner, restricted = rls_harness.owner, rls_harness.restricted+ issuer = f"https://{_id('auth')}.example.test"+ ada = await _person(owner)+ workos = _WorkOS(+ users={+ ada.email: [+ {"id": "user_ADA", "email": ada.email, "email_verified": True}+ ]+ }+ )++ reported = await link_accounts(+ restricted,+ workos, # type: ignore[arg-type]+ UserStore(),+ issuer,+ apply=False,+ )+ [mine] = [link for link in reported if link.user_id == ada.id]+ assert mine.outcome == "would link WorkOS user user_ADA"+ assert await _identities(owner, ada.id) == []++ applied = await link_accounts(+ restricted,+ workos, # type: ignore[arg-type]+ UserStore(),+ issuer,+ apply=True,+ )+ [mine] = [link for link in applied if link.user_id == ada.id]+ assert mine.linked+ assert await _identities(owner, ada.id) == [(issuer, "user_ADA")]++ again = await link_accounts(+ restricted,+ workos, # type: ignore[arg-type]+ UserStore(),+ issuer,+ apply=True,+ )+ assert ada.id not in {link.user_id for link in again}++ @pytest.mark.parametrize(+ ("workos_users", "outcome"),+ [+ ([], "no WorkOS user has this address"),+ (+ [+ {"id": "user_A", "email_verified": True},+ {"id": "user_B", "email_verified": True},+ ],+ "2 WorkOS users have this address",+ ),+ (+ [{"id": "user_A", "email_verified": False}],+ "WorkOS user user_A has not verified this address",+ ),+ ],+ )+ async def test_an_account_without_one_verified_match_is_left(+ self, rls_harness: RLSHarness, workos_users: list[dict], outcome: str+ ) -> None:+ owner = rls_harness.owner+ issuer = f"https://{_id('auth')}.example.test"+ ada = await _person(owner)+ workos = _WorkOS(+ users={ada.email: [{**user, "email": ada.email} for user in workos_users]}+ )++ links = await link_accounts(+ rls_harness.restricted,+ workos, # type: ignore[arg-type]+ UserStore(),+ issuer,+ apply=True,+ )++ [mine] = [link for link in links if link.user_id == ada.id]+ assert mine.outcome == outcome+ assert not mine.linked+ assert await _identities(owner, ada.id) == []++ async def test_a_workos_user_linked_to_another_account_is_left(+ self, rls_harness: RLSHarness+ ) -> None:+ owner = rls_harness.owner+ issuer = f"https://{_id('auth')}.example.test"+ ada, bea = await _person(owner), await _person(owner)+ async with owner() as session:+ session.add(OidcIdentity(user_id=bea.id, iss=issuer, sub="user_ADA"))+ await session.commit()+ workos = _WorkOS(+ users={+ ada.email: [+ {"id": "user_ADA", "email": ada.email, "email_verified": True}+ ]+ }+ )++ links = await link_accounts(+ rls_harness.restricted,+ workos, # type: ignore[arg-type]+ UserStore(),+ issuer,+ apply=True,+ )++ [mine] = [link for link in links if link.user_id == ada.id]+ assert mine.outcome == f"WorkOS user user_ADA is linked to account {bea.id}"+ assert await _identities(owner, ada.id) == []diff --git a/docs/old/workos-directory.md b/docs/old/workos-directory.mdnew file mode 100644index 000000000..f55514230--- /dev/null+++ b/docs/old/workos-directory.md@@ -0,0 +1,73 @@+# Organizations kept in WorkOS++By default a Switch server keeps its own record of organizations and their+members (`ORGANIZATION_DIRECTORY=local`). With `ORGANIZATION_DIRECTORY=workos`+the members of an organization, and who is its Org Admin, come from an+organization in a WorkOS environment instead. This is still being built and is+off by default; this page says what works today and what does not yet.++## What you need++- `ORGANIZATION_DIRECTORY=workos`+- `WORKOS_API_KEY`: an API key of the WorkOS environment (`WORKOS_API_URL`+ defaults to `https://api.workos.com`).+- The gateway's OIDC sign-in (`GATEWAY_OIDC_*`, see+ [`GATEWAY_OIDC_SETUP.md`](GATEWAY_OIDC_SETUP.md)) set up against an AuthKit+ application of the same environment. The sign-in's subject is the person's+ WorkOS user id.+- `GATEWAY_SIGNUP_MODE=invite_only`. Organizations cannot be created in WorkOS+ from Switch yet, so nobody but the operator creates one; the server refuses+ to start with any other mode.++## Linking++Nothing is linked automatically. An operator links each Switch organization to+the one WorkOS organization it follows, and links existing accounts to their+WorkOS users, with `switch-link-workos` (run where switch-core runs, with the+same configuration):++```+switch-link-workos organization <switch organization id> <WorkOS organization id>+switch-link-workos accounts # report only+switch-link-workos accounts --apply # write the links+```++- **Organizations.** Only the linked WorkOS organization counts: being in, or+ `org_admin` of, any other WorkOS organization, even one beneath the same+ customer, gives nothing in Switch. A WorkOS organization's metadata never+ links it. The link is recorded in the organization's audit log.+- **Accounts.** Signing in through WorkOS never joins an existing Switch+ account by email address; someone whose address already has an account is+ refused until it is linked. `accounts` links an account when exactly one+ WorkOS user has its address, verified, and that user is linked to no other+ account, and reports every account it could not link and why. Accounts it+ cannot link can be handled by hand or left to sign in as new people once+ their old account is removed.++## At sign-in++When someone signs in, Switch reads their WorkOS memberships and, for each+linked organization:++- adds them if WorkOS lists them and Switch does not, with WorkOS's role+ (`org_admin` is Org Admin; every other WorkOS role is Member);+- makes them Org Admin if WorkOS has them as `org_admin` and Switch as Member.++Both are recorded in the organization's audit log. If WorkOS cannot be read,+or Switch's rows cannot be written, the sign-in goes ahead with Switch's rows+as they were, and the failure is logged.++## Not done yet++- **Removals and demotions are not applied.** If someone is removed from the+ linked WorkOS organization, or is no longer its `org_admin`, Switch keeps+ their membership and role, and with them their access. Each sign-in by that+ person logs it at `error`. Until this is built, an Org Admin ends such+ access in Switch as well, from the organization's Members page: removing a+ member and taking Org Admin away are allowed on a linked organization, and+ change only Switch. Whatever WorkOS still grants comes back at the person's+ next sign-in.+- **Granting from Switch is refused** for a linked organization: adding a+ member (from the dashboard or by invitation) or making one Org Admin. Make+ those changes in WorkOS.+- A new organization created on the server is Switch's own until it is linked.diff --git a/gateway/src/pages/organization/OrganizationAuditSection.tsx b/gateway/src/pages/organization/OrganizationAuditSection.tsxindex 38932cd8f..8f082f5f3 100644--- a/gateway/src/pages/organization/OrganizationAuditSection.tsx+++ b/gateway/src/pages/organization/OrganizationAuditSection.tsx@@ -19,6 +19,8 @@ const ACTION_LABELS: Record<string, string> = {"organization.service_set": "Service switched","organization.service_cleared": "Service left to workspaces","organization.member_added_at_sign_in": "Member added at sign-in",+ "organization.member_promoted_at_sign_in": "Member made Org Admin at sign-in",+ "organization.linked_to_directory": "Linked to WorkOS",};const COLUMNS: GridColDef<OrganizationAuditEvent>[] = [