Class OAuth2Credentials
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DownscopedCredentials, GoogleCredentials, IdTokenCredentials, OAuth2CredentialsWithRefresh
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interfaceListener for changes to credentials. -
Field Summary
Fields inherited from class Credentials
GOOGLE_DEFAULT_UNIVERSE -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDefault constructor.protectedOAuth2Credentials(AccessToken accessToken) Constructor with explicit access token.protectedOAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) -
Method Summary
Modifier and TypeMethodDescriptionfinal voidAdds a listener that is notified when the Credentials data changes.static OAuth2Credentialscreate(AccessToken accessToken) Returns the credentials instance from the given access token.booleanfinal AccessTokenReturns the cached access token.Provide additional headers to return as request metadata.A constant string name describing the authentication technology.protected static <T> TgetFromServiceLoader(Class<? extends T> clazz, T defaultInstance) getRequestMetadata(URI uri) Provide the request metadata by ensuring there is a current access token and providing it as an authorization bearer token.voidgetRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback) Get the current request metadata without blocking.inthashCode()booleanWhether the credentials have metadata entries that should be added to each request.booleanIndicates whether or not the Auth mechanism works purely by including request metadata.static OAuth2Credentials.Builderprotected static <T> TnewInstance(String className) voidrefresh()Request a new token regardless of the current token state.Method to refresh the access token according to the specific type of credentials.voidRefresh these credentials only if they have expired or are expiring imminently.final voidRemoves a listener that was added previously.toString()Methods inherited from class Credentials
blockingGetToCallback, getMetricsCredentialType, getRequestMetadata, getUniverseDomain
-
Constructor Details
-
OAuth2Credentials
protected OAuth2Credentials()Default constructor. -
OAuth2Credentials
Constructor with explicit access token.- Parameters:
accessToken- initial or temporary access token
-
OAuth2Credentials
protected OAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)
-
-
Method Details
-
create
Returns the credentials instance from the given access token.- Parameters:
accessToken- the access token- Returns:
- the credentials instance
-
getAuthenticationType
Description copied from class:CredentialsA constant string name describing the authentication technology.E.g. “OAuth2”, “SSL”. For use by the transport layer to determine whether it supports the type of authentication in the case where
Credentials.hasRequestMetadataOnly()is false. Also serves as a debugging helper.- Specified by:
getAuthenticationTypein classCredentials- Returns:
- The type of authentication used.
-
hasRequestMetadata
public boolean hasRequestMetadata()Description copied from class:CredentialsWhether the credentials have metadata entries that should be added to each request.This should be called by the transport layer to see if
Credentials.getRequestMetadata()should be used for each request.- Specified by:
hasRequestMetadatain classCredentials- Returns:
- Whether or not the transport layer should call
Credentials.getRequestMetadata()
-
hasRequestMetadataOnly
public boolean hasRequestMetadataOnly()Description copied from class:CredentialsIndicates whether or not the Auth mechanism works purely by including request metadata.This is meant for the transport layer. If this is true a transport does not need to take actions other than including the request metadata. If this is false, a transport must specifically know about the authentication technology to support it, and should fail to accept the credentials otherwise.
- Specified by:
hasRequestMetadataOnlyin classCredentials- Returns:
- Whether or not the Auth mechanism works purely by including request metadata.
-
getAccessToken
Returns the cached access token.If not set, you should call
refresh()to fetch and cache an access token.- Returns:
- The cached access token.
-
getRequestMetadata
Description copied from class:CredentialsGet the current request metadata without blocking.This should be called by the transport layer on each request, and the data should be populated in headers or other context. The implementation can either call the callback inline or asynchronously. Either way it should never block in this method. The executor is provided for tasks that may block.
The default implementation will just call
Credentials.getRequestMetadata(URI)then the callback from the given executor.The convention for handling binary data is for the key in the returned map to end with
"-bin"and for the corresponding values to be base64 encoded.- Overrides:
getRequestMetadatain classCredentials- Parameters:
uri- URI of the entry point for the request.executor- Executor to perform the request.callback- Callback to execute when the request is finished.
-
getRequestMetadata
Provide the request metadata by ensuring there is a current access token and providing it as an authorization bearer token.- Specified by:
getRequestMetadatain classCredentials- Parameters:
uri- URI of the entry point for the request.- Returns:
- The request metadata used for populating headers or other context.
- Throws:
IOException- if there was an error getting up-to-date access. The exception should implementRetryableandisRetryable()will return true if the operation may be retried.
-
refresh
Request a new token regardless of the current token state. If the current token is not expired, it will still be returned during the refresh.- Specified by:
refreshin classCredentials- Throws:
IOException- if there was an error getting up-to-date access.
-
refreshIfExpired
Refresh these credentials only if they have expired or are expiring imminently.- Throws:
IOException- during token refresh.
-
refreshAccessToken
Method to refresh the access token according to the specific type of credentials.Throws IllegalStateException if not overridden since direct use of OAuth2Credentials is only for temporary or non-refreshing access tokens.
- Returns:
- never
- Throws:
IllegalStateException- always. OAuth2Credentials does not support refreshing the access token. An instance with a new access token or a derived type that supports refreshing should be used instead.IOException
-
getAdditionalHeaders
-
addChangeListener
Adds a listener that is notified when the Credentials data changes.This is called when token content changes, such as when the access token is refreshed. This is typically used by code caching the access token.
- Parameters:
listener- the listener to be added
-
removeChangeListener
Removes a listener that was added previously.- Parameters:
listener- The listener to be removed.
-
hashCode
-
getRequestMetadataInternal
-
toString
-
equals
-
newInstance
- Throws:
IOExceptionClassNotFoundException
-
getFromServiceLoader
-
newBuilder
-
toBuilder
-