Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Fix OAuthProxy upstream refresh token revocation (#5243)

Pawel Jaworowski9949d9a646b1
diff --git a/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py b/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
index 2d8f33871..ff601b57c 100644
--- a/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
+++ b/fastmcp_slim/fastmcp/server/auth/oauth_proxy/proxy.py
@@ -2374,10 +2374,39 @@ class OAuthProxy(OAuthProvider, ConsentMixin):
# Attempt upstream revocation if endpoint is configured
if self._upstream_revocation_endpoint:
try:
+ # For refresh tokens, resolve the upstream refresh token via
+ # JTI mapping so we revoke the token the authorization server
+ # actually issued, not the FastMCP-issued JWT wrapper.
+ if isinstance(token, RefreshToken):
+ refresh_payload = self.jwt_issuer.verify_token(
+ token.token, expected_token_use="refresh"
+ )
+ refresh_jti = refresh_payload["jti"]
+ jti_mapping = await self._jti_mapping_store.get(key=refresh_jti)
+ if jti_mapping is None:
+ logger.warning("No JTI mapping found for refresh token")
+ return
+
+ upstream_token_set = await self._upstream_token_store.get(
+ key=jti_mapping.upstream_token_id
+ )
+ if (
+ upstream_token_set is None
+ or not upstream_token_set.refresh_token
+ ):
+ logger.warning("No upstream refresh token found")
+ return
+
+ upstream_token_to_revoke = upstream_token_set.refresh_token
+ else:
+ upstream_token_to_revoke = token.token
+
async with httpx2.AsyncClient(
timeout=HTTP_TIMEOUT_SECONDS
) as http_client:
- revocation_data: dict[str, str] = {"token": token.token}
+ revocation_data: dict[str, str] = {
+ "token": upstream_token_to_revoke
+ }
request_kwargs: dict[str, Any] = {"data": revocation_data}
# Use the factory method when available (supports alternative auth like
diff --git a/tests/server/auth/oauth_proxy/test_tokens.py b/tests/server/auth/oauth_proxy/test_tokens.py
index 0e552d615..ed2545c15 100644
--- a/tests/server/auth/oauth_proxy/test_tokens.py
+++ b/tests/server/auth/oauth_proxy/test_tokens.py
@@ -2011,3 +2011,313 @@ class TestRefreshTokenMissLogging:
and "test-client" in record.getMessage()
for record in caplog.records
)
+
+
+class TestTokenRevocation:
+ @pytest.fixture
+ def oauth_proxy(self, jwt_verifier):
+ proxy = OAuthProxy(
+ upstream_authorization_endpoint="https://idp.example.com/authorize",
+ upstream_token_endpoint="https://idp.example.com/token",
+ upstream_client_id="test-client",
+ upstream_client_secret="test-secret",
+ token_verifier=jwt_verifier,
+ base_url="https://proxy.example.com",
+ jwt_signing_key="test-secret-key",
+ client_storage=MemoryStore(),
+ upstream_revocation_endpoint="https://idp.example.com/revoke",
+ )
+ proxy.set_mcp_path("/mcp")
+ return proxy
+
+ async def test_revoke_refresh_token_posts_upstream_token(
+ self, oauth_proxy, httpx_mock
+ ):
+ """The upstream POST contains the provider's refresh token, not the FastMCP JWT."""
+ now = time.time()
+ refresh_jti = "test-refresh-jti"
+ fastmcp_jwt = oauth_proxy.jwt_issuer.issue_refresh_token(
+ client_id="test-client",
+ scopes=["read"],
+ jti=refresh_jti,
+ expires_in=3600,
+ )
+ refresh_token = RefreshToken(
+ token=fastmcp_jwt,
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=int(now + 3600),
+ )
+
+ upstream_token_id = "upstream-token-id"
+ await oauth_proxy._jti_mapping_store.put(
+ key=refresh_jti,
+ value=JTIMapping(
+ jti=refresh_jti,
+ upstream_token_id=upstream_token_id,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+
+ await oauth_proxy._upstream_token_store.put(
+ key=upstream_token_id,
+ value=UpstreamTokenSet(
+ upstream_token_id=upstream_token_id,
+ access_token="upstream-access-token",
+ refresh_token="upstream-refresh-token",
+ refresh_token_expires_at=now + 86400,
+ expires_at=now + 3600,
+ token_type="Bearer",
+ scope="read",
+ client_id="test-client",
+ created_at=now,
+ ),
+ ttl=86400,
+ )
+
+ await oauth_proxy._refresh_token_store.put(
+ key=_hash_token(fastmcp_jwt),
+ value=RefreshTokenMetadata(
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=refresh_token.expires_at,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+
+ httpx_mock.add_response(
+ url="https://idp.example.com/revoke",
+ method="POST",
+ json={},
+ )
+
+ await oauth_proxy.revoke_token(refresh_token)
+
+ request = httpx_mock.get_request(
+ url="https://idp.example.com/revoke", method="POST"
+ )
+ assert request is not None
+ assert len(httpx_mock.get_requests()) == 1
+ assert parse_qs(request.content.decode()) == {
+ "token": ["upstream-refresh-token"],
+ }
+ assert (
+ await oauth_proxy._refresh_token_store.get(key=_hash_token(fastmcp_jwt))
+ is None
+ )
+
+ async def test_revoke_refresh_token_without_mapping_skips_upstream(
+ self, oauth_proxy, httpx_mock, caplog
+ ):
+ """A missing JTI mapping warns and never sends the FastMCP JWT upstream."""
+ now = time.time()
+ refresh_jti = "test-refresh-jti"
+ fastmcp_jwt = oauth_proxy.jwt_issuer.issue_refresh_token(
+ client_id="test-client",
+ scopes=["read"],
+ jti=refresh_jti,
+ expires_in=3600,
+ )
+ refresh_token = RefreshToken(
+ token=fastmcp_jwt,
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=int(now + 3600),
+ )
+
+ await oauth_proxy._refresh_token_store.put(
+ key=_hash_token(fastmcp_jwt),
+ value=RefreshTokenMetadata(
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=refresh_token.expires_at,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+ # Deliberately do not store a JTI mapping.
+
+ await oauth_proxy.revoke_token(refresh_token)
+
+ assert httpx_mock.get_requests() == []
+ assert any(
+ record.levelno == logging.WARNING
+ and "No JTI mapping found for refresh token" in record.getMessage()
+ for record in caplog.records
+ )
+ assert (
+ await oauth_proxy._refresh_token_store.get(key=_hash_token(fastmcp_jwt))
+ is None
+ )
+
+ async def test_revoke_refresh_token_without_upstream_record_skips_upstream(
+ self, oauth_proxy, httpx_mock, caplog
+ ):
+ """A mapping to an absent upstream record warns and skips the POST."""
+
+ now = time.time()
+ refresh_jti = "test-refresh-jti"
+ fastmcp_jwt = oauth_proxy.jwt_issuer.issue_refresh_token(
+ client_id="test-client",
+ scopes=["read"],
+ jti=refresh_jti,
+ expires_in=3600,
+ )
+ refresh_token = RefreshToken(
+ token=fastmcp_jwt,
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=int(now + 3600),
+ )
+
+ upstream_token_id = "upstream-token-id"
+ await oauth_proxy._jti_mapping_store.put(
+ key=refresh_jti,
+ value=JTIMapping(
+ jti=refresh_jti,
+ upstream_token_id=upstream_token_id,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+
+ await oauth_proxy._refresh_token_store.put(
+ key=_hash_token(fastmcp_jwt),
+ value=RefreshTokenMetadata(
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=refresh_token.expires_at,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+ # The mapping exists, but its upstream token record does not.
+
+ await oauth_proxy.revoke_token(refresh_token)
+
+ assert httpx_mock.get_requests() == []
+ assert any(
+ record.levelno == logging.WARNING
+ and "No upstream refresh token found" in record.getMessage()
+ for record in caplog.records
+ )
+ assert (
+ await oauth_proxy._refresh_token_store.get(key=_hash_token(fastmcp_jwt))
+ is None
+ )
+
+ async def test_revoke_expired_refresh_token_warns_and_skips_upstream(
+ self, oauth_proxy, httpx_mock, caplog
+ ):
+ """Refresh JWT verification failure is warned about after local deletion."""
+ now = time.time()
+ refresh_jti = "test-refresh-jti"
+ fastmcp_jwt = oauth_proxy.jwt_issuer.issue_refresh_token(
+ client_id="test-client",
+ scopes=["read"],
+ jti=refresh_jti,
+ expires_in=-60,
+ )
+ refresh_token = RefreshToken(
+ token=fastmcp_jwt,
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=int(now - 60),
+ )
+
+ await oauth_proxy._refresh_token_store.put(
+ key=_hash_token(fastmcp_jwt),
+ value=RefreshTokenMetadata(
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=refresh_token.expires_at,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+
+ await oauth_proxy.revoke_token(refresh_token)
+
+ assert httpx_mock.get_requests() == []
+ assert any(
+ record.levelno == logging.WARNING
+ and "Failed to revoke token with upstream server" in record.getMessage()
+ for record in caplog.records
+ )
+ assert (
+ await oauth_proxy._refresh_token_store.get(key=_hash_token(fastmcp_jwt))
+ is None
+ )
+
+ @pytest.mark.parametrize("upstream_refresh", [None, ""])
+ async def test_revoke_refresh_token_without_upstream_refresh_skips_upstream(
+ self, oauth_proxy, httpx_mock, caplog, upstream_refresh
+ ):
+ """A missing or empty upstream refresh token never falls back to the JWT."""
+ now = time.time()
+ refresh_jti = "test-refresh-jti"
+ fastmcp_jwt = oauth_proxy.jwt_issuer.issue_refresh_token(
+ client_id="test-client",
+ scopes=["read"],
+ jti=refresh_jti,
+ expires_in=3600,
+ )
+ refresh_token = RefreshToken(
+ token=fastmcp_jwt,
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=int(now + 3600),
+ )
+
+ upstream_token_id = "upstream-token-id"
+ await oauth_proxy._jti_mapping_store.put(
+ key=refresh_jti,
+ value=JTIMapping(
+ jti=refresh_jti,
+ upstream_token_id=upstream_token_id,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+
+ await oauth_proxy._upstream_token_store.put(
+ key=upstream_token_id,
+ value=UpstreamTokenSet(
+ upstream_token_id=upstream_token_id,
+ access_token="upstream-access-token",
+ refresh_token=upstream_refresh,
+ refresh_token_expires_at=now + 86400,
+ expires_at=now + 3600,
+ token_type="Bearer",
+ scope="read",
+ client_id="test-client",
+ created_at=now,
+ ),
+ ttl=86400,
+ )
+
+ await oauth_proxy._refresh_token_store.put(
+ key=_hash_token(fastmcp_jwt),
+ value=RefreshTokenMetadata(
+ client_id="test-client",
+ scopes=["read"],
+ expires_at=refresh_token.expires_at,
+ created_at=now,
+ ),
+ ttl=3600,
+ )
+
+ await oauth_proxy.revoke_token(refresh_token)
+
+ assert httpx_mock.get_requests() == []
+ assert any(
+ record.levelno == logging.WARNING
+ and "No upstream refresh token found" in record.getMessage()
+ for record in caplog.records
+ )
+ assert (
+ await oauth_proxy._refresh_token_store.get(key=_hash_token(fastmcp_jwt))
+ is None
+ )