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 class
static class
Helper to createOAuth2ClientCredentialsGrantCredentialsProvider
instances.static class
TLS configuration for aOAuth2ClientCredentialsGrantCredentialsProvider
.static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
private final String
private final Consumer
<HttpURLConnection> private final String
private final HostnameVerifier
private final String
private final SSLSocketFactory
private final String
private final AtomicReference
<Function<String, OAuth2ClientCredentialsGrantCredentialsProvider.Token>> private static final String
-
Constructor Summary
ConstructorsConstructorDescriptionOAuth2ClientCredentialsGrantCredentialsProvider
(String tokenEndpointUri, String clientId, String clientSecret, String grantType) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.OAuth2ClientCredentialsGrantCredentialsProvider
(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.OAuth2ClientCredentialsGrantCredentialsProvider
(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, Consumer<HttpURLConnection> connectionConfigurator) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.OAuth2ClientCredentialsGrantCredentialsProvider
(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.OAuth2ClientCredentialsGrantCredentialsProvider
(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory, Consumer<HttpURLConnection> connectionConfigurator) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.OAuth2ClientCredentialsGrantCredentialsProvider
(String tokenEndpointUri, String clientId, String clientSecret, String grantType, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static String
basicAuthentication
(String username, String password) protected void
checkContentType
(String headerField) protected void
checkResponseCode
(int responseCode) protected void
configureConnection
(HttpURLConnection connection) protected void
configureConnectionForHttps
(HttpURLConnection connection) private static StringBuilder
encode
(StringBuilder builder, String name, String value) private static String
boolean
protected String
extractResponseBody
(InputStream inputStream) Username to use for authenticationint
hashCode()
parseToken
(String response) protected String
protected Duration
protected String
Methods 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.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.- Parameters:
tokenEndpointUri
-clientId
-clientSecret
-grantType
-
-
OAuth2ClientCredentialsGrantCredentialsProvider
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String, String> parameters) UseOAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to 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.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to 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.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to 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.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to 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.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to 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:CredentialsProvider
Username to use for authentication- Specified by:
getUsername
in interfaceCredentialsProvider
- Overrides:
getUsername
in classRefreshProtectedCredentialsProvider<OAuth2ClientCredentialsGrantCredentialsProvider.Token>
- Returns:
- username
-
usernameFromToken
-
parseToken
-
retrieveToken
- Specified by:
retrieveToken
in 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()
-