Package com.google.auth.oauth2
Class OAuth2Credentials
java.lang.Object
com.google.auth.Credentials
com.google.auth.oauth2.OAuth2Credentials
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DownscopedCredentials,GoogleCredentials,IdTokenCredentials,OAuth2CredentialsWithRefresh
Base type for Credentials using OAuth2.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interfaceListener for changes to credentials. -
Field Summary
Fields inherited from class com.google.auth.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.static <T> TgetFromServiceLoader(Class<? extends T> clazz, T defaultInstance) Returns the first service provider from the given service loader.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) inthashCode()booleanbooleanstatic OAuth2Credentials.Builderprotected static HttpTransportFactorynewInstance(String className) Best-effort safe mechanism to attempt to instantiate anHttpTransportFactoryfrom a class name.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()Returns a string representation of this credential, including request metadata and access token.Methods inherited from class com.google.auth.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
- Specified by:
getAuthenticationTypein classCredentials
-
hasRequestMetadata
public boolean hasRequestMetadata()- Specified by:
hasRequestMetadatain classCredentials
-
hasRequestMetadataOnly
public boolean hasRequestMetadataOnly()- Specified by:
hasRequestMetadataOnlyin classCredentials
-
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
- Overrides:
getRequestMetadatain classCredentials
-
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- Throws:
IOException
-
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
-
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
Provide additional headers to return as request metadata.- Returns:
- additional headers
-
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
public int hashCode() -
getRequestMetadataInternal
-
toString
Returns a string representation of this credential, including request metadata and access token.Security Warning: The output of this method includes the request metadata which contains the raw Bearer access token, and the raw access token value. Do not log this output in production environments as it may expose sensitive credentials.
-
equals
-
newInstance
protected static HttpTransportFactory newInstance(String className) throws IOException, ClassNotFoundException Best-effort safe mechanism to attempt to instantiate anHttpTransportFactoryfrom a class name.This method attempts to avoid Arbitrary Code Execution (ACE) vulnerabilities by:
- Checking if the class name matches the default or ServiceLoader-provided factory, and returning that instance if so.
- If not, loading the class using reflection without running static initializers.
- Verifying that the loaded class is assignable to
HttpTransportFactory. - Only after verification, instantiating the class using its default constructor.
- Parameters:
className- The fully qualified name of the class to instantiate.- Returns:
- An instance of
HttpTransportFactory. - Throws:
IOException- If the class cannot be loaded, is the wrong type, or cannot be instantiated.ClassNotFoundException- If the class cannot be found.
-
getFromServiceLoader
Returns the first service provider from the given service loader.- Type Parameters:
T- The type of the service provider.- Parameters:
clazz- The class of the service provider to load.defaultInstance- The default instance to return if no service providers are found.- Returns:
- The first service provider from the service loader, or the
defaultInstanceif no service providers are found.
-
newBuilder
-
toBuilder
-