|
42 | 42 | import org.jspecify.annotations.Nullable; |
43 | 43 |
|
44 | 44 | /** |
45 | | - * IdTokenCredentials provides a Google Issued OpenIdConnect token. <br> |
46 | | - * Use an ID token to access services that require presenting an ID token for authentication such as |
47 | | - * Cloud Functions or Cloud Run.<br> |
48 | | - * The following Credential subclasses support IDTokens: ServiceAccountCredentials, |
49 | | - * ComputeEngineCredentials, ImpersonatedCredentials. |
| 45 | + * IdTokenCredentials provides a Google-issued OpenID Connect (OIDC) ID token. |
50 | 46 | * |
51 | | - * <p>For more information see <br> |
52 | | - * Usage:<br> |
| 47 | + * <p>Use an ID token to access services that require presenting an ID token for authentication, |
| 48 | + * such as Cloud Functions or Cloud Run. The following credential subclasses support ID tokens: |
| 49 | + * {@link ServiceAccountCredentials}, {@link ComputeEngineCredentials}, and {@link |
| 50 | + * ImpersonatedCredentials}. |
53 | 51 | * |
54 | | - * <pre> |
| 52 | + * <p>For more information, see <a |
| 53 | + * href="https://docs.cloud.google.com/docs/authentication/get-id-token">Get an ID token</a>. |
| 54 | + * |
| 55 | + * <p>Usage: |
| 56 | + * |
| 57 | + * <pre>{@code |
55 | 58 | * String credPath = "/path/to/svc_account.json"; |
56 | 59 | * String targetAudience = "https://example.com"; |
| 60 | + * IdTokenCredentials tokenCredential; |
57 | 61 | * |
58 | | - * // For Application Default Credentials (as ServiceAccountCredentials) |
| 62 | + * // Choose one of the following ways to create IdTokenCredentials: |
| 63 | + * |
| 64 | + * // 1. From Application Default Credentials (as ServiceAccountCredentials) |
59 | 65 | * // export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc.json |
60 | 66 | * GoogleCredentials adcCreds = GoogleCredentials.getApplicationDefault(); |
61 | | - * if (!adcCreds instanceof IdTokenProvider) { |
62 | | - * // handle error message |
| 67 | + * if (!(adcCreds instanceof IdTokenProvider)) { |
| 68 | + * throw new IllegalStateException("Credentials do not support ID tokens."); |
63 | 69 | * } |
64 | 70 | * |
65 | | - * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() |
66 | | - * .setIdTokenProvider(adcCreds) |
67 | | - * .setTargetAudience(targetAudience).build(); |
68 | | - * |
69 | | - * // for ServiceAccountCredentials |
70 | | - * ServiceAccountCredentials saCreds = ServiceAccountCredentials.fromStream(new FileInputStream(credPath)); |
71 | | - * saCreds = (ServiceAccountCredentials) saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/iam")); |
72 | | - * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() |
73 | | - * .setIdTokenProvider(saCreds) |
74 | | - * .setTargetAudience(targetAudience).build(); |
75 | | - * |
76 | | - * // for ComputeEngineCredentials |
77 | | - * ComputeEngineCredentials caCreds = ComputeEngineCredentials.create(); |
78 | | - * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() |
79 | | - * .setIdTokenProvider(caCreds) |
80 | | - * .setTargetAudience(targetAudience) |
81 | | - * .setOptions(Arrays.asList(ComputeEngineCredentials.ID_TOKEN_FORMAT_FULL)) |
82 | | - * .build(); |
83 | | - * |
84 | | - * // for ImpersonatedCredentials |
85 | | - * ImpersonatedCredentials imCreds = ImpersonatedCredentials.create(saCreds, |
86 | | - * "impersonated-account@project.iam.gserviceaccount.com", null, |
87 | | - * Arrays.asList("https://www.googleapis.com/auth/cloud-platform"), 300); |
88 | | - * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() |
89 | | - * .setIdTokenProvider(imCreds) |
90 | | - * .setTargetAudience(targetAudience) |
91 | | - * .setOptions(Arrays.asList(ImpersonatedCredentials.INCLUDE_EMAIL)) |
92 | | - * .build(); |
93 | | - * |
94 | | - * // Use the IdTokenCredential in an authorized transport |
| 71 | + * tokenCredential = |
| 72 | + * IdTokenCredentials.newBuilder() |
| 73 | + * .setIdTokenProvider((IdTokenProvider) adcCreds) |
| 74 | + * .setTargetAudience(targetAudience) |
| 75 | + * .build(); |
| 76 | + * |
| 77 | + * // 2. From ServiceAccountCredentials |
| 78 | + * ServiceAccountCredentials saCreds = |
| 79 | + * ServiceAccountCredentials.fromStream(new FileInputStream(credPath)); |
| 80 | + * saCreds = |
| 81 | + * (ServiceAccountCredentials) |
| 82 | + * saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")); |
| 83 | + * tokenCredential = |
| 84 | + * IdTokenCredentials.newBuilder() |
| 85 | + * .setIdTokenProvider(saCreds) |
| 86 | + * .setTargetAudience(targetAudience) |
| 87 | + * .build(); |
| 88 | + * |
| 89 | + * // 3. From ComputeEngineCredentials |
| 90 | + * ComputeEngineCredentials ceCreds = ComputeEngineCredentials.create(); |
| 91 | + * tokenCredential = |
| 92 | + * IdTokenCredentials.newBuilder() |
| 93 | + * .setIdTokenProvider(ceCreds) |
| 94 | + * .setTargetAudience(targetAudience) |
| 95 | + * .setOptions(Arrays.asList(IdTokenProvider.Option.FORMAT_FULL)) |
| 96 | + * .build(); |
| 97 | + * |
| 98 | + * // 4. From ImpersonatedCredentials (using saCreds from option 2 as source credentials) |
| 99 | + * ImpersonatedCredentials imCreds = |
| 100 | + * ImpersonatedCredentials.create( |
| 101 | + * saCreds, |
| 102 | + * "impersonated-account@project.iam.gserviceaccount.com", |
| 103 | + * null, |
| 104 | + * Arrays.asList("https://www.googleapis.com/auth/cloud-platform"), |
| 105 | + * 300); |
| 106 | + * tokenCredential = |
| 107 | + * IdTokenCredentials.newBuilder() |
| 108 | + * .setIdTokenProvider(imCreds) |
| 109 | + * .setTargetAudience(targetAudience) |
| 110 | + * .setOptions(Arrays.asList(IdTokenProvider.Option.INCLUDE_EMAIL)) |
| 111 | + * .build(); |
| 112 | + * |
| 113 | + * // Use the IdTokenCredentials in an authorized transport |
95 | 114 | * GenericUrl genericUrl = new GenericUrl("https://example.com"); |
96 | 115 | * HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential); |
97 | 116 | * HttpTransport transport = new NetHttpTransport(); |
98 | 117 | * HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl); |
99 | 118 | * HttpResponse response = request.execute(); |
100 | 119 | * |
101 | | - * // Print the token, expiration and the audience |
102 | | - * System.out.println(tokenCredential.getIdToken().getTokenValue()); |
103 | | - * System.out.println(tokenCredential.getIdToken().getJsonWebSignature().getPayload().getAudienceAsList()); |
104 | | - * System.out.println(tokenCredential.getIdToken().getJsonWebSignature().getPayload().getExpirationTimeSeconds()); |
105 | | - * </pre> |
| 120 | + * // Print the token value, expiration, and parsed payload claims |
| 121 | + * tokenCredential.refreshIfExpired(); |
| 122 | + * IdToken idToken = tokenCredential.getIdToken(); |
| 123 | + * System.out.println(idToken.getTokenValue()); |
| 124 | + * System.out.println(idToken.getExpirationTime()); |
| 125 | + * JsonWebSignature jws = |
| 126 | + * JsonWebSignature.parse(GsonFactory.getDefaultInstance(), idToken.getTokenValue()); |
| 127 | + * System.out.println(jws.getPayload().getAudienceAsList()); |
| 128 | + * System.out.println(jws.getPayload().getExpirationTimeSeconds()); |
| 129 | + * }</pre> |
106 | 130 | */ |
107 | 131 | @NullMarked |
108 | 132 | public class IdTokenCredentials extends OAuth2Credentials { |
|
0 commit comments