Class ExternalAccountCredentials (1.7.0)

public abstract class ExternalAccountCredentials extends GoogleCredentials implements QuotaProjectIdProvider

Base external account credentials class.

Handles initializing external credentials, calls to the Security Token Service, and service account impersonation.

Inheritance

java.lang.Object > Credentials > OAuth2Credentials > GoogleCredentials > ExternalAccountCredentials

Static Methods

fromStream(InputStream credentialsStream)

public static ExternalAccountCredentials fromStream(InputStream credentialsStream)

Returns credentials defined by a JSON file stream.

Returns IdentityPoolCredentials or AwsCredentials.

Parameter
NameDescription
credentialsStreamInputStream

the stream with the credential definition

Returns
TypeDescription
ExternalAccountCredentials

the credential defined by the credentialsStream

Exceptions
TypeDescription
IOException

if the credential cannot be created from the stream

fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory)

public static ExternalAccountCredentials fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory)

Returns credentials defined by a JSON file stream.

Returns a IdentityPoolCredentials or AwsCredentials.

Parameters
NameDescription
credentialsStreamInputStream

the stream with the credential definition

transportFactoryHttpTransportFactory

the HTTP transport factory used to create the transport to get access tokens

Returns
TypeDescription
ExternalAccountCredentials

the credential defined by the credentialsStream

Exceptions
TypeDescription
IOException

if the credential cannot be created from the stream

Constructors

ExternalAccountCredentials(HttpTransportFactory transportFactory, String audience, String subjectTokenType, String tokenUrl, ExternalAccountCredentials.CredentialSource credentialSource, String tokenInfoUrl, String serviceAccountImpersonationUrl, String quotaProjectId, String clientId, String clientSecret, Collection<String> scopes)

protected ExternalAccountCredentials(HttpTransportFactory transportFactory, String audience, String subjectTokenType, String tokenUrl, ExternalAccountCredentials.CredentialSource credentialSource, String tokenInfoUrl, String serviceAccountImpersonationUrl, String quotaProjectId, String clientId, String clientSecret, Collection<String> scopes)

Constructor with minimum identifying information and custom HTTP transport. Does not support workforce credentials.

Parameters
NameDescription
transportFactoryHttpTransportFactory

HTTP transport factory, creates the transport used to get access tokens

audienceString

the Security Token Service audience, which is usually the fully specified resource name of the workload/workforce pool provider

subjectTokenTypeString

the Security Token Service subject token type based on the OAuth 2.0 token exchange spec. Indicates the type of the security token in the credential file

tokenUrlString

the Security Token Service token exchange endpoint

credentialSourcecom.google.auth.oauth2.ExternalAccountCredentials.CredentialSource

the external credential source

tokenInfoUrlString

the endpoint used to retrieve account related information. Required for gCloud session account identification.

serviceAccountImpersonationUrlString

the URL for the service account impersonation request. This URL is required for some APIs. If this URL is not available, the access token from the Security Token Service is used directly. May be null.

quotaProjectIdString

the project used for quota and billing purposes. May be null.

clientIdString

client ID of the service account from the console. May be null.

clientSecretString

client secret of the service account from the console. May be null.

scopesCollection<String>

the scopes to request during the authorization grant. May be null.

ExternalAccountCredentials(HttpTransportFactory transportFactory, String audience, String subjectTokenType, String tokenUrl, ExternalAccountCredentials.CredentialSource credentialSource, String tokenInfoUrl, String serviceAccountImpersonationUrl, String quotaProjectId, String clientId, String clientSecret, Collection<String> scopes, EnvironmentProvider environmentProvider)

protected ExternalAccountCredentials(HttpTransportFactory transportFactory, String audience, String subjectTokenType, String tokenUrl, ExternalAccountCredentials.CredentialSource credentialSource, String tokenInfoUrl, String serviceAccountImpersonationUrl, String quotaProjectId, String clientId, String clientSecret, Collection<String> scopes, EnvironmentProvider environmentProvider)

See ExternalAccountCredentials#ExternalAccountCredentials(HttpTransportFactory, String, String, String, CredentialSource, String, String, String, String, String, Collection)

Parameters
NameDescription
transportFactoryHttpTransportFactory
audienceString
subjectTokenTypeString
tokenUrlString
credentialSourcecom.google.auth.oauth2.ExternalAccountCredentials.CredentialSource
tokenInfoUrlString
serviceAccountImpersonationUrlString
quotaProjectIdString
clientIdString
clientSecretString
scopesCollection<String>
environmentProvidercom.google.auth.oauth2.EnvironmentProvider

the environment provider. May be null. Defaults to SystemEnvironmentProvider.

ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)

protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)

Internal constructor with minimum identifying information and custom HTTP transport. See ExternalAccountCredentials.Builder.

Parameter
NameDescription
builderExternalAccountCredentials.Builder

Fields

impersonatedCredentials

protected final ImpersonatedCredentials impersonatedCredentials
Field Value
TypeDescription
ImpersonatedCredentials

transportFactory

protected transient HttpTransportFactory transportFactory
Field Value
TypeDescription
HttpTransportFactory

Methods

exchangeExternalCredentialForAccessToken(StsTokenExchangeRequest stsTokenExchangeRequest)

protected AccessToken exchangeExternalCredentialForAccessToken(StsTokenExchangeRequest stsTokenExchangeRequest)

Exchanges the external credential for a Google Cloud access token.

Parameter
NameDescription
stsTokenExchangeRequestcom.google.auth.oauth2.StsTokenExchangeRequest

the Security Token Service token exchange request

Returns
TypeDescription
AccessToken

the access token returned by the Security Token Service

Exceptions
TypeDescription
IOException

if the call to the Security Token Service fails

getAudience()

public String getAudience()
Returns
TypeDescription
String

getClientId()

public String getClientId()
Returns
TypeDescription
String

getClientSecret()

public String getClientSecret()
Returns
TypeDescription
String

getCredentialSource()

public ExternalAccountCredentials.CredentialSource getCredentialSource()
Returns
TypeDescription
com.google.auth.oauth2.ExternalAccountCredentials.CredentialSource

getQuotaProjectId()

public String getQuotaProjectId()
Returns
TypeDescription
String

getRequestMetadata(URI uri)

public 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.

Parameter
NameDescription
uriURI
Returns
TypeDescription
Map<String,List<String>>
Overrides
Exceptions
TypeDescription
IOException

getRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback)

public void getRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback)

Get 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 #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.

Parameters
NameDescription
uriURI
executorExecutor
callbackRequestMetadataCallback
Overrides

getScopes()

public Collection<String> getScopes()
Returns
TypeDescription
Collection<String>

getServiceAccountImpersonationUrl()

public String getServiceAccountImpersonationUrl()
Returns
TypeDescription
String

getSubjectTokenType()

public String getSubjectTokenType()
Returns
TypeDescription
String

getTokenInfoUrl()

public String getTokenInfoUrl()
Returns
TypeDescription
String

getTokenUrl()

public String getTokenUrl()
Returns
TypeDescription
String

getWorkforcePoolUserProject()

public String getWorkforcePoolUserProject()
Returns
TypeDescription
String

isWorkforcePoolConfiguration()

public boolean isWorkforcePoolConfiguration()

Returns whether or not the current configuration is for Workforce Pools (which enable 3p user identities, rather than workloads).

Returns
TypeDescription
boolean

retrieveSubjectToken()

public abstract String retrieveSubjectToken()

Retrieves the external subject token to be exchanged for a Google Cloud access token.

Must be implemented by subclasses as the retrieval method is dependent on the credential source.

Returns
TypeDescription
String

the external subject token

Exceptions
TypeDescription
IOException