Add enterprise IdP identity resolution for MCP OAuth (#40739)

## What changed

- Resolve stored enterprise IdP sessions against discovered authorization
  metadata, requiring the configured issuer, public-client authentication, and
  supported ID-JAG token exchange capabilities.
- Bind OIDC identity claims and MCP resource indicators to their configured
  issuer, client, and server, and require reauthentication when pinned keyring
  credentials are removed or replaced.
- Hold the credential lock while rereading refresh tokens, and isolate
  enterprise credentials by reserved namespace and Codex home.

## Testing

- Cover metadata and claim validation, resource binding, credential replacement
  and keyring failures, refresh locking, expired ID tokens, and credential
  namespace isolation.

GitOrigin-RevId: edce3c6159f7d6831edf72e9608b3fc3f5823c83
This commit is contained in:
Nick Steele authored and copyberry committed 2026-08-25 23:51:57 +00:00
1 parent 75cb7c903d
commit 9b4a0f8a0a
12 files changed
+932 -14

No files matched your search

@@ -2,7 +2,9 @@
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::bail;
use serde_json::Value;
use url::Host;
use url::Url;
@@ -23,6 +25,10 @@ pub enum EmaInvalidGrantSource {
ResourceAuthorization,
}
pub(crate) fn ema_reauthentication_required(message: &'static str) -> anyhow::Error {
anyhow::Error::new(EmaAuthFailure::ReauthenticationRequired).context(message)
}
pub(crate) fn safe_oauth_error_code(code: Option<&str>) -> &str {
code.filter(|code| {
matches!(
@@ -43,6 +49,24 @@ pub(crate) fn safe_oauth_error_code(code: Option<&str>) -> &str {
.unwrap_or("OAuth token request rejected")
}
pub(crate) fn validate_ema_public_client_auth(
advertised_methods: Option<&Value>,
issuer_description: &str,
) -> Result<()> {
let advertised_methods = advertised_methods.ok_or_else(|| {
anyhow!(
"{issuer_description} does not explicitly advertise public-client token endpoint authentication"
)
})?;
let methods = advertised_methods.as_array().ok_or_else(|| {
anyhow!("{issuer_description} advertised malformed token endpoint authentication methods")
})?;
if !methods.iter().any(|method| method.as_str() == Some("none")) {
bail!("{issuer_description} does not support public-client token endpoint authentication");
}
Ok(())
}
pub(crate) fn validate_ema_oauth_endpoint(endpoint: &str, description: &str) -> Result<()> {
let url = Url::parse(endpoint).with_context(|| format!("{description} is not a valid URL"))?;
validate_credential_destination(&url, description)
@@ -63,3 +87,48 @@ fn validate_credential_destination(url: &Url, description: &str) -> Result<()> {
}
Ok(())
}
pub(crate) fn advertised_capability(
value: Option<&Value>,
expected: &str,
description: &str,
) -> Result<Option<bool>> {
value
.map(|value| {
let values = value
.as_array()
.ok_or_else(|| anyhow!("{description} is malformed"))?;
Ok(values.iter().any(|value| value.as_str() == Some(expected)))
})
.transpose()
}
/// Resource indicators must describe the configured MCP origin, query and path.
pub fn validate_ema_auth_resource(server_url: &str, resource: Option<&str>) -> Result<()> {
let server = Url::parse(server_url).context("enterprise MCP server URL is invalid")?;
validate_credential_destination(&server, "enterprise MCP server URL")?;
let Some(resource) = resource.filter(|resource| !resource.trim().is_empty()) else {
return Ok(());
};
let resource = Url::parse(resource).context("enterprise MCP resource indicator is invalid")?;
validate_credential_destination(&resource, "enterprise MCP resource indicator")?;
if resource.origin() != server.origin() || resource.query() != server.query() {
bail!(
"enterprise MCP resource indicator must match the configured MCP server origin and query"
);
}
let resource_path = resource.path().trim_end_matches('/');
let server_path = server.path().trim_end_matches('/');
if server_path != resource_path
&& !server_path
.strip_prefix(resource_path)
.is_some_and(|suffix| suffix.starts_with('/'))
{
bail!("enterprise MCP resource indicator path must contain the configured MCP server path");
}
Ok(())
}
#[cfg(test)]
#[path = "ema_auth_policy_tests.rs"]
mod tests;