Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

feat(composio): start a Salesforce web connect through Composio (#3814)

蓝鱼bluefish9f809dd041af
diff --git a/example.env b/example.env
index bc5e999e8..3982ce77d 100644
--- a/example.env
+++ b/example.env
@@ -1223,10 +1223,11 @@ COMPOSIO_GMAIL_AUTH_CONFIG_ID=""
XAGENT_GOOGLE_DRIVE_AUTH_BACKEND=""
# The Composio auth config for Google Drive.
COMPOSIO_GOOGLE_DRIVE_AUTH_CONFIG_ID=""
-# Salesforce through Composio's connected app: "composio" sends new Toby
-# (actor) Salesforce connects through Composio's salesforce toolkit; anything
-# else (the default) keeps them native. A web connect stays native, and
-# existing grants keep the backend they were made with. Uses the same
+# Salesforce through Composio's connected app: "composio" sends new Salesforce
+# connects, both Toby (actor) and web, through Composio's salesforce toolkit;
+# anything else (the default) keeps them native. Existing grants keep the
+# backend they were made with until reconnected: a web reconnect replaces the
+# member's native grant with a Composio one. Uses the same
# COMPOSIO_API_KEY and COMPOSIO_USER_ID_SECRET; does not need
# SALESFORCE_CLIENT_ID or SALESFORCE_CLIENT_SECRET.
# Set the auth config's scopes to api,refresh_token,openid, as native
diff --git a/src/xagent/web/api/auth.py b/src/xagent/web/api/auth.py
index cd2633559..a5c6f28d2 100644
--- a/src/xagent/web/api/auth.py
+++ b/src/xagent/web/api/auth.py
@@ -3077,6 +3077,7 @@ def generic_oauth_login(
db_provider: Optional[Any] = None,
account: Optional[str] = None,
force_google_consent: bool = False,
+ auth_backend: str | None = None,
) -> Any:
"""Start ordinary OAuth with a per-state browser-binding cookie."""
return _generic_oauth_login(
@@ -3091,6 +3092,7 @@ def generic_oauth_login(
actor_flow_nonce=None,
account=account,
force_google_consent=force_google_consent,
+ auth_backend=auth_backend,
)
@@ -4395,11 +4397,12 @@ def _generic_oauth_login(
force_google_consent: bool = False,
auth_backend: str | None = None,
) -> Any:
- """Build an ordinary or trusted actor provider authorization redirect.
+ """Build a native, ordinary Composio, or trusted actor OAuth redirect.
``auth_backend`` is ``None`` for a native start, or Composio's backend for
- an actor start of an app that goes through Composio. Any other value is a
- programming error and raises ``ValueError`` before anything is read.
+ a start of an app that goes through Composio, actor or ordinary. Any other
+ value is a programming error and raises ``ValueError`` before anything is
+ read.
"""
from .. import composio_auth
@@ -4408,6 +4411,7 @@ def _generic_oauth_login(
# Every native-only rule below (our client id, the Google gates, shared
# grants) keys on this one flag (#3649).
native_start = auth_backend is None
+ composio_ordinary = not native_start and actor_flow_nonce is None
if db is None:
raise RuntimeError("db session is required")
if native_start:
@@ -4444,8 +4448,7 @@ def _generic_oauth_login(
status_code=401,
)
- # Only the actor start passes ``auth_backend``, and only for an app that
- # goes through Composio, whose start never reaches Google (see above).
+ # A Composio start never reaches Google, so this gate is native-only.
if native_start and (error_response := _google_gmail_error(provider, app_id)):
return error_response
@@ -4514,6 +4517,12 @@ def _generic_oauth_login(
browser_state_nonce.encode("ascii")
).hexdigest()
state_payload["browser_nonce_hash"] = browser_nonce_hash
+ if composio_ordinary:
+ # The one-time claim and flow row an actor flow spends, without the
+ # owner claim that would make the callback read it as an actor flow.
+ assert browser_nonce_hash is not None
+ state_payload["actor_flow_nonce"] = browser_nonce_hash
+ state_payload[composio_auth.AUTH_BACKEND_CLAIM] = auth_backend
if bound_account is not None:
state_payload["oauth_account"] = bound_account
# Newer Salesforce orgs enforce PKCE on this authorization-code grant at
@@ -4693,7 +4702,7 @@ def _generic_oauth_login(
state = create_access_token(data=state_payload, expires_delta=timedelta(minutes=10))
- from urllib.parse import urlencode
+ from urllib.parse import urlencode, urlsplit
params = {
"response_type": "code",
@@ -4762,24 +4771,46 @@ def _generic_oauth_login(
if optional_scope_str:
params["optional_scope"] = optional_scope_str
- separator = "&" if "?" in auth_url else "?"
- full_auth_url = f"{auth_url}{separator}{urlencode(params)}"
- response = RedirectResponse(full_auth_url)
+ cookie_secure: bool
+ cookie_path: str = "/"
+ if composio_ordinary:
+ assert browser_nonce_hash is not None
+ callback_url = composio_auth.shared_callback_url(
+ db,
+ db_provider
+ if provider.lower() == composio_auth.DEFAULT_CALLBACK_PROVIDER
+ else None,
+ )
+ response = RedirectResponse(composio_auth.begin_location(callback_url, state))
+ cookie_secure, cookie_path = _oauth_cookie_scope_for(callback_url)
+ # Committed before the redirect returns: the browser follows it at
+ # once. Begin only checks this row; the verify or denied trip spends it.
+ db.add(
+ ActorOAuthFlowState(
+ nonce=browser_nonce_hash,
+ expires_at=datetime.now(timezone.utc) + _ACTOR_OAUTH_FLOW_TTL,
+ )
+ )
+ db.commit()
+ else:
+ separator = "&" if "?" in auth_url else "?"
+ full_auth_url = f"{auth_url}{separator}{urlencode(params)}"
+ response = RedirectResponse(full_auth_url)
+ cookie_secure = urlsplit(redirect_uri).scheme.lower() == "https"
+ # Match the callback URL's transport, rather than an environment
+ # label. Development can run behind HTTPS and production can be
+ # reached over HTTP during local health checks; the cookie must
+ # follow the browser-visible OAuth origin.
if browser_state_nonce is not None:
- from urllib.parse import urlsplit
-
assert browser_nonce_hash is not None
response.set_cookie(
_ordinary_oauth_cookie_name(browser_nonce_hash),
browser_state_nonce,
max_age=600,
httponly=True,
- # Match the callback URL's transport, rather than an environment
- # label. Development can run behind HTTPS and production can be
- # reached over HTTP during local health checks; the cookie must
- # follow the browser-visible OAuth origin.
- secure=urlsplit(redirect_uri).scheme.lower() == "https",
+ secure=cookie_secure,
samesite="lax",
+ path=cookie_path,
)
return response
@@ -6757,16 +6788,31 @@ def oauth_login(
db: Session = Depends(get_db),
) -> Any:
"""In-process adapter; never expose its token argument as an HTTP query."""
+ from .. import composio_auth
from ..models.oauth_provider import OAuthProvider
+ composio_app = composio_auth.composio_web_app(provider, app_id)
db_provider = (
db.query(OAuthProvider).filter(OAuthProvider.provider_name == provider).first()
)
- if not db_provider:
+ if db_provider is None and composio_app is None:
return HTMLResponse(
content=f"<h1>Unsupported provider: {html.escape(provider)}</h1>",
status_code=400,
)
+ if composio_app is not None:
+ if (refusal := composio_auth.start_preflight(composio_app.app_id)) is not None:
+ return refusal
+ return generic_oauth_login(
+ provider,
+ token,
+ app_id,
+ redirect,
+ db,
+ db_provider,
+ account=account,
+ auth_backend=composio_auth.AUTH_BACKEND,
+ )
# But now everything can be routed through generic
return generic_oauth_login(
@@ -6876,13 +6922,21 @@ def redeem_oauth_launch(
"Invalid or expired authorization ticket",
headers=_OAUTH_LAUNCH_HEADERS,
)
+ from .. import composio_auth
from ..models.oauth_provider import OAuthProvider
+ composio_app = composio_auth.composio_web_app(provider, options.app_id)
db_provider = (
db.query(OAuthProvider).filter(OAuthProvider.provider_name == provider).first()
)
- if db_provider is None:
+ if db_provider is None and composio_app is None:
raise HTTPException(400, "Unsupported provider", headers=_OAUTH_LAUNCH_HEADERS)
+ if (
+ composio_app is not None
+ and (refusal := composio_auth.start_preflight(composio_app.app_id)) is not None
+ ):
+ refusal.headers.update(_OAUTH_LAUNCH_HEADERS)
+ return refusal
response = _generic_oauth_login(
provider,
token=None,
@@ -6895,6 +6949,7 @@ def redeem_oauth_launch(
resource_owner_key=None,
actor_flow_nonce=None,
account=options.account,
+ auth_backend=(composio_auth.AUTH_BACKEND if composio_app is not None else None),
)
response.headers.update(_OAUTH_LAUNCH_HEADERS)
return response
@@ -6937,14 +6992,29 @@ def start_oauth_authorization(
raise HTTPException(
400, "Invalid authorization options", headers=private_headers
) from None
+ from .. import composio_auth
+
+ composio_app = composio_auth.composio_web_app(provider, payload.app_id)
db_provider = (
db.query(OAuthProvider).filter(OAuthProvider.provider_name == provider).first()
)
- if db_provider is None:
+ if db_provider is None and composio_app is None:
raise HTTPException(
status_code=400, detail="Unsupported provider", headers=private_headers
)
- callback = urlparse(_resolve_oauth_redirect_uri(provider, db_provider))
+ if composio_app is not None:
+ if (refusal := composio_auth.start_preflight(composio_app.app_id)) is not None:
+ refusal.headers.update(private_headers)
+ return refusal
+ callback_url = composio_auth.shared_callback_url(
+ db,
+ db_provider
+ if provider.lower() == composio_auth.DEFAULT_CALLBACK_PROVIDER
+ else None,
+ )
+ else:
+ callback_url = _resolve_oauth_redirect_uri(provider, db_provider)
+ callback = urlparse(callback_url)
if (
callback.scheme not in {"http", "https"}
or not callback.netloc
diff --git a/src/xagent/web/composio_apps.py b/src/xagent/web/composio_apps.py
index 51364fe57..f0f135223 100644
--- a/src/xagent/web/composio_apps.py
+++ b/src/xagent/web/composio_apps.py
@@ -84,6 +84,9 @@ class ComposioApp(NamedTuple):
# change on Composio's page when the account names no usable host. Ends
# the sentence "Close this window, select Connect again, and ...".
identity_host_help: str | None = None
+ # Whether ordinary (web) connects also go through Composio; an actor
+ # start follows the app's switch alone.
+ web_start: bool = False
# Graph ids, Facebook user ids and Instagram professional account ids alike.
@@ -258,6 +261,7 @@ COMPOSIO_APPS: dict[str, ComposioApp] = {
identity_id_pattern=_SALESFORCE_USER_ID_RE,
connect_guidance=SALESFORCE_CONNECT_GUIDANCE,
identity_host_help=_SALESFORCE_IDENTITY_HOST_HELP,
+ web_start=True,
),
}
@@ -300,6 +304,14 @@ def composio_app(provider: str, app_id: str | None) -> ComposioApp | None:
return app
+def composio_web_app(provider: str, app_id: str | None) -> ComposioApp | None:
+ """The app whose ordinary web connects go through Composio, if this one does."""
+ app = composio_app(provider, app_id)
+ if app is None or not app.web_start:
+ return None
+ return app
+
+
def _uses_composio(app: ComposioApp) -> bool:
backend = (_env(app.backend_env) or "native").lower()
if (
diff --git a/src/xagent/web/composio_auth.py b/src/xagent/web/composio_auth.py
index 49e71ebde..61d14f532 100644
--- a/src/xagent/web/composio_auth.py
+++ b/src/xagent/web/composio_auth.py
@@ -178,6 +178,7 @@ from .composio_apps import _env as _env
from .composio_apps import _salesforce_userinfo_url as _salesforce_userinfo_url
from .composio_apps import _uses_composio as _uses_composio
from .composio_apps import composio_app as composio_app
+from .composio_apps import composio_web_app as composio_web_app
if TYPE_CHECKING:
from sqlalchemy.orm import Query, Session
diff --git a/src/xagent/web/oauth_handoff.py b/src/xagent/web/oauth_handoff.py
index b92837d78..dbad848a5 100644
--- a/src/xagent/web/oauth_handoff.py
+++ b/src/xagent/web/oauth_handoff.py
@@ -205,6 +205,7 @@ router = APIRouter()
def redeem_handoff(
region: str, request: Request, db: Session = Depends(get_db)
) -> Response:
+ from . import composio_auth
from .api import auth, mcp
from .models.mcp import MCPServer
from .models.mcp_oauth import MCPOAuthClient, MCPOAuthFlowState
@@ -269,25 +270,34 @@ def redeem_handoff(
reject()
kind = claims["purpose"]
provider = None
+ via_composio = False
if kind == "builtin":
provider = resource.get("provider")
if not isinstance(provider, str):
reject()
+ try:
+ options = auth.OAuthLaunchRequest.model_validate(resource.get("options"))
+ except ValueError:
+ reject()
+ # The same web-start decision the entries make, taken in the region
+ # that actually starts the flow.
+ composio_app = composio_auth.composio_web_app(provider, options.app_id)
db_provider = (
db.query(OAuthProvider)
.filter(OAuthProvider.provider_name == provider)
.first()
)
- if db_provider is None:
- reject()
- try:
- options = auth.OAuthLaunchRequest.model_validate(resource.get("options"))
- except ValueError:
+ if db_provider is None and composio_app is None:
reject()
if options.redirect != claims["return_origin"]:
reject()
validate_builtin_resource(db, provider, options.app_id)
- target = str(db_provider.name)
+ via_composio = composio_app is not None
+ if db_provider is not None:
+ target = str(db_provider.name)
+ else:
+ assert composio_app is not None
+ target = composio_app.label
else:
flow = (
db.query(MCPOAuthFlowState)
@@ -341,6 +351,12 @@ def redeem_handoff(
response: Response
if kind == "builtin":
assert provider is not None
+ if via_composio:
+ assert composio_app is not None
+ refusal = composio_auth.start_preflight(composio_app.app_id)
+ if refusal is not None:
+ refusal.headers.update(HEADERS)
+ return refusal
response = auth._generic_oauth_login(
provider,
token=None,
@@ -353,6 +369,7 @@ def redeem_handoff(
resource_owner_key=None,
actor_flow_nonce=None,
account=options.account,
+ auth_backend=composio_auth.AUTH_BACKEND if via_composio else None,
)
if isinstance(response, RedirectResponse):
states = parse_qs(urlsplit(response.headers["location"]).query).get(
diff --git a/tests/web/composio_web_start.py b/tests/web/composio_web_start.py
new file mode 100644
index 000000000..8880c381f
--- /dev/null
+++ b/tests/web/composio_web_start.py
@@ -0,0 +1,126 @@
+"""Drive the three web OAuth entries the way a browser reaches them.
+
+``oauth_login`` is the in-process adapter, ``start_oauth_authorization`` issues
+one short-lived launch ticket, and ``redeem_oauth_launch`` redeems it. Each
+leads to the redirect to an app's Composio begin location when the app's
+Composio switch is on.
+"""
+
+from __future__ import annotations
+
+import json
+import re
+import secrets
+from datetime import timedelta
+from types import SimpleNamespace
+from typing import Any
+from urllib.parse import parse_qs, urlsplit
+
+from xagent.web import composio_auth
+from xagent.web.api import auth as auth_api
+from xagent.web.auth_sessions import user_session_claims
+from xagent.web.models.actor_oauth_flow import ActorOAuthFlowState
+from xagent.web.models.user import User
+
+WEB_ENTRIES = ("oauth_login", "redeem_oauth_launch", "start_oauth_authorization")
+
+
+def access_token(user: User) -> str:
+ return auth_api.create_access_token(
+ data={**user_session_claims(user), "type": "access"},
+ expires_delta=timedelta(minutes=5),
+ )
+
+
+def launch_ticket(user: User, provider: str, app_id: str) -> str:
+ """One launch ticket, minted the way ``start_oauth_authorization`` does."""
+ return auth_api.create_access_token(
+ {
+ **user_session_claims(user),
+ "type": "oauth_launch",
+ "provider": provider,
+ "jti": secrets.token_urlsafe(24),
+ "options": {"app_id": app_id, "redirect": None, "account": None},
+ },
+ expires_delta=timedelta(seconds=30),
+ )
+
+
+def redeem(db: Any, provider: str, ticket: str) -> Any:
+ return auth_api.redeem_oauth_launch(
+ provider, SimpleNamespace(query_params={}), ticket=ticket, db=db
+ )
+
+
+def start_authorization(
+ db: Any, user: User, provider: str, app_id: str
+) -> tuple[Any, str]:
+ response = auth_api.start_oauth_authorization(
+ provider,
+ SimpleNamespace(query_params={}, headers={}),
+ auth_api.OAuthLaunchRequest(app_id=app_id),
+ db,
+ user,
+ )
+ return response, json.loads(response.body)["callback_origin"]
+
+
+def web_begin(
+ db: Any, user: User, provider: str, app_id: str, entry: str
+) -> tuple[Any, str | None]:
+ """The begin-location redirect one web entry leads to, and its origin."""
+ if entry == "oauth_login":
+ return (
+ auth_api.oauth_login(
+ provider, token=access_token(user), app_id=app_id, db=db
+ ),
+ None,
+ )
+ if entry == "redeem_oauth_launch":
+ return redeem(db, provider, launch_ticket(user, provider, app_id)), None
+ response, callback_origin = start_authorization(db, user, provider, app_id)
+ assert response.status_code == 200, response.body
+ ticket = parse_qs(urlsplit(json.loads(response.body)["authorization_url"]).query)[
+ "ticket"
+ ][0]
+ return redeem(db, provider, ticket), callback_origin
+
+
+def begin_redirect(response: Any) -> tuple[str, dict[str, Any]]:
+ """The begin-location redirect and the signed state it carries."""
+ assert response.status_code in (302, 307), response.body
+ location = response.headers["location"]
+ state = auth_api.verify_token(parse_qs(urlsplit(location).query)["state"][0])
+ assert state is not None
+ return location, state
+
+
+def assert_ordinary_state(
+ db: Any, state: dict[str, Any], *, provider: str, app_id: str, session_epoch: Any
+) -> None:
+ assert state["type"] == "oauth_state"
+ assert state["provider"] == provider
+ assert state["app_id"] == app_id
+ assert state[composio_auth.AUTH_BACKEND_CLAIM] == composio_auth.AUTH_BACKEND
+ assert state.get("resource_owner_key") is None
+ assert state["session_epoch"] == session_epoch
+ assert re.fullmatch(r"[0-9a-f]{64}", state["browser_nonce_hash"]) is not None
+ # Keys the flow row, which begin only checks while verify or denied spends
+ # it; it also hydrates the ordinary proof cookie, as an actor nonce does.
+ assert state["actor_flow_nonce"] == state["browser_nonce_hash"]
+ assert db.query(ActorOAuthFlowState).count() == 1
+
+
+def assert_proof_cookie(response: Any, browser: Any, state: dict[str, Any], path: str):
+ name = auth_api._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser.receive(response)
+ assert browser.cookies[name][1] == path
+ (header,) = [
+ value
+ for value in response.headers.getlist("set-cookie")
+ if value.startswith(f"{name}=")
+ ]
+ assert f"Path={path}" in header
+ assert "HttpOnly" in header
+ assert "SameSite=lax" in header
+ assert "Secure" in header
diff --git a/tests/web/test_composio_facebook_connect.py b/tests/web/test_composio_facebook_connect.py
index f66523e5c..2fb3536cc 100644
--- a/tests/web/test_composio_facebook_connect.py
+++ b/tests/web/test_composio_facebook_connect.py
@@ -32,11 +32,14 @@ from xagent.web.mcp_apps import ensure_builtin_oauth_server_definition
from xagent.web.models.actor_oauth_flow import ActorOAuthFlowState
from xagent.web.models.database import Base
from xagent.web.models.mcp import UserMCPServer
+from xagent.web.models.oauth_provider import OAuthProvider
from xagent.web.models.public_mcp import PublicMCPApp
from xagent.web.models.user import User
from xagent.web.models.user_oauth import UserOAuth
from xagent.web.services.user_oauth import delete_scoped_user_oauth_accounts
+from . import composio_web_start as web_start
+
OWNER = "toby:workspace-7:member-alice"
OTHER_OWNER = "toby:workspace-7:member-bob"
API_KEY = "test-composio-api-key-value"
@@ -396,6 +399,55 @@ def test_each_app_follows_only_its_own_switch(composio_env, monkeypatch):
# --- Start ----------------------------------------------------------------------
+def test_a_web_facebook_connect_stays_native_with_the_composio_switch_on(
+ db_session, composio_env, composio
+):
+ """The switch moves the actor start only; a web connect keeps our own app."""
+ db, user = db_session
+ meta = _meta_provider()
+ db.add(
+ OAuthProvider(
+ **{
+ name: getattr(meta, name)
+ for name in (
+ "provider_name",
+ "client_id",
+ "client_secret",
+ "auth_url",
+ "token_url",
+ "redirect_uri",
+ "userinfo_url",
+ "user_id_path",
+ "email_path",
+ "default_scopes",
+ )
+ },
+ name="Meta",
+ )
+ )
+ db.commit()
+ browser = Browser()
+
+ response = auth_api.oauth_login(
+ "meta",
+ token=web_start.access_token(user),
+ app_id="facebook",
+ db=db,
+ )
+
+ assert response.status_code in (302, 307)
+ location = urlsplit(response.headers["location"])
+ assert location.netloc == "www.facebook.com"
+ state = auth_api.verify_token(parse_qs(location.query)["state"][0])
+ assert composio_auth.AUTH_BACKEND_CLAIM not in state
+ assert "actor_flow_nonce" not in state
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert composio.calls == []
+ name = auth_api._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser.receive(response)
+ assert browser.cookies[name][1] == "/"
+
+
def test_start_meets_the_xagent_saas_contract(db_session, composio_env, composio):
db, user = db_session
diff --git a/tests/web/test_composio_google_connect.py b/tests/web/test_composio_google_connect.py
index 62b0f8ea4..012bf5cbc 100644
--- a/tests/web/test_composio_google_connect.py
+++ b/tests/web/test_composio_google_connect.py
@@ -43,6 +43,7 @@ from xagent.web.services.google_oauth_grants import (
)
from xagent.web.services.user_oauth import delete_scoped_user_oauth_accounts
+from . import composio_web_start as web_start
from . import test_composio_facebook_connect as facebook_connect
from .test_composio_facebook_connect import (
CALLBACK_URL,
@@ -519,6 +520,59 @@ def test_a_native_gmail_callback_is_refused_while_the_composio_switch_is_on(
assert _grants(db) == []
+def _add_google_provider(db: Session) -> None:
+ provider = _google_provider()
+ db.add(
+ OAuthProvider(
+ provider_name="google",
+ name="Google",
+ client_id=provider.client_id,
+ client_secret=provider.client_secret,
+ auth_url=provider.auth_url,
+ token_url=provider.token_url,
+ redirect_uri=provider.redirect_uri,
+ userinfo_url=provider.userinfo_url,
+ user_id_path=provider.user_id_path,
+ email_path=provider.email_path,
+ default_scopes=provider.default_scopes,
+ )
+ )
+ db.commit()
+
+
[email protected]("entry", web_start.WEB_ENTRIES)
+def test_each_web_drive_entry_stays_native_while_the_composio_switch_is_on(
+ google, monkeypatch, entry
+):
+ """The switch moves the actor connect only: every web entry keeps the native
+ drive.file connect."""
+ db, user, composio = google
+ _switch_on(monkeypatch, "google-drive")
+ _install(db, user, "google-drive")
+ _add_google_provider(db)
+ browser = Browser()
+
+ response, callback_origin = web_start.web_begin(
+ db, user, "google", "google-drive", entry
+ )
+
+ assert response.status_code in (302, 307), response.body
+ location = urlsplit(response.headers["location"])
+ assert location.netloc == "accounts.google.com"
+ scopes = parse_qs(location.query)["scope"][0].split()
+ assert "https://www.googleapis.com/auth/drive.file" in scopes
+ assert "https://www.googleapis.com/auth/drive" not in scopes
+ assert callback_origin in (None, "https://app.example.com")
+ state = auth_api.verify_token(parse_qs(location.query)["state"][0])
+ assert composio_auth.AUTH_BACKEND_CLAIM not in state
+ assert "actor_flow_nonce" not in state
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert composio.calls == []
+ name = auth_api._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser.receive(response)
+ assert browser.cookies[name][1] == "/"
+
+
def test_a_web_drive_connect_stays_native_while_the_composio_switch_is_on(
google, monkeypatch
):
@@ -539,6 +593,37 @@ def test_a_web_drive_connect_stays_native_while_the_composio_switch_is_on(
assert composio.calls == []
+def test_a_web_drive_connect_stays_native_while_the_composio_switch_is_off(
+ google, monkeypatch
+):
+ db, user, composio = google
+ _install(db, user, "google-drive")
+ _add_google_provider(db)
+ browser = Browser()
+
+ response = auth_api.oauth_login(
+ "google",
+ token=web_start.access_token(user),
+ app_id="google-drive",
+ db=db,
+ )
+
+ assert response.status_code in (302, 307)
+ location = urlsplit(response.headers["location"])
+ assert location.netloc == "accounts.google.com"
+ scopes = parse_qs(location.query)["scope"][0].split()
+ assert "https://www.googleapis.com/auth/drive.file" in scopes
+ assert "https://www.googleapis.com/auth/drive" not in scopes
+ state = auth_api.verify_token(parse_qs(location.query)["state"][0])
+ assert composio_auth.AUTH_BACKEND_CLAIM not in state
+ assert "actor_flow_nonce" not in state
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert composio.calls == []
+ name = auth_api._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser.receive(response)
+ assert browser.cookies[name][1] == "/"
+
+
# --- The catalog -------------------------------------------------------------------
diff --git a/tests/web/test_composio_ordinary_flow.py b/tests/web/test_composio_ordinary_flow.py
index d8fe63210..bd3b41b74 100644
--- a/tests/web/test_composio_ordinary_flow.py
+++ b/tests/web/test_composio_ordinary_flow.py
@@ -1,9 +1,9 @@
"""An ordinary (web) connect completes through Composio-hosted auth.
-No start emits such a state yet, so these tests mint it the way the later
-start will: the Composio auth-backend claim, no ``resource_owner_key``, the
-ordinary browser proof cookie named by the state's ``browser_nonce_hash``
-claim, and a single-use flow row the verify or denied trip spends.
+These tests mint the state by hand, matching what a Composio web start emits:
+the Composio auth-backend claim, no ``resource_owner_key``, the ordinary
+browser proof cookie named by the state's ``browser_nonce_hash`` claim, and a
+single-use flow row the verify or denied trip spends.
Composio is faked by the Facebook connect tests; no request leaves the
process.
diff --git a/tests/web/test_composio_regional_handoff.py b/tests/web/test_composio_regional_handoff.py
new file mode 100644
index 000000000..2207a02ba
--- /dev/null
+++ b/tests/web/test_composio_regional_handoff.py
@@ -0,0 +1,381 @@
+"""A regional web launch starts a Composio flow in the region that redeems it.
+
+The handoff ticket carries the provider and launch options; the target region
+makes the same switch decision as the web entries, so a switched-on app needs
+no native provider row there either.
+"""
+
+from __future__ import annotations
+
+import html
+import json
+import re
+from urllib.parse import parse_qs, urlencode, urlsplit
+
+import pytest
+from fastapi import FastAPI, HTTPException
+from sqlalchemy import create_engine
+from sqlalchemy.orm import Session
+from starlette.requests import Request
+
+from xagent.web import composio_auth
+from xagent.web.api import auth
+from xagent.web.models.actor_oauth_flow import ActorOAuthFlowState
+from xagent.web.models.database import Base
+from xagent.web.models.oauth_provider import OAuthProvider
+from xagent.web.models.user import User
+from xagent.web.oauth_handoff import OAuthHandoffContext, redeem_handoff
+
+from . import test_composio_google_connect as google_connect
+from . import test_composio_salesforce_connect as salesforce_connect
+from .test_composio_facebook_connect import (
+ API_KEY,
+ CALLBACK_PATH,
+ USER_ID_SECRET,
+ Browser,
+ _grants,
+ _verify_url,
+)
+
+OLD = "https://region.example"
+NEW = "https://public.example"
+# Served by the redeeming region, which sets the host-only proof cookie.
+CALLBACK_URL = f"{OLD}{CALLBACK_PATH}"
+
+# app_id -> (provider, fake Composio, switch env, auth-config env, config id,
+# native provider row factory, native authorize host)
+APPS = {
+ "salesforce": (
+ "salesforce",
+ salesforce_connect.SalesforceComposio,
+ "XAGENT_SALESFORCE_AUTH_BACKEND",
+ "COMPOSIO_SALESFORCE_AUTH_CONFIG_ID",
+ salesforce_connect.SALESFORCE_AUTH_CONFIG_ID,
+ salesforce_connect._salesforce_provider,
+ "login.salesforce.com",
+ ),
+ "google-drive": (
+ "google",
+ google_connect.GoogleComposio,
+ "XAGENT_GOOGLE_DRIVE_AUTH_BACKEND",
+ "COMPOSIO_GOOGLE_DRIVE_AUTH_CONFIG_ID",
+ google_connect.AUTH_CONFIGS["google-drive"],
+ google_connect._google_provider,
+ "accounts.google.com",
+ ),
+}
+
+
+class Adapter:
+ def context(self, request):
+ return OAuthHandoffContext("product", "test", "au", OLD)
+
+ def set_route_cookie(self, response, *, state, kind, provider):
+ response.set_cookie("scoped-" + kind, "au")
+
+
+def regional(tmp_path, monkeypatch, app_id):
+ provider, fake_cls, backend_env, config_env, config_id, provider_row, _ = APPS[
+ app_id
+ ]
+ fake = fake_cls()
+ monkeypatch.setattr(composio_auth.requests, "request", fake.request)
+ monkeypatch.setenv("XAGENT_APP_BASE_URL", OLD)
+ monkeypatch.setenv("XAGENT_DUAL_ENTRY_OAUTH_ENABLED", "true")
+ monkeypatch.setenv(
+ "XAGENT_PUBLIC_ENTRIES_JSON",
+ json.dumps(
+ [
+ {
+ "origin": origin,
+ "region_cookie_domain": None,
+ "google_oidc_redirect_uri": origin
+ + "/api/auth/oidc/google/callback",
+ }
+ for origin in [OLD, NEW]
+ ]
+ ),
+ )
+ monkeypatch.setenv("XAGENT_COMPOSIO_CALLBACK_URL", CALLBACK_URL)
+ monkeypatch.setenv(composio_auth.COMPOSIO_API_KEY_ENV, API_KEY)
+ monkeypatch.setenv(composio_auth.COMPOSIO_USER_ID_SECRET_ENV, USER_ID_SECRET)
+ for name in (
+ "XAGENT_FACEBOOK_AUTH_BACKEND",
+ "XAGENT_INSTAGRAM_AUTH_BACKEND",
+ "XAGENT_GMAIL_AUTH_BACKEND",
+ "XAGENT_GOOGLE_DRIVE_AUTH_BACKEND",
+ "XAGENT_SALESFORCE_AUTH_BACKEND",
+ ):
+ monkeypatch.delenv(name, raising=False)
+ engine = create_engine(
+ f"sqlite:///{tmp_path / 'regional.db'}",
+ connect_args={"check_same_thread": False},
+ )
+ Base.metadata.create_all(engine)
+ db = Session(engine)
+ user = User(username="regional", password_hash="unused", session_epoch="epoch")
+ db.add(user)
+ db.commit()
+ app = FastAPI()
+ app.state.oauth_handoff_adapter = Adapter()
+ try:
+ yield (
+ db,
+ user,
+ fake,
+ app,
+ provider,
+ provider_row,
+ backend_env,
+ config_env,
+ config_id,
+ )
+ finally:
+ db.close()
+ engine.dispose()
+
+
+def _request(app: FastAPI, query: dict[str, str], *, cookie: str | None = None):
+ headers = [(b"host", urlsplit(OLD).netloc.encode())]
+ if cookie:
+ headers.append((b"cookie", cookie.encode()))
+ return Request(
+ {
+ "type": "http",
+ "method": "GET",
+ "path": "/api/oauth/launch/au",
+ "query_string": urlencode(query).encode(),
+ "headers": headers,
+ "app": app,
+ }
+ )
+
+
+def _regional_launch(
+ db, user, app, provider, app_id, callback_url: str | None = None
+) -> tuple[str, str, str]:
+ """Start at the public entry, then open the target region's confirmation."""
+ request = Request(
+ {
+ "type": "http",
+ "method": "POST",
+ "path": "/",
+ "query_string": b"",
+ "headers": [
+ (b"host", urlsplit(NEW).netloc.encode()),
+ (b"x-xagent-oauth-launch-version", b"2"),
+ ],
+ "app": app,
+ }
+ )
+ launch = auth.start_oauth_authorization(
+ provider,
+ request,
+ auth.OAuthLaunchRequest(app_id=app_id, redirect=NEW),
+ db,
+ user,
+ )
+ assert launch.status_code == 200, launch.body
+ body = json.loads(launch.body)
+ if callback_url is not None:
+ callback = urlsplit(callback_url)
+ assert body["callback_origin"] == f"{callback.scheme}://{callback.netloc}"
+ ticket = parse_qs(urlsplit(body["authorization_url"]).query)["ticket"][0]
+ page = redeem_handoff("au", _request(app, {"ticket": ticket}), db)
+ assert page.status_code == 200, page.body
+ href = html.unescape(re.search(r'href="([^"]+)"', page.body.decode())[1])
+ confirm = parse_qs(urlsplit(href).query)["confirm"][0]
+ cookie = page.headers["set-cookie"].split(";", 1)[0]
+ return ticket, confirm, cookie
+
+
+def _redeem(db, app, ticket: str, confirm: str, cookie: str):
+ return redeem_handoff(
+ "au", _request(app, {"ticket": ticket, "confirm": confirm}, cookie=cookie), db
+ )
+
+
[email protected]("app_id", ["salesforce"])
+def test_a_regional_web_launch_starts_a_composio_flow(regional, monkeypatch, app_id):
+ db, user, fake, app, provider, _row, backend_env, config_env, config_id = regional
+ monkeypatch.setenv(backend_env, "composio")
+ monkeypatch.setenv(config_env, config_id)
+ assert (
+ db.query(OAuthProvider).filter(OAuthProvider.provider_name == provider).first()
+ is None
+ )
+ ticket, confirm, cookie = _regional_launch(
+ db, user, app, provider, app_id, CALLBACK_URL
+ )
+ page = redeem_handoff("au", _request(app, {"ticket": ticket}), db)
+ assert f"Connect {composio_auth.COMPOSIO_APPS[app_id].label}?" in page.body.decode()
+
+ response = _redeem(db, app, ticket, confirm, cookie)
+
+ assert response.status_code == 303, response.body
+ location = response.headers["location"]
+ assert location.startswith(f"{CALLBACK_URL}?state=")
+ state = auth.verify_token(parse_qs(urlsplit(location).query)["state"][0])
+ assert state[composio_auth.AUTH_BACKEND_CLAIM] == composio_auth.AUTH_BACKEND
+ assert state["app_id"] == app_id
+ assert state["session_epoch"] == "epoch"
+ assert state["actor_flow_nonce"] == state["browser_nonce_hash"]
+ # The proof cookie follows the shared callback resolved in this region.
+ cookie_name = auth._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser = Browser()
+ browser.receive(response)
+ assert browser.cookies[cookie_name][1] == CALLBACK_PATH
+ assert urlsplit(location).netloc == urlsplit(OLD).netloc
+
+ assert browser.get(db, location).status_code == 302
+ verify = browser.get(db, _verify_url(fake))
+ assert verify.status_code == 200, verify.body
+
+ db.expire_all()
+ (grant,) = _grants(db)
+ assert (grant.provider, grant.token_type, grant.access_token) == (
+ app_id,
+ "composio",
+ fake.created[-1],
+ )
+ assert grant.resource_owner_key is None
+ assert db.query(ActorOAuthFlowState).count() == 0
+
+
+INVALID_CALLBACK_URL = f"{CALLBACK_URL}?x=1"
+
+
[email protected]("app_id", ["salesforce"])
+def test_a_regional_composio_launch_refuses_an_unusable_callback_url(
+ regional, monkeypatch, app_id
+):
+ db, user, fake, app, provider, _row, backend_env, config_env, config_id = regional
+ monkeypatch.setenv(backend_env, "composio")
+ monkeypatch.setenv(config_env, config_id)
+ ticket, confirm, cookie = _regional_launch(db, user, app, provider, app_id)
+ monkeypatch.setenv("XAGENT_COMPOSIO_CALLBACK_URL", INVALID_CALLBACK_URL)
+
+ response = _redeem(db, app, ticket, confirm, cookie)
+
+ assert response.status_code == 503
+ body = response.body.decode()
+ assert f"{composio_auth.COMPOSIO_APPS[app_id].label} can't be connected" in body
+ assert "location" not in response.headers
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert fake.calls == []
+ # Spent before the refusal, as redeeming a launch ticket is everywhere else.
+ with pytest.raises(HTTPException) as replay:
+ _redeem(db, app, ticket, confirm, cookie)
+ assert replay.value.status_code == 401
+
+
[email protected]("app_id", ["salesforce"])
+def test_a_regional_composio_launch_refuses_missing_composio_config(
+ regional, monkeypatch, app_id
+):
+ db, user, fake, app, provider, _row, backend_env, config_env, config_id = regional
+ monkeypatch.setenv(backend_env, "composio")
+ monkeypatch.setenv(config_env, config_id)
+ ticket, confirm, cookie = _regional_launch(db, user, app, provider, app_id)
+ monkeypatch.delenv(composio_auth.COMPOSIO_API_KEY_ENV)
+
+ response = _redeem(db, app, ticket, confirm, cookie)
+
+ assert response.status_code == 500
+ body = response.body.decode()
+ assert "OAuth provider not configured" in body
+ assert f"Missing {composio_auth.COMPOSIO_API_KEY_ENV}" in body
+ assert "location" not in response.headers
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert fake.calls == []
+
+
[email protected]("app_id", ["google-drive"])
+def test_an_unusable_composio_setting_never_refuses_a_native_regional_launch(
+ regional, monkeypatch, app_id
+):
+ db, user, fake, app, provider, provider_row, backend_env, config_env, config_id = (
+ regional
+ )
+ monkeypatch.setenv(backend_env, "composio")
+ monkeypatch.setenv(config_env, config_id)
+ _add_native_provider(db, provider, provider_row)
+ ticket, confirm, cookie = _regional_launch(db, user, app, provider, app_id)
+ monkeypatch.setenv("XAGENT_COMPOSIO_CALLBACK_URL", INVALID_CALLBACK_URL)
+
+ response = _redeem(db, app, ticket, confirm, cookie)
+
+ assert response.status_code == 303
+ assert urlsplit(response.headers["location"]).netloc == APPS[app_id][6]
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert fake.calls == []
+
+
+def _add_native_provider(db, provider, provider_row) -> None:
+ native = provider_row()
+ db.add(
+ OAuthProvider(
+ provider_name=provider,
+ name=provider.title(),
+ client_id=native.client_id,
+ client_secret=native.client_secret,
+ auth_url=native.auth_url,
+ token_url=native.token_url,
+ redirect_uri=native.redirect_uri,
+ userinfo_url=native.userinfo_url,
+ user_id_path=native.user_id_path,
+ email_path=native.email_path,
+ default_scopes=native.default_scopes,
+ )
+ )
+ db.commit()
+
+
[email protected]("app_id", ["google-drive"])
+def test_a_regional_web_launch_stays_native_with_the_switch_on(
+ regional, monkeypatch, app_id
+):
+ db, user, fake, app, provider, provider_row, backend_env, config_env, config_id = (
+ regional
+ )
+ monkeypatch.setenv(backend_env, "composio")
+ monkeypatch.setenv(config_env, config_id)
+ _add_native_provider(db, provider, provider_row)
+ ticket, confirm, cookie = _regional_launch(db, user, app, provider, app_id)
+
+ response = _redeem(db, app, ticket, confirm, cookie)
+
+ assert response.status_code == 303
+ location = urlsplit(response.headers["location"])
+ assert location.netloc == APPS[app_id][6]
+ state = auth.verify_token(parse_qs(location.query)["state"][0])
+ assert composio_auth.AUTH_BACKEND_CLAIM not in state
+ assert "actor_flow_nonce" not in state
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert fake.calls == []
+ assert _grants(db) == []
+
+
[email protected]("app_id", sorted(APPS))
+def test_a_regional_web_launch_stays_native_with_the_switch_off(regional, app_id):
+ db, user, fake, app, provider, provider_row, _backend_env, _config_env, _ = regional
+ _add_native_provider(db, provider, provider_row)
+ ticket, confirm, cookie = _regional_launch(db, user, app, provider, app_id)
+
+ response = _redeem(db, app, ticket, confirm, cookie)
+
+ assert response.status_code == 303
+ location = urlsplit(response.headers["location"])
+ assert location.netloc == APPS[app_id][6]
+ state = auth.verify_token(parse_qs(location.query)["state"][0])
+ assert composio_auth.AUTH_BACKEND_CLAIM not in state
+ assert "actor_flow_nonce" not in state
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert fake.calls == []
+ # The native proof cookie keeps its browser-wide default path.
+ cookie_name = auth._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser = Browser()
+ browser.receive(response)
+ assert browser.cookies[cookie_name][1] == "/"
+ assert _grants(db) == []
diff --git a/tests/web/test_composio_salesforce_connect.py b/tests/web/test_composio_salesforce_connect.py
index 0c44c4613..fa4906f52 100644
--- a/tests/web/test_composio_salesforce_connect.py
+++ b/tests/web/test_composio_salesforce_connect.py
@@ -12,7 +12,6 @@ request leaves the process.
from __future__ import annotations
import logging
-from datetime import timedelta
from types import SimpleNamespace
from typing import Any
from urllib.parse import parse_qs, urlsplit
@@ -32,8 +31,10 @@ from xagent.web.models.public_mcp import PublicMCPApp
from xagent.web.models.user import User
from xagent.web.models.user_oauth import UserOAuth
+from . import composio_web_start as web_start
from . import test_composio_facebook_connect as facebook_connect
from .test_composio_facebook_connect import (
+ CALLBACK_PATH,
CALLBACK_URL,
COMPOSIO_REDIRECT,
OTHER_OWNER,
@@ -44,8 +45,14 @@ from .test_composio_facebook_connect import (
_grants,
_meta_provider,
_Response,
+ _store_composio_grant,
_verify_url,
)
+from .test_composio_ordinary_flow import (
+ DEDICATED_PATH,
+ DEDICATED_URL,
+ RoutedBrowser,
+)
# Facebook's fixtures, shared: the environment and a database with the
# Facebook connector installed.
@@ -300,21 +307,141 @@ def test_with_the_switch_off_a_start_without_the_row_is_refused(
assert composio.calls == []
-def test_with_the_switch_on_a_web_salesforce_connect_stays_native(salesforce):
- """Only actor (Toby) connects go through Composio: an ordinary web connect,
- which has no resource owner key, still uses our own connected app."""
[email protected]("entry", web_start.WEB_ENTRIES)
+def test_with_the_switch_on_a_web_salesforce_connect_goes_through_composio(
+ salesforce, monkeypatch, entry
+):
+ """Every web entry starts a Composio flow when the app's switch is on, with
+ no native provider row and no native client configured."""
db, user, composio = salesforce
- token = auth_api.create_access_token(
- data={"sub": user.username, "user_id": user.id, "type": "access"},
- expires_delta=timedelta(minutes=5),
+ monkeypatch.delenv("SALESFORCE_CLIENT_ID", raising=False)
+ monkeypatch.delenv("SALESFORCE_CLIENT_SECRET", raising=False)
+ user.session_epoch = "generation"
+ db.commit()
+ assert (
+ db.query(OAuthProvider)
+ .filter(OAuthProvider.provider_name == "salesforce")
+ .first()
+ is None
+ )
+ _store_composio_grant(db, user, account_id="ca_actor", provider="salesforce")
+ browser = Browser()
+
+ response, callback_origin = web_start.web_begin(
+ db, user, "salesforce", "salesforce", entry
)
- response = auth_api.generic_oauth_login(
+ location, state = web_start.begin_redirect(response)
+ assert location.startswith(f"{CALLBACK_URL}?state=")
+ assert callback_origin in (None, "https://app.example.com")
+ web_start.assert_ordinary_state(
+ db,
+ state,
provider="salesforce",
- token=token,
+ app_id="salesforce",
+ session_epoch="generation",
+ )
+ web_start.assert_proof_cookie(response, browser, state, CALLBACK_PATH)
+
+ assert browser.get(db, location).status_code == 302
+ verify = browser.get(db, _verify_url(composio))
+ assert verify.status_code == 200, verify.body
+
+ db.expire_all()
+ ordinary = [grant for grant in _grants(db) if grant.resource_owner_key is None]
+ assert [
+ (grant.provider, grant.token_type, grant.access_token) for grant in ordinary
+ ] == [("salesforce", "composio", composio.created[-1])]
+ actor = [grant for grant in _grants(db) if grant.resource_owner_key == OWNER]
+ assert [(grant.provider, grant.access_token) for grant in actor] == [
+ ("salesforce", "ca_actor")
+ ]
+ assert db.query(ActorOAuthFlowState).count() == 0
+ assert _flow_cookies(browser) == []
+
+ # The same state is one-time: replaying the begin creates no second link.
+ links = composio.paths().count("/connected_accounts/link")
+ assert browser.get(db, location).status_code == 400
+ assert composio.paths().count("/connected_accounts/link") == links
+
+
+def test_a_web_salesforce_composio_state_is_refused_on_the_native_callback(
+ salesforce,
+):
+ db, user, composio = salesforce
+ response, _ = web_start.web_begin(
+ db, user, "salesforce", "salesforce", "oauth_login"
+ )
+ location, _ = web_start.begin_redirect(response)
+ state = parse_qs(urlsplit(location).query)["state"][0]
+
+ callback = auth_api.generic_oauth_callback(
+ "salesforce",
+ SimpleNamespace(query_params={"code": "code", "state": state}, cookies={}),
+ db,
+ None,
+ )
+
+ assert callback.status_code == 400
+ assert composio.calls == []
+ assert _grants(db) == []
+ assert db.query(ActorOAuthFlowState).count() == 1
+
+
+def test_a_web_salesforce_start_scopes_its_proof_cookie_to_the_dedicated_path(
+ salesforce, monkeypatch
+):
+ db, user, composio = salesforce
+ monkeypatch.setenv("XAGENT_COMPOSIO_CALLBACK_URL", DEDICATED_URL)
+ browser = RoutedBrowser()
+
+ response, _ = web_start.web_begin(
+ db, user, "salesforce", "salesforce", "oauth_login"
+ )
+ location, state = web_start.begin_redirect(response)
+ assert urlsplit(location).path == DEDICATED_PATH
+ web_start.assert_proof_cookie(response, browser, state, DEDICATED_PATH)
+
+ assert browser.get(db, location).status_code == 302
+ verify = browser.get(
+ db, f"{DEDICATED_URL}?session_uri={_verify_url(composio).split('=', 1)[1]}"
+ )
+
+ assert verify.status_code == 200, verify.body
+ db.expire_all()
+ ordinary = [grant for grant in _grants(db) if grant.resource_owner_key is None]
+ assert [grant.provider for grant in ordinary] == ["salesforce"]
+
+
+def test_with_the_switch_off_a_web_salesforce_connect_stays_native(
+ db_session, composio_env, composio, monkeypatch
+):
+ monkeypatch.delenv("XAGENT_SALESFORCE_AUTH_BACKEND", raising=False)
+ db, user = db_session
+ provider = _salesforce_provider()
+ db.add(
+ OAuthProvider(
+ provider_name="salesforce",
+ name="Salesforce",
+ client_id=provider.client_id,
+ client_secret=provider.client_secret,
+ auth_url=provider.auth_url,
+ token_url=provider.token_url,
+ redirect_uri=provider.redirect_uri,
+ userinfo_url=provider.userinfo_url,
+ user_id_path=provider.user_id_path,
+ email_path=provider.email_path,
+ default_scopes=provider.default_scopes,
+ )
+ )
+ db.commit()
+ browser = Browser()
+
+ response = auth_api.oauth_login(
+ "salesforce",
+ token=web_start.access_token(user),
app_id="salesforce",
db=db,
- db_provider=_salesforce_provider(),
)
assert response.status_code in (302, 307)
@@ -326,9 +453,22 @@ def test_with_the_switch_on_a_web_salesforce_connect_stays_native(salesforce):
query = parse_qs(location.query)
assert query["client_id"] == ["salesforce-client-id"]
state = auth_api.verify_token(query["state"][0])
- assert state.get("resource_owner_key") is None
assert composio_auth.AUTH_BACKEND_CLAIM not in state
+ assert "actor_flow_nonce" not in state
+ assert state.get("resource_owner_key") is None
+ assert db.query(ActorOAuthFlowState).count() == 0
assert composio.calls == []
+ # The native proof cookie is untouched: the browser-wide default path.
+ name = auth_api._ordinary_oauth_cookie_name(state["browser_nonce_hash"])
+ browser.receive(response)
+ assert browser.cookies[name][1] == "/"
+ (header,) = [
+ value
+ for value in response.headers.getlist("set-cookie")
+ if value.startswith(f"{name}=")
+ ]
+ assert "Path=/" in header
+ assert "HttpOnly" in header
# --- Connect end to end ---------------------------------------------------------