Class OAuth2ClientCredentialsGrantCredentialsProvider
- All Implemented Interfaces:
CredentialsProvider
CredentialsProvider that performs an
OAuth 2 Client Credentials flow
to retrieve a token.
The provider has different parameters to set, e.g. the token endpoint URI of the OAuth server to
request, the client ID, the client secret, the grant type, etc. The OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
class is the preferred way to create an instance of the provider.
The implementation uses the JDK HttpURLConnection API to request the OAuth server. This can
be easily changed by overriding the retrieveToken() method.
This class expects a JSON document as a response and needs Jackson
to deserialize the response into a OAuth2ClientCredentialsGrantCredentialsProvider.Token. This can be changed by overriding the parseToken(String)
method.
TLS is supported by providing a HTTPS URI and setting a SSLContext. See
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder.tls() for more information.
Applications in production should always use HTTPS to retrieve tokens.
If more customization is needed, a connectionConfigurator callback can be provided to configure
the connection.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classstatic classHelper to createOAuth2ClientCredentialsGrantCredentialsProviderinstances.static classTLS configuration for aOAuth2ClientCredentialsGrantCredentialsProvider.static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Stringprivate final Stringprivate final Consumer<HttpURLConnection> private final Stringprivate final HostnameVerifierprivate final Stringprivate final SSLSocketFactoryprivate final Stringprivate final AtomicReference<Function<String, OAuth2ClientCredentialsGrantCredentialsProvider.Token>> private static final String -
Constructor Summary
ConstructorsConstructorDescriptionOAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, Consumer<HttpURLConnection> connectionConfigurator) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory, Consumer<HttpURLConnection> connectionConfigurator) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringbasicAuthentication(String username, String password) protected voidcheckContentType(String headerField) protected voidcheckResponseCode(int responseCode) protected voidconfigureConnection(HttpURLConnection connection) protected voidconfigureConnectionForHttps(HttpURLConnection connection) private static StringBuilderencode(StringBuilder builder, String name, String value) private static Stringbooleanprotected StringextractResponseBody(InputStream inputStream) Username to use for authenticationinthashCode()parseToken(String response) protected Stringprotected Durationprotected StringMethods inherited from class com.rabbitmq.client.impl.RefreshProtectedCredentialsProvider
getPassword, getTimeBeforeExpiration, refresh
-
Field Details
-
UTF_8_CHARSET
- See Also:
-
tokenEndpointUri
-
clientId
-
clientSecret
-
grantType
-
parameters
-
tokenExtractor
private final AtomicReference<Function<String,OAuth2ClientCredentialsGrantCredentialsProvider.Token>> tokenExtractor -
id
-
hostnameVerifier
-
sslSocketFactory
-
connectionConfigurator
-
-
Constructor Details
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.- Parameters:
tokenEndpointUri-clientId-clientSecret-grantType-
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.- Parameters:
tokenEndpointUri-clientId-clientSecret-grantType-parameters-
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, Consumer<HttpURLConnection> connectionConfigurator) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.- Parameters:
tokenEndpointUri-clientId-clientSecret-grantType-parameters-connectionConfigurator-
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.- Parameters:
tokenEndpointUri-clientId-clientSecret-grantType-hostnameVerifier-sslSocketFactory-
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.- Parameters:
tokenEndpointUri-clientId-clientSecret-grantType-parameters-hostnameVerifier-sslSocketFactory-
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory, Consumer<HttpURLConnection> connectionConfigurator) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilderto create an instance.- Parameters:
tokenEndpointUri-clientId-clientSecret-grantType-parameters-hostnameVerifier-sslSocketFactory-connectionConfigurator-
-
-
Method Details
-
encode
private static StringBuilder encode(StringBuilder builder, String name, String value) throws UnsupportedEncodingException - Throws:
UnsupportedEncodingException
-
encode
- Throws:
UnsupportedEncodingException
-
basicAuthentication
-
getUsername
Description copied from interface:CredentialsProviderUsername to use for authentication- Specified by:
getUsernamein interfaceCredentialsProvider- Overrides:
getUsernamein classRefreshProtectedCredentialsProvider<OAuth2ClientCredentialsGrantCredentialsProvider.Token>- Returns:
- username
-
usernameFromToken
-
parseToken
-
retrieveToken
- Specified by:
retrieveTokenin classRefreshProtectedCredentialsProvider<OAuth2ClientCredentialsGrantCredentialsProvider.Token>
-
checkContentType
- Throws:
OAuthTokenManagementException
-
checkResponseCode
- Throws:
OAuthTokenManagementException
-
extractResponseBody
- Throws:
IOException
-
passwordFromToken
-
timeBeforeExpiration
protected Duration timeBeforeExpiration(OAuth2ClientCredentialsGrantCredentialsProvider.Token token) -
configureConnection
-
configureConnectionForHttps
-
equals
-
hashCode
public int hashCode()
-