Class OAuth2AuthenticationProvider
- All Implemented Interfaces:
AuthenticationProvider<Credentials>
- Direct Known Subclasses:
OpenIDAuthenticationProvider
AuthenticationProvider that supports various OAuth 2.0 flows (e.g., AUTH_CODE,
PASSWORD, CLIENT, etc.). This class handles interaction with an OAuth2 authorization server, optionally
starting a local HTTP server to capture callbacks for redirect-based flows.
Typical usage involves:
- Creating an instance with the desired
OAuth2Options - Directing the end-user to the authorization URL (
authorizeUrl(OAuth2Credentials)) if needed - Using
authenticate(Credentials)to acquire aUserobject
This class can also handle optional tasks such as token introspection, token refresh, token revocation, user information retrieval, and logout.
-
Constructor Summary
ConstructorsConstructorDescriptionOAuth2AuthenticationProvider(@Nullable javafx.stage.Stage stage, @NotNull OAuth2API api) Creates an OAuth2 authentication provider.OAuth2AuthenticationProvider(@Nullable javafx.stage.Stage stage, @NotNull OAuth2Options options) Creates an OAuth2 authentication provider using the specifiedOAuth2Options. -
Method Summary
Modifier and TypeMethodDescriptionauthenticate(@NotNull Credentials credentials) Authenticates a user based on the givenCredentials.authorizeUrl(@NotNull OAuth2Credentials credentials) Generates an authorization URL based on the providedOAuth2Credentials, then prepares (and possibly starts) a local HTTP server to capture the OAuth2 authorization callback.discover()Initiates OpenID Connect Discovery to dynamically obtain configuration data (e.g., token endpoint, authorization endpoint, user info endpoint).final @NotNull OAuth2OptionsReturns theOAuth2Optionsused by this provider.CompletableFuture<org.json.JSONObject> introspect(User user, String tokenType) Performs token introspection for the specifiedtokenTypein the user'sauthdata.Logs out the specified user by invalidating (or otherwise marking as invalid) its access token (and optionally refresh token) on the authorization server.Refreshes an existing user session using its refresh token.Revokes the given user'saccess_tokenorrefresh_token.CompletableFuture<org.json.JSONObject> Retrieves detailed user information (e.g., profile data) from an OpenID Connect or OAuth2userInfoendpoint using the user'saccess_token.
-
Constructor Details
-
OAuth2AuthenticationProvider
public OAuth2AuthenticationProvider(@Nullable @Nullable javafx.stage.Stage stage, @NotNull @NotNull OAuth2API api) Creates an OAuth2 authentication provider.This constructor sets up the internal
HttpServerto handle redirect-based OAuth2 flows, especially ifauthorizeUrl(OAuth2Credentials)is used to open a browser flow.- Parameters:
stage- the JavaFX application stage (maybenullif you are running in a non-JavaFX environment)api- the OAuth2 API utility, which must not benull- Throws:
NullPointerException- ifapiisnullIllegalArgumentException- if any mandatory option inOAuth2Optionsis invalid
-
OAuth2AuthenticationProvider
public OAuth2AuthenticationProvider(@Nullable @Nullable javafx.stage.Stage stage, @NotNull @NotNull OAuth2Options options) Creates an OAuth2 authentication provider using the specifiedOAuth2Options.- Parameters:
stage- the JavaFX application stage (maybenull)options- the OAuth2 options used to configure OAuth2 interaction- Throws:
NullPointerException- ifoptionsisnullIllegalArgumentException- if any mandatory option inOAuth2Optionsis invalid
-
-
Method Details
-
getOptions
Returns theOAuth2Optionsused by this provider.- Returns:
- the OAuth2 configuration options
-
authorizeUrl
Generates an authorization URL based on the providedOAuth2Credentials, then prepares (and possibly starts) a local HTTP server to capture the OAuth2 authorization callback. The returnedCompletableFuturecompletes with the authorization URL once the server is ready.You typically call this method when using
OAuth2Flow.AUTH_CODEor other flows requiring user interaction in a web browser. If not running in a browser environment (i.e.,WebAPI.isBrowser()is false), the method also ensures a fresh local HTTP server is created to handle the callback.- Parameters:
credentials- the OAuth2 credentials containing the flow configuration; must not benull- Returns:
- a future that completes with the authorization URL once the local server is ready, or completes exceptionally if server setup fails
- Throws:
NullPointerException- ifcredentialsisnull
-
authenticate
Authenticates a user based on the givenCredentials. Depending on the type of credentials, different OAuth2 flows or validations may be triggered:UsernamePasswordCredentialstriggers the Password flow.TokenCredentialsattempts to validate an existing token, optionally performing token introspection if the token is not a valid JWT or is expired.OAuth2Credentialstriggers the configured OAuth2 flow (e.g., AUTH_CODE, PASSWORD, CLIENT, etc.).
- Specified by:
authenticatein interfaceAuthenticationProvider<Credentials>- Parameters:
credentials- the credentials to authenticate- Returns:
- a future that completes with the authenticated
User, or completes exceptionally if authentication fails - Throws:
ClassCastException- ifcredentialsis not a recognized subtypeCredentialValidationException- if the credentials fail validationRuntimeException- if the flow is unsupported, or if token introspection is unavailable- See Also:
-
discover
Initiates OpenID Connect Discovery to dynamically obtain configuration data (e.g., token endpoint, authorization endpoint, user info endpoint). TheCompletableFuturecompletes with a newOpenIDAuthenticationProviderupon success or completes exceptionally on failure.- Returns:
- a future that completes with an
OpenIDAuthenticationProvideronce discovery succeeds
-
introspect
Performs token introspection for the specifiedtokenTypein the user'sauthdata.The introspection response is returned as a
JSONObject. For more details, see RFC 7662.- Parameters:
user- the user whose token is to be introspectedtokenType- the token type to introspect (e.g.,"access_token"or"refresh_token")- Returns:
- a future that completes with the introspection response as JSON
- Throws:
NullPointerException- ifuseris null
-
refresh
Refreshes an existing user session using its refresh token. Upon success, returns a newUserinstance with updated tokens.- Parameters:
user- the user whose token will be refreshed- Returns:
- a future that completes with the new user instance containing updated tokens
- Throws:
IllegalStateException- if the user has no valid refresh token
-
revoke
Revokes the given user'saccess_tokenorrefresh_token.See RFC 7009 for details on token revocation.
- Parameters:
user- the user for whom to revoke the tokentokenType- the token type to revoke (either"access_token"or"refresh_token")- Returns:
- a future that completes when the token is successfully revoked
-
userInfo
Retrieves detailed user information (e.g., profile data) from an OpenID Connect or OAuth2userInfoendpoint using the user'saccess_token.This method also checks to ensure that the returned
sub(subject) matches the one in the token's claims if present. If the endpoint returns atokenfield, it will attempt to verify that token as well.- Parameters:
user- the user whose information is to be fetched (must have anaccess_token)- Returns:
- a future that completes with the user information in JSON format, or completes exceptionally if validation fails
- Throws:
NullPointerException- ifuserisnullAuthenticationException- if the subject in the UserInfo response does not matchTokenExpiredException- if thetokenfield has an expired tokenIllegalStateException- if thetokenfield is invalid in any way
-
logout
Logs out the specified user by invalidating (or otherwise marking as invalid) its access token (and optionally refresh token) on the authorization server.- Parameters:
user- the user to logout- Returns:
- a future that completes when the user is logged out
-