|
| 1 | +/* |
| 2 | + * Copyright 2022 Google Inc. |
| 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 | +// [auth_cloud_idtoken_impersonated_credentials] |
| 18 | + |
| 19 | +import com.google.auth.oauth2.GoogleCredentials; |
| 20 | +import com.google.auth.oauth2.IdTokenCredentials; |
| 21 | +import com.google.auth.oauth2.IdTokenProvider.Option; |
| 22 | +import com.google.auth.oauth2.ImpersonatedCredentials; |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +public class IdTokenFromImpersonatedCredentials { |
| 28 | + |
| 29 | + public static void main(String[] args) throws IOException { |
| 30 | + // TODO(Developer): Replace the below variables before running the code. |
| 31 | + |
| 32 | + // Provide the scopes that you might need to request to access Google APIs, |
| 33 | + // depending on the level of access you need. |
| 34 | + // The best practice is to use the cloud-wide scope and use IAM to narrow the permissions. |
| 35 | + // https://cloud.google.com/docs/authentication#authorization_for_services |
| 36 | + // For more information, see: https://developers.google.com/identity/protocols/oauth2/scopes |
| 37 | + String scope = "https://www.googleapis.com/auth/cloud-platform"; |
| 38 | + |
| 39 | + // The service name for which the id token is requested. Service name refers to the |
| 40 | + // logical identifier of an API service, such as "pubsub.googleapis.com". |
| 41 | + String targetAudience = "iap.googleapis.com"; |
| 42 | + |
| 43 | + // The name of the privilege-bearing service account for whom the credential is created. |
| 44 | + String impersonatedServiceAccount = "[email protected]"; |
| 45 | + |
| 46 | + getIdTokenUsingOAuth2(impersonatedServiceAccount, scope, targetAudience); |
| 47 | + } |
| 48 | + |
| 49 | + // Use a service account (SA1) to impersonate as another service account (SA2) and obtain id token |
| 50 | + // for the impersonated account. |
| 51 | + // To obtain token for SA2, SA1 should have the "roles/iam.serviceAccountTokenCreator" permission |
| 52 | + // on SA2. |
| 53 | + public static void getIdTokenUsingOAuth2( |
| 54 | + String impersonatedServiceAccount, String scope, String targetAudience) throws IOException { |
| 55 | + |
| 56 | + // Construct the GoogleCredentials object which obtains the default configuration from your |
| 57 | + // working environment. |
| 58 | + GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault(); |
| 59 | + |
| 60 | + // delegates: The chained list of delegates required to grant the final accessToken. |
| 61 | + // For more information, see: |
| 62 | + // https://cloud.google.com/iam/docs/create-short-lived-credentials-direct#sa-credentials-permissions |
| 63 | + // Delegate is NOT USED here. |
| 64 | + List<String> delegates = null; |
| 65 | + |
| 66 | + // Create the impersonated credential. |
| 67 | + ImpersonatedCredentials impersonatedCredentials = |
| 68 | + ImpersonatedCredentials.create( |
| 69 | + googleCredentials, impersonatedServiceAccount, delegates, Arrays.asList(scope), 300); |
| 70 | + |
| 71 | + // Set the impersonated credential, target audience and token options. |
| 72 | + IdTokenCredentials idTokenCredentials = |
| 73 | + IdTokenCredentials.newBuilder() |
| 74 | + .setIdTokenProvider(impersonatedCredentials) |
| 75 | + .setTargetAudience(targetAudience) |
| 76 | + // Setting this will include email in the id token. |
| 77 | + .setOptions(Arrays.asList(Option.INCLUDE_EMAIL)) |
| 78 | + .build(); |
| 79 | + |
| 80 | + // Get the ID token. |
| 81 | + // Once you've obtained the ID token, use it to make an authenticated call |
| 82 | + // to the target audience. |
| 83 | + String idToken = idTokenCredentials.refreshAccessToken().getTokenValue(); |
| 84 | + System.out.println("Generated ID token."); |
| 85 | + } |
| 86 | +} |
| 87 | +// [auth_cloud_idtoken_impersonated_credentials] |
0 commit comments