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(gateway): backfill legacy OIDC identities, match email case-insensitively, and handle races (CHOO-2624)
Wojciech Wojtyniak556ffb679334
diff --git a/core/switch_core/config.py b/core/switch_core/config.pyindex 3b1bc10bd..103fd4b8f 100644--- a/core/switch_core/config.py+++ b/core/switch_core/config.py@@ -57,6 +57,12 @@ class SwitchConfig(BaseSettings):# directory-provisioned users are permanently false and cannot be fixed# from the Okta side. Set false ONLY for a single-tenant IdP whose# addresses are authoritative (corporate directory, HR-provisioned).+ #+ # This only controls whether an unverified login is accepted at all — for+ # a brand-new identity, or one already linked to an account. It never lets+ # an unverified email link a new identity into a *different*, pre-existing+ # account: that always requires the IdP to assert `email_verified=true`,+ # regardless of this setting. See UserStore.get_or_create_oidc_user.gateway_oidc_require_email_verified: bool = True# Lets the password login path be disabled (OIDC-only) without code changes.gateway_password_login_enabled: bool = Truediff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.pyindex dcdbf5a04..e4c169895 100644--- a/core/switch_core/db/models.py+++ b/core/switch_core/db/models.py@@ -51,6 +51,13 @@ class User(Base):)+# An OIDC login matches an account on email case-insensitively (CHOO-2624):+# the IdP and the person typing a password don't reliably agree on casing for+# the same mailbox, so a case-sensitive compare would silently defeat the+# "same account" guarantee. Backs UserStore.get_by_email's lower() compare.+Index("ix_users_email_lower", func.lower(User.email))++class OidcIdentity(Base):"""A verified IdP identity linked to a user (CHOO-2624).@@ -59,16 +66,25 @@ class OidcIdentity(Base):verified email, not on login method, so a password sign-up that latersigns in with an IdP sharing its email gets this identity added to thesame account rather than a second one.++ ``iss`` is nullable only for rows migrated from a pre-CHOO-2624 login that+ predates the issuer being tracked at all (``oidc_sub`` stored alone); the+ application never writes a NULL issuer itself. Such a row still matches on+ ``sub`` alone and has its issuer backfilled on the next login, same as+ before this identity had its own table — ``sub`` is unique per issuer, so+ matching on it alone is safe in practice given a deployment registers one+ issuer."""__tablename__ = "oidc_identities"__table_args__ = (UniqueConstraint("iss", "sub", name="uq_oidc_identities_iss_sub"),+ Index("ix_oidc_identities_sub", "sub"),)id: Mapped[str] = mapped_column(Text, primary_key=True, default=_uuid)user_id: Mapped[str] = mapped_column(Text, ForeignKey("users.id"), nullable=False)- iss: Mapped[str] = mapped_column(Text, nullable=False)+ iss: Mapped[str | None] = mapped_column(Text, nullable=True)sub: Mapped[str] = mapped_column(Text, nullable=False)created_at: Mapped[str] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=Falsediff --git a/core/switch_core/db/stores/user_store.py b/core/switch_core/db/stores/user_store.pyindex 7e64aab3c..b4b8187b6 100644--- a/core/switch_core/db/stores/user_store.py+++ b/core/switch_core/db/stores/user_store.py@@ -1,10 +1,20 @@from __future__ import annotations-from sqlalchemy import select+import logging++from sqlalchemy import func, select+from sqlalchemy.exc import IntegrityErrorfrom sqlalchemy.ext.asyncio import AsyncSessionfrom switch_core.db.models import OidcIdentity, User+logger = logging.getLogger(__name__)++# One retry: after a unique-constraint conflict, the row the other transaction+# just committed is visible to the retry's lookups, so a second collision+# would mean something other than the race this exists for.+_MAX_ATTEMPTS = 2+class OidcIdentityConflictError(Exception):"""An unverified OIDC login's email already belongs to a different account.@@ -25,19 +35,49 @@ class UserStore:return await session.get(User, user_id)async def get_by_email(self, session: AsyncSession, email: str) -> User | None:- result = await session.execute(select(User).where(User.email == email))- return result.scalar_one_or_none()+ """Case-insensitive: an IdP and a person typing a password don't+ reliably agree on the casing of the same mailbox, and accounts must+ be the same account regardless. Backed by ``ix_users_email_lower``.++ Uses the first match rather than requiring exactly one: two rows+ differing only in case could already exist from before this method+ compared case-insensitively, and a login must not turn that into a+ 500.+ """+ result = await session.execute(+ select(User).where(func.lower(User.email) == email.lower())+ )+ return result.scalars().first()async def get_by_oidc_identity(self, session: AsyncSession, *, iss: str, sub: str) -> User | None:- """Find the user linked to this IdP identity by its immutable (iss, sub)."""+ """Find the user linked to this IdP identity by its immutable (iss, sub).++ A row with no stored issuer predates CHOO-2624 tracking one at all; it+ matches on ``sub`` alone and has its issuer backfilled here, same as+ this identity did before it had its own table.+ """result = await session.execute(select(User).join(OidcIdentity, OidcIdentity.user_id == User.id).where(OidcIdentity.iss == iss, OidcIdentity.sub == sub))- return result.scalar_one_or_none()+ user = result.scalar_one_or_none()+ if user is not None:+ return user++ legacy = await session.execute(+ select(OidcIdentity).where(+ OidcIdentity.iss.is_(None), OidcIdentity.sub == sub+ )+ )+ identity = legacy.scalars().first()+ if identity is None:+ return None+ identity.iss = iss+ await session.flush()+ return await session.get(User, identity.user_id)async def get_or_create_oidc_user(self,@@ -64,29 +104,58 @@ class UserStore:attacker-controllable claim, so a collision with a pre-existingaccount is refused rather than linked. A brand-new email — verifiedor not — provisions a fresh ``user`` (no password hash).++ Two logins racing to provision or link the same identity or the same+ new email hit a unique-constraint conflict rather than either one+ silently overwriting the other; this retries once against whichever+ row won, so the raced request resolves to that row instead of a 500."""- user = await self.get_by_oidc_identity(session, iss=iss, sub=sub)- if user is not None:+ for attempt in range(1, _MAX_ATTEMPTS + 1):+ user = await self.get_by_oidc_identity(session, iss=iss, sub=sub)+ if user is not None:+ return user++ existing = await self.get_by_email(session, email)+ if existing is not None:+ if not email_verified:+ raise OidcIdentityConflictError(+ f"An account with email {email!r} already exists and "+ "this identity's email is not verified."+ )+ try:+ await self._link_identity(session, user=existing, iss=iss, sub=sub)+ except IntegrityError:+ await self._recover_from_race(session, attempt)+ continue+ return existing++ user = User(name=name, email=email, role="user", password_hash=None)+ try:+ await self.create(session, user)+ await self._link_identity(session, user=user, iss=iss, sub=sub)+ except IntegrityError:+ await self._recover_from_race(session, attempt)+ continuereturn user- existing = await self.get_by_email(session, email)- if existing is not None:- if not email_verified:- raise OidcIdentityConflictError(- f"An account with email {email!r} already exists and this "- "identity's email is not verified."- )- await self._link_identity(session, user=existing, iss=iss, sub=sub)- return existing-- user = User(name=name, email=email, role="user", password_hash=None)- await self.create(session, user)- await self._link_identity(session, user=user, iss=iss, sub=sub)- return user+ raise AssertionError("unreachable: the loop above always returns or raises")++ async def _recover_from_race(self, session: AsyncSession, attempt: int) -> None:+ await session.rollback()+ if attempt == _MAX_ATTEMPTS:+ raise OidcIdentityConflictError(+ "OIDC identity resolution raced twice in a row; refusing to "+ "guess a winner a third time."+ )async def _link_identity(self, session: AsyncSession, *, user: User, iss: str, sub: str) -> None:+ # The most security-relevant event this store performs: it decides+ # which account an external identity provider can now sign in as.+ logger.warning(+ "Linking OIDC identity (iss=%r, sub=%r) to user %s", iss, sub, user.id+ )session.add(OidcIdentity(user_id=user.id, iss=iss, sub=sub))await session.flush()diff --git a/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py b/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.pyindex 3bf129082..68fc2a05b 100644--- a/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py+++ b/core/switch_core/migrations/versions/04f27f37e474_oidc_identities_table.py@@ -14,11 +14,17 @@ account, with a user able to hold several.Existing ``oidc_iss``/``oidc_sub`` pairs are copied into the new table andremoved from ``metadata``, which becomes the single remaining source of truth-going forward. Older rows that predate ``oidc_iss`` being stored (``oidc_sub``-only, no issuer) can't be attributed to an issuer here and are left as-is;-the application no longer reads them, but the next login from that IdP-resolves the account the same way any first-time link does now — by the-verified email matching the existing account — so no manual fix-up is needed.+going forward. Rows that predate ``oidc_iss`` being stored (``oidc_sub`` only)+are copied too, with a NULL issuer — the application still resolves those by+subject alone and backfills the issuer on next login, exactly as it did+before this identity had its own table, so a flag-off deployment (whose IdP+never emits ``email_verified``, e.g. Okta's org authorization server for+directory users) keeps logging that account in rather than being newly locked+out or forked into a second account by a changed email.++The email column also gains a case-insensitive index: linking now matches an+existing account by email, and a compare that ignored case here would defeat+the very guarantee this migration exists to add."""from collections.abc import Sequence@@ -37,7 +43,7 @@ def upgrade() -> None:"oidc_identities",sa.Column("id", sa.Text(), primary_key=True),sa.Column("user_id", sa.Text(), sa.ForeignKey("users.id"), nullable=False),- sa.Column("iss", sa.Text(), nullable=False),+ sa.Column("iss", sa.Text(), nullable=True),sa.Column("sub", sa.Text(), nullable=False),sa.Column("created_at",@@ -47,6 +53,8 @@ def upgrade() -> None:),sa.UniqueConstraint("iss", "sub", name="uq_oidc_identities_iss_sub"),)+ op.create_index("ix_oidc_identities_sub", "oidc_identities", ["sub"])+ op.create_index("ix_users_email_lower", "users", [sa.text("lower(email)")])connection = op.get_bind()connection.execute(@@ -55,20 +63,40 @@ def upgrade() -> None:INSERT INTO oidc_identities (id, user_id, iss, sub)SELECT gen_random_uuid()::text, id, metadata->>'oidc_iss', metadata->>'oidc_sub'FROM users- WHERE metadata ? 'oidc_sub' AND metadata ? 'oidc_iss'+ WHERE metadata ? 'oidc_sub'"""))connection.execute(sa.text("UPDATE users SET metadata = metadata - 'oidc_iss' - 'oidc_sub' "- "WHERE metadata ? 'oidc_sub' AND metadata ? 'oidc_iss'"+ "WHERE metadata ? 'oidc_sub'"))def downgrade() -> None:+ """Best effort, not a full inverse.++ A user linked to more than one identity — impossible under the old+ single-slot ``metadata`` pair this recreates — keeps only one of them,+ picked by whichever row this UPDATE visits last for that user. That loss+ is inherent to the old shape, not a defect in this statement: there is+ nowhere to put a second identity once ``oidc_identities`` is gone.+ """connection = op.get_bind()+ connection.execute(+ sa.text(+ """+ UPDATE users+ SET metadata = coalesce(metadata, '{}'::jsonb)+ || jsonb_build_object('oidc_sub', oidc_identities.sub)+ FROM oidc_identities+ WHERE oidc_identities.user_id = users.id+ AND oidc_identities.iss IS NULL+ """+ )+ )connection.execute(sa.text("""@@ -77,7 +105,10 @@ def downgrade() -> None:|| jsonb_build_object('oidc_iss', oidc_identities.iss, 'oidc_sub', oidc_identities.sub)FROM oidc_identitiesWHERE oidc_identities.user_id = users.id+ AND oidc_identities.iss IS NOT NULL"""))+ op.drop_index("ix_users_email_lower", table_name="users")+ op.drop_index("ix_oidc_identities_sub", table_name="oidc_identities")op.drop_table("oidc_identities")diff --git a/core/tests/switch_core/db/stores/test_user_store_oidc.py b/core/tests/switch_core/db/stores/test_user_store_oidc.pyindex ee194b32b..f20db655a 100644--- a/core/tests/switch_core/db/stores/test_user_store_oidc.py+++ b/core/tests/switch_core/db/stores/test_user_store_oidc.py@@ -1,9 +1,13 @@from __future__ import annotations+import logging+import pytest+from sqlalchemy import select+from sqlalchemy.exc import IntegrityErrorfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker-from switch_core.db.models import User+from switch_core.db.models import OidcIdentity, Userfrom switch_core.db.stores.user_store import OidcIdentityConflictError, UserStore_ISS = "https://idp.example.com"@@ -62,9 +66,9 @@ class TestGetOrCreateOidcUser:async def test_verified_email_links_to_existing_account(self, session_factory: async_sessionmaker[AsyncSession]) -> None:- # The reversal this fix makes: someone who signed up with a password- # and later signs in with an IdP sharing that verified email lands in- # the same account, not a second one.+ # Accounts are keyed on verified email, not on login method: a+ # password sign-up that later signs in with an IdP sharing that+ # verified email lands in the same account, not a second one.store = UserStore()async with session_factory() as session:existing = User(@@ -95,6 +99,34 @@ class TestGetOrCreateOidcUser:)assert by_identity is not None and by_identity.id == existing.id+ async def test_verified_email_links_case_insensitively(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ # Accounts must be the same account regardless of how the mailbox's+ # case was typed at signup versus how the IdP asserts it.+ store = UserStore()+ async with session_factory() as session:+ existing = User(+ name="Pat",+ email="[email protected]",+ role="user",+ password_hash="bcrypt-hash",+ )+ await store.create(session, existing)+ await session.commit()++ linked = await store.get_or_create_oidc_user(+ session,+ iss=_ISS,+ email="[email protected]",+ name="Pat",+ sub="okta|43",+ email_verified=True,+ )+ await session.commit()++ assert linked.id == existing.id+async def test_two_subjects_same_issuer_can_both_link_to_one_account(self, session_factory: async_sessionmaker[AsyncSession]) -> None:@@ -135,9 +167,8 @@ class TestGetOrCreateOidcUser:async def test_unverified_email_collision_with_different_identity_is_refused(self, session_factory: async_sessionmaker[AsyncSession]) -> None:- # The takeover this closes: an existing (password) admin must not be- # handed to an OIDC login that merely shares its email with a- # different, unverified subject.+ # An unverified email is attacker-controllable, so a collision with an+ # existing (password) account must be refused, never linked.store = UserStore()async with session_factory() as session:admin = User(@@ -195,3 +226,203 @@ class TestGetOrCreateOidcUser:email_verified=True,)assert again.id == owner.id++ async def test_legacy_identity_without_issuer_matches_by_sub_and_backfills(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ # A row migrated from before the issuer was tracked at all (oidc_sub+ # only) must keep resolving on sub alone, exactly as it did when this+ # identity lived in users.metadata instead of its own table.+ store = UserStore()+ async with session_factory() as session:+ legacy_user = User(+ name="Legacy",+ email="[email protected]",+ role="user",+ password_hash=None,+ )+ await store.create(session, legacy_user)+ session.add(OidcIdentity(user_id=legacy_user.id, iss=None, sub="okta|7"))+ await session.commit()++ got = await store.get_or_create_oidc_user(+ session,+ iss=_ISS,+ email="[email protected]",+ name="Legacy",+ sub="okta|7",+ email_verified=True,+ )+ assert got.id == legacy_user.id++ result = await session.execute(+ select(OidcIdentity).where(OidcIdentity.sub == "okta|7")+ )+ identity = result.scalar_one()+ assert identity.iss == _ISS++ async def test_legacy_identity_without_issuer_resolves_even_if_email_changed(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ # A deployment with GATEWAY_OIDC_REQUIRE_EMAIL_VERIFIED=false (e.g. an+ # Okta org authorization server, which never emits email_verified for+ # directory users) matched a legacy sub-only row before this identity+ # had its own table, regardless of email. That must still hold: the+ # account is not orphaned, and no second account is forked, just+ # because the claimed email no longer matches.+ store = UserStore()+ async with session_factory() as session:+ legacy_admin = User(+ name="Admin",+ email="[email protected]",+ role="admin",+ password_hash=None,+ )+ await store.create(session, legacy_admin)+ session.add(OidcIdentity(user_id=legacy_admin.id, iss=None, sub="okta|8"))+ await session.commit()++ got = await store.get_or_create_oidc_user(+ session,+ iss=_ISS,+ email="[email protected]",+ name="Admin",+ sub="okta|8",+ email_verified=False,+ )+ assert got.id == legacy_admin.id+ assert got.role == "admin"+ assert got.email == "[email protected]"++ async def test_linking_an_identity_is_logged(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ caplog: pytest.LogCaptureFixture,+ ) -> None:+ store = UserStore()+ with caplog.at_level(+ logging.WARNING, logger="switch_core.db.stores.user_store"+ ):+ async with session_factory() as session:+ user = await store.get_or_create_oidc_user(+ session,+ iss=_ISS,+ email="[email protected]",+ name="Logged",+ sub="okta|logged",+ email_verified=True,+ )+ await session.commit()++ assert any(+ _ISS in record.message+ and "okta|logged" in record.message+ and user.id in record.message+ for record in caplog.records+ )++ async def test_iss_sub_unique_constraint_is_enforced_at_the_db_level(+ self, session_factory: async_sessionmaker[AsyncSession]+ ) -> None:+ store = UserStore()+ async with session_factory() as session:+ first = User(name="A", email="[email protected]", role="user")+ second = User(name="B", email="[email protected]", role="user")+ await store.create(session, first)+ await store.create(session, second)+ session.add(OidcIdentity(user_id=first.id, iss=_ISS, sub="dup"))+ await session.commit()++ session.add(OidcIdentity(user_id=second.id, iss=_ISS, sub="dup"))+ with pytest.raises(IntegrityError):+ await session.commit()++ async def test_concurrent_creation_of_the_same_identity_resolves_to_the_winner(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ # Two logins racing to provision the same brand-new (iss, sub) collide+ # on the unique constraint; the loser must resolve to the winner's+ # row instead of surfacing that as a 500.+ store = UserStore()+ async with session_factory() as winner_session:+ winner = await store.get_or_create_oidc_user(+ winner_session,+ iss=_ISS,+ email="[email protected]",+ name="Race",+ sub="okta|race",+ email_verified=True,+ )+ await winner_session.commit()++ async with session_factory() as session:+ original_lookup = store.get_by_oidc_identity+ calls = {"n": 0}++ async def lookup_that_misses_once(+ sess: AsyncSession, *, iss: str, sub: str+ ):+ calls["n"] += 1+ if calls["n"] == 1:+ return None+ return await original_lookup(sess, iss=iss, sub=sub)++ monkeypatch.setattr(store, "get_by_oidc_identity", lookup_that_misses_once)++ resolved = await store.get_or_create_oidc_user(+ session,+ iss=_ISS,+ email="[email protected]",+ name="Other",+ sub="okta|race",+ email_verified=True,+ )+ assert resolved.id == winner.id++ async def test_concurrent_new_email_provisioning_resolves_to_one_account(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ # Two logins racing to provision the same brand-new email collide on+ # the email unique constraint; the loser must link its identity to+ # the winner's row instead of surfacing that as a 500.+ store = UserStore()+ async with session_factory() as winner_session:+ winner = await store.get_or_create_oidc_user(+ winner_session,+ iss=_ISS,+ email="[email protected]",+ name="First",+ sub="okta|first",+ email_verified=True,+ )+ await winner_session.commit()++ async with session_factory() as session:+ original_lookup = store.get_by_email+ calls = {"n": 0}++ async def lookup_that_misses_once(sess: AsyncSession, email: str):+ calls["n"] += 1+ if calls["n"] == 1:+ return None+ return await original_lookup(sess, email)++ monkeypatch.setattr(store, "get_by_email", lookup_that_misses_once)++ resolved = await store.get_or_create_oidc_user(+ session,+ iss=_ISS,+ email="[email protected]",+ name="Second",+ sub="okta|second",+ email_verified=True,+ )+ assert resolved.id == winner.id++ for sub in ("okta|first", "okta|second"):+ identity = await store.get_by_oidc_identity(session, iss=_ISS, sub=sub)+ assert identity is not None and identity.id == winner.iddiff --git a/core/tests/switch_core/gateway/test_oidc_routes.py b/core/tests/switch_core/gateway/test_oidc_routes.pyindex 60da1c5fe..44a04b6c3 100644--- a/core/tests/switch_core/gateway/test_oidc_routes.py+++ b/core/tests/switch_core/gateway/test_oidc_routes.py@@ -8,7 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerimport switch_core.gateway.oidc_routes as oidc_routesfrom switch_core.config import SwitchConfig-from switch_core.db.models import User+from switch_core.db.models import OidcIdentity, Userfrom switch_core.db.stores.user_store import UserStorefrom switch_core.gateway.auth import hash_passwordfrom switch_core.gateway.auth_routes import auth_config@@ -310,6 +310,54 @@ class TestOidcCallback:assert linked is not None and linked.id == carol_idassert linked.email == "[email protected]"+ async def test_legacy_sub_only_identity_logs_in_when_check_disabled_despite_unverified_and_changed_email(+ self,+ session_factory: async_sessionmaker[AsyncSession],+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ # A deployment that disabled the verified-email check (its IdP never+ # emits the claim, e.g. an Okta org authorization server for+ # directory users) must keep logging a pre-existing sub-only identity+ # in — main did, by matching on sub alone regardless of email — even+ # though this account's email claim has since changed and is+ # unverified. Losing this would be both a lockout and, worse, a+ # silent fork into a brand-new "user"-role account.+ async with session_factory() as session:+ legacy = User(+ name="Legacy Admin",+ email="[email protected]",+ role="admin",+ password_hash=None,+ )+ await UserStore().create(session, legacy)+ session.add(OidcIdentity(user_id=legacy.id, iss=None, sub="okta|legacy"))+ await session.commit()+ legacy_id = legacy.id++ token = {+ "userinfo": {+ "email": "[email protected]",+ "email_verified": False,+ "sub": "okta|legacy",+ "name": "Legacy Admin",+ }+ }+ monkeypatch.setattr(oidc_routes, "_client", lambda: _FakeClient(token))++ async with session_factory() as session:+ response = await oidc_routes.oidc_callback(+ request=SimpleNamespace(), # type: ignore[arg-type]+ config=_config(gateway_oidc_require_email_verified=False),+ session=session,+ user_store=UserStore(),+ )+ assert response.status_code == 303++ user = await UserStore().get(session, legacy_id)+ assert user is not None+ assert user.role == "admin"+ assert user.email == "[email protected]"+async def test_missing_email_claim_raises_401(self,session_factory: async_sessionmaker[AsyncSession],diff --git a/core/tests/switch_core/test_migration_oidc_identities_backfill.py b/core/tests/switch_core/test_migration_oidc_identities_backfill.pynew file mode 100644index 000000000..53b2f2a1f--- /dev/null+++ b/core/tests/switch_core/test_migration_oidc_identities_backfill.py@@ -0,0 +1,124 @@+"""The oidc_identities backfill migration moves real data correctly.++`test_migration_parity.py` replays the chain into an *empty* database, so it+can never catch a bug in the data the `04f27f37e474` migration moves — a+lost, duplicated or misattributed row would pass every other test and only+show up against a deployment's actual `users` table. This test seeds rows the+way years of real logins would have left them, stops the chain one revision+short of the migration under test, then runs it and inspects what came out.+"""++from __future__ import annotations++from pathlib import Path+from typing import Any++import pytest+from alembic.config import Config+from alembic.runtime.environment import EnvironmentContext+from alembic.script import ScriptDirectory+from sqlalchemy import Connection, text+from sqlalchemy.ext.asyncio import create_async_engine++_CORE = Path(__file__).resolve().parents[2]+_BACKFILL_DB = "migration_oidc_identities_backfill"++_PRE_MIGRATION_REVISION = "c8e4a10b7f36"+_MIGRATION_UNDER_TEST = "04f27f37e474"+++def _script_directory(config: Config) -> ScriptDirectory:+ config.set_main_option("script_location", str(_CORE / "switch_core" / "migrations"))+ return ScriptDirectory.from_config(config)+++def _upgrade_to(revision: str) -> Any:+ def upgrade(connection: Connection) -> None:+ config = Config(str(_CORE / "alembic.ini"))+ script = _script_directory(config)++ def do_upgrade(current_revision: str, context: Any) -> Any:+ return script._upgrade_revs(revision, current_revision)++ with EnvironmentContext(config, script, fn=do_upgrade) as environment:+ environment.configure(connection=connection)+ with environment.begin_transaction():+ environment.run_migrations()++ return upgrade+++async def backfill_url(postgres_url: str) -> Any:+ admin = create_async_engine(postgres_url, isolation_level="AUTOCOMMIT")+ async with admin.connect() as connection:+ await connection.execute(text(f'DROP DATABASE IF EXISTS "{_BACKFILL_DB}"'))+ await connection.execute(text(f'CREATE DATABASE "{_BACKFILL_DB}"'))+ await admin.dispose()++ base, _, _ = postgres_url.rpartition("/")+ try:+ yield f"{base}/{_BACKFILL_DB}"+ finally:+ admin = create_async_engine(postgres_url, isolation_level="AUTOCOMMIT")+ async with admin.connect() as connection:+ await connection.execute(text(f'DROP DATABASE IF EXISTS "{_BACKFILL_DB}"'))+ await admin.dispose()+++async def test_backfill_migrates_full_pairs_and_legacy_rows_and_leaves_others_alone(+ backfill_url: str,+) -> None:+ engine = create_async_engine(backfill_url)+ try:+ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_PRE_MIGRATION_REVISION))++ async with engine.begin() as connection:+ await connection.execute(+ text(+ """+ INSERT INTO users (id, name, email, role, password_hash, metadata)+ VALUES+ ('full', 'Full', '[email protected]', 'user', NULL,+ '{"oidc_iss": "https://idp.example", "oidc_sub": "okta|9"}'),+ ('legacy', 'Legacy', '[email protected]', 'user', NULL,+ '{"oidc_sub": "okta|7"}'),+ ('password', 'Password', '[email protected]', 'user',+ 'bcrypt-hash', NULL)+ """+ )+ )++ async with engine.begin() as connection:+ await connection.run_sync(_upgrade_to(_MIGRATION_UNDER_TEST))++ async with engine.connect() as connection:+ identities = (+ await connection.execute(+ text(+ "SELECT user_id, iss, sub FROM oidc_identities ORDER BY user_id"+ )+ )+ ).all()+ metadata = (+ await connection.execute(+ text("SELECT id, metadata FROM users ORDER BY id")+ )+ ).all()+ finally:+ await engine.dispose()++ assert identities == [+ ("full", "https://idp.example", "okta|9"),+ ("legacy", None, "okta|7"),+ ]++ by_id = {row.id: row.metadata for row in metadata}+ # Migrated rows lose the pair from metadata — the table is now the only+ # place it lives.+ assert by_id["full"] == {}+ assert by_id["legacy"] == {}+ # A row untouched by OIDC (no oidc_sub key, NULL metadata outright) is+ # left exactly as it was.+ assert by_id["password"] is None