Package com.google.auth.oauth2
Class UserCredentials
- java.lang.Object
-
- com.google.auth.Credentials
-
- com.google.auth.oauth2.OAuth2Credentials
-
- com.google.auth.oauth2.GoogleCredentials
-
- com.google.auth.oauth2.UserCredentials
-
- All Implemented Interfaces:
IdTokenProvider
,QuotaProjectIdProvider
,java.io.Serializable
public class UserCredentials extends GoogleCredentials implements IdTokenProvider
OAuth2 Credentials representing a user's identity and consent.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UserCredentials.Builder
-
Nested classes/interfaces inherited from class com.google.auth.oauth2.OAuth2Credentials
OAuth2Credentials.AsyncRefreshResult, OAuth2Credentials.CacheState, OAuth2Credentials.CredentialsChangedListener, OAuth2Credentials.FutureCallbackToMetadataCallbackAdapter, OAuth2Credentials.OAuthValue, OAuth2Credentials.RefreshTask, OAuth2Credentials.RefreshTaskListener
-
Nested classes/interfaces inherited from interface com.google.auth.oauth2.IdTokenProvider
IdTokenProvider.Option
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
clientId
private java.lang.String
clientSecret
private static java.lang.String
GRANT_TYPE
private static java.lang.String
PARSE_ERROR_PREFIX
private java.lang.String
refreshToken
private static long
serialVersionUID
private java.net.URI
tokenServerUri
private HttpTransportFactory
transportFactory
private java.lang.String
transportFactoryClassName
-
Fields inherited from class com.google.auth.oauth2.GoogleCredentials
GDCH_SERVICE_ACCOUNT_FILE_TYPE, QUOTA_PROJECT_ID_HEADER_KEY, quotaProjectId, SERVICE_ACCOUNT_FILE_TYPE, USER_FILE_TYPE
-
Fields inherited from class com.google.auth.oauth2.OAuth2Credentials
clock, DEFAULT_EXPIRATION_MARGIN, DEFAULT_REFRESH_MARGIN, lock, refreshTask
-
Fields inherited from class com.google.auth.Credentials
GOOGLE_DEFAULT_UNIVERSE
-
-
Constructor Summary
Constructors Modifier Constructor Description private
UserCredentials(UserCredentials.Builder builder)
Internal constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private com.google.api.client.util.GenericData
doRefreshAccessToken()
Does refresh access token requestboolean
equals(java.lang.Object obj)
(package private) static UserCredentials
fromJson(java.util.Map<java.lang.String,java.lang.Object> json, HttpTransportFactory transportFactory)
Returns user credentials defined by JSON contents using the format supported by the Cloud SDK.static UserCredentials
fromStream(java.io.InputStream credentialsStream)
Returns credentials defined by a JSON file stream using the format supported by the Cloud SDK.static UserCredentials
fromStream(java.io.InputStream credentialsStream, HttpTransportFactory transportFactory)
Returns credentials defined by a JSON file stream using the format supported by the Cloud SDK.java.lang.String
getClientId()
Returns client ID of the credential from the console.java.lang.String
getClientSecret()
Returns client secret of the credential from the console.CredentialTypeForMetrics
getMetricsCredentialType()
Gets the credential type used for internal metrics header.java.lang.String
getRefreshToken()
Returns the refresh token resulting from a OAuth2 consent flow.private java.io.InputStream
getUserCredentialsStream()
Returns the instance of InputStream containing the following user credentials in JSON format: - RefreshToken - ClientId - ClientSecret - ServerTokenUriint
hashCode()
IdToken
idTokenWithAudience(java.lang.String targetAudience, java.util.List<IdTokenProvider.Option> options)
Returns a Google ID Token from the refresh token response.static UserCredentials.Builder
newBuilder()
private void
readObject(java.io.ObjectInputStream input)
AccessToken
refreshAccessToken()
Refreshes the OAuth2 access token by getting a new access token from the refresh tokenvoid
save(java.lang.String filePath)
Saves the end user credentials into the given file path.UserCredentials.Builder
toBuilder()
java.lang.String
toString()
-
Methods inherited from class com.google.auth.oauth2.GoogleCredentials
addQuotaProjectIdToRequestMetadata, create, create, createDelegated, createScoped, createScoped, createScoped, createScopedRequired, createWithCustomRetryStrategy, createWithQuotaProject, getAdditionalHeaders, getApplicationDefault, getApplicationDefault, getQuotaProjectId, getUniverseDomain, isDefaultUniverseDomain, isExplicitUniverseDomain, toStringHelper
-
Methods inherited from class com.google.auth.oauth2.OAuth2Credentials
addChangeListener, getAccessToken, getAuthenticationType, getExpirationMargin, getFromServiceLoader, getRefreshMargin, getRequestMetadata, getRequestMetadata, getRequestMetadataInternal, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshIfExpired, removeChangeListener
-
Methods inherited from class com.google.auth.Credentials
blockingGetToCallback, getRequestMetadata
-
-
-
-
Field Detail
-
GRANT_TYPE
private static final java.lang.String GRANT_TYPE
- See Also:
- Constant Field Values
-
PARSE_ERROR_PREFIX
private static final java.lang.String PARSE_ERROR_PREFIX
- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
clientId
private final java.lang.String clientId
-
clientSecret
private final java.lang.String clientSecret
-
refreshToken
private final java.lang.String refreshToken
-
tokenServerUri
private final java.net.URI tokenServerUri
-
transportFactoryClassName
private final java.lang.String transportFactoryClassName
-
transportFactory
private transient HttpTransportFactory transportFactory
-
-
Constructor Detail
-
UserCredentials
private UserCredentials(UserCredentials.Builder builder)
Internal constructor- Parameters:
builder
- A builder forUserCredentials
SeeUserCredentials.Builder
-
-
Method Detail
-
getMetricsCredentialType
public CredentialTypeForMetrics getMetricsCredentialType()
Description copied from class:Credentials
Gets the credential type used for internal metrics header.The default is
CredentialTypeForMetrics.DO_NOT_SEND
. For a credential that is established to track for metrics, this default should be overridden.- Overrides:
getMetricsCredentialType
in classCredentials
- Returns:
- a enum value for credential type
-
fromJson
static UserCredentials fromJson(java.util.Map<java.lang.String,java.lang.Object> json, HttpTransportFactory transportFactory) throws java.io.IOException
Returns user credentials defined by JSON contents using the format supported by the Cloud SDK.- Parameters:
json
- a map from the JSON representing the credentials.transportFactory
- HTTP transport factory, creates the transport used to get access tokens.- Returns:
- the credentials defined by the JSON.
- Throws:
java.io.IOException
- if the credential cannot be created from the JSON.
-
fromStream
public static UserCredentials fromStream(java.io.InputStream credentialsStream) throws java.io.IOException
Returns credentials defined by a JSON file stream using the format supported by the Cloud SDK.- Parameters:
credentialsStream
- the stream with the credential definition.- Returns:
- the credential defined by the credentialsStream.
- Throws:
java.io.IOException
- if the credential cannot be created from the stream.
-
fromStream
public static UserCredentials fromStream(java.io.InputStream credentialsStream, HttpTransportFactory transportFactory) throws java.io.IOException
Returns credentials defined by a JSON file stream using the format supported by the Cloud SDK.- Parameters:
credentialsStream
- the stream with the credential definition.transportFactory
- HTTP transport factory, creates the transport used to get access tokens.- Returns:
- the credential defined by the credentialsStream.
- Throws:
java.io.IOException
- if the credential cannot be created from the stream.
-
refreshAccessToken
public AccessToken refreshAccessToken() throws java.io.IOException
Refreshes the OAuth2 access token by getting a new access token from the refresh token- Overrides:
refreshAccessToken
in classOAuth2Credentials
- Returns:
- never
- Throws:
java.io.IOException
-
idTokenWithAudience
public IdToken idTokenWithAudience(java.lang.String targetAudience, java.util.List<IdTokenProvider.Option> options) throws java.io.IOException
Returns a Google ID Token from the refresh token response.- Specified by:
idTokenWithAudience
in interfaceIdTokenProvider
- Parameters:
targetAudience
- This can't be used for UserCredentials.options
- list of Credential specific options for the token. Currently unused for UserCredentials.- Returns:
- IdToken object which includes the raw id_token, expiration and audience
- Throws:
java.io.IOException
- if the attempt to get an IdToken failed
-
getClientId
public final java.lang.String getClientId()
Returns client ID of the credential from the console.- Returns:
- client ID
-
getClientSecret
public final java.lang.String getClientSecret()
Returns client secret of the credential from the console.- Returns:
- client secret
-
getRefreshToken
public final java.lang.String getRefreshToken()
Returns the refresh token resulting from a OAuth2 consent flow.- Returns:
- refresh token
-
doRefreshAccessToken
private com.google.api.client.util.GenericData doRefreshAccessToken() throws java.io.IOException
Does refresh access token request- Returns:
- Refresh token response data
- Throws:
java.io.IOException
-
getUserCredentialsStream
private java.io.InputStream getUserCredentialsStream() throws java.io.IOException
Returns the instance of InputStream containing the following user credentials in JSON format: - RefreshToken - ClientId - ClientSecret - ServerTokenUri- Returns:
- user credentials stream
- Throws:
java.io.IOException
-
save
public void save(java.lang.String filePath) throws java.io.IOException
Saves the end user credentials into the given file path.- Parameters:
filePath
- Path to file where to store the credentials- Throws:
java.io.IOException
- An error storing the credentials.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classGoogleCredentials
-
toString
public java.lang.String toString()
- Overrides:
toString
in classGoogleCredentials
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classGoogleCredentials
-
readObject
private void readObject(java.io.ObjectInputStream input) throws java.io.IOException, java.lang.ClassNotFoundException
- Throws:
java.io.IOException
java.lang.ClassNotFoundException
-
newBuilder
public static UserCredentials.Builder newBuilder()
-
toBuilder
public UserCredentials.Builder toBuilder()
- Overrides:
toBuilder
in classGoogleCredentials
-
-