public class ServiceAccountCredentials extends GoogleCredentials implements ServiceAccountSigner, IdTokenProvider, JwtProvider
By default uses a JSON Web Token (JWT) to fetch access tokens.
Modifier and Type | Class and Description |
---|---|
static class |
ServiceAccountCredentials.Builder |
OAuth2Credentials.CredentialsChangedListener
ServiceAccountSigner.SigningException
IdTokenProvider.Option
quotaProjectId
Modifier and Type | Method and Description |
---|---|
GoogleCredentials |
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.
|
GoogleCredentials |
createScoped(Collection<String> newScopes)
Clones the service account with the specified scopes.
|
GoogleCredentials |
createScoped(Collection<String> newScopes,
Collection<String> newDefaultScopes)
Clones the service account with the specified scopes.
|
boolean |
createScopedRequired()
Returns whether the scopes are empty, meaning createScoped must be called before use.
|
ServiceAccountCredentials |
createWithCustomLifetime(int lifetime)
Clones the service account with a new lifetime value.
|
ServiceAccountCredentials |
createWithCustomRetryStrategy(boolean defaultRetriesEnabled)
Clones the service account with the specified default retries.
|
ServiceAccountCredentials |
createWithUseJwtAccessWithScope(boolean useJwtAccessWithScope)
Clones the service account with a new useJwtAccessWithScope value.
|
boolean |
equals(Object obj) |
static ServiceAccountCredentials |
fromPkcs8(String clientId,
String clientEmail,
String privateKeyPkcs8,
String privateKeyId,
Collection<String> scopes)
Factory with minimum identifying information using PKCS#8 for the private key.
|
static ServiceAccountCredentials |
fromPkcs8(String clientId,
String clientEmail,
String privateKeyPkcs8,
String privateKeyId,
Collection<String> scopes,
Collection<String> defaultScopes)
Factory with minimum identifying information using PKCS#8 for the private key.
|
static ServiceAccountCredentials |
fromPkcs8(String clientId,
String clientEmail,
String privateKeyPkcs8,
String privateKeyId,
Collection<String> scopes,
Collection<String> defaultScopes,
HttpTransportFactory transportFactory,
URI tokenServerUri)
Factory with minimum identifying information and custom transport using PKCS#8 for the private
key.
|
static ServiceAccountCredentials |
fromPkcs8(String clientId,
String clientEmail,
String privateKeyPkcs8,
String privateKeyId,
Collection<String> scopes,
Collection<String> defaultScopes,
HttpTransportFactory transportFactory,
URI tokenServerUri,
String serviceAccountUser)
Factory with minimum identifying information and custom transport using PKCS#8 for the private
key.
|
static ServiceAccountCredentials |
fromPkcs8(String clientId,
String clientEmail,
String privateKeyPkcs8,
String privateKeyId,
Collection<String> scopes,
HttpTransportFactory transportFactory,
URI tokenServerUri)
Factory with minimum identifying information and custom transport using PKCS#8 for the private
key.
|
static ServiceAccountCredentials |
fromPkcs8(String clientId,
String clientEmail,
String privateKeyPkcs8,
String privateKeyId,
Collection<String> scopes,
HttpTransportFactory transportFactory,
URI tokenServerUri,
String serviceAccountUser)
Factory with minimum identifying information and custom transport using PKCS#8 for the private
key.
|
static ServiceAccountCredentials |
fromStream(InputStream credentialsStream)
Returns credentials defined by a Service Account key file in JSON format from the Google
Developers Console.
|
static ServiceAccountCredentials |
fromStream(InputStream credentialsStream,
HttpTransportFactory transportFactory)
Returns credentials defined by a Service Account key file in JSON format from the Google
Developers Console.
|
String |
getAccount()
Returns the service account associated with the signer.
|
String |
getClientEmail() |
String |
getClientId() |
Collection<String> |
getDefaultScopes() |
PrivateKey |
getPrivateKey() |
String |
getPrivateKeyId() |
String |
getProjectId() |
Map<String,List<String>> |
getRequestMetadata(URI uri)
Provide the request metadata by putting an access JWT directly in the metadata.
|
void |
getRequestMetadata(URI uri,
Executor executor,
RequestMetadataCallback callback)
Get the current request metadata without blocking.
|
Collection<String> |
getScopes() |
String |
getServiceAccountUser() |
URI |
getTokenServerUri() |
boolean |
getUseJwtAccessWithScope() |
int |
hashCode() |
IdToken |
idTokenWithAudience(String targetAudience,
List<IdTokenProvider.Option> options)
Returns a Google ID Token from the metadata server on ComputeEngine.
|
JwtCredentials |
jwtWithClaims(JwtClaims newClaims)
Returns a new JwtCredentials instance with modified claims.
|
static ServiceAccountCredentials.Builder |
newBuilder() |
AccessToken |
refreshAccessToken()
Refreshes the OAuth2 access token by getting a new access token using a JSON Web Token (JWT).
|
byte[] |
sign(byte[] toSign)
Signs the provided bytes using the private key associated with the service account.
|
ServiceAccountCredentials.Builder |
toBuilder() |
String |
toString() |
create, createScoped, createWithQuotaProject, getAdditionalHeaders, getApplicationDefault, getApplicationDefault, getQuotaProjectId
addChangeListener, getAccessToken, getAuthenticationType, getFromServiceLoader, getRequestMetadataInternal, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshIfExpired, removeChangeListener
blockingGetToCallback, getRequestMetadata
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes) throws IOException
clientId
- Client ID of the service account from the console. May be null.clientEmail
- Client email address of the service account from the console.privateKeyPkcs8
- RSA private key object for the service account in PKCS#8 format.privateKeyId
- Private key identifier for the service account. May be null.scopes
- Scope strings for the APIs to be called. May be null or an empty collection,
which results in a credential that must have createScoped called before use.IOException
- if the credential cannot be created from the private key.public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes) throws IOException
clientId
- client ID of the service account from the console. May be null.clientEmail
- client email address of the service account from the consoleprivateKeyPkcs8
- RSA private key object for the service account in PKCS#8 format.privateKeyId
- private key identifier for the service account. May be null.scopes
- scope strings for the APIs to be called. May be null or an empty collection.defaultScopes
- default scope strings for the APIs to be called. May be null or an empty.IOException
- if the credential cannot be created from the private keypublic static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, HttpTransportFactory transportFactory, URI tokenServerUri) throws IOException
clientId
- Client ID of the service account from the console. May be null.clientEmail
- Client email address of the service account from the console.privateKeyPkcs8
- RSA private key object for the service account in PKCS#8 format.privateKeyId
- Private key identifier for the service account. May be null.scopes
- Scope strings for the APIs to be called. May be null or an empty collection,
which results in a credential that must have createScoped called before use.transportFactory
- HTTP transport factory, creates the transport used to get access
tokens.tokenServerUri
- URI of the end point that provides tokens.IOException
- if the credential cannot be created from the private key.public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes, HttpTransportFactory transportFactory, URI tokenServerUri) throws IOException
clientId
- client ID of the service account from the console. May be null.clientEmail
- client email address of the service account from the consoleprivateKeyPkcs8
- RSA private key object for the service account in PKCS#8 format.privateKeyId
- private key identifier for the service account. May be null.scopes
- scope strings for the APIs to be called. May be null or an empty collection,
which results in a credential that must have createScoped called before use.defaultScopes
- default scope strings for the APIs to be called. May be null or an empty
collection, which results in a credential that must have createScoped called before use.transportFactory
- HTTP transport factory, creates the transport used to get access
tokens.tokenServerUri
- URI of the end point that provides tokensIOException
- if the credential cannot be created from the private keypublic static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) throws IOException
clientId
- Client ID of the service account from the console. May be null.clientEmail
- Client email address of the service account from the console.privateKeyPkcs8
- RSA private key object for the service account in PKCS#8 format.privateKeyId
- Private key identifier for the service account. May be null.scopes
- Scope strings for the APIs to be called. May be null or an empty collection,
which results in a credential that must have createScoped called before use.transportFactory
- HTTP transport factory, creates the transport used to get access
tokens.tokenServerUri
- URI of the end point that provides tokens.serviceAccountUser
- The email of the user account to impersonate, if delegating
domain-wide authority to the service account.IOException
- if the credential cannot be created from the private key.public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) throws IOException
clientId
- client ID of the service account from the console. May be null.clientEmail
- client email address of the service account from the consoleprivateKeyPkcs8
- RSA private key object for the service account in PKCS#8 format.privateKeyId
- private key identifier for the service account. May be null.scopes
- scope strings for the APIs to be called. May be null or an empty collection,
which results in a credential that must have createScoped called before use.defaultScopes
- default scope strings for the APIs to be called. May be null or an empty
collection, which results in a credential that must have createScoped called before use.transportFactory
- HTTP transport factory, creates the transport used to get access
tokens.tokenServerUri
- URI of the end point that provides tokensserviceAccountUser
- the email of the user account to impersonate, if delegating
domain-wide authority to the service account.IOException
- if the credential cannot be created from the private keypublic static ServiceAccountCredentials fromStream(InputStream credentialsStream) throws IOException
credentialsStream
- the stream with the credential definition.IOException
- if the credential cannot be created from the stream.public static ServiceAccountCredentials fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException
credentialsStream
- the stream with the credential definition.transportFactory
- HTTP transport factory, creates the transport used to get access
tokens.IOException
- if the credential cannot be created from the stream.public boolean createScopedRequired()
createScopedRequired
in class GoogleCredentials
public AccessToken refreshAccessToken() throws IOException
refreshAccessToken
in class OAuth2Credentials
IOException
public IdToken idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) throws IOException
idTokenWithAudience
in interface IdTokenProvider
targetAudience
- the aud: field the IdToken should include.options
- list of Credential specific options for the token. Currently, unused for
ServiceAccountCredentials.IOException
- if the attempt to get an IdToken failedpublic ServiceAccountCredentials createWithCustomRetryStrategy(boolean defaultRetriesEnabled)
createWithCustomRetryStrategy
in class GoogleCredentials
defaultRetriesEnabled
- a flag enabling or disabling default retriespublic GoogleCredentials createScoped(Collection<String> newScopes)
Should be called before use for instances with empty scopes.
createScoped
in class GoogleCredentials
newScopes
- Collection of scopes to request.public GoogleCredentials createScoped(Collection<String> newScopes, Collection<String> newDefaultScopes)
Should be called before use for instances with empty scopes.
createScoped
in class GoogleCredentials
newScopes
- Collection of scopes to request.newDefaultScopes
- Collection of default scopes to request.public ServiceAccountCredentials createWithCustomLifetime(int lifetime)
lifetime
- life time value in seconds. The value should be at most 43200 (12 hours). If
the token is used for calling a Google API, then the value should be at most 3600 (1 hour).
If the given value is 0, then the default value 3600 will be used when creating the
credentials.public ServiceAccountCredentials createWithUseJwtAccessWithScope(boolean useJwtAccessWithScope)
useJwtAccessWithScope
- whether self signed JWT with scopes should be usedpublic GoogleCredentials createDelegated(String user)
GoogleCredentials
createDelegated
in class GoogleCredentials
user
- User to impersonate.public final String getClientId()
public final String getClientEmail()
public final PrivateKey getPrivateKey()
public final String getPrivateKeyId()
public final Collection<String> getScopes()
public final Collection<String> getDefaultScopes()
public final String getServiceAccountUser()
public final String getProjectId()
public final URI getTokenServerUri()
public boolean getUseJwtAccessWithScope()
public String getAccount()
ServiceAccountSigner
getAccount
in interface ServiceAccountSigner
public byte[] sign(byte[] toSign)
ServiceAccountSigner
sign
in interface ServiceAccountSigner
toSign
- bytes to signpublic JwtCredentials jwtWithClaims(JwtClaims newClaims)
jwtWithClaims
in interface JwtProvider
newClaims
- new claims. Any unspecified claim fields will default to the the current
values.public int hashCode()
hashCode
in class OAuth2Credentials
public String toString()
toString
in class OAuth2Credentials
public boolean equals(Object obj)
equals
in class OAuth2Credentials
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 OAuth2Credentials
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 OAuth2Credentials
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 static ServiceAccountCredentials.Builder newBuilder()
public ServiceAccountCredentials.Builder toBuilder()
toBuilder
in class GoogleCredentials
Copyright © 2023 Google. All rights reserved.