Class DownscopedCredentials
- All Implemented Interfaces:
Serializable
This class provides a server-side approach for generating downscoped tokens, suitable for
situations where Credential Access Boundary rules change infrequently or a single downscoped
credential is reused many times. For scenarios where rules change frequently, or you need to
generate many unique downscoped tokens, the client-side approach using
com.google.auth.credentialaccessboundary.ClientSideCredentialAccessBoundaryFactory is more
efficient.
To downscope permissions you must define a CredentialAccessBoundary which specifies
the upper bound of permissions that the credential can access. You must also provide a source
credential which will be used to acquire the downscoped credential.
Usage:
GoogleCredentials sourceCredentials = GoogleCredentials.getApplicationDefault()
.createScoped("https://www.googleapis.com/auth/cloud-platform");
CredentialAccessBoundary.AccessBoundaryRule rule =
CredentialAccessBoundary.AccessBoundaryRule.newBuilder()
.setAvailableResource(
"//storage.googleapis.com/projects/_/buckets/bucket")
.addAvailablePermission("inRole:roles/storage.objectViewer")
.build();
DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(
CredentialAccessBoundary.newBuilder().addRule(rule).build())
.build();
AccessToken accessToken = downscopedCredentials.refreshAccessToken();
OAuth2Credentials credentials = OAuth2Credentials.create(accessToken);
Storage storage =
StorageOptions.newBuilder().setCredentials(credentials).build().getService();
Blob blob = storage.get(BlobId.of("bucket", "object"));
System.out.printf("Blob %s retrieved.", blob.getBlobId());
Note that OAuth2CredentialsWithRefresh can instead be used to consume the downscoped
token, allowing for automatic token refreshes by providing a OAuth2CredentialsWithRefresh.OAuth2RefreshHandler.- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.google.auth.oauth2.OAuth2Credentials
OAuth2Credentials.CredentialsChangedListener -
Field Summary
Fields inherited from class com.google.auth.Credentials
GOOGLE_DEFAULT_UNIVERSE -
Method Summary
Modifier and TypeMethodDescriptionReturns the universe domain for the credential.Method to refresh the access token according to the specific type of credentials.Methods inherited from class com.google.auth.oauth2.OAuth2Credentials
addChangeListener, create, equals, getAccessToken, getAdditionalHeaders, getAuthenticationType, getFromServiceLoader, getRequestMetadata, getRequestMetadata, getRequestMetadataInternal, hashCode, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshIfExpired, removeChangeListener, toBuilder, toStringMethods inherited from class com.google.auth.Credentials
blockingGetToCallback, getMetricsCredentialType, getRequestMetadata
-
Method Details
-
refreshAccessToken
Description copied from class:OAuth2CredentialsMethod to refresh the access token according to the specific type of credentials.Throws IllegalStateException if not overridden since direct use of OAuth2Credentials is only for temporary or non-refreshing access tokens.
- Overrides:
refreshAccessTokenin classOAuth2Credentials- Returns:
- never
- Throws:
IOException
-
getSourceCredentials
-
getCredentialAccessBoundary
-
getUniverseDomain
Returns the universe domain for the credential.- Overrides:
getUniverseDomainin classCredentials- Returns:
- An explicit universe domain if it was explicitly provided, otherwise the default Google universe will be returned.
-
newBuilder
-