mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
Add CIMD support to MCP OAuth registration (#38089)
## What changed - Make automatic MCP OAuth registration prefer Client ID Metadata Documents (CIMD) when the authorization server advertises support for public clients and Codex is using its native loopback callback. Fall back to advertised Dynamic Client Registration (DCR) otherwise. - Add explicit `cimd` and `dcr` registration overrides to the CLI and app-server OAuth login API. Validate CIMD metadata and callback URLs before starting the authorization flow. - Use a callback-specific Codex client metadata URL for CIMD and retain the exact redirect URI through authorization and token exchange. ## Testing - Cover automatic and forced CIMD selection, DCR fallback, invalid metadata and redirects, token refresh, authenticated MCP requests, and conformance regression checks. GitOrigin-RevId: 4238372ca53b0f38e781e141ab5da97e0a6ddf45
This commit is contained in:
1 parent
85f331772f
commit
4c89139da9
21 files changed
+822
-57
No files matched your search
@@ -1,17 +1,22 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::bail;
|
||||
use rmcp::transport::AuthorizationManager;
|
||||
use rmcp::transport::AuthorizationRequest;
|
||||
use rmcp::transport::AuthorizationSession;
|
||||
use rmcp::transport::auth::OAuthHttpClient;
|
||||
use rmcp::transport::auth::OAuthState;
|
||||
use url::Url;
|
||||
|
||||
/// OAuth client-registration strategy for one interactive HTTP MCP login.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum McpOAuthClientRegistration {
|
||||
/// Preserve the current automatic Dynamic Client Registration flow.
|
||||
/// Prefer a supported native CIMD and otherwise use advertised DCR.
|
||||
#[default]
|
||||
Auto,
|
||||
/// Require a ChatGPT-hosted Codex public native Client ID Metadata Document.
|
||||
Cimd,
|
||||
/// Require the authorization server's Dynamic Client Registration endpoint.
|
||||
Dcr,
|
||||
}
|
||||
@@ -21,20 +26,79 @@ pub(crate) async fn start_authorization(
|
||||
http_client: Arc<dyn OAuthHttpClient>,
|
||||
scopes: &[&str],
|
||||
redirect_uri: &str,
|
||||
callback_id: &str,
|
||||
client_registration: McpOAuthClientRegistration,
|
||||
) -> Result<OAuthState> {
|
||||
let request = match client_registration {
|
||||
McpOAuthClientRegistration::Auto | McpOAuthClientRegistration::Dcr => {
|
||||
AuthorizationRequest::new(redirect_uri)
|
||||
.with_scopes(scopes.iter().copied())
|
||||
.with_client_name("Codex")
|
||||
}
|
||||
};
|
||||
|
||||
let mut auth_manager =
|
||||
AuthorizationManager::new_with_oauth_http_client(server_url, http_client).await?;
|
||||
auth_manager.set_allow_missing_issuer(true);
|
||||
let mut oauth_state = OAuthState::Unauthorized(auth_manager);
|
||||
oauth_state.start_authorization(request).await?;
|
||||
Ok(oauth_state)
|
||||
let metadata = auth_manager.resolve_metadata().await?.metadata;
|
||||
|
||||
let cimd_advertised = metadata
|
||||
.additional_fields
|
||||
.get("client_id_metadata_document_supported")
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
let public_client_auth_supported = metadata
|
||||
.additional_fields
|
||||
.get("token_endpoint_auth_methods_supported")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.is_some_and(|methods| methods.iter().any(|method| method.as_str() == Some("none")));
|
||||
|
||||
let parsed_redirect_uri = Url::parse(redirect_uri)?;
|
||||
let native_redirect_supported = parsed_redirect_uri.scheme() == "http"
|
||||
&& matches!(
|
||||
parsed_redirect_uri.host_str(),
|
||||
Some("127.0.0.1" | "localhost")
|
||||
)
|
||||
&& parsed_redirect_uri.port().is_some_and(|port| port > 0)
|
||||
&& parsed_redirect_uri.path() == format!("/callback/{callback_id}")
|
||||
&& parsed_redirect_uri.query().is_none()
|
||||
&& parsed_redirect_uri.fragment().is_none()
|
||||
&& parsed_redirect_uri.username().is_empty()
|
||||
&& parsed_redirect_uri.password().is_none();
|
||||
// MCP 2026-07-28 priority: pre-registered clients never reach this path; offer
|
||||
// advertised CIMD here and otherwise let rmcp fall back to DCR.
|
||||
// https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration
|
||||
let offer_cimd = match client_registration {
|
||||
McpOAuthClientRegistration::Auto => {
|
||||
cimd_advertised && native_redirect_supported && public_client_auth_supported
|
||||
}
|
||||
McpOAuthClientRegistration::Cimd => {
|
||||
if !cimd_advertised || !public_client_auth_supported {
|
||||
bail!(
|
||||
"MCP authorization server does not advertise CIMD with token endpoint auth method `none`"
|
||||
);
|
||||
}
|
||||
if !native_redirect_supported {
|
||||
bail!(
|
||||
"MCP OAuth CIMD requires an ephemeral loopback callback at `/callback/{callback_id}`"
|
||||
);
|
||||
}
|
||||
true
|
||||
}
|
||||
McpOAuthClientRegistration::Dcr => false,
|
||||
};
|
||||
|
||||
auth_manager.set_metadata(metadata);
|
||||
let mut request = AuthorizationRequest::new(redirect_uri)
|
||||
.with_scopes(scopes.iter().copied())
|
||||
.with_client_name("Codex");
|
||||
if offer_cimd {
|
||||
// CIMD is an active IETF Internet-Draft: this HTTPS client identifier resolves
|
||||
// to its self-referential JSON metadata document.
|
||||
// https://datatracker.ietf.org/doc/draft-ietf-oauth-client-id-metadata-document/
|
||||
request = request.with_client_metadata_url(format!(
|
||||
"https://chatgpt.com/oauth/codex/{callback_id}/client.json"
|
||||
));
|
||||
}
|
||||
let session = AuthorizationSession::new(auth_manager, request)
|
||||
.await
|
||||
.map_err(|(_auth_manager, error)| error)?;
|
||||
|
||||
Ok(OAuthState::Session(session))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "oauth_client_registration_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,256 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use codex_exec_server::RouteAwareHttpClient;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_http_client::OutboundProxyPolicy;
|
||||
use http::HeaderMap;
|
||||
use pretty_assertions::assert_eq;
|
||||
use rmcp::transport::auth::OAuthState;
|
||||
use serde_json::Value;
|
||||
use serde_json::json;
|
||||
use url::Url;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::Request;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
use super::McpOAuthClientRegistration;
|
||||
use super::start_authorization;
|
||||
use crate::oauth_http_client::OAuthHttpClientAdapter;
|
||||
|
||||
const CALLBACK_ID: &str = "abc123ABC_-x";
|
||||
|
||||
async fn oauth_server(overrides: Value) -> MockServer {
|
||||
let server = MockServer::start().await;
|
||||
let base_url = server.uri();
|
||||
let mut metadata = json!({
|
||||
"authorization_endpoint": format!("{base_url}/authorize"),
|
||||
"token_endpoint": format!("{base_url}/token"),
|
||||
"registration_endpoint": format!("{base_url}/register"),
|
||||
"client_id_metadata_document_supported": true,
|
||||
"token_endpoint_auth_methods_supported": ["none"],
|
||||
"code_challenge_methods_supported": ["S256"],
|
||||
"scopes_supported": ["read", "offline_access"],
|
||||
});
|
||||
metadata
|
||||
.as_object_mut()
|
||||
.expect("metadata should be an object")
|
||||
.extend(
|
||||
overrides
|
||||
.as_object()
|
||||
.expect("overrides should be an object")
|
||||
.clone(),
|
||||
);
|
||||
if metadata["authorization_response_iss_parameter_supported"] == json!(true)
|
||||
&& metadata.get("issuer").is_none()
|
||||
{
|
||||
metadata["issuer"] = json!(format!("{base_url}/mcp"));
|
||||
}
|
||||
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/.well-known/oauth-authorization-server/mcp"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(metadata))
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/register"))
|
||||
.respond_with(|request: &Request| {
|
||||
let registration: Value = serde_json::from_slice(&request.body)
|
||||
.expect("dynamic registration should contain JSON");
|
||||
ResponseTemplate::new(200).set_body_json(json!({
|
||||
"client_id": "dcr-client",
|
||||
"redirect_uris": registration["redirect_uris"],
|
||||
}))
|
||||
})
|
||||
.mount(&server)
|
||||
.await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/token"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
||||
"access_token": "test-access-token",
|
||||
"token_type": "Bearer",
|
||||
"refresh_token": "test-refresh-token",
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
server
|
||||
}
|
||||
|
||||
async fn requests_to(server: &MockServer, request_path: &str) -> Vec<Request> {
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("mock server should record requests")
|
||||
.into_iter()
|
||||
.filter(|request| request.url.path() == request_path)
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn authorization(
|
||||
server: &MockServer,
|
||||
redirect_uri: &str,
|
||||
registration: McpOAuthClientRegistration,
|
||||
) -> Result<(OAuthState, HashMap<String, String>)> {
|
||||
let state = start_authorization(
|
||||
&format!("{}/mcp", server.uri()),
|
||||
Arc::new(OAuthHttpClientAdapter::new(
|
||||
Arc::new(RouteAwareHttpClient::new(HttpClientFactory::new(
|
||||
OutboundProxyPolicy::ReqwestDefault,
|
||||
))),
|
||||
HeaderMap::new(),
|
||||
)),
|
||||
&["read"],
|
||||
redirect_uri,
|
||||
CALLBACK_ID,
|
||||
registration,
|
||||
)
|
||||
.await?;
|
||||
let query = Url::parse(&state.get_authorization_url().await?)?
|
||||
.query_pairs()
|
||||
.into_owned()
|
||||
.collect();
|
||||
|
||||
Ok((state, query))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn automatic_cimd_uses_callback_specific_identity() -> Result<()> {
|
||||
for host in ["127.0.0.1", "localhost"] {
|
||||
let server = oauth_server(json!({})).await;
|
||||
let redirect = format!("http://{host}:43123/callback/{CALLBACK_ID}");
|
||||
let (mut state, query) =
|
||||
authorization(&server, &redirect, McpOAuthClientRegistration::Auto).await?;
|
||||
let expected_id = format!("https://chatgpt.com/oauth/codex/{CALLBACK_ID}/client.json");
|
||||
assert_eq!(query["client_id"], expected_id);
|
||||
assert_eq!(query["redirect_uri"], redirect);
|
||||
assert_eq!(query["code_challenge_method"], "S256");
|
||||
assert_eq!(query["scope"], "read offline_access");
|
||||
|
||||
state
|
||||
.handle_callback_with_issuer("valid-authorization-code", &query["state"], None)
|
||||
.await?;
|
||||
let token_requests = requests_to(&server, "/token").await;
|
||||
assert_eq!(token_requests.len(), 1);
|
||||
let request = &token_requests[0];
|
||||
let body: HashMap<_, _> = url::form_urlencoded::parse(&request.body)
|
||||
.into_owned()
|
||||
.collect();
|
||||
assert_eq!(body["client_id"], expected_id);
|
||||
assert_eq!(body["redirect_uri"], redirect);
|
||||
assert_eq!(body["grant_type"], "authorization_code");
|
||||
assert!(body.contains_key("code_verifier"));
|
||||
assert!(!body.contains_key("client_secret"));
|
||||
assert!(!request.headers.contains_key("authorization"));
|
||||
assert!(requests_to(&server, "/register").await.is_empty());
|
||||
assert_eq!(
|
||||
requests_to(&server, "/.well-known/oauth-authorization-server/mcp")
|
||||
.await
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn registration_selection_preserves_dcr_capabilities_and_exact_redirects() -> Result<()> {
|
||||
let native = "http://localhost:43123/callback/abc123ABC_-x";
|
||||
let custom = "https://callbacks.example.com/oauth/callback/abc123ABC_-x";
|
||||
for (metadata, redirect, registration, expected_redirect) in [
|
||||
(
|
||||
json!({"client_id_metadata_document_supported": false}),
|
||||
native,
|
||||
McpOAuthClientRegistration::Auto,
|
||||
native,
|
||||
),
|
||||
(
|
||||
json!({"token_endpoint_auth_methods_supported": null}),
|
||||
native,
|
||||
McpOAuthClientRegistration::Auto,
|
||||
native,
|
||||
),
|
||||
(
|
||||
json!({"token_endpoint_auth_methods_supported": ["private_key_jwt"]}),
|
||||
native,
|
||||
McpOAuthClientRegistration::Auto,
|
||||
native,
|
||||
),
|
||||
(json!({}), custom, McpOAuthClientRegistration::Auto, custom),
|
||||
(
|
||||
json!({"authorization_response_iss_parameter_supported": true}),
|
||||
native,
|
||||
McpOAuthClientRegistration::Dcr,
|
||||
native,
|
||||
),
|
||||
] {
|
||||
let server = oauth_server(metadata).await;
|
||||
let (_, query) = authorization(&server, redirect, registration).await?;
|
||||
assert_eq!(query["client_id"], "dcr-client");
|
||||
assert_eq!(query["redirect_uri"], expected_redirect);
|
||||
let registrations = requests_to(&server, "/register").await;
|
||||
assert_eq!(registrations.len(), 1);
|
||||
let registration: Value = serde_json::from_slice(®istrations[0].body)?;
|
||||
assert_eq!(registration["redirect_uris"], json!([expected_redirect]));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_cimd_metadata_and_redirects_fail_without_dynamic_registration() {
|
||||
let valid = "http://127.0.0.1:43123/callback/abc123ABC_-x";
|
||||
for (metadata, redirect, expected_error) in [
|
||||
(
|
||||
json!({"token_endpoint_auth_methods_supported": ["private_key_jwt"]}),
|
||||
valid,
|
||||
"token endpoint auth method `none`",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1.evil.example:43123/callback/abc123ABC_-x",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1/callback/abc123ABC_-x",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1:43123/callback/wrong-id",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1:43123/callback/abc123ABC_-x?unexpected=true",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://[::1]:43123/callback/abc123ABC_-x",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
] {
|
||||
let server = oauth_server(metadata).await;
|
||||
let error = authorization(&server, redirect, McpOAuthClientRegistration::Cimd)
|
||||
.await
|
||||
.err()
|
||||
.expect("invalid CIMD metadata or callback should fail");
|
||||
assert!(error.to_string().contains(expected_error));
|
||||
assert!(requests_to(&server, "/register").await.is_empty());
|
||||
assert!(requests_to(&server, "/token").await.is_empty());
|
||||
}
|
||||
|
||||
let server = oauth_server(json!({"registration_endpoint": null})).await;
|
||||
let error = authorization(&server, valid, McpOAuthClientRegistration::Dcr)
|
||||
.await
|
||||
.err()
|
||||
.expect("explicit DCR should require an advertised registration endpoint");
|
||||
assert!(error.to_string().contains("registration not supported"));
|
||||
}
|
||||
@@ -446,6 +446,8 @@ fn resolve_redirect_uri(server: &Server, callback_url: Option<&str>) -> Result<S
|
||||
}
|
||||
|
||||
fn callback_id_from_server_url(server_url: &str) -> Result<String> {
|
||||
// Native Codex callback IDs intentionally hash the complete MCP URL (minus its fragment)
|
||||
// with SHA-256. Python connector callback IDs use SHAKE-256 over the origin and are distinct.
|
||||
let mut parsed =
|
||||
Url::parse(server_url).with_context(|| format!("invalid MCP server URL `{server_url}`"))?;
|
||||
parsed
|
||||
@@ -568,6 +570,7 @@ impl OauthLoginFlow {
|
||||
oauth_http_client,
|
||||
&scope_refs,
|
||||
&redirect_uri,
|
||||
&callback_id,
|
||||
client_registration,
|
||||
)
|
||||
.await?
|
||||
@@ -698,7 +701,6 @@ async fn start_authorization(
|
||||
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;
|
||||
auth_manager.set_metadata(metadata);
|
||||
auth_manager.configure_client(
|
||||
@@ -895,7 +897,6 @@ mod tests {
|
||||
assert_eq!(registration_requests.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn oauth_callback_validates_rfc_9207_issuer_before_token_exchange() {
|
||||
for (supports_issuer, callback_issuer, expected_token_requests) in [
|
||||
@@ -1132,12 +1133,7 @@ mod tests {
|
||||
assert_ne!(callback_id, different_path);
|
||||
assert_ne!(callback_id, different_query);
|
||||
assert_ne!(callback_id, different_origin);
|
||||
assert_eq!(callback_id.len(), 12);
|
||||
assert!(
|
||||
callback_id
|
||||
.chars()
|
||||
.all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
|
||||
);
|
||||
assert_eq!(callback_id, "XuuuHAzzHOni");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user