etzel-mes opened a new pull request, #31372: URL: https://github.com/apache/camel/pull/31372
# Description [CAMEL-27121](https://issues.apache.org/jira/browse/CAMEL-27121), the "on behalf of the user" part of [CAMEL-27120](https://issues.apache.org/jira/browse/CAMEL-27120): a route that has validated a user's token can now call another API **as that user**, through RFC 8693 token exchange. It needs only configuration. ## Why this matters Camel can already validate a caller's token on `platform-http`, the MCP server and A2A, and since CAMEL-27122 camel-http can send a profile token per exchange. What was missing is the link between the two. A tool route that has to call a protected API today has two options, and both are wrong: - **It forwards the user's token.** The MCP authorization specification forbids this ("token passthrough"). The token was issued for the MCP server, not for the downstream API, so either the API accepts a token meant for someone else or the call fails. Either way, a server that relays tokens it did not receive for itself is a confused deputy. - **It calls as itself, with client credentials.** The downstream API then sees one service identity for every user. It cannot apply the user's permissions, and its audit log no longer says who acted. Every agent becomes a superuser behind a shared secret. Token exchange is the standard answer (RFC 8693, and what the MCP specification points to). The MCP server exchanges the user's token at the authorization server for a new token with `sub` = the user, `aud` = the downstream API and `azp` = the MCP server. The downstream API applies the user's own rights and records who acted through whom. The model, the prompt and message headers cannot choose that identity; only the endpoint configuration decides it. This is the base of CAMEL-27120. The later slices (SPIFFE client assertions, the JWT bearer grant for LLM providers, the other `oauthProfile` producers) build on the per-exchange resolution and the retained token added here. As AI agents call business systems through Camel, it is what lets those calls stay tied to the person who asked. ```properties camel.server.mcp-oauth-profile = users camel.oauth.users.base-uri = https://idp.example.com/realms/demo camel.oauth.users.expected-audience = mcp-server-api camel.oauth.users.retain-token = true camel.oauth.on-behalf-of-user.grant-type = token-exchange camel.oauth.on-behalf-of-user.token-endpoint = https://idp.example.com/realms/demo/protocol/openid-connect/token camel.oauth.on-behalf-of-user.client-id = mcp-server camel.oauth.on-behalf-of-user.client-secret = {{env:MCP_SERVER_SECRET}} camel.oauth.on-behalf-of-user.audience = conference-api ``` ```yaml - to: uri: https://api.example.com/api/my-sessions parameters: oauthProfile: on-behalf-of-user ``` ## What changes - **`retain-token`** on validation profiles (default `false`). A valid token is kept on `OAuthTokenValidationResult` as `bearerToken()`. That accessor is deliberately not a bean getter, so Jackson and introspection do not write it. `toString()` leaves it out, nothing copies it to a header, and an invalid result never carries one. The result already travels in the exchange property every OAuth consumer sets (HTTP consumers, MCP server, A2A), so there is one path and no new one. A sender cannot set an exchange property, whereas it can set a header. - **`grant-type=token-exchange`** on client profiles, with `audience`, `resource`, `scope`, `requested-token-type`, `subject-token-type` (default: the access_token URN) and `allow-insecure-http`. The subject token is the retained token of the exchange's caller. - **`OAuthClientAuthenticationFactory.resolveToken(Exchange, String)`** (a default method, so existing factories keep working) and **`OAuthHelper.resolveOAuthToken(Exchange, String)`**. camel-http's `oauthProfile` resolves for the exchange it sends; the polling consumer, which has no exchange, keeps the context path. - **`OAuthTokenValidationResult.EXCHANGE_PROPERTY`** names the property once. The existing constants in camel-http-base, A2A and the MCP server API become aliases of it. - **Docs:** a camel-oauth section "Token Exchange on Behalf of the Caller", plus camel-http and mcp-server notes. ## Security behaviour, following `security-model.adoc` - **Fails closed:** no validated caller, an invalid caller, or a validation profile without `retain-token` refuses before any request reaches the authorization server. There is never a fallback to the client's own identity. - **Per-user cache:** keyed by token endpoint, client, a SHA-256 fingerprint of the secret, a SHA-256 fingerprint of the caller's token, and every exchange parameter. Two users never share a token. - Bounded LRU of 1000 tokens, one token request per key under concurrency. - A response without `expires_in` is not cached. - Once the caller's own token has expired the cache is skipped, so the authorization server decides. - **Only `Bearer` tokens are sent:** `DPoP`, `N_A` (RFC 8693 ยง2.2.1: not usable as an access token), any other type and a missing `token_type` are refused. - **The user's token is in no error, log or cache key.** Authorization server errors report `error` and `error_description`. - **Plain HTTP:** a plain-HTTP token endpoint needs `allow-insecure-http`, as validation profiles already do, because the request carries the user's token. - **Bounded response:** the token response is limited to 64 KiB, like introspection. - **Redirects:** camel-http's authority check (`HttpOAuth2RedirectTokenLeakTest`) still runs before the token is resolved. ## Differences from the JIRA text - `@since 4.25`, because 4.24.0 is already released. - `allow-insecure-http` on token-exchange profiles is not in the JIRA. It is added because the request carries the user's token. - No new fields on `OAuthClientConfig`. Its only entry point has no exchange, so a token exchange there could only be refused. Token exchange is therefore configured through property profiles; the jwt-bearer and assertion slices are the place to extend `OAuthClientConfig`. - Client authentication is `client_secret_basic` only; client assertions are CAMEL-27121's next slice. This PR does **not** resolve CAMEL-27121. Still open there: the jwt-bearer and assertion grants, client assertions, `subject-token-provider`, `extra-parameter` and `token-request-format`. ## Tests (JDK 21, no reruns) - **Unit suites:** - camel-oauth full suite: 291 run, 0 failures. The 9 skipped tests were skipped before this change. - camel-http-base `OAuthHttpSecuritySupportTest`: 24/24. - camel-http OAuth and activity tests: 57 run. `HttpOAuthProfileProducerTest` passed 5/5 in every run; `HttpOAuth2TokenCachingTest` is the exception, see the note below. - camel-mcp-server OAuth, authentication and caller-identity tests: 33/33. - camel-a2a auth tests: 30/30. - **Without containers**, `OAuthRetainTokenPlatformHttpTest` runs the whole path: platform-http (`retain-token`) โ camel-http `oauthProfile` (token exchange) โ local token endpoint โ API route. It also runs the refusal path. `McpServerMainOAuthTest` asserts that a tool route receives the retained token. - **Against a real Keycloak 26.8.2** (`OAuthTokenExchangeKeycloakIT`, test-infra, gated like the existing MCP Keycloak IT): 5/5. - alice and bob each reach the API as themselves: `sub` = their own id, `aud=[conference-api]`, `azp=mcp-server`. - A client without the exchange permission gets `400 invalid_request (Standard token exchange is not enabled for the requested client)`. - The anonymous route and the not-retained route are refused before Keycloak is called. - The API counted only the successful calls, and no caller header contains the user token. - **Red/green and mutants:** the key tests fail with the behaviour reverted. 37 mutants on the branches that matter: 36 killed, 1 equivalent (the expiry check on read still guards). - **Source check:** `-Psourcecheck` is clean on the seven changed modules. The catalog doc copies are byte-identical, and the YAML examples on the changed pages pass Camel's YAML validator. - **Not run locally:** the full root `mvn clean install -DskipTests` and the full camel-http suite. **Note: `HttpOAuth2TokenCachingTest`** (the `oauth2*` options, a path this change does not run) sometimes gets a `java.net.SocketException` after it closes its own token server, on a loaded machine. It also failed 1 of 10 runs on upstream code. It is worth its own small change, as CAMEL-25579 widened the accepted exceptions once. ## Follow-ups (not in this PR) - The other producers with `oauthProfile` (camel-openai, docling, the langchain4j/spring-ai MCP clients, the A2A client) still resolve without the exchange. A token-exchange profile there fails closed with a clear message. - `OAuthTokenRequest`'s client-credentials and refresh requests keep their current error handling. Unifying them with the new token request (OAuth error reporting, UTF-8 form) changes existing behaviour and belongs in its own change. @Croway @gnodet: Federico, this follows your CAMEL-27120 design. Guillaume, you handled CAMEL-27122 and CAMEL-27123. A review would be very welcome. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (Not from the root: `-Psourcecheck` on the seven changed modules, catalog doc copies checked byte for byte.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. Written with Claude Code (Claude Opus 5.5 implementation, independent review by a second model, review fixes in the second commit). _Claude Code on behalf of etzel-mes_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
