Class OAuth2Handler
java.lang.Object
com.mastercard.developer.oauth2.core.OAuth2Handler
Acts as an orchestrator for adding OAuth2 authentication to API requests.
This class uses the adapter pattern (
HttpAdapter) to work with different HTTP clients,
making it client-agnostic.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordModel for the response from an OAuth2 token endpoint. -
Constructor Summary
ConstructorsConstructorDescriptionOAuth2Handler(OAuth2Config config) Creates a new instance of this class with the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcreateAccessTokenRequestBody(String clientId, String scope, String clientAssertion) Creates a URL-encoded request body for an OAuth2 client credentials token request.static StringcreateClientAssertion(OAuth2Config config) Creates a private_key_jwt client assertion for OAuth2 token requests.static StringcreateResourceRequestDPoP(OAuth2Config config, String dpopKeyId, String htm, String resourceUrl, String accessToken, String nonceOrNull) Creates a DPoP proof token for resource access requests.static StringcreateTokenRequestDPoP(OAuth2Config config, String dpopKeyId, String nonceOrNull) Creates a DPoP proof token for OAuth2 token endpoint requests.<Request,Response>
Responseexecute(Request request, HttpAdapter<Request, Response> adapter) Main entry point that coordinates the entire OAuth2 flow for each intercepted request.parseAccessTokenJson(String accessTokenResponse) Parses an OAuth2 token response JSON.
-
Constructor Details
-
OAuth2Handler
Creates a new instance of this class with the given configuration.
-
-
Method Details
-
execute
public <Request,Response> Response execute(Request request, HttpAdapter<Request, Response> adapter) throws ExceptionMain entry point that coordinates the entire OAuth2 flow for each intercepted request.- Throws:
Exception
-
createClientAssertion
Creates a private_key_jwt client assertion for OAuth2 token requests. The assertion is signed with the client's private key. See: Private Key JWT -
createTokenRequestDPoP
public static String createTokenRequestDPoP(OAuth2Config config, String dpopKeyId, String nonceOrNull) Creates a DPoP proof token for OAuth2 token endpoint requests. This token proves possession of the DPoP key pair when requesting an access token. -
createResourceRequestDPoP
public static String createResourceRequestDPoP(OAuth2Config config, String dpopKeyId, String htm, String resourceUrl, String accessToken, String nonceOrNull) Creates a DPoP proof token for resource access requests. This token proves possession of the DPoP key pair and binds it to the access token. -
parseAccessTokenJson
Parses an OAuth2 token response JSON. Example:{ "access_token": "eyJ4NXQjUzI1NiI6Ii1sb...LTE69XYj5oPIq4PZf2WaMxLow", "token_type": "DPoP", "expires_in": 900, "scope": "service:scope1 service:scope2" }See: RFC 6749 Section 5.1 -
createAccessTokenRequestBody
public static String createAccessTokenRequestBody(String clientId, String scope, String clientAssertion) Creates a URL-encoded request body for an OAuth2 client credentials token request.
-