Class McpAuthorizationCodeAcquirer
Builds on the relocated OAuthAuthorizationCodeFlow: opens a LoopbackRedirectReceiver, generates the
state and PKCE code_verifier/code_challenge, hands the authorization URL to a caller-supplied
browser launcher, waits for the loopback redirect, and exchanges the returned code for a token.
Security checks on the callback:
- CSRF — the returned
statemust match the one that was issued (single-use viaEphemeralStore). - SEP-2468 (
iss, RFC 9207) — the callbackissparameter must equal the discovered/expected issuer; a mismatch is rejected. The check gates BOTH the success and error paths (a client MUST NOT act on an error response whose issuer doesn't match).McpAuthorizationCodeAcquirer.Builder.expectedIssuer(URI)is required by default (opt out withMcpAuthorizationCodeAcquirer.Builder.skipIssuerValidation(boolean)); a missingissis accepted unlessMcpAuthorizationCodeAcquirer.Builder.requireIssuerResponseParameter(boolean)is set.
McpAuthorizationCodeAcquirer.Builder.resource(URI) (the RFC 8707 resource indicator) is required so the acquired token's audience is bound
to the target MCP server.
The optional McpAuthorizationCodeAcquirer.Builder.offlineAccess(boolean) adds the offline_access scope so the IdP issues a refresh
token that can subsequently drive an McpTokenProvider refresh-token provider (SEP-2207).
- Since:
- 10.0.0
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor. -
Method Summary
Modifier and TypeMethodDescriptionRuns the full interactive acquisition: starts the loopback listener, opens the authorization URL, waits for the redirect, validates it, and exchanges the code for a token.buildAuthorizationUrl(OAuthAuthorizationCodeFlow flow, String state, com.nimbusds.oauth2.sdk.pkce.CodeVerifier verifier) Builds the authorization URL for a given state and PKCE verifier (exposed for callers driving their own user-agent interaction).create()Static creator.handleCallback(OAuthAuthorizationCodeFlow flow, URI callbackUri) Validates a loopback callback and exchanges the authorization code for a token.static StringvalidateAuthorizationResponse(URI callbackUri, String expectedState, URI expectedIssuer) Validates an authorization-code callback and returns the authorization code, without any token exchange or state-store interaction.static StringvalidateAuthorizationResponse(URI callbackUri, String expectedState, URI expectedIssuer, boolean requireIssuerParameter) Validates an authorization-code callback and returns the authorization code, without any token exchange or state-store interaction.
-
Constructor Details
-
McpAuthorizationCodeAcquirer
Constructor.- Parameters:
b- The builder.
-
-
Method Details
-
create
Static creator.- Returns:
- A new builder.
-
acquire
Runs the full interactive acquisition: starts the loopback listener, opens the authorization URL, waits for the redirect, validates it, and exchanges the code for a token.- Parameters:
timeout- The maximum time to wait for the user to complete authorization at the IdP. Must not benull .- Returns:
- The acquired token.
- Throws:
McpAuthException- If the listener cannot start, the wait times out, or the callback fails validation.OAuthFlowException- If the token-exchange HTTP round-trip fails or the IdP returns an error.
-
buildAuthorizationUrl
public URI buildAuthorizationUrl(OAuthAuthorizationCodeFlow flow, String state, com.nimbusds.oauth2.sdk.pkce.CodeVerifier verifier) Builds the authorization URL for a given state and PKCE verifier (exposed for callers driving their own user-agent interaction).- Parameters:
flow- The configured flow. Must not benull .state- The opaque state. Must not benull or blank.verifier- The PKCE verifier. Must not benull .- Returns:
- The authorization URL.
-
handleCallback
Validates a loopback callback and exchanges the authorization code for a token.Consumes the pending
stateentry (single-use), performs the SEP-2468isscheck against the issuer recorded when the request was started, then exchanges the code.- Parameters:
flow- The configured flow. Must not benull .callbackUri- The full loopback callback URI. Must not benull .- Returns:
- The acquired token.
- Throws:
McpAuthException- If the callback is an error response, the state is unknown/replayed, or theisscheck fails.
-
validateAuthorizationResponse
public static String validateAuthorizationResponse(URI callbackUri, String expectedState, URI expectedIssuer) Validates an authorization-code callback and returns the authorization code, without any token exchange or state-store interaction.This is the pure SEP-2468 / CSRF validation step. The
isscomparison gates BOTH success and error responses (per the MCP spec a client MUST NOT act on anerror/error_description/error_urifrom a response whoseissdoes not match), so an error response with a mismatched issuer is reported as an issuer mismatch rather than a denial. A missing/mismatchedstateis rejected as CSRF. Whenissis absent it is accepted (future-proofed for the eventual "reject if absent" MUST); usevalidateAuthorizationResponse(URI, String, URI, boolean)to require it.- Parameters:
callbackUri- The full callback URI. Must not benull .expectedState- The state issued when the authorization request was built. Must not benull .expectedIssuer- The discovered issuer to validateissagainst. Must not benull .- Returns:
- The authorization code.
- Throws:
McpAuthException- If validation fails.
-
validateAuthorizationResponse
public static String validateAuthorizationResponse(URI callbackUri, String expectedState, URI expectedIssuer, boolean requireIssuerParameter) Validates an authorization-code callback and returns the authorization code, without any token exchange or state-store interaction.- Parameters:
callbackUri- The full callback URI. Must not benull .expectedState- The state issued when the authorization request was built. Must not benull .expectedIssuer- The discovered issuer to validateissagainst. Must not benull .requireIssuerParameter-true to reject a response that omits theissparameter (RFC 9207authorization_response_iss_parameter_supported).- Returns:
- The authorization code.
- Throws:
McpAuthException- If validation fails.
-