|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.auth.samples; |
| 18 | + |
| 19 | +// [START auth_client_cab_consumer] |
| 20 | +import com.google.auth.oauth2.AccessToken; |
| 21 | +import com.google.auth.oauth2.OAuth2CredentialsWithRefresh; |
| 22 | +import com.google.cloud.storage.Blob; |
| 23 | +import com.google.cloud.storage.Storage; |
| 24 | +import com.google.cloud.storage.StorageOptions; |
| 25 | +import java.io.IOException; |
| 26 | +// [END auth_client_cab_consumer] |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Demonstrates retrieving a Cloud Storage blob using a downscoped. This example showcases the |
| 31 | + * consumer side of the downscoping process. It retrieves a blob's content using credentials that |
| 32 | + * have limited access based on a pre-defined Credential Access Boundary. |
| 33 | + */ |
| 34 | +public class DownscopedAccessTokenConsumer { |
| 35 | + |
| 36 | + public static void main(String[] args) throws IOException { |
| 37 | + // TODO(developer): Replace these variables before running the sample. |
| 38 | + // The Cloud Storage bucket name. |
| 39 | + String bucketName = "your-gcs-bucket-name"; |
| 40 | + // The Cloud Storage object name that resides in the specified bucket. |
| 41 | + String objectName = "your-gcs-object-name"; |
| 42 | + |
| 43 | + retrieveBlobWithDownscopedToken(bucketName, objectName); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Simulates token consumer readonly access to the specified object. |
| 48 | + * |
| 49 | + * @param bucketName The name of the Cloud Storage bucket containing the blob. |
| 50 | + * @param objectName The name of the Cloud Storage object (blob). |
| 51 | + * @return The content of the blob as a String, or {@code null} if the blob does not exist. |
| 52 | + * @throws IOException If an error occurs during communication with Cloud Storage or token |
| 53 | + * retrieval. This can include issues with authentication, authorization, or network |
| 54 | + * connectivity. |
| 55 | + */ |
| 56 | + // [START auth_client_cab_consumer] |
| 57 | + public static String retrieveBlobWithDownscopedToken( |
| 58 | + final String bucketName, final String objectName) throws IOException { |
| 59 | + // You can pass an `OAuth2RefreshHandler` to `OAuth2CredentialsWithRefresh` which will allow the |
| 60 | + // library to seamlessly handle downscoped token refreshes on expiration. |
| 61 | + OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = |
| 62 | + new OAuth2CredentialsWithRefresh.OAuth2RefreshHandler() { |
| 63 | + @Override |
| 64 | + public AccessToken refreshAccessToken() throws IOException { |
| 65 | + // The common pattern of usage is to have a token broker pass the downscoped short-lived |
| 66 | + // access tokens to a token consumer via some secure authenticated channel. |
| 67 | + // For illustration purposes, we are generating the downscoped token locally. |
| 68 | + // We want to test the ability to limit access to objects with a certain prefix string |
| 69 | + // in the resource bucket. objectName.substring(0, 3) is the prefix here. This field is |
| 70 | + // not required if access to all bucket resources are allowed. If access to limited |
| 71 | + // resources in the bucket is needed, this mechanism can be used. |
| 72 | + return DownscopedAccessTokenGenerator |
| 73 | + .getTokenFromBroker(bucketName, objectName); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + AccessToken downscopedToken = handler.refreshAccessToken(); |
| 78 | + |
| 79 | + OAuth2CredentialsWithRefresh credentials = |
| 80 | + OAuth2CredentialsWithRefresh.newBuilder() |
| 81 | + .setAccessToken(downscopedToken) |
| 82 | + .setRefreshHandler(handler) |
| 83 | + .build(); |
| 84 | + |
| 85 | + StorageOptions options = StorageOptions.newBuilder().setCredentials(credentials).build(); |
| 86 | + Storage storage = options.getService(); |
| 87 | + |
| 88 | + Blob blob = storage.get(bucketName, objectName); |
| 89 | + if (blob == null) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + return new String(blob.getContent()); |
| 93 | + } |
| 94 | + // [END auth_client_cab_consumer] |
| 95 | +} |
0 commit comments