Files
hermes-agent/tools/managed_gateway_auth.py
alt-glitch dd478aa3f3 refactor(connectors-mcp): delete unreached connector and MCP OAuth code
Removed. A search for callers by name, attribute and string (Python, TypeScript,
docs, the plugin API list and the frozen old-updater list) finds no supported
path that reaches any of them:
- tools/openrouter_client.py (whole module; check_api_key had test callers only)
- tools/connectors/gateway/__init__.py: re-exports and __all__ (every importer names a submodule)
- tools/connectors/gateway/wire.py::WIRE_BATCH_MAX
- tools/managed_gateway_auth.py::managed_gateway_origin
- tools/discord_tool.py::_detect_capabilities
- tools/mcp_tool_health.py::MCPServerHealthMixin.ensure_healthy
- tools/mcp_oauth.py::build_oauth_auth, HermesOAuthClientProvider, _oauth_port
- tools/mcp_dashboard_oauth.py::DashboardOAuthFlow.profile (write-only field)
- tools/mcp_tool_events.py::EVENT_UTILITY_KEYS
- agent/tool_guardrails.py: mcp_filesystem_* entries in IDEMPOTENT_TOOL_NAMES
  (MCP tools register as mcp__<server>__<tool>)

Tests that covered live code through a deleted symbol now call the live path
(MCPOAuthManager().get_or_build_provider, _fetch_capabilities,
build_vendor_gateway_url); tests that only exercised deleted code are removed.

Kept on purpose: _SSH_HINT_PROXY, its branch, the redirect_uri parameters and
build_provider_kwargs' ssh_proxy_hint. Nothing reaches them today, but they are the
proxy-user hint from d0afcb125ce; wiring it into MCPOAuthManager is a separate fix.

test_cache_is_keyed_by_token now covers _detect_capabilities_nonblocking (two tokens,
memory and disk). ensure_healthy leaves the SuspectableBackend Protocol, which no
backend implements any more.
2026-11-16 15:39:10 +05:30

76 lines
2.8 KiB
Python

"""First-party gateway bearer trust, ported from connectors-only 43608084f4."""
import logging
from typing import Callable, Optional
from urllib.parse import urlsplit
from tools.managed_tool_gateway import build_vendor_gateway_url, read_nous_access_token
logger = logging.getLogger(__name__)
def connector_gateway_origin() -> str:
"""Separate connector deployment; honors CONNECTOR_GATEWAY_URL."""
return build_vendor_gateway_url("connector")
def is_managed_nous_gateway_url(
url: object,
gateway_builder: Optional[Callable[[str], str]] = None,
) -> bool:
"""True when ``url`` is on one of the first-party gateway origins we build.
Both first-party surfaces count: the media/on-origin-vendor host
(``build_vendor_gateway_url("tool")``) and the connectors host
(:func:`connector_gateway_origin`). Each is compared as an exact
``(scheme, netloc)`` pair — never as a name or a domain suffix — so
``evil-connector-gateway.nousresearch.com.attacker.dev`` and an ``http``
downgrade of a real host both stay outside the set.
Anything granting a URL extra trust — our bearer, reading files off disk to
upload — must gate on this, so an arbitrary URL can never inherit it.
"""
if not isinstance(url, str) or not url.strip():
return False
build_origin = gateway_builder or build_vendor_gateway_url
try:
expected = {
urlsplit(build_origin(label))[:2]
for label in ("tool", "connector")
}
actual = urlsplit(url.strip())
except ValueError:
return False
return bool(actual.scheme) and (actual.scheme, actual.netloc) in expected
def managed_gateway_auth_headers(
url: object,
gateway_builder: Optional[Callable[[str], str]] = None,
token_reader: Optional[Callable[[], Optional[str]]] = None,
) -> dict:
"""Live auth headers for a managed gateway URL, or ``{}`` when not managed.
Read fresh on every call rather than cached: a Nous access token expires
within the hour, and a long session would otherwise keep presenting a dead
bearer. Returns ``{}`` rather than raising when no token is available, so a
caller can report "sign in" instead of sending an unauthenticated request.
"""
if not is_managed_nous_gateway_url(url, gateway_builder):
return {}
resolved_token_reader = token_reader or read_nous_access_token
try:
token = resolved_token_reader()
except Exception as exc: # pragma: no cover — defensive
logger.debug("Managed gateway token read failed for %s: %s", url, exc)
return {}
if not isinstance(token, str) or not token.strip():
return {}
from tools.managed_gateway_identity import user_agent
return {"Authorization": f"Bearer {token.strip()}", "User-Agent": user_agent()}