public class OAuth2ClientCredentialsGrantCredentialsProvider extends RefreshProtectedCredentialsProvider<OAuth2ClientCredentialsGrantCredentialsProvider.Token>
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.
Modifier and Type | Class and Description |
---|---|
static class |
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
Helper to create
OAuth2ClientCredentialsGrantCredentialsProvider instances. |
static class |
OAuth2ClientCredentialsGrantCredentialsProvider.TlsConfiguration
TLS configuration for a
OAuth2ClientCredentialsGrantCredentialsProvider . |
static class |
OAuth2ClientCredentialsGrantCredentialsProvider.Token |
Modifier and Type | Method and Description |
---|---|
protected void |
checkContentType(String headerField) |
protected void |
checkResponseCode(int responseCode) |
protected void |
configureConnection(HttpURLConnection connection) |
protected void |
configureConnectionForHttps(HttpURLConnection connection) |
boolean |
equals(Object o) |
protected String |
extractResponseBody(InputStream inputStream) |
String |
getUsername()
Username to use for authentication
|
int |
hashCode() |
protected OAuth2ClientCredentialsGrantCredentialsProvider.Token |
parseToken(String response) |
protected String |
passwordFromToken(OAuth2ClientCredentialsGrantCredentialsProvider.Token token) |
protected OAuth2ClientCredentialsGrantCredentialsProvider.Token |
retrieveToken() |
protected Duration |
timeBeforeExpiration(OAuth2ClientCredentialsGrantCredentialsProvider.Token token) |
protected String |
usernameFromToken(OAuth2ClientCredentialsGrantCredentialsProvider.Token token) |
getPassword, getTimeBeforeExpiration, refresh
public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType)
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.tokenEndpointUri
- clientId
- clientSecret
- grantType
- public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String,String> parameters)
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.tokenEndpointUri
- clientId
- clientSecret
- grantType
- parameters
- public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String,String> parameters, Consumer<HttpURLConnection> connectionConfigurator)
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.tokenEndpointUri
- clientId
- clientSecret
- grantType
- parameters
- connectionConfigurator
- public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory)
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.tokenEndpointUri
- clientId
- clientSecret
- grantType
- hostnameVerifier
- sslSocketFactory
- public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String,String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory)
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.tokenEndpointUri
- clientId
- clientSecret
- grantType
- parameters
- hostnameVerifier
- sslSocketFactory
- public OAuth2ClientCredentialsGrantCredentialsProvider(String tokenEndpointUri, String clientId, String clientSecret, String grantType, Map<String,String> parameters, HostnameVerifier hostnameVerifier, SSLSocketFactory sslSocketFactory, Consumer<HttpURLConnection> connectionConfigurator)
OAuth2ClientCredentialsGrantCredentialsProvider.OAuth2ClientCredentialsGrantCredentialsProviderBuilder
to create an instance.tokenEndpointUri
- clientId
- clientSecret
- grantType
- parameters
- hostnameVerifier
- sslSocketFactory
- connectionConfigurator
- public String getUsername()
CredentialsProvider
getUsername
in interface CredentialsProvider
getUsername
in class RefreshProtectedCredentialsProvider<OAuth2ClientCredentialsGrantCredentialsProvider.Token>
protected String usernameFromToken(OAuth2ClientCredentialsGrantCredentialsProvider.Token token)
protected OAuth2ClientCredentialsGrantCredentialsProvider.Token parseToken(String response)
protected OAuth2ClientCredentialsGrantCredentialsProvider.Token retrieveToken()
retrieveToken
in class RefreshProtectedCredentialsProvider<OAuth2ClientCredentialsGrantCredentialsProvider.Token>
protected void checkContentType(String headerField) throws OAuthTokenManagementException
OAuthTokenManagementException
protected void checkResponseCode(int responseCode) throws OAuthTokenManagementException
OAuthTokenManagementException
protected String extractResponseBody(InputStream inputStream) throws IOException
IOException
protected String passwordFromToken(OAuth2ClientCredentialsGrantCredentialsProvider.Token token)
protected Duration timeBeforeExpiration(OAuth2ClientCredentialsGrantCredentialsProvider.Token token)
protected void configureConnection(HttpURLConnection connection)
protected void configureConnectionForHttps(HttpURLConnection connection)
Copyright © 2022 VMware, Inc. or its affiliates.. All rights reserved.