Repository navigation
Conversation
…ated credentials Wave 1 Auth library Javadoc snippets (AUTH-0a, separated from VerifyGoogleIdToken sample): - Fix 6 compile/doc bugs in IdTokenCredentials Javadoc snippet. - Fix uncompilable JwtCredentials and JwtClaims snippets and ImpersonatedCredentials snippet scope.
There was a problem hiding this comment.
Code Review
This pull request updates and corrects Javadoc documentation and code examples across several classes, including IdTokenCredentials, ImpersonatedCredentials, JwtClaims, and JwtCredentials. The feedback suggests improving the Javadoc code snippets further: in IdTokenCredentials, reusing a single tokenCredential variable to prevent unused variable warnings and make the snippet fully copy-pasteable; and in JwtCredentials, using the specific JwtCredentials type instead of the generic Credentials to be more idiomatic.
| * IdTokenCredentials tokenCredential = | ||
| * IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider((IdTokenProvider) adcCreds) | ||
| * .setTargetAudience(targetAudience) | ||
| * .build(); | ||
| * | ||
| * // for ServiceAccountCredentials | ||
| * ServiceAccountCredentials saCreds = ServiceAccountCredentials.fromStream(new FileInputStream(credPath)); | ||
| * saCreds = (ServiceAccountCredentials) saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/iam")); | ||
| * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider(saCreds) | ||
| * .setTargetAudience(targetAudience).build(); | ||
| * // For ServiceAccountCredentials | ||
| * ServiceAccountCredentials saCreds = | ||
| * ServiceAccountCredentials.fromStream(new FileInputStream(credPath)); | ||
| * saCreds = | ||
| * (ServiceAccountCredentials) | ||
| * saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")); | ||
| * IdTokenCredentials saTokenCredential = | ||
| * IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider(saCreds) | ||
| * .setTargetAudience(targetAudience) | ||
| * .build(); | ||
| * | ||
| * // for ComputeEngineCredentials | ||
| * ComputeEngineCredentials caCreds = ComputeEngineCredentials.create(); | ||
| * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider(caCreds) | ||
| * .setTargetAudience(targetAudience) | ||
| * .setOptions(Arrays.asList(ComputeEngineCredentials.ID_TOKEN_FORMAT_FULL)) | ||
| * .build(); | ||
| * // For ComputeEngineCredentials | ||
| * ComputeEngineCredentials ceCreds = ComputeEngineCredentials.create(); | ||
| * IdTokenCredentials ceTokenCredential = | ||
| * IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider(ceCreds) | ||
| * .setTargetAudience(targetAudience) | ||
| * .setOptions(Arrays.asList(IdTokenProvider.Option.FORMAT_FULL)) | ||
| * .build(); | ||
| * | ||
| * // for ImpersonatedCredentials | ||
| * ImpersonatedCredentials imCreds = ImpersonatedCredentials.create(saCreds, | ||
| * "impersonated-account@project.iam.gserviceaccount.com", null, | ||
| * Arrays.asList("https://www.googleapis.com/auth/cloud-platform"), 300); | ||
| * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider(imCreds) | ||
| * .setTargetAudience(targetAudience) | ||
| * .setOptions(Arrays.asList(ImpersonatedCredentials.INCLUDE_EMAIL)) | ||
| * .build(); | ||
| * // For ImpersonatedCredentials | ||
| * ImpersonatedCredentials imCreds = | ||
| * ImpersonatedCredentials.create( | ||
| * saCreds, | ||
| * "impersonated-account@project.iam.gserviceaccount.com", | ||
| * null, | ||
| * Arrays.asList("https://www.googleapis.com/auth/cloud-platform"), | ||
| * 300); | ||
| * IdTokenCredentials imTokenCredential = | ||
| * IdTokenCredentials.newBuilder() | ||
| * .setIdTokenProvider(imCreds) | ||
| * .setTargetAudience(targetAudience) | ||
| * .setOptions(Arrays.asList(IdTokenProvider.Option.INCLUDE_EMAIL)) | ||
| * .build(); |
There was a problem hiding this comment.
In this Javadoc snippet, multiple distinct variables are declared (saTokenCredential, ceTokenCredential, imTokenCredential) but never used, while tokenCredential is declared once at the beginning and then used at the end of the snippet. This can lead to unused variable warnings or compilation errors if a developer copy-pastes the entire block. Reusing a single tokenCredential variable across all examples makes the snippet cleaner, more cohesive, and fully copy-pasteable.
* IdTokenCredentials tokenCredential;
*
* // For Application Default Credentials (as ServiceAccountCredentials)
* // export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc.json
* GoogleCredentials adcCreds = GoogleCredentials.getApplicationDefault();
* if (!(adcCreds instanceof IdTokenProvider)) {
* throw new IllegalStateException("Credentials do not support ID tokens.");
* }
*
* tokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider((IdTokenProvider) adcCreds)
* .setTargetAudience(targetAudience)
* .build();
*
* // For ServiceAccountCredentials
* ServiceAccountCredentials saCreds =
* ServiceAccountCredentials.fromStream(new FileInputStream(credPath));
* saCreds =
* (ServiceAccountCredentials)
* saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
* tokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider(saCreds)
* .setTargetAudience(targetAudience)
* .build();
*
* // For ComputeEngineCredentials
* ComputeEngineCredentials ceCreds = ComputeEngineCredentials.create();
* tokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider(ceCreds)
* .setTargetAudience(targetAudience)
* .setOptions(Arrays.asList(IdTokenProvider.Option.FORMAT_FULL))
* .build();
*
* // For ImpersonatedCredentials
* ImpersonatedCredentials imCreds =
* ImpersonatedCredentials.create(
* saCreds,
* "impersonated-account@project.iam.gserviceaccount.com",
* null,
* Arrays.asList("https://www.googleapis.com/auth/cloud-platform"),
* 300);
* tokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider(imCreds)
* .setTargetAudience(targetAudience)
* .setOptions(Arrays.asList(IdTokenProvider.Option.INCLUDE_EMAIL))
* .build();References
- When calling
createScopedonServiceAccountCredentials, the returned object is of typeGoogleCredentials. A cast toServiceAccountCredentialsis required if you need to access subclass-specific methods such ascreateWithUseJwtAccessWithScope.
| * .setSubject("some-subject@example.com") | ||
| * .build(); | ||
| * Credentials = JwtCredentials.newBuilder() | ||
| * Credentials credentials = JwtCredentials.newBuilder() |
There was a problem hiding this comment.
Using the specific type JwtCredentials instead of the generic Credentials is more idiomatic, matches the builder type, and avoids requiring an extra import for com.google.auth.Credentials when developers copy-paste this snippet.
| * Credentials credentials = JwtCredentials.newBuilder() | |
| * JwtCredentials credentials = JwtCredentials.newBuilder() |
No description provided.