Class GoogleCredentials
- All Implemented Interfaces:
QuotaProjectIdProvider,Serializable
- Direct Known Subclasses:
CloudShellCredentials,ComputeEngineCredentials,ExternalAccountAuthorizedUserCredentials,ExternalAccountCredentials,GdchCredentials,ImpersonatedCredentials,ServiceAccountCredentials,UserCredentials
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.google.auth.oauth2.OAuth2Credentials
OAuth2Credentials.CredentialsChangedListener -
Field Summary
FieldsFields inherited from class com.google.auth.Credentials
GOOGLE_DEFAULT_UNIVERSE -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDefault constructor.GoogleCredentials(AccessToken accessToken) Deprecated.protectedGoogleCredentials(AccessToken accessToken, String quotaProjectId) Deprecated.protectedGoogleCredentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) Deprecated.protectedConstructor that relies on aGoogleCredentials.Builderto provide all the necessary field values for initialization. -
Method Summary
Modifier and TypeMethodDescriptionstatic GoogleCredentialscreate(AccessToken accessToken) Returns the credentials instance from the given access token.static GoogleCredentialscreate(String universeDomain, AccessToken accessToken) Returns the credentials instance from the given access token and universe domain.createDelegated(String user) If the credentials support domain-wide delegation, creates a copy of the identity so that it impersonates the specified user; otherwise, returns the same instance.createScoped(String... scopes) If the credentials support scopes, creates a copy of the identity with the specified scopes; otherwise, returns the same instance.createScoped(Collection<String> scopes) If the credentials support scopes, creates a copy of the identity with the specified scopes, invalidates the existing scoped access token; otherwise, return the same instance.createScoped(Collection<String> scopes, Collection<String> defaultScopes) If the credentials support scopes, creates a copy of the identity with the specified scopes and default scopes; otherwise, returns the same instance.booleanIndicates whether the credentials require scopes to be specified via a call tocreateScoped(java.util.Collection<java.lang.String>)before use.createWithCustomRetryStrategy(boolean defaultRetriesEnabled) If the credentials support automatic retries, creates a copy of the identity with the provided retry strategycreateWithQuotaProject(String quotaProject) Creates a credential with the provided quota project.booleanstatic GoogleCredentialsfromStream(InputStream credentialsStream) This method is obsolete because of a potential security risk.static GoogleCredentialsfromStream(InputStream credentialsStream, HttpTransportFactory transportFactory) This method is obsolete because of a potential security risk.Provide additional headers to return as request metadata.static GoogleCredentialsReturns the Application Default Credentials.static GoogleCredentialsgetApplicationDefault(HttpTransportFactory transportFactory) Returns the Application Default Credentials.Provides additional information regarding credential initialization source credential source - Initialized via the GOOGLE_APPLICATION_CREDENTIALS env var or well known file type credential name - The user-friendly name of the credential created principal - Identity used for the credential Unknown field values (i.e. null) are not included in the mapping (e.g.The projectId value for a Credential type.Gets the universe domain for the credential.inthashCode()protected booleanGets the flag indicating whether universeDomain was explicitly set by the developer.static GoogleCredentials.BuildertoString()Returns a string representation of this credential, including request metadata and access token.protected com.google.common.base.MoreObjects.ToStringHelperA helper for overriding the toString() method.Methods inherited from class com.google.auth.oauth2.OAuth2Credentials
addChangeListener, getAccessToken, getAuthenticationType, getFromServiceLoader, getRequestMetadata, getRequestMetadata, getRequestMetadataInternal, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshAccessToken, refreshIfExpired, removeChangeListenerMethods inherited from class com.google.auth.Credentials
blockingGetToCallback, getMetricsCredentialType, getRequestMetadata
-
Field Details
-
quotaProjectId
-
-
Constructor Details
-
GoogleCredentials
protected GoogleCredentials()Default constructor. -
GoogleCredentials
Deprecated.Constructor with an explicit access token and quotaProjectId.Deprecated, please use the
GoogleCredentials(Builder)constructor whenever possible.- Parameters:
accessToken- initial or temporary access tokenquotaProjectId- a quotaProjectId, a project id to be used for billing purposes
-
GoogleCredentials
Deprecated.Constructor with explicit access token.- Parameters:
accessToken- initial or temporary access token
-
GoogleCredentials
Constructor that relies on aGoogleCredentials.Builderto provide all the necessary field values for initialization.- Parameters:
builder- an instance of a builder
-
GoogleCredentials
@Deprecated protected GoogleCredentials(AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) Deprecated.Constructor with explicit access token and refresh margins.Deprecated, please use the
GoogleCredentials(Builder)constructor whenever possible.- Parameters:
accessToken- initial or temporary access token
-
-
Method Details
-
create
Returns the credentials instance from the given access token.- Parameters:
accessToken- the access token- Returns:
- the credentials instance
-
create
Returns the credentials instance from the given access token and universe domain.- Parameters:
universeDomain- the universe domainaccessToken- the access token- Returns:
- the credentials instance
-
getApplicationDefault
Returns the Application Default Credentials.Returns the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:
- Credentials file pointed to by the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable - Credentials provided by the Google Cloud SDK.
gcloud auth application-default loginfor user account credentials.gcloud auth application-default login --impersonate-service-accountfor impersonated service account credentials.
- Google App Engine built-in credentials
- Google Cloud Shell built-in credentials
- Google Compute Engine built-in credentials
- Returns:
- the credentials instance.
- Throws:
IOException- if the credentials cannot be created in the current environment.
- Credentials file pointed to by the
-
getApplicationDefault
public static GoogleCredentials getApplicationDefault(HttpTransportFactory transportFactory) throws IOException Returns the Application Default Credentials.Returns the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:
- Credentials file pointed to by the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable - Credentials provided by the Google Cloud SDK
gcloud auth application-default logincommand - Google App Engine built-in credentials
- Google Cloud Shell built-in credentials
- Google Compute Engine built-in credentials
- Parameters:
transportFactory- HTTP transport factory, creates the transport used to get access tokens.- Returns:
- the credentials instance.
- Throws:
IOException- if the credentials cannot be created in the current environment.
- Credentials file pointed to by the
-
fromStream
@ObsoleteApi("This method is obsolete because of a potential security risk. Use the credential specific load method instead") public static GoogleCredentials fromStream(InputStream credentialsStream) throws IOException This method is obsolete because of a potential security risk. Use the credential specific load method insteadImportant: This method does not validate the credential configuration. A security risk holds when a credential configuration is accepted from a source that is not under your control and used without validation on your side.
If you know that you will be loading credential configurations of a specific type, it is recommended to use a credential-type-specific `fromStream()` method. This will ensure that an unexpected credential type with potential for malicious intent is not loaded unintentionally. You might still have to do validation for certain credential types. Please follow the recommendation for that method. For example, if you want to load only service accounts, you can use:
GoogleCredentials credentials = ServiceAccountCredentials.fromStream(json);. SeeServiceAccountCredentials.fromStream(InputStream, HttpTransportFactory).If you are loading your credential configuration from an untrusted source and have not mitigated the risks (e.g. by validating the configuration yourself), make these changes as soon as possible to prevent security risks to your environment.
Regardless of the method used, it is always your responsibility to validate configurations received from external sources.
See the for more details.
Returns credentials defined by a JSON file stream.
The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
- Parameters:
credentialsStream- the stream with the credential definition.- Returns:
- the credential defined by the credentialsStream.
- Throws:
IOException- if the credential cannot be created from the stream.
-
fromStream
@ObsoleteApi("This method is obsolete because of a potential security risk. Use the credential specific load method instead") public static GoogleCredentials fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException This method is obsolete because of a potential security risk. Use the credential specific load method insteadImportant: This method does not validate the credential configuration. A security risk holds when a credential configuration is accepted from a source that is not under your control and used without validation on your side.
If you know that you will be loading credential configurations of a specific type, it is recommended to use a credential-type-specific `fromStream()` method. This will ensure that an unexpected credential type with potential for malicious intent is not loaded unintentionally. You might still have to do validation for certain credential types. Please follow the recommendation for that method. For example, if you want to load only service accounts, you can use:
GoogleCredentials credentials = ServiceAccountCredentials.fromStream(json);. SeeServiceAccountCredentials.fromStream(InputStream, HttpTransportFactory).If you are loading your credential configuration from an untrusted source and have not mitigated the risks (e.g. by validating the configuration yourself), make these changes as soon as possible to prevent security risks to your environment.
Regardless of the method used, it is always your responsibility to validate configurations received from external sources.
See the for more details.
Returns credentials defined by a JSON file stream.
The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.
- Parameters:
credentialsStream- the stream with the credential definition.- Returns:
- the credential defined by the credentialsStream.
- Throws:
IOException- if the credential cannot be created from the stream.
-
createWithQuotaProject
Creates a credential with the provided quota project.- Parameters:
quotaProject- the quota project to set on the credential- Returns:
- credential with the provided quota project
-
getUniverseDomain
Gets the universe domain for the credential.- Overrides:
getUniverseDomainin classCredentials- Returns:
- An explicit universe domain if it was explicitly provided, invokes the super implementation otherwise
- Throws:
IOException
-
isExplicitUniverseDomain
protected boolean isExplicitUniverseDomain()Gets the flag indicating whether universeDomain was explicitly set by the developer.If subclass has a requirement to give priority to developer-set universeDomain, this property must be used to check if the universeDomain value was provided by the user. It could be a default otherwise.
- Returns:
- true if universeDomain value was provided by the developer, false otherwise
-
getAdditionalHeaders
Description copied from class:OAuth2CredentialsProvide additional headers to return as request metadata.- Overrides:
getAdditionalHeadersin classOAuth2Credentials- Returns:
- additional headers
-
toStringHelper
protected com.google.common.base.MoreObjects.ToStringHelper toStringHelper()A helper for overriding the toString() method. This allows inheritance of super class fields. Extending classes can override this implementation and call super implementation and add more fields. Same cannot be done with overriding the toString() directly.- Returns:
- an instance of the ToStringHelper that has public fields added
-
toString
Description copied from class:OAuth2CredentialsReturns 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.
- Overrides:
toStringin classOAuth2Credentials
-
equals
- Overrides:
equalsin classOAuth2Credentials
-
hashCode
public int hashCode()- Overrides:
hashCodein classOAuth2Credentials
-
newBuilder
-
toBuilder
- Overrides:
toBuilderin classOAuth2Credentials
-
getQuotaProjectId
- Specified by:
getQuotaProjectIdin interfaceQuotaProjectIdProvider- Returns:
- the quota project ID used for quota and billing purposes
-
getProjectId
The projectId value for a Credential type. Since not all GoogleCredentials subclass have a projectId associated, the projectId may be null. A subset of GoogleCredentials subclasses will override to return their projectId.- Returns:
- the project id for a Credential type
-
createScopedRequired
public boolean createScopedRequired()Indicates whether the credentials require scopes to be specified via a call tocreateScoped(java.util.Collection<java.lang.String>)before use.- Returns:
- Whether the credentials require scopes to be specified.
-
createScoped
If the credentials support scopes, creates a copy of the identity with the specified scopes, invalidates the existing scoped access token; otherwise, return the same instance.- Parameters:
scopes- Collection of scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
createScoped
If the credentials support scopes, creates a copy of the identity with the specified scopes and default scopes; otherwise, returns the same instance. This is mainly used by client libraries.- Parameters:
scopes- Collection of scopes to request.defaultScopes- Collection of default scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
createScoped
If the credentials support scopes, creates a copy of the identity with the specified scopes; otherwise, returns the same instance.- Parameters:
scopes- Collection of scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
createWithCustomRetryStrategy
If the credentials support automatic retries, creates a copy of the identity with the provided retry strategy- Parameters:
defaultRetriesEnabled- a flag enabling or disabling default retries- Returns:
- GoogleCredentials with the new default retries configuration.
-
createDelegated
If the credentials support domain-wide delegation, creates a copy of the identity so that it impersonates the specified user; otherwise, returns the same instance.- Parameters:
user- User to impersonate.- Returns:
- GoogleCredentials with a delegated user.
-
getCredentialInfo
Provides additional information regarding credential initialization source- credential source - Initialized via the GOOGLE_APPLICATION_CREDENTIALS env var or well known file type
- credential name - The user-friendly name of the credential created
- principal - Identity used for the credential
- Returns:
- ImmutableMap of information regarding how the Credential was initialized
-