Enterprise agent platform for building, deploying, and operating AI teammates.
feat: record the issuing provider on native OAuth credentials (#3817)
Zhenghao Yin8396b06f433e
diff --git a/.github/workflows/test-migrations.yml b/.github/workflows/test-migrations.ymlindex d6332b417..4659a13f1 100644--- a/.github/workflows/test-migrations.yml+++ b/.github/workflows/test-migrations.yml@@ -1159,6 +1159,21 @@ jobs:env:XAGENT_TEST_POSTGRES_URL: postgresql://xagent:xagent@localhost:5432/xagent_test+ # The issuer backfill decodes the catalog audit trail's JSON snapshots,+ # which each dialect stores and returns differently.+ - name: Test UserOAuth issuer backfill (Postgres-only)+ if: needs.detect-migration-changes.outputs.should-test == 'true'+ shell: bash+ run: |+ pytest tests/migrations/test_20261115_user_oauth_issuer.py -m postgresql 2>&1 | tee pg.log+ if grep -Eq '(^|[^0-9])[0-9]+ skipped' pg.log; then+ echo "::error::Postgres-only cells were skipped; skipped is not passed"+ exit 1+ fi+ grep -Eq '[1-9][0-9]* passed' pg.log+ env:+ XAGENT_TEST_POSTGRES_URL: postgresql://xagent:xagent@localhost:5432/xagent_test+migrations-summary:name: Migrations Summaryruns-on: ubuntu-latestdiff --git a/src/xagent/migrations/versions/20261115_user_oauth_issuer.py b/src/xagent/migrations/versions/20261115_user_oauth_issuer.pynew file mode 100644index 000000000..f30928784--- /dev/null+++ b/src/xagent/migrations/versions/20261115_user_oauth_issuer.py@@ -0,0 +1,317 @@+"""Record the issuing OAuth provider on each native UserOAuth credential.++``user_oauth.provider`` is the connecting app's key, not the provider that+issued the tokens: a custom app ``acme-mail`` backed by Google stores+``provider="acme-mail"``. Revocation used to infer the issuer from the app's+current ``public_mcp_apps.provider_name``, which an admin can delete,+retarget (Google -> GitHub), or delete and re-create under the same id. The+issuer is a fact known when the callback writes the tokens, so it now lives+on the credential itself as ``user_oauth.oauth_provider`` (nullable+``String(50)``, an ``oauth_providers.provider_name`` such as ``google``).++Backfill rule -- an existing row gets an issuer only from durable evidence,+never from a catalog history that may be missing part of the app's life.+Catalog auditing (``public_mcp_app_audits``, actions ``create`` / ``update``+/ ``delete``) only began with ``20260715_add_public_mcp_app_audits``; an+app's earlier provider changes left no trace, so "one provider observed" is+not proof on its own. The evidence accepted, first match wins:++1. **Shared Google grant.** A row that a ``google_oauth_grants`` row owns+ was written by the shared Google callback writer -> ``google``.+2. **Built-in app id** (``BUILTIN_APP_PROVIDERS``, frozen from the code+ registry at this revision) -> its canonical provider, when every provider+ the current catalog row and every audit snapshot of that id name is that+ canonical one. A built-in's provider has been normalized and is protected+ from admin edits (and the app from deletion and id reuse) since auditing+ began, so it can never be pointed elsewhere again. A row from a pre-audit+ detour through another provider therefore holds a token the app can never+ use: mislabelling it cannot cost a live connection.+3. **Custom app with complete audited history** -> its provider, when the+ app id's earliest audit row is its ``create``, the credential was created+ at or after that ``create`` (an older row may belong to a pre-audit+ incarnation of the same id), and every provider named from then on --+ all audit snapshots plus the current catalog row -- is one single value.++Everything else stays NULL, notably:++* a custom app that existed before auditing began (no ``create`` audit+ first), whatever single provider is visible today -- a pre-audit retarget+ would be invisible;+* a custom app whose history names more than one provider (retargeted, or+ re-created for another provider);+* a bare provider key that is not a built-in app id and has no shared-grant+ link (for example a bare ``google`` login): an app whose id equals a+ provider name may have written that key before auditing began;+* rows of a table without ``created_at`` under the custom-app rule;+* Composio-backed rows (``token_type = 'composio'``): their grant belongs to+ Composio's own OAuth app, never this deployment's client. A table that+ predates ``token_type`` holds no Composio rows, so all of its rows are+ treated as native.++A NULL ``provider_name`` in the history is not a candidate: an app without+an OAuth provider cannot have driven a native OAuth callback.++NULL means "issuer unknown" and lasts until the row is reconnected:+provider-side revocation refuses to guess for it and counts it as a+possible reference of every provider, so it can block a revoke but never be+cut off by one.++Remaining assumption: each row was connected through its app's configured+provider at the time (the UI always does; a hand-crafted login for another+provider is not detectable after the fact).++Portable across PostgreSQL and SQLite: the evidence is read through Core+selects (JSON snapshots decoded by SQLAlchemy) and resolved in Python, then+applied with ``UPDATE``s that only touch rows whose issuer is still NULL, so+a re-run is a no-op.++Revision ID: 20261115_user_oauth_issuer+Revises: 20261114_oauth_revocation_fences+"""++from collections.abc import Mapping+from dataclasses import dataclass, field+from datetime import datetime+from typing import Any, Sequence, Union++import sqlalchemy as sa+from alembic import op++revision: str = "20261115_user_oauth_issuer"+down_revision: Union[str, None] = "20261114_oauth_revocation_fences"+branch_labels: Union[str, Sequence[str], None] = None+depends_on: Union[str, Sequence[str], None] = None++TABLE = "user_oauth"+COLUMN = "oauth_provider"+# Mirrors models.user_oauth.COMPOSIO_TOKEN_TYPE; migrations never import+# application models, whose shape may move on after this revision.+COMPOSIO_TOKEN_TYPE = "composio"+GOOGLE = "google"+# Every built-in catalog app with an OAuth provider, as+# builtin_mcp_registry.get_builtin_public_mcp_app_rows() defines it at this+# revision. Frozen here so the backfill does not change with later code.+BUILTIN_APP_PROVIDERS: dict[str, str] = {+ "deputy": "deputy",+ "employment-hero": "employment-hero",+ "excel": "microsoft",+ "facebook": "meta",+ "github": "github",+ "gmail": GOOGLE,+ "google-ads": GOOGLE,+ "google-analytics": GOOGLE,+ "google-calendar": GOOGLE,+ "google-docs": GOOGLE,+ "google-drive": GOOGLE,+ "google-search-console": GOOGLE,+ "google-sheets": GOOGLE,+ "google-slides": GOOGLE,+ "instagram": "meta",+ "intercom": "intercom",+ "jira": "jira",+ "linear": "linear",+ "linkedin": "linkedin",+ "meta-ads": "meta",+ "myob": "myob",+ "onedrive": "microsoft",+ "outlook": "microsoft",+ "planner": "microsoft",+ "powerpoint": "microsoft",+ "salesforce": "salesforce",+ "sharepoint": "microsoft",+ "slack": "slack",+ "teams": "microsoft",+ "whatsapp": "meta",+ "word": "microsoft",+ "xero": "xero",+ "zendesk": "zendesk",+ "zoom": "zoom",+}+_UPDATE_CHUNK = 500++_user_oauth = sa.table(+ TABLE,+ sa.column("id", sa.Integer),+ sa.column("provider", sa.String),+ sa.column("token_type", sa.String),+ sa.column("created_at", sa.DateTime(timezone=True)),+ sa.column(COLUMN, sa.String),+)+_google_oauth_grants = sa.table(+ "google_oauth_grants", sa.column("oauth_account_id", sa.Integer)+)+_public_mcp_apps = sa.table(+ "public_mcp_apps",+ sa.column("app_id", sa.String),+ sa.column("provider_name", sa.String),+)+_public_mcp_app_audits = sa.table(+ "public_mcp_app_audits",+ sa.column("id", sa.Integer),+ sa.column("action", sa.String),+ sa.column("app_id", sa.String),+ sa.column("before_values", sa.JSON),+ sa.column("after_values", sa.JSON),+ sa.column("created_at", sa.DateTime(timezone=True)),+)+++@dataclass+class AppHistory:+ """What the catalog proves about one app id.++ ``providers`` is every non-NULL provider the current row and the audit+ snapshots name. ``audited_since`` is when the id's audit trail began with+ its ``create`` -- the point from which its history is complete -- or+ ``None`` when the trail starts later in the app's life (or not at all).+ """++ providers: set[str] = field(default_factory=set)+ audited_since: datetime | None = None+ first_action: str | None = None+++def _snapshot_provider_name(snapshot: object) -> str | None:+ """The ``provider_name`` one audit snapshot recorded, if any."""+ if not isinstance(snapshot, Mapping):+ return None+ value = snapshot.get("provider_name")+ return value if isinstance(value, str) and value else None+++def _catalog_history(+ bind: sa.engine.Connection, tables: set[str]+) -> dict[str, AppHistory]:+ histories: dict[str, AppHistory] = {}+ if "public_mcp_app_audits" in tables:+ audits = bind.execute(+ sa.select(+ _public_mcp_app_audits.c.app_id,+ _public_mcp_app_audits.c.action,+ _public_mcp_app_audits.c.created_at,+ _public_mcp_app_audits.c.before_values,+ _public_mcp_app_audits.c.after_values,+ ).order_by(_public_mcp_app_audits.c.id)+ )+ for app_id, action, created_at, before, after in audits:+ history = histories.setdefault(str(app_id), AppHistory())+ if history.first_action is None:+ history.first_action = str(action)+ if action == "create":+ history.audited_since = created_at+ history.providers.update(+ name+ for name in map(_snapshot_provider_name, (before, after))+ if name is not None+ )+ if "public_mcp_apps" in tables:+ for app_id, provider_name in bind.execute(+ sa.select(_public_mcp_apps.c.app_id, _public_mcp_apps.c.provider_name)+ ):+ history = histories.setdefault(str(app_id), AppHistory())+ if provider_name:+ history.providers.add(str(provider_name))+ return histories+++def resolve_issuer(+ key: str,+ created_at: datetime | None,+ *,+ history: AppHistory | None,+) -> str | None:+ """The issuer of one non-Composio row without a shared-grant link, or+ ``None`` when the evidence does not prove it (rules 2 and 3 of the+ module docstring). Pure so the rule can be tested without a database."""+ observed = history.providers if history is not None else set()+ canonical = BUILTIN_APP_PROVIDERS.get(key)+ if canonical is not None:+ return canonical if observed <= {canonical} else None+ if (+ history is None+ or history.audited_since is None+ or created_at is None+ or created_at < history.audited_since+ or len(observed) != 1+ ):+ return None+ return next(iter(observed))+++def _assign(bind: sa.engine.Connection, issuer: str, ids: list[int]) -> None:+ for start in range(0, len(ids), _UPDATE_CHUNK):+ bind.execute(+ sa.update(_user_oauth)+ .where(+ _user_oauth.c.id.in_(ids[start : start + _UPDATE_CHUNK]),+ _user_oauth.c.oauth_provider.is_(None),+ )+ .values({COLUMN: issuer})+ )+++def _backfill(bind: sa.engine.Connection, tables: set[str], columns: set[str]) -> None:+ # A user_oauth that predates ``token_type`` (an old schema being carried+ # through the chain) cannot hold Composio rows, so every row is native.+ native = (+ sa.or_(+ _user_oauth.c.token_type.is_(None),+ _user_oauth.c.token_type != COMPOSIO_TOKEN_TYPE,+ )+ if "token_type" in columns+ else sa.true()+ )+ unknown = sa.and_(_user_oauth.c.oauth_provider.is_(None), native)++ if "google_oauth_grants" in tables:+ bind.execute(+ sa.update(_user_oauth)+ .where(+ unknown,+ _user_oauth.c.id.in_(+ sa.select(_google_oauth_grants.c.oauth_account_id)+ ),+ )+ .values({COLUMN: GOOGLE})+ )++ created_at = _user_oauth.c.created_at if "created_at" in columns else sa.null()+ rows = bind.execute(+ sa.select(_user_oauth.c.id, _user_oauth.c.provider, created_at).where(unknown)+ ).all()+ if not rows:+ return+ histories = _catalog_history(bind, tables)+ by_issuer: dict[str, list[int]] = {}+ for row_id, key, row_created_at in rows:+ issuer = resolve_issuer(+ str(key), row_created_at, history=histories.get(str(key))+ )+ if issuer is not None:+ by_issuer.setdefault(issuer, []).append(int(row_id))+ for issuer, ids in by_issuer.items():+ _assign(bind, issuer, ids)+++def _columns(inspector: Any) -> set[str]:+ return {column["name"] for column in inspector.get_columns(TABLE)}+++def upgrade() -> None:+ bind = op.get_bind()+ inspector = sa.inspect(bind)+ tables = set(inspector.get_table_names())+ # A fresh install creates user_oauth from the models after Alembic runs,+ # already with this column and no rows to backfill.+ if TABLE not in tables:+ return+ columns = _columns(inspector)+ if COLUMN not in columns:+ op.add_column(TABLE, sa.Column(COLUMN, sa.String(50), nullable=True))+ _backfill(bind, tables, columns)+++def downgrade() -> None:+ inspector = sa.inspect(op.get_bind())+ if TABLE in inspector.get_table_names() and COLUMN in _columns(inspector):+ op.drop_column(TABLE, COLUMN)diff --git a/src/xagent/web/api/auth.py b/src/xagent/web/api/auth.pyindex a5c6f28d2..4a5730b8e 100644--- a/src/xagent/web/api/auth.py+++ b/src/xagent/web/api/auth.py@@ -3773,9 +3773,10 @@ class BuiltinOAuthRevocation(NamedTuple)::func:`has_other_builtin_oauth_reference`.``provider`` is the OAuth provider that issued the grant (``github`` or- ``google``), not the ``UserOAuth.provider`` key the row was stored under:- a Google grant is stored under its app id (``gmail``, ``google-drive``,- ...) or the bare ``google`` key. ``client_id``/``client_secret``+ ``google``) -- the row's recorded ``UserOAuth.oauth_provider`` -- not the+ ``UserOAuth.provider`` key the row was stored under: a Google grant is+ stored under its app id (``gmail``, ``google-drive``, a custom app id) or+ the bare ``google`` key. ``client_id``/``client_secret``authenticate GitHub's grant-delete call; Google's revoke endpoint isunauthenticated, so they are empty for Google. ``refresh_token`` is onlyused for Google, where it is the token revoked whenever it is present:@@ -3804,46 +3805,10 @@ class BuiltinOAuthRevocation(NamedTuple):)-def _builtin_oauth_provider_for_key(db: Session, key: str) -> str:- """The OAuth provider whose client issued a ``UserOAuth.provider`` key's- grant.-- A grant is stored under the connecting app's id (``gmail``) or, for a- bare provider login, the provider name itself (``google``, ``github``);- the catalog row of an app id names its provider. A key with no catalog- row is taken to be a provider name.- """- from ..models.public_mcp import PublicMCPApp-- provider_name = (- db.query(PublicMCPApp.provider_name).filter(PublicMCPApp.app_id == key).scalar()- )- return str(provider_name or key)---def _builtin_oauth_provider_keys(db: Session, provider: str) -> list[str]:- """Every ``UserOAuth.provider`` key a grant from ``provider``'s OAuth- client may be stored under: the bare provider name plus the id of each- catalog app that names it. The inverse of- :func:`_builtin_oauth_provider_for_key`.- """- from ..models.public_mcp import PublicMCPApp-- return [- provider,- *(- str(app_id)- for (app_id,) in db.query(PublicMCPApp.app_id).filter(- PublicMCPApp.provider_name == provider- )- ),- ]--def resolve_builtin_oauth_revocation(db: Session,*,- provider: str,+ oauth_provider: str | None,access_token: str,provider_user_id: str | None,refresh_token: str | None = None,@@ -3853,10 +3818,22 @@ def resolve_builtin_oauth_revocation(record, decrypting the stored client_id/secret the same waygeneric_oauth_login/generic_oauth_callback already do for this row.- ``provider`` is the row's ``UserOAuth.provider`` key; the OAuth provider- behind it is resolved through the catalog (see- :func:`_builtin_oauth_provider_for_key`), so a Gmail or Drive row- resolves to a Google revocation. Pass the row's ``refresh_token`` and+ ``oauth_provider`` is the row's recorded issuer+ (``UserOAuth.oauth_provider``), so a Gmail, Drive or custom-app row whose+ tokens Google issued resolves to a Google revocation whatever the catalog+ says about its app today: an admin may since have deleted the app,+ retargeted its ``provider_name``, or re-created the same id for another+ provider, none of which changes who issued the stored tokens. The catalog+ is never consulted for the issuer.++ ``oauth_provider=None`` on a native row means the issuer is unknown (a+ row the backfill migration could not prove). That outcome is explicit:+ a warning is logged and ``None`` is returned, so the row is never+ revoked against a guessed provider -- failing closed leaves a grant+ alive rather than sending a token to the wrong issuer. A caller that+ must tell "issuer unknown" apart from "nothing to revoke" reads the+ row's ``oauth_provider`` itself before calling. Pass the row's+ ``refresh_token`` and``token_type`` too: Google revokes through the refresh token, and aComposio-backed row (``token_type == COMPOSIO_TOKEN_TYPE``) holds aComposio connected-account id rather than a provider token -- its grant@@ -3870,10 +3847,10 @@ def resolve_builtin_oauth_revocation(relies on that grant.Returns ``None`` when there's nothing to revoke -- a Composio-backed row,- a provider with no revoke implementation (only GitHub and Google have- one), a Google row without an upstream identity, an empty token, no- configured provider row, or a missing secret -- so callers can skip- revocation outright. Only reads the+ an unknown issuer, a provider with no revoke implementation (only GitHub+ and Google have one), a Google row without an upstream identity, an+ empty token, no configured provider row, or a missing secret -- so+ callers can skip revocation outright. Only reads thedatabase and makes no network call, so it is safe to call while stillholding a disconnect transaction's locks; do the actual provider call(:func:`revoke_builtin_oauth_grant_if_unreferenced`) only after that@@ -3881,7 +3858,12 @@ def resolve_builtin_oauth_revocation("""if token_type == COMPOSIO_TOKEN_TYPE:return None- oauth_provider = _builtin_oauth_provider_for_key(db, provider)+ if oauth_provider is None:+ logger.warning(+ "Skipping OAuth grant revocation: the connection recorded no "+ "issuing provider, so it cannot be revoked without guessing"+ )+ return Noneif oauth_provider == "google":if not (refresh_token or access_token):return None@@ -3961,7 +3943,7 @@ def snapshot_builtin_oauth_revocations(continuesnapshot = resolve_builtin_oauth_revocation(db,- provider=str(account.provider),+ oauth_provider=cast(str | None, account.oauth_provider),access_token=str(account.access_token),# is not None, not a truthy check: an empty-string# provider_user_id is still a real (if unlikely) identity value,@@ -3985,12 +3967,16 @@ def has_other_builtin_oauth_reference(db: Session, *, provider: str, provider_user_id: str | None) -> bool:"""True if any current ``UserOAuth`` row -- any local user, any owner- namespace, any key ``provider``'s grants are stored under -- still- points at the same upstream (provider, provider_user_id) identity.+ namespace, any app key -- still points at the same upstream+ (provider, provider_user_id) identity.``provider`` is the OAuth provider (``BuiltinOAuthRevocation.provider``),- and every key its grants may be stored under counts- (:func:`_builtin_oauth_provider_keys`). GitHub's grant-delete revokes+ matched against each row's recorded issuer (``UserOAuth.oauth_provider``),+ never against the catalog: a row stays a reference after an admin+ deletes or retargets the app it was connected through. A native row+ whose issuer is unknown (NULL) counts as well, because nothing can prove+ it was not issued by ``provider``; that keeps a revoke from cutting off a+ connection the backfill could not classify. GitHub's grant-delete revokesevery token issued to that GitHub account for this OAuth App, andGoogle's revoke revokes the account's whole grant for this OAuth client,not just the one token this disconnect deleted -- a different XAgent@@ -4050,7 +4036,9 @@ def _builtin_oauth_references() -> list[UserOAuth]:"""The live rows :func:`has_other_builtin_oauth_reference` counts, so acaller can tell *why* a revoke is blocked: rows recording the same- account, and -- for Google only -- rows recording no account at all."""+ account, and -- for Google only -- rows recording no account at all.+ Either kind must have been issued by ``provider`` or have an unknown+ issuer."""if not provider_user_id:return []same_identity = UserOAuth.provider_user_id == provider_user_id@@ -4062,7 +4050,10 @@ def _builtin_oauth_references(candidates = (db.query(UserOAuth).filter(- UserOAuth.provider.in_(_builtin_oauth_provider_keys(db, provider)),+ or_(+ UserOAuth.oauth_provider == provider,+ UserOAuth.oauth_provider.is_(None),+ ),same_identity,or_(UserOAuth.token_type.is_(None),@@ -4295,7 +4286,7 @@ def revoke_builtin_oauth_grant_if_unreferenced(def revoke_builtin_oauth_grant(db: Session,*,- provider: str,+ oauth_provider: str | None,access_token: str,provider_user_id: str | None,refresh_token: str | None = None,@@ -4319,7 +4310,7 @@ def revoke_builtin_oauth_grant("""resolved = resolve_builtin_oauth_revocation(db,- provider=provider,+ oauth_provider=oauth_provider,access_token=access_token,provider_user_id=provider_user_id,refresh_token=refresh_token,@@ -6436,6 +6427,12 @@ def generic_oauth_callback("provider_user_id",str(provider_user_id) if provider_user_id else None,)+ # The issuer of the tokens written below is the provider whose+ # route served this callback (an ``OAuthProvider.provider_name``),+ # whatever app key the row is stored under. Recorded on every+ # write, new or revived row alike, so a later revocation never+ # has to infer it from the mutable catalog.+ setattr(oauth_account, "oauth_provider", provider)setattr(oauth_account, "access_token", access_token)setattr(oauth_account, "token_type", token_data.get("token_type", "Bearer"))diff --git a/src/xagent/web/api/cloud_storage.py b/src/xagent/web/api/cloud_storage.pyindex 190345b3f..4262ddcef 100644--- a/src/xagent/web/api/cloud_storage.py+++ b/src/xagent/web/api/cloud_storage.py@@ -922,11 +922,11 @@ async def delete_connected_account(raise HTTPException(status_code=404, detail="Account not found")from ..services.google_oauth_grants import (+ GOOGLE_OAUTH_ISSUER,GOOGLE_SHARED_OAUTH_PROVIDER,detach_google_shared_oauth_legacy_account,)from .auth import (- _builtin_oauth_provider_for_key,revoke_builtin_oauth_grants,snapshot_builtin_oauth_revocations,)@@ -939,9 +939,13 @@ async def delete_connected_account(# snapshotted too; one that survives the detach is still there for the# post-commit reference check, which then skips its revoke. Any other# provider's account has no shared Google link to detach, so it skips- # that extra fence and reference work.+ # that extra fence and reference work. An account whose issuer is unknown+ # may still be such a Google account, so it takes the cautious branch:+ # snapshotting a shared credential that survives costs only a skipped+ # revoke, while missing one that the detach deletes would leave its+ # grant alive unnoticed.snapshotted = [account]- if _builtin_oauth_provider_for_key(db, str(account.provider)) == "google":+ if account.oauth_provider in (GOOGLE_OAUTH_ISSUER, None):snapshotted += (scoped_user_oauth_query(db, user_id=cast(int, user.id), resource_owner_key=Nonediff --git a/src/xagent/web/composio_auth.py b/src/xagent/web/composio_auth.pyindex 61d14f532..d67dd7bdb 100644--- a/src/xagent/web/composio_auth.py+++ b/src/xagent/web/composio_auth.py@@ -1916,6 +1916,10 @@ def _store_grant(provider_user_id=provider_user_id,access_token=account_id,token_type=COMPOSIO_TOKEN_TYPE,+ # The grant behind a Composio account was issued to Composio's own+ # OAuth app, never this deployment's client, so there is no+ # native issuer to record or revoke with.+ oauth_provider=None,refresh_token=None,expires_at=None,instance_url=composio_user,diff --git a/src/xagent/web/models/user_oauth.py b/src/xagent/web/models/user_oauth.pyindex bb33362a8..62b94ef9c 100644--- a/src/xagent/web/models/user_oauth.py+++ b/src/xagent/web/models/user_oauth.py@@ -108,6 +108,21 @@ class UserOAuth(Base): # type: ignore[no-any-unimported]Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)provider = Column(String(50), nullable=False) # e.g. "google-drive"+ # The OAuth provider that issued this row's tokens, by its+ # ``OAuthProvider.provider_name`` (``google``, ``github``, ...). It is+ # distinct from ``provider`` above, which is the connecting app's key: a+ # custom app ``acme-mail`` backed by Google stores ``provider="acme-mail"``+ # and ``oauth_provider="google"``. The issuer is a fact known only when+ # the tokens are written (the callback's provider route), so it is+ # recorded then and never re-derived from the mutable catalog, whose+ # ``provider_name`` an admin may change or delete afterwards.+ #+ # NULL means the issuer is unknown: a Composio-backed row (its grant+ # belongs to Composio's own OAuth app, never this deployment's client),+ # or a pre-existing native row whose issuer the backfill migration+ # (20261115_user_oauth_issuer) could not prove. Provider-side revocation+ # treats NULL as "do not revoke" and never guesses.+ oauth_provider = Column(String(50), nullable=True)resource_owner_key = Column(String(USER_OAUTH_RESOURCE_OWNER_KEY_MAX_LENGTH), nullable=True)diff --git a/src/xagent/web/services/google_oauth_grants.py b/src/xagent/web/services/google_oauth_grants.pyindex 16b399d2f..0bc2034e5 100644--- a/src/xagent/web/services/google_oauth_grants.py+++ b/src/xagent/web/services/google_oauth_grants.py@@ -28,6 +28,11 @@ from .user_oauth import ()GOOGLE_SHARED_OAUTH_PROVIDER = "google"+# ``UserOAuth.oauth_provider`` of every row holding Google-issued tokens: the+# Google ``OAuthProvider.provider_name``. Same value as the shared row's key+# above, but a different fact -- the issuer, not the app key a row is stored+# under -- so legacy app rows (``gmail``, a custom ``acme-mail``) record it too.+GOOGLE_OAUTH_ISSUER = "google"class GoogleOAuthGrantConflict(ValueError):@@ -248,6 +253,14 @@ def _connected_google_apps_covered_by_bare_account(def _copy_shared_google_credentials(source: UserOAuth, target: UserOAuth, *, preserve_missing_refresh: bool) -> None:+ """Mirror a shared Google credential's token material onto ``target``.++ ``source`` is always a shared Google credential, so whatever ``target``+ held before, the tokens it holds afterwards were issued by Google: the+ issuer is stamped as a fact rather than copied, which also repairs a+ mirror whose issuer the backfill left unknown.+ """+ setattr(target, "oauth_provider", GOOGLE_OAUTH_ISSUER)for field in ("access_token","refresh_token",@@ -680,6 +693,7 @@ def write_google_shared_oauth_callback(candidate = UserOAuth(user_id=int(user_id),provider=GOOGLE_SHARED_OAUTH_PROVIDER,+ oauth_provider=GOOGLE_OAUTH_ISSUER,resource_owner_key=owner_key,provider_user_id=subject,access_token=access_token,@@ -747,6 +761,9 @@ def write_google_shared_oauth_callback(scope_value = " ".join(sorted(scopes))for account in (shared_account, legacy_oauth_account):+ # Both rows now hold the tokens Google issued to this callback; an+ # adopted pre-rollout bare row may still carry a NULL issuer.+ setattr(account, "oauth_provider", GOOGLE_OAUTH_ISSUER)setattr(account, "provider_user_id", subject)setattr(account, "access_token", access_token)setattr(account, "refresh_token", resolved_refresh_token)@@ -801,6 +818,7 @@ def write_google_shared_oauth_callback(sibling_legacy = UserOAuth(user_id=int(user_id),provider=sibling_app_id,+ oauth_provider=GOOGLE_OAUTH_ISSUER,resource_owner_key=owner_key,provider_user_id=subject,access_token=access_token,diff --git a/tests/migrations/test_20261115_user_oauth_issuer.py b/tests/migrations/test_20261115_user_oauth_issuer.pynew file mode 100644index 000000000..9f36746b1--- /dev/null+++ b/tests/migrations/test_20261115_user_oauth_issuer.py@@ -0,0 +1,403 @@+"""Tests for migration 20261115_user_oauth_issuer.++The pre-migration shape of ``user_oauth`` and the catalog tables the+backfill reads is built directly, then only the migration under test runs+against it. Every backfill case runs on SQLite and, under+``@pytest.mark.postgresql``, on a real PostgreSQL server in a throwaway+schema, so the JSON audit snapshots are decoded by each dialect.+"""++from __future__ import annotations++import importlib.util+import os+import uuid+from collections.abc import Iterator+from datetime import datetime, timedelta, timezone+from pathlib import Path+from types import ModuleType+from unittest.mock import patch++import pytest+import sqlalchemy as sa+from alembic.migration import MigrationContext+from alembic.operations import Operations++TARGET_REVISION = "20261115_user_oauth_issuer"+++def _migration_module() -> ModuleType:+ import xagent.migrations as migrations_pkg++ migrations_dir = Path(next(iter(migrations_pkg.__path__)))+ path = migrations_dir / "versions" / f"{TARGET_REVISION}.py"+ spec = importlib.util.spec_from_file_location(TARGET_REVISION, path)+ assert spec is not None and spec.loader is not None+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++migration = _migration_module()+++def sqlite_engine(tmp_path: Path) -> Iterator[sa.Engine]:+ engine = sa.create_engine(f"sqlite:///{tmp_path / 'test.db'}")+ yield engine+ engine.dispose()+++def postgresql_engine() -> Iterator[sa.Engine]:+ """An engine whose default schema is a fresh, dropped-afterwards schema,+ so the shared test database's own ``user_oauth`` is never touched."""+ url = os.environ.get("XAGENT_TEST_POSTGRES_URL")+ if not url:+ pytest.skip("XAGENT_TEST_POSTGRES_URL is not set")+ schema = f"oauth_issuer_{uuid.uuid4().hex[:12]}"+ admin = sa.create_engine(url)+ with admin.begin() as conn:+ conn.execute(sa.text(f"CREATE SCHEMA {schema}"))+ engine = sa.create_engine(url, connect_args={"options": f"-csearch_path={schema}"})+ try:+ yield engine+ finally:+ engine.dispose()+ with admin.begin() as conn:+ conn.execute(sa.text(f"DROP SCHEMA {schema} CASCADE"))+ admin.dispose()+++ params=[+ "sqlite",+ pytest.param("postgresql", marks=pytest.mark.postgresql),+ ]+)+def engine(request: pytest.FixtureRequest) -> sa.Engine:+ return request.getfixturevalue(f"{request.param}_engine")+++T0 = datetime(2026, 8, 1, tzinfo=timezone.utc)+++def _at(hours: int | None) -> datetime | None:+ return None if hours is None else T0 + timedelta(hours=hours)+++def _create_tables(conn: sa.Connection) -> None:+ """The columns the migration reads, as they stand at its parent revision."""+ conn.execute(+ sa.text(+ "CREATE TABLE user_oauth (id INTEGER PRIMARY KEY, "+ "user_id INTEGER NOT NULL, provider VARCHAR(50) NOT NULL, "+ "access_token VARCHAR NOT NULL, token_type VARCHAR(50), "+ "created_at TIMESTAMP WITH TIME ZONE)"+ )+ )+ conn.execute(+ sa.text(+ "CREATE TABLE public_mcp_apps (id INTEGER PRIMARY KEY, "+ "app_id VARCHAR(100) NOT NULL, provider_name VARCHAR(50))"+ )+ )+ conn.execute(+ sa.text(+ "CREATE TABLE public_mcp_app_audits (id INTEGER PRIMARY KEY, "+ "action VARCHAR(16) NOT NULL, app_id VARCHAR(100) NOT NULL, "+ "before_values JSON, after_values JSON, "+ "created_at TIMESTAMP WITH TIME ZONE NOT NULL)"+ )+ )+ conn.execute(+ sa.text(+ "CREATE TABLE google_oauth_grants (id INTEGER PRIMARY KEY, "+ "oauth_account_id INTEGER NOT NULL)"+ )+ )+++_audits = sa.table(+ "public_mcp_app_audits",+ sa.column("id", sa.Integer),+ sa.column("action", sa.String),+ sa.column("app_id", sa.String),+ sa.column("before_values", sa.JSON),+ sa.column("after_values", sa.JSON),+ sa.column("created_at", sa.DateTime(timezone=True)),+)+_rows = sa.table(+ "user_oauth",+ sa.column("id", sa.Integer),+ sa.column("user_id", sa.Integer),+ sa.column("provider", sa.String),+ sa.column("access_token", sa.String),+ sa.column("token_type", sa.String),+ sa.column("created_at", sa.DateTime(timezone=True)),+)++Snapshot = dict[str, object] | None+++def _snap(provider_name: str | None) -> dict[str, object]:+ """An admin audit snapshot as ``_public_mcp_app_values`` records it."""+ return {"name": "App", "transport": "oauth", "provider_name": provider_name}+++def _seed(+ conn: sa.Connection,+ *,+ rows: list[tuple[str, str | None, int | None]],+ apps: tuple[tuple[str, str | None], ...] = (),+ audits: tuple[tuple[str, str, Snapshot, Snapshot, int], ...] = (),+ grant_account_ids: tuple[int, ...] = (),+) -> None:+ """Insert ``user_oauth`` rows ``(provider key, token_type, created hour)``+ with ids 1..n, current catalog rows, audit rows ``(action, app_id,+ before_values, after_values, hour)`` in id order, and shared-grant links.+ Hours count from ``T0``; ``None`` leaves ``created_at`` NULL."""+ for index, (app_id, provider_name) in enumerate(apps, start=1):+ conn.execute(+ sa.text("INSERT INTO public_mcp_apps VALUES (:id, :app, :provider)"),+ {"id": index, "app": app_id, "provider": provider_name},+ )+ for index, (action, app_id, before, after, hour) in enumerate(audits, start=1):+ conn.execute(+ sa.insert(_audits).values(+ id=index,+ action=action,+ app_id=app_id,+ before_values=before,+ after_values=after,+ created_at=_at(hour),+ )+ )+ for index, (key, token_type, hour) in enumerate(rows, start=1):+ conn.execute(+ sa.insert(_rows).values(+ id=index,+ user_id=1,+ provider=key,+ access_token="tok",+ token_type=token_type,+ created_at=_at(hour),+ )+ )+ for index, account_id in enumerate(grant_account_ids, start=1):+ conn.execute(+ sa.text("INSERT INTO google_oauth_grants VALUES (:id, :account)"),+ {"id": index, "account": account_id},+ )+++def _upgrade(conn: sa.Connection) -> None:+ with patch.object(migration, "op", Operations(MigrationContext.configure(conn))):+ migration.upgrade()+++def _issuers(conn: sa.Connection) -> dict[int, str | None]:+ return dict(+ conn.execute(sa.text("SELECT id, oauth_provider FROM user_oauth")).all()+ )+++def _columns(conn: sa.Connection) -> set[str]:+ return {column["name"] for column in sa.inspect(conn).get_columns("user_oauth")}+++def test_builtin_app_ids_get_their_pinned_provider(engine: sa.Engine) -> None:+ """Built-in providers are normalized and protected, so a built-in key --+ with or without a catalog row or audits -- resolves to its canonical+ provider; that includes the bare ``github`` key."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[("gmail", None, 1), ("github", "bearer", None), ("slack", None, 1)],+ apps=(("gmail", "google"), ("github", "github")),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: "google", 2: "github", 3: "slack"}+++def test_a_builtin_id_whose_history_names_another_provider_stays_unknown(+ engine: sa.Engine,+) -> None:+ """A row for a built-in id that still names a non-canonical provider (a+ custom app that took the id before the built-in shipped) is not pinned."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(conn, rows=[("gmail", None, 1)], apps=(("gmail", "github"),))+ _upgrade(conn)+ assert _issuers(conn) == {1: None}+++def test_a_custom_app_audited_from_creation_gets_its_provider(+ engine: sa.Engine,+) -> None:+ """A complete history naming one provider: edited without changing it,+ deleted afterwards, or created without a provider first."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[+ ("acme-mail", "Bearer", 2),+ ("gone-mail", None, 2),+ ("late", None, 3),+ ],+ apps=(("acme-mail", "google"), ("late", "google")),+ audits=(+ ("create", "acme-mail", None, _snap("google"), 1),+ ("update", "acme-mail", _snap("google"), _snap("google"), 3),+ ("create", "gone-mail", None, _snap("google"), 1),+ ("delete", "gone-mail", _snap("google"), None, 3),+ ("create", "late", None, _snap(None), 1),+ ("update", "late", _snap(None), _snap("google"), 2),+ ),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: "google", 2: "google", 3: "google"}+++def test_a_custom_app_that_predates_auditing_stays_unknown(engine: sa.Engine) -> None:+ """Without its ``create`` audit an app may have been retargeted before+ auditing began, so a single provider visible today proves nothing: a+ pre-audit Google -> GitHub retarget shows only ``github``."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[("retargeted", None, 0), ("edited", None, 0)],+ apps=(("retargeted", "github"), ("edited", "google")),+ audits=(("update", "edited", _snap("google"), _snap("google"), 5),),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: None, 2: None}+++def test_a_row_older_than_its_apps_create_audit_stays_unknown(+ engine: sa.Engine,+) -> None:+ """It may belong to a pre-audit incarnation of the same app id; a row+ without ``created_at`` cannot be placed in the history either."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[("acme-mail", None, 1), ("acme-mail", None, None)],+ apps=(("acme-mail", "google"),),+ audits=(("create", "acme-mail", None, _snap("google"), 5),),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: None, 2: None}+++def test_a_history_that_changed_provider_stays_unknown(engine: sa.Engine) -> None:+ """A retarget and a re-create for another provider are both ambiguous."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[("retargeted", None, 2), ("recreated", None, 2)],+ apps=(("retargeted", "github"), ("recreated", "github")),+ audits=(+ ("create", "retargeted", None, _snap("google"), 1),+ ("update", "retargeted", _snap("google"), _snap("github"), 3),+ ("create", "recreated", None, _snap("google"), 1),+ ("delete", "recreated", _snap("google"), None, 3),+ ("create", "recreated", None, _snap("github"), 4),+ ),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: None, 2: None}+++def test_bare_non_builtin_keys_need_a_shared_grant(engine: sa.Engine) -> None:+ """A bare ``google`` key may have been written by an app whose id is+ ``google`` before auditing; only a shared Google grant proves Google.+ An unknown key stays unknown."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[("google", None, 1), ("google", None, 1), ("mystery-app", None, 1)],+ grant_account_ids=(2,),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: None, 2: "google", 3: None}+++def test_a_table_without_token_type_is_backfilled(engine: sa.Engine) -> None:+ """An old ``user_oauth`` carried through the chain may predate+ ``token_type``; it holds no Composio rows, so every row is native."""+ with engine.begin() as conn:+ _create_tables(conn)+ conn.execute(sa.text("ALTER TABLE user_oauth DROP COLUMN token_type"))+ conn.execute(+ sa.text(+ "INSERT INTO user_oauth (id, user_id, provider, access_token) "+ "VALUES (1, 1, 'gmail', 'tok'), (2, 1, 'mystery-app', 'tok')"+ )+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: "google", 2: None}+++def test_composio_rows_are_left_untouched(engine: sa.Engine) -> None:+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(+ conn,+ rows=[("gmail", "composio", 1), ("google", "composio", 1)],+ apps=(("gmail", "google"),),+ grant_account_ids=(2,),+ )+ _upgrade(conn)+ assert _issuers(conn) == {1: None, 2: None}+++def test_rerun_keeps_recorded_issuers(engine: sa.Engine) -> None:+ """A second run only touches rows still NULL, so an issuer a callback+ recorded in between is never overwritten by catalog evidence."""+ with engine.begin() as conn:+ _create_tables(conn)+ _seed(conn, rows=[("gmail", None, 1)])+ _upgrade(conn)+ conn.execute(sa.text("UPDATE user_oauth SET oauth_provider = 'github'"))+ _upgrade(conn)+ assert _issuers(conn) == {1: "github"}+++def test_downgrade_removes_the_column(engine: sa.Engine) -> None:+ with engine.begin() as conn:+ _create_tables(conn)+ _upgrade(conn)+ assert "oauth_provider" in _columns(conn)+ with patch.object(+ migration, "op", Operations(MigrationContext.configure(conn))+ ):+ migration.downgrade()+ assert "oauth_provider" not in _columns(conn)+++def test_upgrade_without_user_oauth_is_a_no_op(sqlite_engine: sa.Engine) -> None:+ """A fresh install creates user_oauth from the models after Alembic."""+ with sqlite_engine.begin() as conn:+ _upgrade(conn)+ assert not sa.inspect(conn).has_table("user_oauth")+++def test_resolve_issuer_accepts_only_durable_evidence() -> None:+ complete = migration.AppHistory(providers={"google"}, audited_since=_at(1))+ resolve = migration.resolve_issuer+ assert resolve("gmail", None, history=None) == "google"+ assert resolve("gmail", None, history=migration.AppHistory({"github"})) is None+ assert resolve("acme", _at(2), history=complete) == "google"+ assert resolve("acme", _at(0), history=complete) is None+ assert resolve("acme", None, history=complete) is None+ assert resolve("acme", _at(2), history=migration.AppHistory({"google"})) is None+ two = migration.AppHistory(providers={"google", "github"}, audited_since=_at(1))+ assert resolve("acme", _at(2), history=two) is None+ assert resolve("google", _at(2), history=None) is Nonediff --git a/tests/web/api/test_builtin_oauth_grant_revocation_on_disconnect.py b/tests/web/api/test_builtin_oauth_grant_revocation_on_disconnect.pyindex 35563a3d7..a1f78917e 100644--- a/tests/web/api/test_builtin_oauth_grant_revocation_on_disconnect.py+++ b/tests/web/api/test_builtin_oauth_grant_revocation_on_disconnect.py@@ -97,6 +97,7 @@ def _connected_github_server(db: Session) -> tuple[User, MCPServer, UserMCPServeUserOAuth(user_id=int(user.id),provider="github",+ oauth_provider="github",access_token="live-access-token",provider_user_id="42",)@@ -158,6 +159,7 @@ async def test_delete_mcp_server_skips_revocation_with_a_live_sibling(db, monkeyUserOAuth(user_id=other_user.id,provider="github",+ oauth_provider="github",provider_user_id="42",access_token="bobs-still-live-token",)@@ -249,6 +251,7 @@ async def test_teardown_mcp_app_server_skips_revocation_with_a_live_sibling(UserOAuth(user_id=other_user.id,provider="github",+ oauth_provider="github",provider_user_id="42",access_token="bobs-still-live-token",)@@ -327,6 +330,7 @@ async def test_snapshot_builtin_oauth_revocations_captures_every_matching_row(dbUserOAuth(user_id=int(user.id),provider="github",+ oauth_provider="github",provider_user_id="42",access_token="first-account-token",)@@ -335,6 +339,7 @@ async def test_snapshot_builtin_oauth_revocations_captures_every_matching_row(dbUserOAuth(user_id=int(user.id),provider="github",+ oauth_provider="github",provider_user_id="43",access_token="second-account-token",)@@ -378,6 +383,7 @@ def _seed_bare_github_account(db: Session) -> tuple[User, UserOAuth]:account = UserOAuth(user_id=int(user.id),provider="github",+ oauth_provider="github",access_token="live-access-token",provider_user_id="42",)@@ -417,6 +423,7 @@ async def test_delete_connected_account_skips_revocation_with_a_live_sibling(UserOAuth(user_id=other_user.id,provider="github",+ oauth_provider="github",provider_user_id="42",access_token="bobs-still-live-token",)@@ -446,6 +453,7 @@ async def test_delete_connected_account_ignores_token_cleared_sibling_reference(tombstone = UserOAuth(user_id=int(other_user.id),provider="github",+ oauth_provider="github",provider_user_id="42",access_token="",)diff --git a/tests/web/services/test_google_oauth_grants.py b/tests/web/services/test_google_oauth_grants.pyindex c925e30b7..c51d584fb 100644--- a/tests/web/services/test_google_oauth_grants.py+++ b/tests/web/services/test_google_oauth_grants.py@@ -667,6 +667,9 @@ def test_callback_writer_adopts_existing_unowned_bare_google_account(db_session)assert association.legacy_oauth_account_id == legacy.idassert db.query(UserOAuth).filter_by(provider="google").count() == 1assert shared.access_token == "new-shared-access"+ # Both rows predate issuer tracking (NULL); the callback records Google.+ assert shared.oauth_provider == "google"+ assert mirrored.oauth_provider == "google"def test_resolver_rejects_association_after_legacy_link_is_deleted(db_session):@@ -882,6 +885,8 @@ def test_mirror_preserves_legacy_refresh_when_shared_token_is_missing(db_sessionassert legacy.access_token == "new-shared-access"assert legacy.refresh_token == "rollback-refresh"+ # A mirror holds Google-issued tokens, whatever issuer it recorded before.+ assert legacy.oauth_provider == "google"def test_mirror_uses_canonical_builtin_scope_policy(db_session):diff --git a/tests/web/test_github_oauth.py b/tests/web/test_github_oauth.pyindex 8120bfa18..0c2d3af65 100644--- a/tests/web/test_github_oauth.py+++ b/tests/web/test_github_oauth.py@@ -1375,7 +1375,7 @@ def test_revoke_builtin_oauth_grant_deletes_github_grant_with_decrypted_secret(auth_api.revoke_builtin_oauth_grant(db,- provider="github",+ oauth_provider="github",access_token="live-access-token",provider_user_id="42",)@@ -1400,7 +1400,7 @@ def test_revoke_builtin_oauth_grant_ignores_providers_without_a_revoke(monkeypatch.setattr(auth_api.requests, "post", post)auth_api.revoke_builtin_oauth_grant(- db, provider="slack", access_token="tok", provider_user_id="99"+ db, oauth_provider="slack", access_token="tok", provider_user_id="99")delete.assert_not_called()@@ -1424,7 +1424,7 @@ def test_revoke_builtin_oauth_grant_swallows_network_failure(with caplog.at_level(logging.WARNING, logger=auth_api.logger.name):auth_api.revoke_builtin_oauth_grant(db,- provider="github",+ oauth_provider="github",access_token="the-live-access-token",provider_user_id="42",)@@ -1451,7 +1451,7 @@ def test_revoke_builtin_oauth_grant_treats_404_as_success_without_a_warning(monkeypatch.setattr(auth_api.logger, "warning", warning)auth_api.revoke_builtin_oauth_grant(- db, provider="github", access_token="tok", provider_user_id="42"+ db, oauth_provider="github", access_token="tok", provider_user_id="42")delete.assert_called_once()@@ -1472,7 +1472,7 @@ def test_revoke_builtin_oauth_grant_logs_422_as_a_real_failure(db_session, monkemonkeypatch.setattr(auth_api.logger, "warning", warning)auth_api.revoke_builtin_oauth_grant(- db, provider="github", access_token="tok", provider_user_id="42"+ db, oauth_provider="github", access_token="tok", provider_user_id="42")delete.assert_called_once()@@ -1490,7 +1490,7 @@ def test_revoke_builtin_oauth_grant_is_noop_without_a_provider_row(monkeypatch.setattr(auth_api.requests, "delete", delete)auth_api.revoke_builtin_oauth_grant(- db, provider="github", access_token="tok", provider_user_id="42"+ db, oauth_provider="github", access_token="tok", provider_user_id="42")delete.assert_not_called()@@ -1524,7 +1524,10 @@ def test_revoke_builtin_oauth_grant_skips_when_another_user_shares_the_account(monkeypatch.setattr(auth_api.requests, "delete", delete)auth_api.revoke_builtin_oauth_grant(- db, provider="github", access_token="alices-old-token", provider_user_id="42"+ db,+ oauth_provider="github",+ access_token="alices-old-token",+ provider_user_id="42",)delete.assert_not_called()@@ -1541,7 +1544,7 @@ def test_resolve_builtin_oauth_revocation_decrypts_credentials(db_session):resolved = auth_api.resolve_builtin_oauth_revocation(db,- provider="github",+ oauth_provider="github",access_token="live-access-token",provider_user_id="42",)@@ -1562,19 +1565,19 @@ def test_resolve_builtin_oauth_revocation_is_none_when_nothing_to_revoke(db_sessassert (auth_api.resolve_builtin_oauth_revocation(- db, provider="slack", access_token="tok", provider_user_id="42"+ db, oauth_provider="slack", access_token="tok", provider_user_id="42")is None)assert (auth_api.resolve_builtin_oauth_revocation(- db, provider="github", access_token="", provider_user_id="42"+ db, oauth_provider="github", access_token="", provider_user_id="42")is None)assert (auth_api.resolve_builtin_oauth_revocation(- db, provider="github", access_token="tok", provider_user_id="42"+ db, oauth_provider="github", access_token="tok", provider_user_id="42")is None) # no OAuthProvider row seeded yet@@ -1592,7 +1595,7 @@ def test_resolve_builtin_oauth_revocation_warns_on_missing_provider_row(monkeypatch.setattr(auth_api.logger, "warning", warning)auth_api.resolve_builtin_oauth_revocation(- db, provider="github", access_token="tok", provider_user_id="42"+ db, oauth_provider="github", access_token="tok", provider_user_id="42")warning.assert_called_once()@@ -1625,7 +1628,7 @@ def test_resolve_builtin_oauth_revocation_warns_on_unresolvable_secret(monkeypatch.setattr(auth_api.logger, "warning", warning)result = auth_api.resolve_builtin_oauth_revocation(- db, provider="github", access_token="tok", provider_user_id="42"+ db, oauth_provider="github", access_token="tok", provider_user_id="42")assert result is Nonediff --git a/tests/web/test_google_oauth_revocation.py b/tests/web/test_google_oauth_revocation.pyindex c123e4700..f15176e81 100644--- a/tests/web/test_google_oauth_revocation.py+++ b/tests/web/test_google_oauth_revocation.py@@ -4,8 +4,9 @@ disconnected.Google grants are stored under the connecting app's id (``gmail``,``google-drive``, ...) or the bare ``google`` key, and every one of them wasissued to the same Google OAuth client -- so the revoke resolves the-provider through the catalog, revokes through the refresh token, and skips-while any sibling row for the same Google account is still connected.+provider from the row's recorded issuer (``UserOAuth.oauth_provider``),+revokes through the refresh token, and skips while any sibling row for the+same Google account is still connected."""from __future__ import annotations@@ -83,10 +84,10 @@ def _assert_revoked_with(post: Mock, token: str) -> None:assert kwargs["data"] == {"token": token}-def test_resolve_maps_an_app_key_to_a_google_revocation(db):+def test_resolve_maps_a_google_issuer_to_a_google_revocation(db):resolved = auth_api.resolve_builtin_oauth_revocation(db,- provider="gmail",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -108,7 +109,7 @@ def test_resolve_skips_a_composio_backed_row(db):assert (auth_api.resolve_builtin_oauth_revocation(db,- provider="gmail",+ oauth_provider=None,access_token="ca_composio-account",provider_user_id="sub-1",token_type=COMPOSIO_TOKEN_TYPE,@@ -136,7 +137,7 @@ def test_revoke_prefers_the_refresh_token(db, post):with invalid_token without revoking the grant behind it."""auth_api.revoke_builtin_oauth_grant(db,- provider="google-drive",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -148,7 +149,7 @@ def test_revoke_prefers_the_refresh_token(db, post):def test_revoke_falls_back_to_the_access_token(db, post):auth_api.revoke_builtin_oauth_grant(db,- provider="google",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",)@@ -164,6 +165,7 @@ def test_revoke_skips_while_a_sibling_google_app_is_connected(db, post):UserOAuth(user_id=int(user.id),provider="google-drive",+ oauth_provider="google",provider_user_id="sub-1",access_token="drive-access",)@@ -172,7 +174,7 @@ def test_revoke_skips_while_a_sibling_google_app_is_connected(db, post):auth_api.revoke_builtin_oauth_grant(db,- provider="gmail",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -196,7 +198,7 @@ def test_a_composio_sibling_does_not_block_the_revoke(db, post):auth_api.revoke_builtin_oauth_grant(db,- provider="gmail",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -213,6 +215,7 @@ def test_a_google_row_without_an_upstream_id_is_never_revoked(db, post):UserOAuth(user_id=int(user.id),provider="google-drive",+ oauth_provider="google",provider_user_id="sub-1",access_token="drive-access",)@@ -221,7 +224,7 @@ def test_a_google_row_without_an_upstream_id_is_never_revoked(db, post):auth_api.revoke_builtin_oauth_grant(db,- provider="gmail",+ oauth_provider="google",access_token="access",provider_user_id=None,refresh_token="refresh",@@ -238,6 +241,7 @@ def test_a_same_user_google_row_without_an_upstream_id_blocks_the_revoke(db, posUserOAuth(user_id=int(user.id),provider="gmail",+ oauth_provider="google",provider_user_id=None,access_token="legacy-gmail-access",)@@ -246,7 +250,7 @@ def test_a_same_user_google_row_without_an_upstream_id_blocks_the_revoke(db, posauth_api.revoke_builtin_oauth_grant(db,- provider="google-drive",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -265,6 +269,7 @@ def test_another_users_google_row_without_an_upstream_id_blocks_the_revoke(db, pUserOAuth(user_id=int(other.id),provider="gmail",+ oauth_provider="google",provider_user_id=None,access_token="bobs-legacy-access",)@@ -273,7 +278,7 @@ def test_another_users_google_row_without_an_upstream_id_blocks_the_revoke(db, pauth_api.revoke_builtin_oauth_grant(db,- provider="google-drive",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -293,6 +298,7 @@ def test_a_tokenless_or_composio_google_row_without_an_upstream_id_does_not_blocUserOAuth(user_id=int(user.id),provider="gmail",+ oauth_provider="google",provider_user_id=None,access_token="",),@@ -309,7 +315,7 @@ def test_a_tokenless_or_composio_google_row_without_an_upstream_id_does_not_blocauth_api.revoke_builtin_oauth_grant(db,- provider="google-drive",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -330,6 +336,7 @@ async def test_the_skip_log_names_an_unidentified_legacy_row_and_the_context(UserOAuth(user_id=int(user.id),provider="gmail",+ oauth_provider="google",provider_user_id=None,access_token="legacy-gmail-access",)@@ -361,6 +368,7 @@ def test_the_skip_log_names_a_sibling_row_recording_the_same_account(db, post, cUserOAuth(user_id=int(user.id),provider="google-drive",+ oauth_provider="google",provider_user_id="sub-1",access_token="drive-access",)@@ -370,7 +378,7 @@ def test_the_skip_log_names_a_sibling_row_recording_the_same_account(db, post, cwith caplog.at_level(logging.INFO, logger=auth_api.logger.name):auth_api.revoke_builtin_oauth_grant(db,- provider="gmail",+ oauth_provider="google",access_token="access",provider_user_id="sub-1",refresh_token="refresh",@@ -477,6 +485,7 @@ async def test_delete_mcp_server_revokes_the_google_grant(db, post):UserOAuth(user_id=int(user.id),provider="gmail",+ oauth_provider="google",provider_user_id="sub-1",access_token="access",refresh_token="refresh",@@ -498,6 +507,7 @@ def _shared_grant_with_tombstoned_last_link(db: Session, user: User) -> UserOAutshared = UserOAuth(user_id=int(user.id),provider="google",+ oauth_provider="google",provider_user_id="sub-1",access_token="shared-access",refresh_token="shared-refresh",@@ -506,6 +516,7 @@ def _shared_grant_with_tombstoned_last_link(db: Session, user: User) -> UserOAutlegacy = UserOAuth(user_id=int(user.id),provider="google-drive",+ oauth_provider="google",provider_user_id="sub-1",access_token="shared-access",refresh_token="shared-refresh",@@ -580,6 +591,7 @@ async def test_a_shared_grant_a_sibling_app_still_links_is_not_revoked(db, post)calendar = UserOAuth(user_id=int(user.id),provider="google-calendar",+ oauth_provider="google",provider_user_id="sub-1",access_token="shared-access",refresh_token="shared-refresh",@@ -612,6 +624,7 @@ async def test_deleting_a_non_google_account_leaves_the_google_fences_alone(db,dropbox = UserOAuth(user_id=int(user.id),provider="dropbox",+ oauth_provider="dropbox",provider_user_id="dropbox-1",access_token="dropbox-access",)diff --git a/tests/web/test_oauth_revocation_fence.py b/tests/web/test_oauth_revocation_fence.pyindex 2d8f9761c..845ac1934 100644--- a/tests/web/test_oauth_revocation_fence.py+++ b/tests/web/test_oauth_revocation_fence.py@@ -106,7 +106,7 @@ def _revoke_alices_grant(db: Session) -> None:"""Alice's disconnect of the same Google account, after its commit."""auth_api.revoke_builtin_oauth_grant(db,- provider="google",+ oauth_provider="google",access_token="alice-access",provider_user_id=SUBJECT,refresh_token="alice-refresh",@@ -273,7 +273,7 @@ def test_a_second_revoker_cannot_close_the_fence_under_one_in_flight(reconnect_status.append(0) # re-entrancy guardauth_api.revoke_builtin_oauth_grant(carol_db,- provider="google",+ oauth_provider="google",access_token="carol-access",provider_user_id=SUBJECT,refresh_token="carol-refresh",diff --git a/tests/web/test_user_oauth_issuer.py b/tests/web/test_user_oauth_issuer.pynew file mode 100644index 000000000..005d27d9c--- /dev/null+++ b/tests/web/test_user_oauth_issuer.py@@ -0,0 +1,365 @@+"""The issuing OAuth provider recorded on each native credential.++``UserOAuth.provider`` is the connecting app's key, which for a custom app+says nothing about who issued the tokens. The callback records the issuer in+``UserOAuth.oauth_provider``, and provider-side revocation reads only that:+it keeps targeting the real issuer after an admin deletes, retargets or+re-creates the app, and refuses to guess when the issuer is unknown.+"""++from __future__ import annotations++import importlib.util+import logging+from datetime import timedelta+from pathlib import Path+from types import ModuleType, SimpleNamespace+from unittest.mock import Mock, patch++import pytest+from alembic.migration import MigrationContext+from alembic.operations import Operations+from sqlalchemy import create_engine+from sqlalchemy.orm import Session+from sqlalchemy.pool import StaticPool++from tests.shared.oauth_browser_fixture import (+ create_access_token,+ generic_oauth_callback,+)+from xagent.core.utils.encryption import encrypt_value+from xagent.web.api import auth as auth_api+from xagent.web.models.database import Base+from xagent.web.models.public_mcp import PublicMCPApp+from xagent.web.models.user import User+from xagent.web.models.user_oauth import COMPOSIO_TOKEN_TYPE, UserOAuth++CUSTOM_APP = "acme-mail"+++class MockResponse:+ def __init__(self, json_data: dict | None = None, status_code: int = 200):+ self._json_data = json_data or {}+ self.status_code = status_code+ self.text = ""++ def json(self) -> dict:+ return self._json_data+++def db():+ engine = create_engine(+ "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool+ )+ Base.metadata.create_all(bind=engine)+ with Session(engine) as session:+ yield session+ engine.dispose()+++def user(db: Session) -> User:+ user = User(username="alice", password_hash="h", is_admin=False)+ db.add(user)+ db.commit()+ db.refresh(user)+ return user+++def _custom_app(db: Session, provider_name: str) -> PublicMCPApp:+ app = PublicMCPApp(+ app_id=CUSTOM_APP,+ name="Acme Mail",+ transport="oauth",+ provider_name=provider_name,+ launch_config={},+ )+ db.add(app)+ db.commit()+ return app+++def _callback(+ db: Session, user: User, monkeypatch, *, provider: str, app_id: str, userinfo+):+ state = create_access_token(+ data={+ "type": "oauth_state",+ "user_id": user.id,+ "provider": provider,+ "app_id": app_id,+ },+ expires_delta=timedelta(minutes=10),+ )+ monkeypatch.setattr(+ auth_api.requests,+ "post",+ Mock(+ return_value=MockResponse(+ {"access_token": f"{provider}-token", "token_type": "Bearer"}+ )+ ),+ )+ monkeypatch.setattr(+ auth_api.requests, "get", Mock(return_value=MockResponse(userinfo))+ )+ db_provider = SimpleNamespace(+ provider_name=provider,+ client_id=encrypt_value(f"{provider}-client-id"),+ client_secret=encrypt_value(f"{provider}-client-secret"),+ token_url=f"https://{provider}.example.com/token",+ redirect_uri=f"https://app.example.com/api/auth/{provider}/callback",+ userinfo_url=f"https://{provider}.example.com/userinfo",+ user_id_path="sub" if provider == "google" else "id",+ email_path="email" if provider == "google" else "login",+ default_scopes=[],+ )+ request = SimpleNamespace(query_params={"code": "code", "state": state})+ return generic_oauth_callback(provider, request, db, db_provider)+++def _row(db: Session, key: str) -> UserOAuth:+ return db.query(UserOAuth).filter(UserOAuth.provider == key).one()+++def test_the_callback_records_a_builtin_provider_as_the_issuer(db, user, monkeypatch):+ response = _callback(+ db,+ user,+ monkeypatch,+ provider="github",+ app_id="github",+ userinfo={"id": 42, "login": "octocat"},+ )++ assert response.status_code == 200+ assert _row(db, "github").oauth_provider == "github"+++def test_the_callback_records_google_for_a_custom_app_backed_by_google(+ db, user, monkeypatch+):+ _custom_app(db, "google")++ response = _callback(+ db,+ user,+ monkeypatch,+ provider="google",+ app_id=CUSTOM_APP,+ userinfo={"sub": "google-sub", "email": "[email protected]"},+ )++ assert response.status_code == 200+ row = _row(db, CUSTOM_APP)+ assert row.provider == CUSTOM_APP+ assert row.oauth_provider == "google"+++def _google_row(db: Session, user: User, *, subject: str = "google-sub") -> UserOAuth:+ row = UserOAuth(+ user_id=int(user.id),+ provider=CUSTOM_APP,+ oauth_provider="google",+ provider_user_id=subject,+ access_token="google-access",+ refresh_token="google-refresh",+ )+ db.add(row)+ db.commit()+ return row+++def _retarget(db: Session) -> None:+ db.query(PublicMCPApp).filter_by(app_id=CUSTOM_APP).one().provider_name = "github"+ db.commit()+++def _delete(db: Session) -> None:+ db.delete(db.query(PublicMCPApp).filter_by(app_id=CUSTOM_APP).one())+ db.commit()+++def _delete_and_recreate(db: Session) -> None:+ _delete(db)+ _custom_app(db, "github")+++ "admin_change", [_retarget, _delete, _delete_and_recreate], ids=lambda f: f.__name__+)+def test_revocation_targets_the_recorded_issuer_after_a_catalog_change(+ db, user, admin_change+):+ _custom_app(db, "google")+ row = _google_row(db, user)+ admin_change(db)++ (revocation,) = auth_api.snapshot_builtin_oauth_revocations(db, [row])++ assert revocation.provider == "google"+ assert revocation.refresh_token == "google-refresh"+ assert revocation.provider_user_id == "google-sub"+++ "admin_change", [_retarget, _delete, _delete_and_recreate], ids=lambda f: f.__name__+)+def test_a_sibling_still_references_google_after_a_catalog_change(+ db, user, admin_change+):+ """The reference check matches the recorded issuer too: a retargeted+ custom app's Google row keeps another Google connection's grant alive."""+ _custom_app(db, "google")+ _google_row(db, user)+ admin_change(db)++ assert auth_api.has_other_builtin_oauth_reference(+ db, provider="google", provider_user_id="google-sub"+ )+ assert not auth_api.has_other_builtin_oauth_reference(+ db, provider="github", provider_user_id="google-sub"+ )+++def test_an_unknown_issuer_resolves_to_no_revocation(db, caplog):+ """Never a guess from the catalog, even when it names a provider."""+ _custom_app(db, "google")++ with caplog.at_level(logging.WARNING, logger=auth_api.logger.name):+ resolved = auth_api.resolve_builtin_oauth_revocation(+ db,+ oauth_provider=None,+ access_token="access",+ provider_user_id="google-sub",+ refresh_token="refresh",+ )++ assert resolved is None+ assert "recorded no issuing provider" in caplog.text+++def test_an_unknown_issuer_row_is_never_snapshotted(db, user):+ _custom_app(db, "google")+ row = UserOAuth(+ user_id=int(user.id),+ provider=CUSTOM_APP,+ oauth_provider=None,+ provider_user_id="google-sub",+ access_token="access",+ refresh_token="refresh",+ )+ db.add(row)+ db.commit()++ assert auth_api.snapshot_builtin_oauth_revocations(db, [row]) == []+++def test_an_unknown_issuer_row_blocks_every_providers_revoke(db, user):+ """Nothing proves which grant it holds, so it may share any of them."""+ db.add(+ UserOAuth(+ user_id=int(user.id),+ provider=CUSTOM_APP,+ oauth_provider=None,+ provider_user_id="shared-id",+ access_token="access",+ )+ )+ db.commit()++ for provider in ("google", "github"):+ assert auth_api.has_other_builtin_oauth_reference(+ db, provider=provider, provider_user_id="shared-id"+ )+++def test_composio_rows_have_no_issuer_and_are_never_revoked(db, user):+ row = UserOAuth(+ user_id=int(user.id),+ provider="gmail",+ provider_user_id="google-sub",+ access_token="ca_composio-account",+ token_type=COMPOSIO_TOKEN_TYPE,+ )+ db.add(row)+ db.commit()++ assert row.oauth_provider is None+ assert auth_api.snapshot_builtin_oauth_revocations(db, [row]) == []+ assert not auth_api.has_other_builtin_oauth_reference(+ db, provider="google", provider_user_id="google-sub"+ )+++def _issuer_migration() -> ModuleType:+ import xagent.migrations as migrations_pkg++ path = (+ Path(next(iter(migrations_pkg.__path__)))+ / "versions"+ / "20261115_user_oauth_issuer.py"+ )+ spec = importlib.util.spec_from_file_location("user_oauth_issuer", path)+ assert spec is not None and spec.loader is not None+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++def test_a_pre_audit_retarget_cannot_expose_a_live_google_row_to_revocation(+ db, user, monkeypatch+):+ """``acme-mail`` issued a live Google credential, then was retargeted to+ GitHub before catalog auditing existed, so the upgrade sees only+ ``github``. The backfill must leave the row unknown rather than label it+ ``github``: once the app is restored to Google, another user's+ disconnect of the same Google account must still find it and skip the+ project-wide revoke."""+ _custom_app(db, "github")+ survivor = UserOAuth(+ user_id=int(user.id),+ provider=CUSTOM_APP,+ provider_user_id="google-sub",+ access_token="alice-live-google-access",+ refresh_token="alice-live-google-refresh",+ )+ db.add(survivor)+ db.commit()++ migration = _issuer_migration()+ with patch.object(+ migration, "op", Operations(MigrationContext.configure(db.connection()))+ ):+ migration.upgrade()+ db.commit()+ db.refresh(survivor)+ assert survivor.oauth_provider is None++ db.query(PublicMCPApp).filter_by(app_id=CUSTOM_APP).one().provider_name = "google"+ bob = User(username="bob", password_hash="h", is_admin=False)+ db.add(bob)+ db.flush()+ disconnected = UserOAuth(+ user_id=int(bob.id),+ provider="gmail",+ oauth_provider="google",+ provider_user_id="google-sub",+ access_token="bob-access",+ refresh_token="bob-refresh",+ )+ db.add(disconnected)+ db.commit()+ (revocation,) = auth_api.snapshot_builtin_oauth_revocations(db, [disconnected])+ db.delete(disconnected)+ db.commit()++ post = Mock()+ monkeypatch.setattr(auth_api.requests, "post", post)+ auth_api.revoke_builtin_oauth_grant_if_unreferenced(db, revocation)++ post.assert_not_called()+ assert db.get(UserOAuth, int(survivor.id)).access_token == (+ "alice-live-google-access"+ )