public final class OidcTokenExchange
extends java.lang.Object
/oauth2/token endpoint
for exchanging authorisation codes and refreshing tokens.
All methods read configuration from OidcConfig and POST
form-encoded requests to the Cognito token endpoint.
| Modifier and Type | Field and Description |
|---|---|
static int |
REFRESH_COOLDOWN_SECONDS
Cooldown period in seconds — refresh tokens created within this window cannot be refreshed.
|
| Modifier and Type | Method and Description |
|---|---|
static java.lang.String |
buildAuthorizationCodeRequestForm(java.lang.String code,
java.lang.String redirectUri,
java.lang.String codeVerifier)
Builds the
application/x-www-form-urlencoded body for a Cognito
authorization_code token request. |
static void |
clearAllCooldowns()
Clears all cooldown entries.
|
static void |
clearRefreshTokenCooldown(java.lang.String refreshToken)
Removes a refresh token from the cooldown cache.
|
static OidcTokenResponse |
exchangeAuthCode(java.lang.String code)
Exchanges an authorisation code for tokens using the
authorization_code grant type. |
static OidcTokenResponse |
exchangeAuthCodeWithPkce(java.lang.String code,
java.lang.String redirectUri,
java.lang.String codeVerifier)
Exchanges an authorisation code for tokens using PKCE and an explicit
redirect_uri (typically the SPA callback URL registered in Cognito). |
static OidcTokenResponse |
parseTokenResponse(java.lang.String responseBody)
Parses a successful JSON response into an
OidcTokenResponse. |
static OidcTokenResponse |
postTokenRequest(java.lang.String tokenEndpoint,
java.lang.String formBody)
Posts a form-encoded request to the given token endpoint and parses
the JSON response.
|
static void |
recordRefreshTokenCreation(java.lang.String refreshToken)
Records that a refresh token has just been created or refreshed.
|
static OidcTokenResponse |
refreshTokens(java.lang.String refreshToken)
Exchanges a refresh token for new access and ID tokens using the
refresh_token grant type. |
public static final int REFRESH_COOLDOWN_SECONDS
@CheckReturnValue @Nonnull public static OidcTokenResponse exchangeAuthCode(@Nonnull java.lang.String code) throws OidcTokenExchangeException
authorization_code grant type.code - the authorisation code received from Cognito's callbackOidcTokenExchangeException - if Cognito returns an error or the request fails@CheckReturnValue @Nonnull public static OidcTokenResponse exchangeAuthCodeWithPkce(@Nonnull java.lang.String code, @Nonnull java.lang.String redirectUri, @Nonnull java.lang.String codeVerifier) throws OidcTokenExchangeException
redirect_uri (typically the SPA callback URL registered in Cognito).
Use this for Option A: the browser lands on the SPA with ?code=; the SPA
POSTs code, redirect_uri, and code_verifier to the backend.
code - the authorisation coderedirectUri - the exact redirect URI sent to Cognito on authorizecodeVerifier - the PKCE code verifierOidcTokenExchangeException - if Cognito returns an error or the request fails@CheckReturnValue
@Nonnull
public static java.lang.String buildAuthorizationCodeRequestForm(@Nonnull
java.lang.String code,
@Nonnull
java.lang.String redirectUri,
@Nullable
java.lang.String codeVerifier)
application/x-www-form-urlencoded body for a Cognito
authorization_code token request.code - authorisation coderedirectUri - redirect URI (must match the authorize request)codeVerifier - PKCE verifier, or null to omit@CheckReturnValue @Nonnull public static OidcTokenResponse refreshTokens(@Nonnull java.lang.String refreshToken) throws OidcTokenExchangeException
refresh_token grant type.
Note: Cognito does not return a new refresh token in the response for refresh_token grants.
refreshToken - the refresh tokenOidcTokenExchangeException - if Cognito returns an error or the request fails@CheckReturnValue @Nonnull public static OidcTokenResponse postTokenRequest(@Nonnull java.lang.String tokenEndpoint, @Nonnull java.lang.String formBody) throws OidcTokenExchangeException
tokenEndpoint - the Cognito token endpoint URLformBody - the URL-encoded form bodyOidcTokenExchangeException - on HTTP or parsing errors@CheckReturnValue @Nonnull public static OidcTokenResponse parseTokenResponse(@Nonnull java.lang.String responseBody) throws OidcTokenExchangeException
OidcTokenResponse.OidcTokenExchangeExceptionpublic static void recordRefreshTokenCreation(@Nonnull
java.lang.String refreshToken)
refreshTokens(java.lang.String) within the cooldown period
will be rejected with a too_many_requests error.refreshToken - the refresh token to recordpublic static void clearRefreshTokenCooldown(@Nonnull
java.lang.String refreshToken)
refreshToken - the refresh token to removepublic static void clearAllCooldowns()
stSoftware Copyright © 2001-2014 stSoftware All Rights Reserved.