public class OAuth2Credentials extends Credentials
Modifier and Type | Class and Description |
---|---|
static class |
OAuth2Credentials.Builder |
static interface |
OAuth2Credentials.CredentialsChangedListener
Listener for changes to credentials.
|
Modifier | Constructor and Description |
---|---|
protected |
OAuth2Credentials()
Default constructor.
|
protected |
OAuth2Credentials(AccessToken accessToken)
Constructor with explicit access token.
|
protected |
OAuth2Credentials(AccessToken accessToken,
Duration refreshMargin,
Duration expirationMargin) |
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(OAuth2Credentials.CredentialsChangedListener listener)
Adds a listener that is notified when the Credentials data changes.
|
static OAuth2Credentials |
create(AccessToken accessToken)
Returns the credentials instance from the given access token.
|
boolean |
equals(Object obj) |
AccessToken |
getAccessToken()
Returns the cached access token.
|
protected Map<String,List<String>> |
getAdditionalHeaders()
Provide additional headers to return as request metadata.
|
String |
getAuthenticationType()
A constant string name describing the authentication technology.
|
protected static <T> T |
getFromServiceLoader(Class<? extends T> clazz,
T defaultInstance) |
Map<String,List<String>> |
getRequestMetadata(URI uri)
Provide the request metadata by ensuring there is a current access token and providing it as an
authorization bearer token.
|
void |
getRequestMetadata(URI uri,
Executor executor,
RequestMetadataCallback callback)
Get the current request metadata without blocking.
|
protected Map<String,List<String>> |
getRequestMetadataInternal() |
int |
hashCode() |
boolean |
hasRequestMetadata()
Whether the credentials have metadata entries that should be added to each request.
|
boolean |
hasRequestMetadataOnly()
Indicates whether or not the Auth mechanism works purely by including request metadata.
|
static OAuth2Credentials.Builder |
newBuilder() |
protected static <T> T |
newInstance(String className) |
void |
refresh()
Request a new token regardless of the current token state.
|
AccessToken |
refreshAccessToken()
Method to refresh the access token according to the specific type of credentials.
|
void |
refreshIfExpired()
Refresh these credentials only if they have expired or are expiring imminently.
|
void |
removeChangeListener(OAuth2Credentials.CredentialsChangedListener listener)
Removes a listener that was added previously.
|
OAuth2Credentials.Builder |
toBuilder() |
String |
toString() |
blockingGetToCallback, getRequestMetadata
protected OAuth2Credentials()
protected OAuth2Credentials(AccessToken accessToken)
accessToken
- initial or temporary access tokenprotected OAuth2Credentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin)
public static OAuth2Credentials create(AccessToken accessToken)
accessToken
- the access tokenpublic String getAuthenticationType()
Credentials
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.
getAuthenticationType
in class Credentials
public boolean hasRequestMetadata()
Credentials
This should be called by the transport layer to see if Credentials.getRequestMetadata()
should be used for each request.
hasRequestMetadata
in class Credentials
Credentials.getRequestMetadata()
public boolean hasRequestMetadataOnly()
Credentials
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.
hasRequestMetadataOnly
in class Credentials
public final AccessToken getAccessToken()
If not set, you should call refresh()
to fetch and cache an access token.
public void getRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback)
Credentials
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.
getRequestMetadata
in class Credentials
uri
- URI of the entry point for the request.executor
- Executor to perform the request.callback
- Callback to execute when the request is finished.public Map<String,List<String>> getRequestMetadata(URI uri) throws IOException
getRequestMetadata
in class Credentials
uri
- URI of the entry point for the request.IOException
- if there was an error getting up-to-date access. The exception should
implement Retryable
and isRetryable()
will return true if the operation may
be retried.public void refresh() throws IOException
refresh
in class Credentials
IOException
- if there was an error getting up-to-date access.public void refreshIfExpired() throws IOException
IOException
- during token refresh.public AccessToken refreshAccessToken() throws IOException
Throws IllegalStateException if not overridden since direct use of OAuth2Credentials is only for temporary or non-refreshing access tokens.
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
protected Map<String,List<String>> getAdditionalHeaders()
public final void addChangeListener(OAuth2Credentials.CredentialsChangedListener listener)
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.
listener
- the listener to be addedpublic final void removeChangeListener(OAuth2Credentials.CredentialsChangedListener listener)
listener
- The listener to be removed.@Nullable protected Map<String,List<String>> getRequestMetadataInternal()
protected static <T> T newInstance(String className) throws IOException, ClassNotFoundException
IOException
ClassNotFoundException
protected static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance)
public static OAuth2Credentials.Builder newBuilder()
public OAuth2Credentials.Builder toBuilder()
Copyright © 2023 Google. All rights reserved.