public class ImpersonatedCredentials extends GoogleCredentials implements ServiceAccountSigner, IdTokenProvider
String credPath = "/path/to/svc_account.json";
ServiceAccountCredentials sourceCredentials = ServiceAccountCredentials
.fromStream(new FileInputStream(credPath));
sourceCredentials = (ServiceAccountCredentials) sourceCredentials
.createScoped(Arrays.asList("https://www.googleapis.com/auth/iam"));
ImpersonatedCredentials targetCredentials = ImpersonatedCredentials.create(sourceCredentials,
"impersonated-account@project.iam.gserviceaccount.com", null,
Arrays.asList("https://www.googleapis.com/auth/devstorage.read_only"), 300);
Storage storage_service = StorageOptions.newBuilder().setProjectId("project-id")
.setCredentials(targetCredentials).build().getService();
for (Bucket b : storage_service.list().iterateAll())
System.out.println(b);
| Modifier and Type | Class and Description |
|---|---|
static class |
ImpersonatedCredentials.Builder |
OAuth2Credentials.CredentialsChangedListenerServiceAccountSigner.SigningExceptionIdTokenProvider.Option| Modifier and Type | Method and Description |
|---|---|
static ImpersonatedCredentials |
create(GoogleCredentials sourceCredentials,
String targetPrincipal,
List<String> delegates,
List<String> scopes,
int lifetime) |
static ImpersonatedCredentials |
create(GoogleCredentials sourceCredentials,
String targetPrincipal,
List<String> delegates,
List<String> scopes,
int lifetime,
HttpTransportFactory transportFactory) |
boolean |
equals(Object obj) |
String |
getAccount()
Returns the email field of the serviceAccount that is being impersonated.
|
int |
hashCode() |
IdToken |
idTokenWithAudience(String targetAudience,
List<IdTokenProvider.Option> options)
Returns an IdToken for the current Credential.
|
static ImpersonatedCredentials.Builder |
newBuilder() |
AccessToken |
refreshAccessToken()
Method to refresh the access token according to the specific type of credentials.
|
byte[] |
sign(byte[] toSign)
Signs the provided bytes using the private key associated with the impersonated service account
|
ImpersonatedCredentials.Builder |
toBuilder() |
String |
toString() |
create, createDelegated, createScoped, createScoped, createScopedRequired, fromStream, fromStream, getApplicationDefault, getApplicationDefaultaddChangeListener, getAccessToken, getAuthenticationType, getFromServiceLoader, getRequestMetadata, getRequestMetadata, getRequestMetadataInternal, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshIfExpired, removeChangeListenerblockingGetToCallback, getRequestMetadatapublic static ImpersonatedCredentials create(GoogleCredentials sourceCredentials, String targetPrincipal, List<String> delegates, List<String> scopes, int lifetime, HttpTransportFactory transportFactory)
sourceCredentials - The source credential used as to acquire the impersonated credentialstargetPrincipal - The service account to impersonate.delegates - The chained list of delegates required to grant the final access_token. If
set, the sequence of identities must have "Service Account Token Creator" capability
granted to the preceding identity. For example, if set to [serviceAccountB,
serviceAccountC], the sourceCredential must have the Token Creator role on serviceAccountB.
serviceAccountB must have the Token Creator on serviceAccountC. Finally, C must have Token
Creator on target_principal. If left unset, sourceCredential must have that role on
targetPrincipal.scopes - Scopes to request during the authorization grant.lifetime - Number of seconds the delegated credential should be valid for (up to 3600).transportFactory - HTTP transport factory, creates the transport used to get access
tokens.public static ImpersonatedCredentials create(GoogleCredentials sourceCredentials, String targetPrincipal, List<String> delegates, List<String> scopes, int lifetime)
sourceCredentials - The source credential used as to acquire the impersonated credentialstargetPrincipal - The service account to impersonate.delegates - The chained list of delegates required to grant the final access_token. If
set, the sequence of identities must have "Service Account Token Creator" capability
granted to the preceding identity. For example, if set to [serviceAccountB,
serviceAccountC], the sourceCredential must have the Token Creator role on serviceAccountB.
serviceAccountB must have the Token Creator on serviceAccountC. Finally, C must have Token
Creator on target_principal. If left unset, sourceCredential must have that role on
targetPrincipal.scopes - Scopes to request during the authorization grant.lifetime - Number of seconds the delegated credential should be valid for (up to 3600).public String getAccount()
getAccount in interface ServiceAccountSignerpublic byte[] sign(byte[] toSign)
sign in interface ServiceAccountSignertoSign - bytes to signSigningException - if the attempt to sign the provided bytes failedpublic AccessToken refreshAccessToken() throws IOException
OAuth2CredentialsThrows IllegalStateException if not overridden since direct use of OAuth2Credentials is only for temporary or non-refreshing access tokens.
refreshAccessToken in class OAuth2CredentialsIOException - from derived implementations@Beta public IdToken idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) throws IOException
idTokenWithAudience in interface IdTokenProvidertargetAudience - the audience field for the issued ID Tokenoptions - List of Credential specific options for for the token. For example, an IDToken
for a ImpersonatedCredentials can return the email address within the token claims if
"ImpersonatedCredentials.INCLUDE_EMAIL" is provided as a list option.IOException - if the attempt to get an IdToken failedpublic int hashCode()
hashCode in class OAuth2Credentialspublic String toString()
toString in class OAuth2Credentialspublic boolean equals(Object obj)
equals in class OAuth2Credentialspublic ImpersonatedCredentials.Builder toBuilder()
toBuilder in class GoogleCredentialspublic static ImpersonatedCredentials.Builder newBuilder()
Copyright © 2020 Google. All rights reserved.