mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
## 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
53 lines
1.7 KiB
Rust
53 lines
1.7 KiB
Rust
use anyhow::Context;
|
|
use anyhow::Result;
|
|
use rmcp::transport::auth::AuthError;
|
|
use rmcp::transport::auth::AuthorizationMetadata;
|
|
|
|
use super::StoredOAuthTokens;
|
|
/// Verifies that a stored refresh token remains bound to its original issuer.
|
|
///
|
|
/// Call this with the same metadata snapshot that RMCP will use for the credentials. Missing or
|
|
/// changed issuers require a new login rather than risking sending a refresh token to a different
|
|
/// authorization server.
|
|
pub(crate) fn validate_refresh_token_issuer(
|
|
metadata: &AuthorizationMetadata,
|
|
tokens: &StoredOAuthTokens,
|
|
) -> Result<()> {
|
|
if !tokens.has_refresh_token() {
|
|
return Ok(());
|
|
}
|
|
|
|
let Some(stored_issuer) = tokens.bound_issuer() else {
|
|
return Err(AuthError::AuthorizationRequired).with_context(|| {
|
|
format!(
|
|
"OAuth refresh credentials for server {} are missing an authorization server issuer; authorization required",
|
|
tokens.server_name
|
|
)
|
|
});
|
|
};
|
|
|
|
let Some(current_issuer) = metadata
|
|
.issuer
|
|
.as_deref()
|
|
.filter(|issuer| !issuer.trim().is_empty())
|
|
else {
|
|
return Err(AuthError::AuthorizationRequired).with_context(|| {
|
|
format!(
|
|
"OAuth metadata for server {} did not include an authorization server issuer; authorization required",
|
|
tokens.server_name
|
|
)
|
|
});
|
|
};
|
|
|
|
if current_issuer != stored_issuer {
|
|
return Err(AuthError::AuthorizationRequired).with_context(|| {
|
|
format!(
|
|
"OAuth authorization server issuer changed for server {}; authorization required",
|
|
tokens.server_name
|
|
)
|
|
});
|
|
}
|
|
|
|
Ok(())
|
|
}
|