Class ServiceAccountJwtAccessCredentials
- All Implemented Interfaces:
JwtProvider
,QuotaProjectIdProvider
,ServiceAccountSigner
,Serializable
Uses a JSON Web Token (JWT) directly in the request metadata to provide authorization.
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface com.google.auth.ServiceAccountSigner
ServiceAccountSigner.SigningException
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final String
private final String
(package private) com.google.api.client.util.Clock
private static final long
private com.google.common.cache.LoadingCache
<JwtClaims, JwtCredentials> private final URI
(package private) static final String
(package private) static final long
private final PrivateKey
private final String
private final String
private static final long
Fields inherited from class com.google.auth.Credentials
GOOGLE_DEFAULT_UNIVERSE
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
ServiceAccountJwtAccessCredentials
(String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId) Constructor with minimum identifying information.private
ServiceAccountJwtAccessCredentials
(String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId, URI defaultAudience, String quotaProjectId) Constructor with full information. -
Method Summary
Modifier and TypeMethodDescriptionprivate com.google.common.cache.LoadingCache
<JwtClaims, JwtCredentials> boolean
(package private) static ServiceAccountJwtAccessCredentials
Returns service account credentials defined by JSON using the format supported by the Google Developers Console.(package private) static ServiceAccountJwtAccessCredentials
Returns service account credentials defined by JSON using the format supported by the Google Developers Console.Factory using PKCS#8 for the private key.fromPkcs8
(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, URI defaultAudience) Factory using PKCS#8 for the private key.(package private) static ServiceAccountJwtAccessCredentials
fromPkcs8
(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, URI defaultAudience, String quotaProjectId) fromStream
(InputStream credentialsStream) Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.fromStream
(InputStream credentialsStream, URI defaultAudience) Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.Returns the service account associated with the signer.A constant string name describing the authentication technology.final String
final String
final PrivateKey
final 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.int
hashCode()
boolean
Whether the credentials have metadata entries that should be added to each request.boolean
Indicates whether or not the Auth mechanism works purely by including request metadata.jwtWithClaims
(JwtClaims newClaims) Returns a new JwtCredentials instance with modified claims.private void
readObject
(ObjectInputStream input) void
refresh()
Discard any cached databyte[]
sign
(byte[] toSign) Signs the provided bytes using the private key associated with the service account.toString()
Methods inherited from class com.google.auth.Credentials
blockingGetToCallback, getMetricsCredentialType, getRequestMetadata, getUniverseDomain
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
JWT_ACCESS_PREFIX
- See Also:
-
LIFE_SPAN_SECS
static final long LIFE_SPAN_SECS -
CLOCK_SKEW
private static final long CLOCK_SKEW -
clientId
-
clientEmail
-
privateKey
-
privateKeyId
-
defaultAudience
-
quotaProjectId
-
credentialsCache
-
clock
transient com.google.api.client.util.Clock clock
-
-
Constructor Details
-
ServiceAccountJwtAccessCredentials
private ServiceAccountJwtAccessCredentials(String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId) Constructor with minimum identifying information.- Parameters:
clientId
- Client ID of the service account from the console. May be null.clientEmail
- Client email address of the service account from the console.privateKey
- RSA private key object for the service account.privateKeyId
- Private key identifier for the service account. May be null.
-
ServiceAccountJwtAccessCredentials
private ServiceAccountJwtAccessCredentials(String clientId, String clientEmail, PrivateKey privateKey, String privateKeyId, URI defaultAudience, String quotaProjectId) Constructor with full information.- Parameters:
clientId
- Client ID of the service account from the console. May be null.clientEmail
- Client email address of the service account from the console.privateKey
- RSA private key object for the service account.privateKeyId
- Private key identifier for the service account. May be null.defaultAudience
- Audience to use if not provided by transport. May be null.
-
-
Method Details
-
fromJson
Returns service account credentials defined by JSON using the format supported by the Google Developers Console.- Parameters:
json
- a map from the JSON representing the credentials.- Returns:
- the credentials defined by the JSON.
- Throws:
IOException
- if the credential cannot be created from the JSON.
-
fromJson
static ServiceAccountJwtAccessCredentials fromJson(Map<String, Object> json, URI defaultAudience) throws IOExceptionReturns service account credentials defined by JSON using the format supported by the Google Developers Console.- Parameters:
json
- a map from the JSON representing the credentials.defaultAudience
- Audience to use if not provided by transport. May be null.- Returns:
- the credentials defined by the JSON.
- Throws:
IOException
- if the credential cannot be created from the JSON.
-
fromPkcs8
public static ServiceAccountJwtAccessCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId) throws IOException Factory using PKCS#8 for the private key.- Parameters:
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.- Returns:
- New ServiceAccountJwtAcceessCredentials created from a private key.
- Throws:
IOException
- if the credential cannot be created from the private key.
-
fromPkcs8
public static ServiceAccountJwtAccessCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, URI defaultAudience) throws IOException Factory using PKCS#8 for the private key.- Parameters:
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.defaultAudience
- Audience to use if not provided by transport. May be null.- Returns:
- New ServiceAccountJwtAcceessCredentials created from a private key.
- Throws:
IOException
- if the credential cannot be created from the private key.
-
fromPkcs8
static ServiceAccountJwtAccessCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, URI defaultAudience, String quotaProjectId) throws IOException - Throws:
IOException
-
fromStream
public static ServiceAccountJwtAccessCredentials fromStream(InputStream credentialsStream) throws IOException Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.- 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
public static ServiceAccountJwtAccessCredentials fromStream(InputStream credentialsStream, URI defaultAudience) throws IOException Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.- Parameters:
credentialsStream
- the stream with the credential definition.defaultAudience
- Audience to use if not provided by transport. May be null.- Returns:
- the credential defined by the credentialsStream.
- Throws:
IOException
- if the credential cannot be created from the stream.
-
createCache
-
jwtWithClaims
Returns a new JwtCredentials instance with modified claims.- Specified by:
jwtWithClaims
in interfaceJwtProvider
- Parameters:
newClaims
- new claims. Any unspecified claim fields will default to the the current values.- Returns:
- new credentials
-
getAuthenticationType
Description copied from class:Credentials
A constant string name describing the authentication technology.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.- Specified by:
getAuthenticationType
in classCredentials
- Returns:
- The type of authentication used.
-
hasRequestMetadata
public boolean hasRequestMetadata()Description copied from class:Credentials
Whether the credentials have metadata entries that should be added to each request.This should be called by the transport layer to see if
Credentials.getRequestMetadata()
should be used for each request.- Specified by:
hasRequestMetadata
in classCredentials
- Returns:
- Whether or not the transport layer should call
Credentials.getRequestMetadata()
-
hasRequestMetadataOnly
public boolean hasRequestMetadataOnly()Description copied from class:Credentials
Indicates whether or not the Auth mechanism works purely by including request metadata.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.
- Specified by:
hasRequestMetadataOnly
in classCredentials
- Returns:
- Whether or not the Auth mechanism works purely by including request metadata.
-
getRequestMetadata
Description copied from class:Credentials
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
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.- Overrides:
getRequestMetadata
in classCredentials
- Parameters:
uri
- URI of the entry point for the request.executor
- Executor to perform the request.callback
- Callback to execute when the request is finished.
-
getRequestMetadata
Provide the request metadata by putting an access JWT directly in the metadata.- Specified by:
getRequestMetadata
in classCredentials
- Parameters:
uri
- URI of the entry point for the request.- Returns:
- The request metadata used for populating headers or other context.
- Throws:
IOException
- if there was an error getting up-to-date access. The exception should implementRetryable
andisRetryable()
will return true if the operation may be retried.
-
refresh
public void refresh()Discard any cached data- Specified by:
refresh
in classCredentials
-
getClientId
-
getClientEmail
-
getPrivateKey
-
getPrivateKeyId
-
getAccount
Description copied from interface:ServiceAccountSigner
Returns the service account associated with the signer.- Specified by:
getAccount
in interfaceServiceAccountSigner
- Returns:
- The service account associated with the signer.
-
sign
public byte[] sign(byte[] toSign) Description copied from interface:ServiceAccountSigner
Signs the provided bytes using the private key associated with the service account.- Specified by:
sign
in interfaceServiceAccountSigner
- Parameters:
toSign
- bytes to sign- Returns:
- signed bytes
-
hashCode
public int hashCode() -
toString
-
equals
-
readObject
- Throws:
IOException
ClassNotFoundException
-
newBuilder
-
toBuilder
-
getQuotaProjectId
- Specified by:
getQuotaProjectId
in interfaceQuotaProjectIdProvider
- Returns:
- the quota project ID used for quota and billing purposes
-