The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(mcp): shared OAuth token foundation - challenge, store seam, expiry-aware cache, single-flight refresh (#31275)
tin-berri2e69708ef85c
diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.pyindex 39db2314aee..91876df762f 100644--- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py@@ -126,7 +126,16 @@ def raise_public(error: CredError) -> NoReturn:"""Map a resolver CredError onto the proxy's public HTTP contract. The one edge that raises."""match error.tag:case "unauthorized":- raise HTTPException(status_code=401, detail=error.summary)+ challenge = error.unauthorized+ raise HTTPException(+ status_code=401,+ detail=challenge.body if challenge.body is not None else error.summary,+ headers=(+ {"WWW-Authenticate": challenge.www_authenticate}+ if challenge.www_authenticate+ else None+ ),+ )case "misconfigured":raise HTTPException(status_code=500, detail=error.summary)case "upstream_unavailable":diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/oauth_token_store.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/oauth_token_store.pynew file mode 100644index 00000000000..1473d15da5e--- /dev/null+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/oauth_token_store.py@@ -0,0 +1,292 @@+"""Per-user OAuth token store for the ``authorization_code`` mode.++The resolver reads a user's token through the injected ``OAuthTokenStore`` seam;+``CachedOAuthTokenStore`` is an expiry-aware cache in front of it. ``TokenStoreUnavailable``+signals an unreachable backing store, so an outage is never cached or read as "not authorized".++``RefreshingTokenStore`` mints a fresh token through an injected ``TokenRefresher`` when the stored+one is near expiry, under in-process per-(user, server) single-flight so concurrent callers share+one refresh. Distributed (cross-replica) single-flight and reactive-401 refresh are the later+hardening. The mode plugs in its own source and refresher; the cache, store seam, and refresh+machinery are shared across the oauth2 modes (authorization_code / client_credentials /+token_exchange).+"""++from __future__ import annotations++import asyncio+import time+from collections.abc import Awaitable, Callable+from dataclasses import dataclass+from typing import Protocol+++@dataclass(frozen=True, slots=True, repr=False)+class OAuthToken:+ """A user's OAuth credential: the bearer value, when it expires, and how to refresh it.++ ``expires_at`` is epoch seconds (``None`` means no known expiry). ``refresh_token`` is what a+ ``TokenRefresher`` uses to mint a new access token when this one nears expiry (the refresh+ mechanism, ``RefreshingTokenStore``, is in this module; the concrete per-mode refresher lands+ with each mode); it is never minted into a header directly. ``repr`` masks both secrets so a+ stray log line cannot leak them (the values are still plain ``str`` for the header path, since+ ``SecretStr`` resolves as unknown under this repo's basedpyright).+ """++ access_token: str+ expires_at: float | None = None+ refresh_token: str | None = None++ def __repr__(self) -> str:+ has_refresh = self.refresh_token is not None+ return f"OAuthToken(access_token=***, expires_at={self.expires_at!r}, has_refresh_token={has_refresh})"+++class TokenStoreUnavailable(Exception):+ """Raised by ``fetch`` when the backing token store is unreachable (e.g. the DB is down).++ Distinct from returning ``None`` for "the user has not authorized this server": a read-through+ cache skips caching the failure, and the resolver maps it to its fail-closed status rather than+ treating an outage as a definite absence.+ """+++class OAuthTokenStore(Protocol):+ """Per-user OAuth token lookup for the ``authorization_code`` mode.++ Returns the user's token for an upstream, or ``None`` when they have not completed the OAuth+ flow (the arm turns that into a 401 challenge). The ``(user_id, server_id)`` pair fully scopes+ the lookup, so an implementation must never return one subject's token to another. Raises+ ``TokenStoreUnavailable`` when the backing store is unreachable, so an outage is never cached or+ read as a definite absence.+ """++ async def fetch(self, user_id: str, server_id: str) -> OAuthToken | None: ...+++class TokenRefresher(Protocol):+ """Mints a fresh token from an expired one and persists it, returning the new token.++ The action is mode-specific: the ``authorization_code`` refresh_token grant, the+ ``client_credentials`` grant, or an RFC 8693 re-exchange. Returns ``None`` when it cannot+ refresh (e.g. no ``refresh_token``), which the caller turns into a 401 challenge. It must+ persist the new token so later requests (and the surrounding cache) read it without refreshing.++ ``server_id`` selects the upstream's config (token endpoint, client credentials, scopes) the+ grant runs against; ``(user_id, server_id)`` is the key the new token is persisted under. They+ are not derivable from ``token``, so the seam threads them alongside it.+ """++ async def refresh(+ self, user_id: str, server_id: str, token: OAuthToken+ ) -> OAuthToken | None: ...+++class TokenCacheBackend(Protocol):+ """Storage behind ``CachedOAuthTokenStore``: hold a token under ``(user_id, server_id)`` for+ ``ttl_seconds``, then forget it. The default ``InMemoryTokenCacheBackend`` is per-process; a+ cross-replica deployment injects a shared (Redis) backend so every worker reads one refresh,+ matching v1. ``get`` returns ``None`` once the entry's TTL has elapsed.+ """++ async def get(self, user_id: str, server_id: str) -> OAuthToken | None: ...++ async def set(+ self, user_id: str, server_id: str, token: OAuthToken, ttl_seconds: float+ ) -> None: ...++ async def delete(self, user_id: str, server_id: str) -> None: ...+++class InMemoryTokenCacheBackend:+ """Per-process token cache: a bounded dict with wall-clock TTLs (the default backend)."""++ def __init__(+ self, *, max_size: int = 4096, clock: Callable[[], float] = time.time+ ) -> None:+ self._max_size = max_size+ self._clock = clock+ self._cache: dict[tuple[str, str], tuple[OAuthToken, float]] = {}++ async def get(self, user_id: str, server_id: str) -> OAuthToken | None:+ key = (user_id, server_id)+ hit = self._cache.get(key)+ if hit is None:+ return None+ token, valid_until = hit+ if self._clock() < valid_until:+ return token+ self._cache.pop(key, None)+ return None++ async def set(+ self, user_id: str, server_id: str, token: OAuthToken, ttl_seconds: float+ ) -> None:+ key = (user_id, server_id)+ if key not in self._cache and len(self._cache) >= self._max_size:+ # Evict the oldest entry (insertion order), rather than clearing the whole cache and+ # forcing every key to re-read the store at once.+ self._cache.pop(next(iter(self._cache)), None)+ self._cache[key] = (token, self._clock() + ttl_seconds)++ async def delete(self, user_id: str, server_id: str) -> None:+ self._cache.pop((user_id, server_id), None)+++class CachedOAuthTokenStore:+ """Expiry-aware cache over an ``OAuthTokenStore``. Caches positive tokens only.++ A cached token is served only while it is unexpired (minus ``expiry_skew_seconds``), or for+ ``default_ttl_seconds`` if it carries no expiry; past that the inner store is read again. A+ "not authorized" (``None``) result is never cached: every miss re-reads the inner store, so a+ token written after the OAuth flow is visible immediately on every replica, matching v1 (which+ never caches misses). The clock is injected (wall-clock, since ``expires_at`` is epoch) so+ expiry is deterministic in tests, and a store outage (``TokenStoreUnavailable``) propagates+ without being cached.+ """++ def __init__(+ self,+ inner: OAuthTokenStore,+ *,+ default_ttl_seconds: float,+ expiry_skew_seconds: float = 60.0,+ max_size: int = 4096,+ backend: TokenCacheBackend | None = None,+ clock: Callable[[], float] = time.time,+ ) -> None:+ self._inner = inner+ self._default_ttl_seconds = default_ttl_seconds+ self._expiry_skew_seconds = expiry_skew_seconds+ self._clock = clock+ self._backend: TokenCacheBackend = backend or InMemoryTokenCacheBackend(+ max_size=max_size, clock=clock+ )++ def _ttl(self, token: OAuthToken) -> float:+ if token.expires_at is not None:+ return max(+ 0.0, token.expires_at - self._expiry_skew_seconds - self._clock()+ )+ return self._default_ttl_seconds++ async def fetch(self, user_id: str, server_id: str) -> OAuthToken | None:+ hit = await self._backend.get(user_id, server_id)+ if hit is not None:+ return hit++ token = await self._inner.fetch(user_id, server_id)+ if token is None:+ # Never cache "not authorized": drop any stale entry and re-read on the next call, so+ # a token stored after the OAuth flow is seen immediately rather than after a TTL.+ await self._backend.delete(user_id, server_id)+ return token+ await self._backend.set(user_id, server_id, token, self._ttl(token))+ return token++ async def invalidate(self, user_id: str, server_id: str) -> None:+ """Drop a cached entry after the user (re)authorizes or revokes, so a stale token or a+ stale "not authorized" None cannot mask the change."""+ await self._backend.delete(user_id, server_id)+++class RefreshCoordinator(Protocol):+ """Ensures one refresh runs per ``(user_id, server_id)`` at a time. Concurrent callers either+ share the winner's result (the default ``InProcessRefreshCoordinator``) or, in a cross-replica+ coordinator, wait for the holder and ``reread`` the token it persisted - so the IdP sees one+ refresh per key across all workers, not one per worker.+ """++ async def run(+ self,+ user_id: str,+ server_id: str,+ refresh: Callable[[], Awaitable[OAuthToken | None]],+ reread: Callable[[], Awaitable[OAuthToken | None]],+ ) -> OAuthToken | None: ...+++class InProcessRefreshCoordinator:+ """Single-flight within one event loop (the default): the first caller per key refreshes while+ concurrent callers await the same in-flight task and share its result. ``reread`` is unused here -+ the shared task already yields the new token - and exists for the cross-replica coordinator, where+ losers re-read the persisted token instead of sharing an in-process future.+ """++ def __init__(self) -> None:+ # In-flight refreshes, one task per (user, server); each entry is removed by the task's+ # done-callback, so the map is bounded by concurrent refreshes, not by distinct keys seen.+ self._inflight: dict[tuple[str, str], asyncio.Future[OAuthToken | None]] = {}++ async def run(+ self,+ user_id: str,+ server_id: str,+ refresh: Callable[[], Awaitable[OAuthToken | None]],+ reread: Callable[[], Awaitable[OAuthToken | None]],+ ) -> OAuthToken | None:+ key = (user_id, server_id)+ task = self._inflight.get(key)+ if task is None:+ # The task is detached from the caller, so a cancelled caller does not abort the refresh.+ task = asyncio.ensure_future(refresh())+ self._inflight[key] = task+ task.add_done_callback(lambda _t, k=key: self._inflight.pop(k, None))+ return await task+++class RefreshingTokenStore:+ """An ``OAuthTokenStore`` that proactively refreshes a near-expiry token.++ Reads from an inner store; if the token is within ``expiry_skew_seconds`` of expiry, it mints a+ fresh one via the injected ``TokenRefresher``, serialized per ``(user, server)`` by the injected+ ``RefreshCoordinator`` so callers don't stampede the IdP. The refresher persists the new token so+ later requests (and the surrounding cache) read it without refreshing again. An expired token the+ refresher cannot renew (``None``) is surfaced as ``None`` so the arm challenges, never a stale+ bearer.++ The default coordinator is in-process; a cross-replica deployment injects a distributed one (Redis+ SET NX). Reactive-401 refresh is later hardening (it lives in the egress transport, which sees the+ upstream's 401). Composes under ``CachedOAuthTokenStore`` so the refreshed token is cached.+ """++ def __init__(+ self,+ inner: OAuthTokenStore,+ refresher: TokenRefresher,+ *,+ expiry_skew_seconds: float = 60.0,+ coordinator: RefreshCoordinator | None = None,+ clock: Callable[[], float] = time.time,+ ) -> None:+ self._inner = inner+ self._refresher = refresher+ self._expiry_skew_seconds = expiry_skew_seconds+ self._clock = clock+ self._coordinator: RefreshCoordinator = (+ coordinator or InProcessRefreshCoordinator()+ )++ def _is_expired(self, token: OAuthToken) -> bool:+ return (+ token.expires_at is not None+ and self._clock() >= token.expires_at - self._expiry_skew_seconds+ )++ async def fetch(self, user_id: str, server_id: str) -> OAuthToken | None:+ token = await self._inner.fetch(user_id, server_id)+ if token is None or not self._is_expired(token):+ return token++ async def refresh_latest_token() -> OAuthToken | None:+ latest_token = await self._inner.fetch(user_id, server_id)+ if latest_token is None or not self._is_expired(latest_token):+ return latest_token+ return await self._refresher.refresh(user_id, server_id, latest_token)++ return await self._coordinator.run(+ user_id,+ server_id,+ refresh=refresh_latest_token,+ reread=lambda: self._inner.fetch(user_id, server_id),+ )diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.pyindex 2088dc77252..8e589d3a24b 100644--- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/types.py@@ -25,6 +25,8 @@ union (see `result.py`), not `expression.Result`.from __future__ import annotations+from collections.abc import Mapping+from dataclasses import dataclassfrom enum import Enumfrom typing import Annotated, Literal@@ -59,6 +61,19 @@ class AuthSpecKind(str, Enum):aws_sigv4 = "aws_sigv4" # AWS SigV4 per-request signing (e.g. Bedrock AgentCore)+@dataclass(frozen=True, slots=True)+class Unauthorized:+ """A 401 plus the optional challenge a client needs to recover.++ ``detail`` is the human message; ``www_authenticate`` and ``body`` carry a scheme-specific+ challenge (e.g. BYOK's provisioning prompt) so the edge can reproduce it verbatim.+ """++ detail: str+ www_authenticate: str | None = None+ body: Mapping[str, str] | None = None++@tagged_union(frozen=True)class CredError:"""Why a credential could not be produced. Fail-closed: an arm yields this or an `httpx.Auth`.@@ -76,7 +91,7 @@ class CredError:"not_implemented",] = tag()- unauthorized: str = (+ unauthorized: Unauthorized = (case()) # no usable credential for this (subject, server) -> 401 challengemisconfigured: str = (@@ -96,8 +111,17 @@ class CredError:) # the declared mode's resolver arm is not built yet -> 501 (not operator error)@staticmethod- def of_unauthorized(detail: str) -> CredError:- return CredError(unauthorized=detail)+ def of_unauthorized(+ detail: str,+ *,+ www_authenticate: str | None = None,+ body: Mapping[str, str] | None = None,+ ) -> CredError:+ return CredError(+ unauthorized=Unauthorized(+ detail=detail, www_authenticate=www_authenticate, body=body+ )+ )@staticmethoddef of_misconfigured(detail: str) -> CredError:@@ -125,7 +149,7 @@ class CredError:# only while that stays true (a `case _` would defeat reportMatchNotExhaustive).match self.tag:case "unauthorized":- return f"unauthorized: {self.unauthorized}"+ return f"unauthorized: {self.unauthorized.detail}"case "misconfigured":return f"misconfigured: {self.misconfigured}"case "upstream_unavailable":diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_adapter.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_adapter.pyindex 5481d60a22a..594e9dcc969 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_adapter.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_adapter.py@@ -139,3 +139,26 @@ def test_raise_public_maps_each_error_to_its_status(error, status):with pytest.raises(HTTPException) as exc_info:raise_public(error)assert exc_info.value.status_code == status+++def test_raise_public_emits_unauthorized_challenge():+ body = {"error": "byok_auth_required", "server_id": "s1"}+ error = CredError.of_unauthorized(+ "needs key", www_authenticate='Bearer resource_metadata="/x"', body=body+ )+ with pytest.raises(HTTPException) as exc_info:+ raise_public(error)+ exc = exc_info.value+ assert exc.status_code == 401+ assert exc.detail == body+ assert exc.headers is not None+ assert exc.headers["WWW-Authenticate"] == 'Bearer resource_metadata="/x"'+++def test_raise_public_plain_unauthorized_has_no_challenge():+ with pytest.raises(HTTPException) as exc_info:+ raise_public(CredError.of_unauthorized("nope"))+ exc = exc_info.value+ assert exc.status_code == 401+ assert exc.detail == "unauthorized: nope"+ assert exc.headers is Nonediff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_oauth_token_store.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_oauth_token_store.pynew file mode 100644index 00000000000..63cb407de19--- /dev/null+++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_oauth_token_store.py@@ -0,0 +1,446 @@+"""Tests for the v2 OAuth token cache and refresh (CachedOAuthTokenStore, RefreshingTokenStore)."""++import asyncio+from typing import Dict, List, Optional, Tuple++import pytest++from litellm.proxy._experimental.mcp_server.outbound_credentials.oauth_token_store import (+ CachedOAuthTokenStore,+ OAuthToken,+ RefreshingTokenStore,+ TokenStoreUnavailable,+)+++class _FakeStore:+ """An OAuthTokenStore that records calls and returns canned tokens."""++ def __init__(self, values: Dict[Tuple[str, str], Optional[OAuthToken]]) -> None:+ self._values = values+ self.calls: List[Tuple[str, str]] = []++ async def fetch(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ self.calls.append((user_id, server_id))+ return self._values.get((user_id, server_id))+++class _Clock:+ def __init__(self, t: float = 1000.0) -> None:+ self.t = t++ def __call__(self) -> float:+ return self.t+++async def test_serves_token_until_its_expiry():+ token = OAuthToken(access_token="at", expires_at=1100.0)+ inner = _FakeStore({("u", "s"): token})+ clock = _Clock(1000.0)+ store = CachedOAuthTokenStore(+ inner, default_ttl_seconds=60, expiry_skew_seconds=30, clock=clock+ )++ assert await store.fetch("u", "s") is token+ clock.t = 1060.0 # still before expiry - skew (1100 - 30 = 1070)+ assert await store.fetch("u", "s") is token+ assert inner.calls == [("u", "s")] # served from cache, store hit once+++async def test_refetches_once_token_has_expired():+ token = OAuthToken(access_token="at", expires_at=1100.0)+ inner = _FakeStore({("u", "s"): token})+ clock = _Clock(1000.0)+ store = CachedOAuthTokenStore(+ inner, default_ttl_seconds=60, expiry_skew_seconds=30, clock=clock+ )++ await store.fetch("u", "s")+ clock.t = 1080.0 # past expiry - skew (1070)+ await store.fetch("u", "s")+ assert len(inner.calls) == 2 # re-read after the cached token expired+++async def test_does_not_cache_the_not_authorized_miss():+ inner = _FakeStore({}) # user has not authorized+ clock = _Clock(1000.0)+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=60, clock=clock)++ assert await store.fetch("u", "s") is None+ clock.t = 1059.0+ assert await store.fetch("u", "s") is None+ assert inner.calls == [+ ("u", "s"),+ ("u", "s"),+ ] # misses re-read the store, never cached+++async def test_token_stored_after_a_miss_is_visible_immediately():+ # No invalidation needed: a miss is never cached, so a token written after the OAuth flow is+ # served on the very next call (matching v1, where misses always re-read the source).+ inner = _FakeStore({})+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=60, clock=_Clock())++ assert await store.fetch("u", "s") is None+ inner._values[("u", "s")] = OAuthToken(access_token="fresh")+ result = await store.fetch("u", "s")+ assert result is not None and result.access_token == "fresh"+++async def test_default_ttl_applies_to_tokens_without_expiry():+ token = OAuthToken(access_token="at", expires_at=None)+ inner = _FakeStore({("u", "s"): token})+ clock = _Clock(1000.0)+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=60, clock=clock)++ await store.fetch("u", "s")+ clock.t = 1061.0+ await store.fetch("u", "s")+ assert len(inner.calls) == 2 # no-expiry token re-read after the default TTL+++async def test_invalidate_drops_a_cached_token():+ # invalidate covers rotation/revocation of a *cached* token (the miss path needs no invalidate).+ inner = _FakeStore({("u", "s"): OAuthToken(access_token="t1")})+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=60, clock=_Clock())++ first = await store.fetch("u", "s")+ assert first is not None and first.access_token == "t1" # cached+ inner._values[("u", "s")] = OAuthToken(access_token="t2") # rotated+ await store.invalidate("u", "s")+ second = await store.fetch("u", "s")+ assert (+ second is not None and second.access_token == "t2"+ ) # re-read after invalidate+++async def test_store_unavailable_is_not_cached():+ class _FailingStore:+ def __init__(self) -> None:+ self.calls = 0++ async def fetch(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ self.calls += 1+ raise TokenStoreUnavailable("down")++ inner = _FailingStore()+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=60, clock=_Clock())++ for _ in range(2):+ with pytest.raises(TokenStoreUnavailable):+ await store.fetch("u", "s")+ assert inner.calls == 2 # outage re-attempted, not cached+++async def test_isolates_by_subject():+ a = OAuthToken(access_token="a")+ b = OAuthToken(access_token="b")+ inner = _FakeStore({("u1", "s"): a, ("u2", "s"): b})+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=60, clock=_Clock())++ first = await store.fetch("u1", "s")+ second = await store.fetch("u2", "s")+ assert first is not None and first.access_token == "a"+ assert second is not None and second.access_token == "b"+++async def test_bounded_cache_evicts_oldest_not_everything():+ inner = _FakeStore(+ {+ ("u1", "s"): OAuthToken(access_token="k1"),+ ("u2", "s"): OAuthToken(access_token="k2"),+ ("u3", "s"): OAuthToken(access_token="k3"),+ }+ )+ store = CachedOAuthTokenStore(+ inner, default_ttl_seconds=60, max_size=2, clock=_Clock()+ )++ await store.fetch("u1", "s")+ await store.fetch("u2", "s")+ await store.fetch("u3", "s") # at capacity -> evict the oldest (u1), keep u2+ await store.fetch("u2", "s") # still cached+ await store.fetch("u1", "s") # was evicted -> re-read++ assert (+ inner.calls.count(("u2", "s")) == 1+ ) # only the oldest was evicted, not everything+ assert inner.calls.count(("u1", "s")) == 2+++class _RefreshablePair:+ """A store + refresher pair that simulates persistence: refresh() updates what fetch returns,+ and yields once so concurrent callers actually contend on the single-flight lock."""++ def __init__(self, initial: Optional[OAuthToken]) -> None:+ self._current = initial+ self.fetch_calls = 0+ self.refresh_calls = 0+ self.refresh_args: List[Tuple[str, str]] = []++ async def fetch(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ self.fetch_calls += 1+ return self._current++ async def refresh(+ self, user_id: str, server_id: str, token: OAuthToken+ ) -> Optional[OAuthToken]:+ self.refresh_calls += 1+ self.refresh_args.append((user_id, server_id))+ await asyncio.sleep(+ 0+ ) # yield so other concurrent callers reach the lock and wait+ self._current = OAuthToken(access_token="refreshed", expires_at=9999.0)+ return self._current+++async def test_refreshing_passes_through_a_fresh_token():+ pair = _RefreshablePair(OAuthToken(access_token="ok", expires_at=9999.0))+ store = RefreshingTokenStore(+ pair, pair, expiry_skew_seconds=30, clock=_Clock(1000.0)+ )++ token = await store.fetch("u", "s")+ assert token is not None and token.access_token == "ok"+ assert pair.refresh_calls == 0 # not near expiry -> no refresh+++async def test_refreshing_mints_a_fresh_token_when_expired():+ pair = _RefreshablePair(OAuthToken(access_token="old", expires_at=900.0))+ store = RefreshingTokenStore(+ pair, pair, expiry_skew_seconds=30, clock=_Clock(1000.0)+ )++ token = await store.fetch("u", "s")+ assert token is not None and token.access_token == "refreshed"+ assert pair.refresh_calls == 1+ assert pair.refresh_args == [+ ("u", "s")+ ] # the seam threads the grant/persist key through+++async def test_refreshing_returns_none_when_it_cannot_refresh():+ class _NoRefresh:+ async def fetch(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ return OAuthToken(access_token="old", expires_at=900.0)++ async def refresh(+ self, user_id: str, server_id: str, token: OAuthToken+ ) -> Optional[OAuthToken]:+ return None # e.g. no refresh_token++ src = _NoRefresh()+ store = RefreshingTokenStore(src, src, expiry_skew_seconds=30, clock=_Clock(1000.0))+ # expired and unrefreshable -> None (the arm challenges), never a stale bearer+ assert await store.fetch("u", "s") is None+++async def test_refreshing_is_single_flight_under_concurrency():+ pair = _RefreshablePair(OAuthToken(access_token="old", expires_at=900.0))+ store = RefreshingTokenStore(+ pair, pair, expiry_skew_seconds=30, clock=_Clock(1000.0)+ )++ results = await asyncio.gather(*[store.fetch("u", "s") for _ in range(5)])+ assert pair.refresh_calls == 1 # one refresh shared across 5 concurrent callers+ assert all(r is not None and r.access_token == "refreshed" for r in results)+++class _StaleReadRacePair:+ def __init__(self) -> None:+ self._old_token = OAuthToken(access_token="old", expires_at=900.0)+ self._current = self._old_token+ self.fetch_calls = 0+ self.refresh_calls = 0+ self.refresh_started = asyncio.Event()+ self.finish_refresh = asyncio.Event()+ self.stale_read_started = asyncio.Event()+ self.finish_stale_read = asyncio.Event()++ async def fetch(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ self.fetch_calls += 1+ if self.fetch_calls == 3:+ self.stale_read_started.set()+ await self.finish_stale_read.wait()+ return self._old_token+ return self._current++ async def refresh(+ self, user_id: str, server_id: str, token: OAuthToken+ ) -> Optional[OAuthToken]:+ self.refresh_calls += 1+ self.refresh_started.set()+ await self.finish_refresh.wait()+ self._current = OAuthToken(access_token="refreshed", expires_at=9999.0)+ return self._current+++async def test_stale_read_after_refresh_rereads_before_starting_new_refresh():+ pair = _StaleReadRacePair()+ store = RefreshingTokenStore(+ pair, pair, expiry_skew_seconds=30, clock=_Clock(1000.0)+ )++ first = asyncio.create_task(store.fetch("u", "s"))+ await pair.refresh_started.wait()+ second = asyncio.create_task(store.fetch("u", "s"))+ await pair.stale_read_started.wait()++ pair.finish_refresh.set()+ first_result = await first+ pair.finish_stale_read.set()+ second_result = await second++ assert first_result is not None and first_result.access_token == "refreshed"+ assert second_result is not None and second_result.access_token == "refreshed"+ assert pair.refresh_calls == 1+++async def test_refresh_failure_is_shared_by_joiners_not_re_run():+ class _FailingRefresher:+ def __init__(self) -> None:+ self.calls = 0++ async def fetch(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ return OAuthToken(access_token="old", expires_at=900.0)++ async def refresh(+ self, user_id: str, server_id: str, token: OAuthToken+ ) -> Optional[OAuthToken]:+ self.calls += 1+ await asyncio.sleep(0) # let the concurrent callers join the same task+ raise RuntimeError("refresh boom")++ src = _FailingRefresher()+ store = RefreshingTokenStore(src, src, expiry_skew_seconds=30, clock=_Clock(1000.0))++ results = await asyncio.gather(+ *[store.fetch("u", "s") for _ in range(3)], return_exceptions=True+ )+ assert src.calls == 1 # single-flight: one attempt, the failure is shared+ assert all(isinstance(r, RuntimeError) for r in results)+++async def test_cache_default_skew_is_60_seconds():+ # The default expiry skew is the industry-standard 60s (Spring Security's clock-skew and+ # refresh-buffer default; within RFC 7519's "a few minutes" leeway), so a cached token stops+ # being served 60s before its real expiry. The boundary sits at expires_at - 60 = 1040.+ token = OAuthToken(access_token="at", expires_at=1100.0)+ inner = _FakeStore({("u", "s"): token})+ clock = _Clock(1000.0)+ store = CachedOAuthTokenStore(inner, default_ttl_seconds=600, clock=clock)++ await store.fetch("u", "s")+ clock.t = 1039.0 # just inside expires_at - 60 -> still served from cache+ await store.fetch("u", "s")+ assert len(inner.calls) == 1+ clock.t = 1041.0 # just past expires_at - 60 -> re-read (a 30s skew would still cache here)+ await store.fetch("u", "s")+ assert len(inner.calls) == 2+++async def test_refreshing_default_skew_is_60_seconds():+ # Same 60s default for the proactive refresh threshold: a token within 60s of expiry refreshes,+ # one further out does not. At clock 1000 the boundary expires_at - 60 = 1000 lands on expires_at+ # 1060 (refresh) vs 1061 (no refresh), pinning the default to exactly 60 (a 30s skew would not+ # refresh either case).+ not_near = _RefreshablePair(OAuthToken(access_token="ok", expires_at=1061.0))+ not_near_store = RefreshingTokenStore(not_near, not_near, clock=_Clock(1000.0))+ not_near_token = await not_near_store.fetch("u", "s")+ assert not_near_token is not None and not_near_token.access_token == "ok"+ assert not_near.refresh_calls == 0++ near = _RefreshablePair(OAuthToken(access_token="old", expires_at=1060.0))+ near_store = RefreshingTokenStore(near, near, clock=_Clock(1000.0))+ near_token = await near_store.fetch("u", "s")+ assert near_token is not None and near_token.access_token == "refreshed"+ assert near.refresh_calls == 1+++def test_oauth_token_repr_masks_the_secrets():+ token = OAuthToken(+ access_token="super-secret", expires_at=123.0, refresh_token="rt-secret"+ )+ rendered = repr(token)+ assert "super-secret" not in rendered+ assert "rt-secret" not in rendered+ assert "access_token=***" in rendered+ assert "has_refresh_token=True" in rendered+++class _RecordingBackend:+ """A TokenCacheBackend that records calls, proving CachedOAuthTokenStore delegates storage."""++ def __init__(self) -> None:+ self.sets: List[Tuple[str, str, OAuthToken, float]] = []+ self.deletes: List[Tuple[str, str]] = []+ self._store: Dict[Tuple[str, str], OAuthToken] = {}++ async def get(self, user_id: str, server_id: str) -> Optional[OAuthToken]:+ return self._store.get((user_id, server_id))++ async def set(+ self, user_id: str, server_id: str, token: OAuthToken, ttl_seconds: float+ ) -> None:+ self.sets.append((user_id, server_id, token, ttl_seconds))+ self._store[(user_id, server_id)] = token++ async def delete(self, user_id: str, server_id: str) -> None:+ self.deletes.append((user_id, server_id))+ self._store.pop((user_id, server_id), None)+++async def test_cache_delegates_storage_to_an_injected_backend():+ backend = _RecordingBackend()+ inner = _FakeStore({("u", "s"): OAuthToken(access_token="at", expires_at=1100.0)})+ store = CachedOAuthTokenStore(+ inner,+ default_ttl_seconds=60,+ expiry_skew_seconds=30,+ backend=backend,+ clock=_Clock(1000.0),+ )++ token = await store.fetch("u", "s")+ assert token is not None and token.access_token == "at"+ # written through to the injected backend, TTL = expires_at - skew - now = 1100 - 30 - 1000+ assert backend.sets == [("u", "s", token, 70.0)]+ again = await store.fetch("u", "s") # served by the backend, not the inner store+ assert again is not None+ assert inner.calls == [("u", "s")]+++async def test_cache_miss_deletes_from_the_injected_backend():+ backend = _RecordingBackend()+ store = CachedOAuthTokenStore(+ _FakeStore({}), default_ttl_seconds=60, backend=backend, clock=_Clock()+ )+ assert await store.fetch("u", "s") is None+ assert backend.deletes == [("u", "s")] # a miss is never cached+++class _RecordingCoordinator:+ """A RefreshCoordinator that records the call and runs the refresh, proving delegation."""++ def __init__(self) -> None:+ self.calls = 0++ async def run(self, user_id, server_id, refresh, reread):+ self.calls += 1+ return await refresh()+++async def test_refreshing_delegates_single_flight_to_an_injected_coordinator():+ pair = _RefreshablePair(OAuthToken(access_token="old", expires_at=900.0))+ coordinator = _RecordingCoordinator()+ store = RefreshingTokenStore(+ pair,+ pair,+ expiry_skew_seconds=30,+ coordinator=coordinator,+ clock=_Clock(1000.0),+ )++ token = await store.fetch("u", "s")+ assert token is not None and token.access_token == "refreshed"+ assert coordinator.calls == 1 # the injected coordinator drove the refresh