Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(marketplace): keep a browser OAuth grant when its capability check fails (#814)

Ruben Lütke5245bec5af18
diff --git a/docs/marketplace/browser-auth-standard.md b/docs/marketplace/browser-auth-standard.md
index 192bc19142..cb7752250e 100644
--- a/docs/marketplace/browser-auth-standard.md
+++ b/docs/marketplace/browser-auth-standard.md
@@ -52,9 +52,16 @@ Rules:
(use the `ERROR_*` codes + `sanitize_provider_error`).
- Cancel, timeout, retry, reconnect, disconnect. Understandable errors for
denied consent, missing admin approval, and provider outages.
-- "Connected" is shown only after authorization completed AND a
- side-effect-free functional check passed. An opened browser tab or a
- merely stored token is not enough.
+- "Connected" is shown only after authorization completed. An opened
+ browser tab is not enough. The completed grant is stored FIRST, then a
+ side-effect-free functional check runs, and its outcome is reported as
+ capability health beside the auth status (`capability_state`
+ "verified" or "degraded" with a `capability_reason`). A check that fails
+ for any reason other than the provider refusing the credential (missing
+ scope, rate limit, outage) never discards the grant: the plugin stays
+ connected but degraded, so the user is not sent through a consent that
+ already succeeded. A credential the provider rejects (HTTP 401) is an
+ error and leaves the previous stored state untouched.
- Existing working connections migrate cleanly. Required re-logins go
through the new browser flow. Own OAuth clients stay, at most, a clearly
separated expert option — never the default prerequisite.
@@ -95,7 +102,8 @@ Enforced by:
instance-address authorization configuration;
- `dodaios/marketplace/publisher_clients.py` — shared resolution;
- `dodaios/marketplace/connection_verification.py` — bounded resource checks
- before publishing a successful OAuth connection; unknown hooks fail closed;
+ after a grant is stored, classified as auth rejection or degraded
+ capability; an unknown hook never counts as verified;
- `tests/contract/test_plugin_auth_standard.py` — mandatory contract tests;
- `scripts/ci/check_plugin_auth_contract.py` — blocking configuration and
browser-evidence gate. `plugin-e2e-audit.json` records all seven observed
diff --git a/dodaios/marketplace/connection_verification.py b/dodaios/marketplace/connection_verification.py
index 6a123e9a42..25abb32bf7 100644
--- a/dodaios/marketplace/connection_verification.py
+++ b/dodaios/marketplace/connection_verification.py
@@ -1,7 +1,14 @@
-"""Bounded, read-only resource checks before a new grant becomes connected.
+"""Bounded, read-only resource checks for a freshly granted connection.
These checks validate access, not a completed plugin smoke-test audit. No token
is stored here, and provider payloads never become diagnostic messages.
+
+The connect flow persists the grant BEFORE it runs this check. A failure is
+then classified: only a provider that rejects the credential itself (HTTP 401,
+or Slack's ``invalid_auth`` family) means the grant is unusable. Every other
+failure (a missing scope, a rate limit, an outage, an empty tool list) leaves a
+valid grant connected with degraded capability, so the user is never sent back
+through a browser consent that already succeeded.
"""
from __future__ import annotations
@@ -18,8 +25,70 @@ VERIFY_TIMEOUT_SECONDS = 30.0
CLOSE_TIMEOUT_SECONDS = 5.0
+# Why a capability check failed. Fixed codes only: provider text never leaves
+# this module, because an error body can echo a token back.
+CAPABILITY_UNAUTHORIZED = "unauthorized"
+"""The provider rejected the credential itself; the grant is unusable."""
+CAPABILITY_FORBIDDEN = "forbidden"
+"""Signed in, but the account or scopes do not reach the probed resource."""
+CAPABILITY_RATE_LIMITED = "rate_limited"
+"""The provider throttled the check; access itself is unknown."""
+CAPABILITY_UNAVAILABLE = "unavailable"
+"""Anything else: outage, timeout, transport error, unusable response."""
+
+# Slack answers HTTP 200 with ok=false; these error codes reject the token.
+_SLACK_REJECTED_ERRORS = frozenset(
+ {"invalid_auth", "not_authed", "token_revoked", "token_expired", "account_inactive"}
+)
+
+
class ConnectionVerificationError(RuntimeError):
- """A fixed, safe explanation suitable for the connection dialog."""
+ """A fixed, safe explanation suitable for the connection dialog.
+
+ ``reason`` is one of the ``CAPABILITY_*`` codes. Only
+ ``CAPABILITY_UNAUTHORIZED`` means the grant itself was refused.
+ """
+
+ def __init__(self, message: str, *, reason: str = CAPABILITY_UNAVAILABLE) -> None:
+ super().__init__(message)
+ self.reason = reason
+
+ @property
+ def rejected(self) -> bool:
+ """True when the provider refused the credential, not just a capability."""
+ return self.reason == CAPABILITY_UNAUTHORIZED
+
+
+def _http_status(exc: BaseException) -> int | None:
+ """Find the HTTP status behind a failure, through groups and chained causes."""
+ pending: list[BaseException] = [exc]
+ seen: set[int] = set()
+ while pending:
+ current = pending.pop()
+ if id(current) in seen:
+ continue
+ seen.add(id(current))
+ if isinstance(current, httpx.HTTPStatusError):
+ return current.response.status_code
+ status = getattr(current, "status_code", None)
+ if isinstance(status, int) and not isinstance(status, bool):
+ return status
+ if isinstance(current, BaseExceptionGroup):
+ pending.extend(current.exceptions)
+ for linked in (current.__cause__, current.__context__):
+ if linked is not None:
+ pending.append(linked)
+ return None
+
+
+def _reason_for_status(status: int | None) -> str:
+ if status == 401:
+ return CAPABILITY_UNAUTHORIZED
+ if status == 403:
+ return CAPABILITY_FORBIDDEN
+ if status == 429:
+ return CAPABILITY_RATE_LIMITED
+ return CAPABILITY_UNAVAILABLE
# Explicit operations avoid accidentally verifying through a future write tool.
@@ -82,6 +151,16 @@ async def _verify_native(spec: PluginSpec, tokens: Tokens) -> None:
)
response.raise_for_status()
payload = response.json()
+ if (
+ spec.id == "slack"
+ and isinstance(payload, dict)
+ and payload.get("ok") is False
+ and str(payload.get("error", "")) in _SLACK_REJECTED_ERRORS
+ ):
+ raise ConnectionVerificationError(
+ "Slack rejected the authorization. Connect again.",
+ reason=CAPABILITY_UNAUTHORIZED,
+ )
if (
not isinstance(payload, dict)
or expected not in payload
@@ -138,10 +217,17 @@ async def _verify_mcp(spec: PluginSpec, tokens: Tokens) -> None:
async def verify_connection(spec: PluginSpec, tokens: Tokens) -> None:
- """Raise safely on failed access; leave persistence to the successful caller."""
+ """Raise a safe, classified ``ConnectionVerificationError`` on failed access.
+
+ Persistence is the caller's job; ``exc.rejected`` tells it whether the
+ grant itself was refused or only a capability is degraded.
+ """
try:
if not tokens.access or tokens.needs_reauth:
- raise ConnectionVerificationError("Authorization is missing or expired. Connect again.")
+ raise ConnectionVerificationError(
+ "Authorization is missing or expired. Connect again.",
+ reason=CAPABILITY_UNAUTHORIZED,
+ )
if spec.verification_hook is not None:
if spec.verification_hook != "home_assistant_api" or spec.id != "home_assistant":
raise ConnectionVerificationError(
@@ -156,19 +242,24 @@ async def verify_connection(spec: PluginSpec, tokens: Tokens) -> None:
await _verify_mcp(spec, tokens)
except ConnectionVerificationError:
raise
- except asyncio.CancelledError:
+ except asyncio.CancelledError as exc:
task = asyncio.current_task()
if task is not None and task.cancelling():
raise
# A failed MCP transport can cancel its internal owner and forward
# that exception without cancellation of this verification task.
raise ConnectionVerificationError(
- "The provider connection closed during verification. Try again."
+ "The provider connection closed during verification. Try again.",
+ reason=_reason_for_status(_http_status(exc)),
) from None
except TimeoutError:
raise ConnectionVerificationError("The connection check timed out. Try again.") from None
- except Exception:
- # Exception text can contain tokens, response bodies or account data.
- raise ConnectionVerificationError(
- "The provider connection check failed. Check account access and try again."
- ) from None
+ except Exception as exc:
+ # Exception text can contain tokens, response bodies or account data;
+ # only the HTTP status is read from it, never the message.
+ reason = _reason_for_status(_http_status(exc))
+ if reason == CAPABILITY_UNAUTHORIZED:
+ message = "The provider rejected the authorization. Connect again."
+ else:
+ message = "The provider connection check failed. Check account access and try again."
+ raise ConnectionVerificationError(message, reason=reason) from None
diff --git a/dodaios/plugins/tool/connected_server.py b/dodaios/plugins/tool/connected_server.py
index 6783d88536..8e794c81fe 100644
--- a/dodaios/plugins/tool/connected_server.py
+++ b/dodaios/plugins/tool/connected_server.py
@@ -55,6 +55,17 @@ def operation_schema(operation: Operation) -> dict[str, Any]:
}
+class ProviderHttpError(RuntimeError):
+ """A provider HTTP failure whose status callers may branch on.
+
+ The message is our own fixed text; the provider body never enters it.
+ """
+
+ def __init__(self, message: str, *, status_code: int) -> None:
+ super().__init__(message)
+ self.status_code = status_code
+
+
class ConnectedRestClient:
"""One pool per connected service; requests never follow credential redirects."""
@@ -183,17 +194,21 @@ class ConnectedRestClient:
"outcome may be unknown, check before retrying a write"
) from None
if response.status_code in {401, 403}:
- raise RuntimeError(
+ raise ProviderHttpError(
f"{self.plugin_id}: HTTP {response.status_code}; "
- "reconnect in Plugins or check account permissions"
+ "reconnect in Plugins or check account permissions",
+ status_code=response.status_code,
)
if response.status_code == 429:
- raise RuntimeError(f"{self.plugin_id}: provider rate limit; retry later")
+ raise ProviderHttpError(
+ f"{self.plugin_id}: provider rate limit; retry later", status_code=429
+ )
if not 200 <= response.status_code < 300:
# Provider bodies may echo submitted secrets; never propagate them.
- raise RuntimeError(
+ raise ProviderHttpError(
f"{self.plugin_id}: HTTP {response.status_code}; "
- "check resource IDs and request fields"
+ "check resource IDs and request fields",
+ status_code=response.status_code,
)
if response.status_code == 204 or not response.content:
return {"accepted": True, "http_status": response.status_code}
diff --git a/dodaios/ui/web/marketplace_routes.py b/dodaios/ui/web/marketplace_routes.py
index 5e9fbcf1d9..489e4b2609 100644
--- a/dodaios/ui/web/marketplace_routes.py
+++ b/dodaios/ui/web/marketplace_routes.py
@@ -23,7 +23,7 @@ import asyncio
import json
import logging
import time
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
@@ -160,6 +160,12 @@ class _PluginStatusMeta:
# When the provider itself ends the refresh grant (Google: an OAuth app in
# Testing status). ``None`` when the provider stated no fixed lifetime.
refresh_expires_at: str | None = None
+ # Capability health, separate from auth health: "verified" when the
+ # connect-time resource check passed, "degraded" when the grant is valid
+ # but the check failed (``capability_reason`` says why, as one of the
+ # CAPABILITY_* codes). ``None`` for a grant that was never checked.
+ capability_state: str | None = None
+ capability_reason: str | None = None
def _plugin_status_meta(plugin_id: str, store: TokenStore) -> _PluginStatusMeta:
@@ -189,6 +195,8 @@ def _plugin_status_meta(plugin_id: str, store: TokenStore) -> _PluginStatusMeta:
tokens.reauth_at.isoformat() if tokens.needs_reauth and tokens.reauth_at else None
),
refresh_expires_at=tokens.extra.get("refresh_expires_at") or None,
+ capability_state=tokens.extra.get(_CAPABILITY_STATE_KEY) or None,
+ capability_reason=tokens.extra.get(_CAPABILITY_REASON_KEY) or None,
)
@@ -409,6 +417,8 @@ async def list_plugins(response: Response) -> dict[str, Any]:
item["reauth_reason"] = meta.reauth_reason
item["reauth_at"] = meta.reauth_at
item["refresh_expires_at"] = meta.refresh_expires_at
+ item["capability_state"] = meta.capability_state
+ item["capability_reason"] = meta.capability_reason
item["unavailable_reason"] = None
item["unavailable_reason_code"] = ""
if spec.id == "amd_gpu" and spec.auth.mode == "local":
@@ -701,6 +711,104 @@ class BrowserConnectBody(BaseModel):
instance_url: str | None = None
+# Where a grant's capability health lives inside ``Tokens.extra``.
+_CAPABILITY_STATE_KEY = "capability_state"
+_CAPABILITY_REASON_KEY = "capability_reason"
+CAPABILITY_VERIFIED = "verified"
+CAPABILITY_DEGRADED = "degraded"
+
+
+def _roll_back_rejected_grant(
+ store: TokenStore, plugin_id: str, granted: Tokens, previous: Tokens | None
+) -> None:
+ """Undo the save of a grant the provider refused, leaving the prior state.
+
+ Only when the store still holds exactly that grant: a reconnect or
+ disconnect that ran meanwhile wins and is never overwritten.
+ """
+ try:
+ if previous is not None:
+ store.compare_and_save(plugin_id, granted, previous)
+ elif store.load(plugin_id) == granted:
+ store.delete(plugin_id)
+ except Exception as exc: # noqa: BLE001 - the error is reported either way
+ log.warning("plugin %s rejected-grant rollback failed (%s)", plugin_id, type(exc).__name__)
+
+
+async def _persist_grant_then_check(plugin_id: str, spec: Any, granted: Tokens) -> FlowResult:
+ """Store a completed OAuth grant FIRST, then check what it can reach.
+
+ Auth health and capability health are separate. The grant is saved before
+ the resource check runs, so an outage, a missing scope or a crash during
+ the check never throws away a consent the user already gave in the
+ browser. Only a provider that refuses the credential itself (HTTP 401)
+ undoes the save and reports an error, exactly as before. Any other failed
+ check leaves the plugin connected with ``capability_state="degraded"``.
+ """
+ store = TokenStore()
+ try:
+ previous = store.load(plugin_id)
+ except RuntimeError:
+ # An unreadable entry is replaced by the new grant anyway; a later
+ # rejection then removes it instead of restoring the unreadable blob.
+ log.info("plugin %s previous grant unreadable before save", plugin_id)
+ previous = None
+ # Persist BEFORE publishing the result: connect_poll reads `slot.result`
+ # to decide "connected" vs "pending", so a result set before the save
+ # landed let a poll (or a crash right after) report success for a token
+ # that was never written to the store.
+ try:
+ store.save(plugin_id, granted)
+ except Exception as exc: # noqa: BLE001
+ log.warning("plugin %s token save failed (%s)", plugin_id, type(exc).__name__)
+ return FlowResult(
+ tokens=None,
+ error="The connection could not be saved. Try again.",
+ error_code=ERROR_UNKNOWN,
+ )
+
+ failure: ConnectionVerificationError | None = None
+ try:
+ await verify_connection(spec, granted)
+ except ConnectionVerificationError as exc: # classified and reported just below
+ failure = exc
+
+ if failure is not None and failure.rejected:
+ log.info("plugin %s authorization rejected at verification", plugin_id)
+ _roll_back_rejected_grant(store, plugin_id, granted, previous)
+ return FlowResult(tokens=None, error=str(failure), error_code="provider_unreachable")
+
+ if failure is None:
+ health = {_CAPABILITY_STATE_KEY: CAPABILITY_VERIFIED}
+ else:
+ log.info("plugin %s connected, capability degraded (%s)", plugin_id, failure.reason)
+ health = {
+ _CAPABILITY_STATE_KEY: CAPABILITY_DEGRADED,
+ _CAPABILITY_REASON_KEY: failure.reason,
+ }
+ extra = {
+ key: value
+ for key, value in granted.extra.items()
+ if key not in (_CAPABILITY_STATE_KEY, _CAPABILITY_REASON_KEY)
+ }
+ checked = replace(granted, extra={**extra, **health})
+ try:
+ if not store.compare_and_save(plugin_id, granted, checked):
+ # A reconnect or disconnect won the race; report this grant as
+ # saved but leave the newer state alone.
+ checked = granted
+ except Exception as exc: # noqa: BLE001 - the grant itself is already stored
+ log.warning("plugin %s health save failed (%s)", plugin_id, type(exc).__name__)
+ checked = granted
+ _refresh_plugin_in_live_registry(plugin_id)
+ log.info("plugin %s connected via browser authorization", plugin_id)
+ return FlowResult(
+ tokens=checked,
+ error=None,
+ extra={"capability_message": str(failure)} if failure is not None else {},
+ )
+
+
@router.post("/plugins/{plugin_id}/connect/start")
async def connect_start(
plugin_id: str,
@@ -876,34 +984,12 @@ async def connect_start(
log.info("plugin %s connect flow cancelled", plugin_id)
return
if result.tokens is not None:
- try:
- await verify_connection(spec, result.tokens)
- except ConnectionVerificationError as exc:
- log.info("plugin %s resource verification failed", plugin_id)
- if registry.get(session.flow_id) is slot:
- slot.result = FlowResult(
- tokens=None, error=str(exc), error_code="provider_unreachable"
- )
- return
+ # The grant is stored before its resource check runs. A
+ # dialog closed DURING the check therefore keeps the consent
+ # the user already gave; only the result is no longer shown.
+ result = await _persist_grant_then_check(plugin_id, spec, result.tokens)
if registry.get(session.flow_id) is not slot:
return
- # Persist BEFORE publishing the result: connect_poll reads
- # `slot.result` to decide "connected" vs "pending", so setting
- # it before the save actually lands let a poll (or a crash
- # right after) report success for a token that was never
- # written to the store.
- try:
- TokenStore().save(plugin_id, result.tokens)
- except Exception as exc: # noqa: BLE001
- log.warning("plugin %s token save failed (%s)", plugin_id, type(exc).__name__)
- slot.result = FlowResult(
- tokens=None,
- error="The connection could not be saved. Try again.",
- error_code=ERROR_UNKNOWN,
- )
- return
- _refresh_plugin_in_live_registry(plugin_id)
- log.info("plugin %s connected via DCR", plugin_id)
slot.result = result
asyncio.create_task(_drive(), name=f"oauth-drive:{plugin_id}:{session.flow_id}")
@@ -972,7 +1058,17 @@ async def connect_poll(plugin_id: str, flow_id: str) -> dict[str, Any]:
}
registry.drop(flow_id)
- return {"state": "connected", "flow_id": flow_id, "plugin_id": plugin_id}
+ extra = slot.result.tokens.extra
+ return {
+ "state": "connected",
+ "flow_id": flow_id,
+ "plugin_id": plugin_id,
+ # Capability health beside the auth outcome: a valid grant whose
+ # resource check failed is connected but "degraded", never dropped.
+ "capability_state": extra.get(_CAPABILITY_STATE_KEY) or None,
+ "capability_reason": extra.get(_CAPABILITY_REASON_KEY) or None,
+ "capability_message": slot.result.extra.get("capability_message") or None,
+ }
# ----------------------------------------------------------------------
diff --git a/tests/unit/marketplace/test_connect_start_redirect_uri.py b/tests/unit/marketplace/test_connect_start_redirect_uri.py
index 56df8e083a..82b0e52eec 100644
--- a/tests/unit/marketplace/test_connect_start_redirect_uri.py
+++ b/tests/unit/marketplace/test_connect_start_redirect_uri.py
@@ -21,7 +21,7 @@ from dodaios.marketplace.auth.oauth_pkce_loopback import (
PkceLoopbackConfig,
PkceLoopbackHandler,
)
-from dodaios.marketplace.token_store import Tokens
+from dodaios.marketplace.token_store import InMemoryBackend, Tokens, TokenStore
from dodaios.ui.web import marketplace_routes as mr
@@ -112,7 +112,8 @@ async def test_connect_start_response_carries_redirect_uri(monkeypatch) -> None:
monkeypatch.setattr(mr.asyncio, "create_task", _capturing)
monkeypatch.setattr(mr, "verify_connection", _verified)
- monkeypatch.setattr(mr.TokenStore, "save", lambda self, plugin_id, tokens: None)
+ backend = InMemoryBackend()
+ monkeypatch.setattr(mr, "TokenStore", lambda: TokenStore(backend=backend))
monkeypatch.setattr(mr, "_refresh_plugin_in_live_registry", lambda plugin_id: None)
spec = _pkce_spec("real-client-id.apps.example")
monkeypatch.setattr(mr, "load_catalog", lambda: _Catalog([spec]))
diff --git a/tests/unit/marketplace/test_connect_token_save_failure.py b/tests/unit/marketplace/test_connect_token_save_failure.py
index 99b0ce8341..58ce6860a0 100644
--- a/tests/unit/marketplace/test_connect_token_save_failure.py
+++ b/tests/unit/marketplace/test_connect_token_save_failure.py
@@ -1,34 +1,47 @@
-"""connect_start's background _drive() must not report "connected" before the
-token actually landed in the TokenStore.
-
-Previously `slot.result` (what `connect_poll` reads) was set to the
-successful FlowResult BEFORE `TokenStore().save()` ran, so a save failure
-(or a crash right after `await_completion`) still let the poll endpoint
-report a successful connect for a token that was never persisted.
+"""connect_start's background _drive() persists a grant before it checks it.
+
+Two orderings matter here:
+
+* `connect_poll` must never report "connected" before the token actually
+ landed in the TokenStore (a save failure, or a crash right after
+ `await_completion`, once let the poll report a token that was never stored).
+* A completed browser consent is stored BEFORE the resource check runs. A
+ check that fails for any reason other than the provider refusing the
+ credential (a missing scope, an outage, an empty tool list) leaves the
+ plugin connected with a degraded capability, instead of throwing the grant
+ away and sending the user through the browser again. A credential the
+ provider rejects (HTTP 401) is still an error and leaves the store as it was.
+
+Every connect flow (PKCE loopback, DCR, device) shares this drive, so each
+one is exercised with a fake handler.
"""
from __future__ import annotations
import asyncio
+import httpx
import pytest
-from fastapi import BackgroundTasks
+from fastapi import BackgroundTasks, Response
+from dodaios.marketplace import connection_verification as verification
from dodaios.marketplace.auth.base import AuthSession, FlowResult
-from dodaios.marketplace.token_store import Tokens
+from dodaios.marketplace.token_store import InMemoryBackend, Tokens, TokenStore
from dodaios.ui.web import marketplace_routes as mr
+# Captured before any test patches `mr.load_catalog`.
+_load_shipped_catalog = mr.load_catalog
-class _Catalog:
- def __init__(self, specs):
- self.plugins = specs
- def by_id(self, plugin_id: str):
- return next((s for s in self.plugins if s.id == plugin_id), None)
+def _Catalog(specs): # noqa: N802 - reads like the catalog it builds
+ """The real shipped catalog, narrowed to the plugins under test."""
+ return _load_shipped_catalog().model_copy(update={"plugins": list(specs)})
-def _pkce_spec(client_id: str):
- for spec in mr.load_catalog().plugins:
+def _pkce_spec(client_id: str, plugin_id: str | None = None):
+ for spec in _load_shipped_catalog().plugins:
+ if plugin_id is not None and spec.id != plugin_id:
+ continue
if spec.auth is not None and getattr(spec.auth, "mode", "") == "oauth_pkce_loopback":
return spec.model_copy(
update={"auth": spec.auth.model_copy(update={"client_id": client_id})}
@@ -36,13 +49,23 @@ def _pkce_spec(client_id: str):
pytest.skip("no oauth_pkce_loopback plugin in catalog")
-class _StubPkceHandler:
- """Stands in for the real PkceLoopbackHandler: returns a session
- immediately and a completed FlowResult carrying tokens, without ever
- touching a socket or a real OAuth provider."""
+def _spec_for(flow: str):
+ if flow == "pkce":
+ return _pkce_spec("real-client-id.apps.example")
+ if flow == "dcr":
+ return _load_shipped_catalog().by_id("notion")
+ spec = _load_shipped_catalog().by_id("github")
+ assert spec.auth.mode == "oauth_device_flow"
+ return spec
+
- def __init__(self, config) -> None:
- self.config = config
+class _FakeHandler:
+ """Stands in for a real auth handler: returns a session immediately and a
+ completed FlowResult carrying tokens, without ever touching a socket or a
+ real OAuth provider."""
+
+ def __init__(self, *args, **kwargs) -> None:
+ self.args = args
async def start(self, plugin_spec) -> AuthSession:
return AuthSession(
@@ -52,7 +75,16 @@ class _StubPkceHandler:
)
async def await_completion(self, session: AuthSession) -> FlowResult:
- return FlowResult(tokens=Tokens(access="tok-123"), error=None)
+ return FlowResult(tokens=Tokens(access="tok-123", refresh="ref-1"), error=None)
+
+
+def store(monkeypatch) -> TokenStore:
+ """One in-memory token store shared by every `TokenStore()` the routes open."""
+ backend = InMemoryBackend()
+ monkeypatch.setattr(mr, "TokenStore", lambda: TokenStore(backend=backend))
+ monkeypatch.setattr(mr, "_refresh_plugin_in_live_registry", lambda plugin_id: None)
+ return TokenStore(backend=backend)
@pytest.fixture
@@ -71,22 +103,32 @@ def _capture_background_tasks(monkeypatch):
return captured
+def _verifier(failure: verification.ConnectionVerificationError | None, seen: list | None = None):
+ async def verify(spec, tokens):
+ if seen is not None:
+ seen.append(tokens)
+ if failure is not None:
+ raise failure
+
+ return verify
+
+
async def _start_and_drain(
- monkeypatch, capture_background_tasks, *, verification_error=False
+ monkeypatch, capture_background_tasks, *, flow: str = "pkce", spec=None
) -> tuple[str, str]:
- async def verified(spec, tokens):
- if verification_error:
- raise mr.ConnectionVerificationError("Provider resource access was denied.")
- return None
-
- monkeypatch.setattr(mr, "verify_connection", verified)
- spec = _pkce_spec("real-client-id.apps.example")
+ spec = spec if spec is not None else _spec_for(flow)
monkeypatch.setattr(mr, "load_catalog", lambda: _Catalog([spec]))
monkeypatch.setattr(
"dodaios.marketplace.connect_helpers.resolve_pkce_client",
lambda pid, cid, csec: ("real-client-id.apps.example", None),
)
- monkeypatch.setattr(mr, "PkceLoopbackHandler", _StubPkceHandler)
+ monkeypatch.setattr(
+ "dodaios.marketplace.publisher_clients.resolve_publisher_client",
+ lambda pid, cid, csec: ("real-device-client", None, "publisher"),
+ )
+ monkeypatch.setattr(mr, "PkceLoopbackHandler", _FakeHandler)
+ monkeypatch.setattr(mr, "DeviceFlowHandler", _FakeHandler)
+ monkeypatch.setattr(mr, "_build_dcr_handler", lambda *a: _FakeHandler())
session = await mr.connect_start(spec.id, BackgroundTasks())
for task in capture_background_tasks:
@@ -96,12 +138,13 @@ async def _start_and_drain(
@pytest.mark.asyncio
async def test_connect_poll_reports_error_when_token_save_fails(
- monkeypatch, _capture_background_tasks
+ monkeypatch, store, _capture_background_tasks
) -> None:
def _raising_save(self, plugin_id, tokens) -> None:
raise RuntimeError("simulated keyring save failure")
- monkeypatch.setattr(mr.TokenStore, "save", _raising_save)
+ monkeypatch.setattr(mr, "verify_connection", _verifier(None))
+ monkeypatch.setattr(TokenStore, "save", _raising_save)
plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks)
@@ -110,48 +153,170 @@ async def test_connect_poll_reports_error_when_token_save_fails(
@pytest.mark.asyncio
-async def test_connect_poll_reports_connected_when_save_succeeds(
- monkeypatch, _capture_background_tasks
[email protected]("flow", ["pkce", "dcr", "device"])
+async def test_verified_grant_is_saved_and_reported_connected(
+ monkeypatch, store, _capture_background_tasks, flow
) -> None:
- saved: dict[str, Tokens] = {}
+ monkeypatch.setattr(mr, "verify_connection", _verifier(None))
- def _recording_save(self, plugin_id, tokens) -> None:
- saved[plugin_id] = tokens
+ plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks, flow=flow)
- monkeypatch.setattr(mr.TokenStore, "save", _recording_save)
- monkeypatch.setattr(mr, "_refresh_plugin_in_live_registry", lambda plugin_id: None)
+ result = await mr.connect_poll(plugin_id, flow_id)
+ assert result["state"] == "connected"
+ assert result["capability_state"] == "verified"
+ saved = store.load(plugin_id)
+ assert saved is not None and saved.access == "tok-123"
+ assert saved.extra["capability_state"] == "verified"
- plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks)
[email protected]("flow", ["pkce", "dcr", "device"])
+ "reason", ["forbidden", "rate_limited", "unavailable"], ids=["403", "429", "outage"]
+)
+async def test_failed_capability_check_keeps_the_grant_connected_but_degraded(
+ monkeypatch, store, _capture_background_tasks, flow, reason
+) -> None:
+ """The bug: a capability/health probe failure threw away a valid grant."""
+ stored_during_check: list[Tokens | None] = []
+
+ async def verify(spec, tokens):
+ # The grant is already durable while the check runs: a crash or an
+ # outage here can no longer lose the consent.
+ stored_during_check.append(store.load(spec.id))
+ raise verification.ConnectionVerificationError(
+ "The provider connection check failed. Check account access and try again.",
+ reason=reason,
+ )
+
+ monkeypatch.setattr(mr, "verify_connection", verify)
+
+ plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks, flow=flow)
+
+ assert stored_during_check[0] is not None
+ assert stored_during_check[0].access == "tok-123"
result = await mr.connect_poll(plugin_id, flow_id)
assert result["state"] == "connected"
- assert saved # the token was actually persisted before the state flipped
+ assert result["capability_state"] == "degraded"
+ assert result["capability_reason"] == reason
+ assert result["capability_message"].startswith("The provider connection check failed")
+ saved = store.load(plugin_id)
+ assert saved is not None
+ assert saved.access == "tok-123" and saved.refresh == "ref-1"
+ assert saved.needs_reauth is False
+ assert saved.extra["capability_state"] == "degraded"
+
+ listing = await mr.list_plugins(Response())
+ item = next(p for p in listing["plugins"] if p["id"] == plugin_id)
+ assert item["status"] == "connected"
+ assert item["capability_state"] == "degraded"
+ assert item["capability_reason"] == reason
@pytest.mark.asyncio
-async def test_failed_resource_verification_never_saves_or_reports_connected(
- monkeypatch, _capture_background_tasks
-):
- saved = []
- monkeypatch.setattr(mr.TokenStore, "save", lambda *args: saved.append(args))
- plugin_id, flow_id = await _start_and_drain(
- monkeypatch, _capture_background_tasks, verification_error=True
[email protected]("flow", ["pkce", "dcr", "device"])
+async def test_rejected_grant_is_not_kept_and_reports_error(
+ monkeypatch, store, _capture_background_tasks, flow
+) -> None:
+ """A credential the provider refuses (401 at verify) is handled as before."""
+ failure = verification.ConnectionVerificationError(
+ "The provider rejected the authorization. Connect again.", reason="unauthorized"
)
+ monkeypatch.setattr(mr, "verify_connection", _verifier(failure))
+
+ plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks, flow=flow)
+
result = await mr.connect_poll(plugin_id, flow_id)
assert result["state"] == "error"
- assert result["error"] == "Provider resource access was denied."
- assert saved == []
+ assert result["error"] == "The provider rejected the authorization. Connect again."
+ assert result["error_code"] == "provider_unreachable"
+ assert store.load(plugin_id) is None
+
+
+async def test_real_401_from_the_provider_rejects_and_restores_the_previous_grant(
+ monkeypatch, store, _capture_background_tasks
+) -> None:
+ """End to end through the real verifier: Google Drive answers 401."""
+ spec = _pkce_spec("real-client-id.apps.example", plugin_id="google_drive")
+ previous = Tokens(access="old", needs_reauth=True, reauth_reason="provider_rejected")
+ store.save(spec.id, previous)
+ original = httpx.AsyncClient
+ requests: list[httpx.Request] = []
+
+ def handle(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ return httpx.Response(401, json={"error": "private-provider-token"})
+
+ monkeypatch.setattr(
+ verification.httpx,
+ "AsyncClient",
+ lambda **kwargs: original(**kwargs, transport=httpx.MockTransport(handle)),
+ )
+
+ plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks, spec=spec)
+
+ result = await mr.connect_poll(plugin_id, flow_id)
+ assert result["state"] == "error"
+ assert "private-provider-token" not in result["error"]
+ assert requests and requests[0].headers["Authorization"] == "Bearer tok-123"
+ # The reconnect that was refused leaves the earlier grant exactly as it was.
+ assert store.load(plugin_id) == previous
+
+
+async def test_real_403_from_the_provider_keeps_the_grant_degraded(
+ monkeypatch, store, _capture_background_tasks
+) -> None:
+ """End to end through the real verifier: signed in, but a scope is missing."""
+ spec = _pkce_spec("real-client-id.apps.example", plugin_id="google_drive")
+ original = httpx.AsyncClient
+
+ monkeypatch.setattr(
+ verification.httpx,
+ "AsyncClient",
+ lambda **kwargs: original(
+ **kwargs,
+ transport=httpx.MockTransport(lambda request: httpx.Response(403, json={})),
+ ),
+ )
+
+ plugin_id, flow_id = await _start_and_drain(monkeypatch, _capture_background_tasks, spec=spec)
+
+ result = await mr.connect_poll(plugin_id, flow_id)
+ assert result["state"] == "connected"
+ assert result["capability_state"] == "degraded"
+ assert result["capability_reason"] == "forbidden"
+ assert store.load(plugin_id).access == "tok-123"
+
+
+async def test_reconnect_during_the_check_is_not_overwritten(
+ monkeypatch, store, _capture_background_tasks
+) -> None:
+ """A newer grant saved while the check ran wins over the health update."""
+ newer = Tokens(access="newer")
+
+ async def verify(spec, tokens):
+ store.save(spec.id, newer)
+ raise verification.ConnectionVerificationError("x", reason="unauthorized")
+
+ monkeypatch.setattr(mr, "verify_connection", verify)
+
+ plugin_id, _ = await _start_and_drain(monkeypatch, _capture_background_tasks)
+
+ assert store.load(plugin_id) == newer
@pytest.mark.asyncio
async def test_cancelled_reconnect_does_not_replace_needs_reauth_grant(
- monkeypatch, _capture_background_tasks
+ monkeypatch, store, _capture_background_tasks
) -> None:
release = asyncio.Event()
cleanup_started = asyncio.Event()
finish_cleanup = asyncio.Event()
- class _DelayedPkceHandler(_StubPkceHandler):
+ class _DelayedPkceHandler(_FakeHandler):
async def await_completion(self, session: AuthSession) -> FlowResult:
await release.wait()
return FlowResult(tokens=Tokens(access="replacement"), error=None)
@@ -161,11 +326,9 @@ async def test_cancelled_reconnect_does_not_replace_needs_reauth_grant(
await finish_cleanup.wait()
spec = _pkce_spec("real-client-id.apps.example")
- store: dict[str, Tokens] = {spec.id: Tokens(access="expired", needs_reauth=True)}
-
- class _Store:
- def save(self, plugin_id: str, tokens: Tokens) -> None:
- store[plugin_id] = tokens
+ store.save(spec.id, Tokens(access="expired", needs_reauth=True))
+ checked: list[Tokens] = []
+ monkeypatch.setattr(mr, "verify_connection", _verifier(None, checked))
monkeypatch.setattr(mr, "load_catalog", lambda: _Catalog([spec]))
monkeypatch.setattr(
@@ -173,7 +336,6 @@ async def test_cancelled_reconnect_does_not_replace_needs_reauth_grant(
lambda pid, cid, csec: ("real-client-id.apps.example", None),
)
monkeypatch.setattr(mr, "PkceLoopbackHandler", _DelayedPkceHandler)
- monkeypatch.setattr(mr, "TokenStore", _Store)
session = await mr.connect_start(spec.id, BackgroundTasks())
cancel_task = asyncio.create_task(mr.cancel_connect(spec.id, session["flow_id"]))
@@ -186,5 +348,7 @@ async def test_cancelled_reconnect_does_not_replace_needs_reauth_grant(
finish_cleanup.set()
result = await cancel_task
assert result["state"] == "cancelled"
- assert store[spec.id].access == "expired"
- assert store[spec.id].needs_reauth is True
+ saved = store.load(spec.id)
+ assert saved.access == "expired"
+ assert saved.needs_reauth is True
+ assert checked == []
diff --git a/tests/unit/marketplace/test_connection_verification.py b/tests/unit/marketplace/test_connection_verification.py
index 34965e2817..7bd692ced2 100644
--- a/tests/unit/marketplace/test_connection_verification.py
+++ b/tests/unit/marketplace/test_connection_verification.py
@@ -1,4 +1,4 @@
-"""Resource verification rejects unusable grants without saving credentials."""
+"""Resource verification classifies failures; it never saves credentials."""
import asyncio
@@ -217,3 +217,119 @@ def test_both_mcp_bridges_carry_figma_auth_mode(oauth):
catalog = PluginCatalog(version=1, schema_version="1", plugins=[spec])
entry = assemble_claude_mcp_servers(catalog, Store())["figma"]
assert entry["env"]["JARVIS_CONNECTOR_AUTH_TYPE"] == expected
+
+
+def _fake_http(monkeypatch, status, payload):
+ original = httpx.AsyncClient
+ monkeypatch.setattr(
+ verification.httpx,
+ "AsyncClient",
+ lambda **kwargs: original(
+ **kwargs,
+ transport=httpx.MockTransport(lambda request: httpx.Response(status, json=payload)),
+ ),
+ )
+
+
+ "status,reason,rejected",
+ [
+ (401, "unauthorized", True),
+ (403, "forbidden", False),
+ (429, "rate_limited", False),
+ (503, "unavailable", False),
+ ],
+)
+async def test_native_failure_is_classified_auth_vs_capability(
+ monkeypatch, status, reason, rejected
+):
+ """Only a refused credential rejects the grant; the rest is degraded capability."""
+ _fake_http(monkeypatch, status, {"error": "private-provider-token"})
+ with pytest.raises(verification.ConnectionVerificationError) as caught:
+ await verification.verify_connection(plugin("google_drive"), Tokens(access="t"))
+ assert caught.value.reason == reason
+ assert caught.value.rejected is rejected
+ assert "private-provider-token" not in str(caught.value)
+
+
+ "error,rejected", [("invalid_auth", True), ("token_revoked", True), ("ratelimited", False)]
+)
+async def test_slack_rejection_codes_are_auth_failures(monkeypatch, error, rejected):
+ _fake_http(monkeypatch, 200, {"ok": False, "error": error})
+ with pytest.raises(verification.ConnectionVerificationError) as caught:
+ await verification.verify_connection(plugin("slack"), Tokens(access="t"))
+ assert caught.value.rejected is rejected
+ assert error not in str(caught.value)
+
+
[email protected]("status,rejected", [(401, True), (403, False)])
+async def test_rest_probe_status_is_classified(monkeypatch, status, rejected):
+ from dodaios.plugins.tool import connected_server
+
+ original = connected_server.ConnectedRestClient
+ monkeypatch.setattr(
+ connected_server,
+ "ConnectedRestClient",
+ lambda *a, **k: original(
+ *a,
+ **k,
+ transport=httpx.MockTransport(lambda request: httpx.Response(status, json={})),
+ ),
+ )
+ with pytest.raises(verification.ConnectionVerificationError) as caught:
+ await verification.verify_connection(plugin("outlook"), Tokens(access="t"))
+ assert caught.value.rejected is rejected
+
+
+class _GroupedStatusMcp(FakeMcp):
+ """An MCP transport that fails inside a task group, as AnyIO reports it."""
+
+ def __init__(self, status):
+ super().__init__()
+ self.status = status
+
+ async def start(self):
+ request = httpx.Request("POST", "https://mcp.example/mcp")
+ error = httpx.HTTPStatusError(
+ "private-provider-token",
+ request=request,
+ response=httpx.Response(self.status, request=request),
+ )
+ raise ExceptionGroup("transport", [error])
+
+
[email protected]("status,rejected", [(401, True), (500, False)])
+async def test_mcp_status_inside_an_exception_group_is_classified(monkeypatch, status, rejected):
+ from dodaios.marketplace import plugin_registry
+
+ client = _GroupedStatusMcp(status)
+ monkeypatch.setattr(plugin_registry, "_default_client_factory", lambda *a, **k: client)
+ with pytest.raises(verification.ConnectionVerificationError) as caught:
+ await verification.verify_connection(plugin("notion"), Tokens(access="t"))
+ assert caught.value.rejected is rejected
+ assert "private-provider-token" not in str(caught.value)
+ assert client.stopped
+
+
+async def test_missing_or_flagged_token_is_an_auth_failure():
+ with pytest.raises(verification.ConnectionVerificationError) as caught:
+ await verification.verify_connection(plugin("gmail"), Tokens(access="t", needs_reauth=True))
+ assert caught.value.rejected
+
+
+async def test_unusable_tool_list_is_a_capability_failure(monkeypatch):
+ from dodaios.marketplace import plugin_registry
+
+ monkeypatch.setattr(
+ plugin_registry, "_default_client_factory", lambda *a, **k: FakeMcp(tools=[])
+ )
+ with pytest.raises(verification.ConnectionVerificationError) as caught:
+ await verification.verify_connection(plugin("notion"), Tokens(access="t"))
+ assert caught.value.rejected is False