Bind MCP OAuth refresh tokens to their issuer (#39615)

## Why

An authorization server discovered for an MCP server can change. A stored refresh token must not be sent to a different issuer than the one that originally granted it.

## What changed

- Persist the discovered authorization server issuer with new OAuth credentials and preserve it across refreshes.
- Validate that issuer against a single pinned metadata snapshot before refreshing or adopting stored refresh credentials.
- Require reauthentication when refresh credentials have no issuer or the issuer changed. An unexpired access token can still be used without exposing its refresh token.

## Testing

Add coverage for issuer persistence, missing and changed issuers, metadata changes during startup, and refresh-token preservation.

GitOrigin-RevId: 07e48ba892f627428a28bbed5cca4ad703e2a5fd
This commit is contained in:
Jeremy Rose authored and copyberry committed 2026-08-20 06:25:17 +00:00
1 parent 530c1aed58
commit 250b5ea2bf
16 files changed
+722 -110

No files matched your search

@@ -21,6 +21,12 @@ pub enum McpOAuthClientRegistration {
Dcr,
}
/// OAuth state prepared from one authorization-server metadata resolution.
pub(crate) struct PreparedOAuthLogin {
pub(crate) oauth_state: OAuthState,
pub(crate) authorization_server_issuer: Option<String>,
}
pub(crate) async fn start_authorization(
server_url: &str,
http_client: Arc<dyn OAuthHttpClient>,
@@ -28,11 +34,12 @@ pub(crate) async fn start_authorization(
redirect_uri: &str,
callback_id: &str,
client_registration: McpOAuthClientRegistration,
) -> Result<OAuthState> {
) -> Result<PreparedOAuthLogin> {
let mut auth_manager =
AuthorizationManager::new_with_oauth_http_client(server_url, http_client).await?;
auth_manager.set_allow_missing_issuer(true);
let metadata = auth_manager.resolve_metadata().await?.metadata;
let authorization_server_issuer = metadata.issuer.clone();
let cimd_advertised = metadata
.additional_fields
@@ -96,7 +103,10 @@ pub(crate) async fn start_authorization(
.await
.map_err(|(_auth_manager, error)| error)?;
Ok(OAuthState::Session(session))
Ok(PreparedOAuthLogin {
oauth_state: OAuthState::Session(session),
authorization_server_issuer,
})
}
#[cfg(test)]