Add enterprise ID-JAG exchange for MCP OAuth (#40722)

## What changed

- Add a non-interactive two-step exchange that obtains an ID-JAG from an
  enterprise identity provider and trades it for a resource-bound MCP bearer
  token.
- Validate trusted endpoint URLs, request inputs, ID-JAG claims, resource and
  scope bindings, and token responses before credentials are forwarded or a
  bearer token is returned.
- Expose structured authentication failures while redacting credentials and
  provider-controlled error details from diagnostics.

## Testing

- Cover successful exchanges, signed scope narrowing, invalid claims and token
  responses, request validation, and error redaction.

GitOrigin-RevId: d716e0e1c2dc6b230cecbc0e9cc09afeee81d599
This commit is contained in:
Nick Steele authored and copyberry committed 2026-08-25 22:12:41 +00:00
1 parent d6174a8798
commit f6805328c4
6 files changed
+913 -1

No files matched your search

@@ -0,0 +1,65 @@
//! Credential-destination policy for enterprise MCP OAuth.
use anyhow::Context;
use anyhow::Result;
use anyhow::bail;
use url::Host;
use url::Url;
/// A sanitized enterprise-auth failure that callers may handle without parsing text.
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum EmaAuthFailure {
#[error("invalid_grant")]
InvalidGrant { grant_source: EmaInvalidGrantSource },
#[error("insufficient_user_authentication")]
InsufficientUserAuthentication,
#[error("enterprise identity requires authentication")]
ReauthenticationRequired,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmaInvalidGrantSource {
EnterpriseIdentity,
ResourceAuthorization,
}
pub(crate) fn safe_oauth_error_code(code: Option<&str>) -> &str {
code.filter(|code| {
matches!(
*code,
"invalid_request"
| "invalid_client"
| "invalid_grant"
| "invalid_scope"
| "invalid_target"
| "unauthorized_client"
| "unsupported_grant_type"
| "access_denied"
| "temporarily_unavailable"
| "server_error"
| "insufficient_user_authentication"
)
})
.unwrap_or("OAuth token request rejected")
}
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)
}
fn validate_credential_destination(url: &Url, description: &str) -> Result<()> {
let loopback = match url.host() {
Some(Host::Domain(host)) => host.eq_ignore_ascii_case("localhost"),
Some(Host::Ipv4(address)) => address.is_loopback(),
Some(Host::Ipv6(address)) => address.is_loopback(),
None => false,
};
if url.scheme() != "https" && !(url.scheme() == "http" && loopback) {
bail!("{description} must use HTTPS or an HTTP loopback address");
}
if !url.username().is_empty() || url.password().is_some() || url.fragment().is_some() {
bail!("{description} contains disallowed credentials or a URL fragment");
}
Ok(())
}