From ddea5e970614d1a8820d4c1da64b44aa529869e4 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Mon, 23 Nov 2026 05:35:48 +0000 Subject: [PATCH] docs(auth): fix broken Javadoc snippets in IdToken, Jwt, and Impersonated 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. --- .../auth/oauth2/IdTokenCredentials.java | 103 +++++++++++------- .../auth/oauth2/ImpersonatedCredentials.java | 2 +- .../com/google/auth/oauth2/JwtClaims.java | 9 +- .../google/auth/oauth2/JwtCredentials.java | 2 +- 4 files changed, 69 insertions(+), 47 deletions(-) diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/IdTokenCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/IdTokenCredentials.java index 2689ff80d84b..e2736b181a70 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/IdTokenCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/IdTokenCredentials.java @@ -42,67 +42,88 @@ import org.jspecify.annotations.Nullable; /** - * IdTokenCredentials provides a Google Issued OpenIdConnect token.
- * Use an ID token to access services that require presenting an ID token for authentication such as - * Cloud Functions or Cloud Run.
- * The following Credential subclasses support IDTokens: ServiceAccountCredentials, - * ComputeEngineCredentials, ImpersonatedCredentials. + * IdTokenCredentials provides a Google-issued OpenID Connect (OIDC) ID token. * - *

For more information see
- * Usage:
+ *

Use an ID token to access services that require presenting an ID token for authentication, + * such as Cloud Functions or Cloud Run. The following credential subclasses support ID tokens: + * {@link ServiceAccountCredentials}, {@link ComputeEngineCredentials}, and {@link + * ImpersonatedCredentials}. * - *

+ * 

For more information, see Get an ID token. + * + *

Usage: + * + *

{@code
  * String credPath = "/path/to/svc_account.json";
  * String targetAudience = "https://example.com";
  *
  * // For Application Default Credentials (as ServiceAccountCredentials)
  * // export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc.json
  * GoogleCredentials adcCreds = GoogleCredentials.getApplicationDefault();
- * if (!adcCreds instanceof IdTokenProvider) {
- *   // handle error message
+ * if (!(adcCreds instanceof IdTokenProvider)) {
+ *   throw new IllegalStateException("Credentials do not support ID tokens.");
  * }
  *
- * IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder()
- *     .setIdTokenProvider(adcCreds)
- *     .setTargetAudience(targetAudience).build();
+ * 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();
  *
- * // Use the IdTokenCredential in an authorized transport
+ * // Use the IdTokenCredentials in an authorized transport
  * GenericUrl genericUrl = new GenericUrl("https://example.com");
  * HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential);
  * HttpTransport transport = new NetHttpTransport();
  * HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl);
  * HttpResponse response = request.execute();
  *
- * // Print the token, expiration and the audience
- * System.out.println(tokenCredential.getIdToken().getTokenValue());
- * System.out.println(tokenCredential.getIdToken().getJsonWebSignature().getPayload().getAudienceAsList());
- * System.out.println(tokenCredential.getIdToken().getJsonWebSignature().getPayload().getExpirationTimeSeconds());
- * 
+ * // Print the token value, expiration, and parsed payload claims + * tokenCredential.refreshIfExpired(); + * IdToken idToken = tokenCredential.getIdToken(); + * System.out.println(idToken.getTokenValue()); + * System.out.println(idToken.getExpirationTime()); + * JsonWebSignature jws = + * JsonWebSignature.parse(GsonFactory.getDefaultInstance(), idToken.getTokenValue()); + * System.out.println(jws.getPayload().getAudienceAsList()); + * System.out.println(jws.getPayload().getExpirationTimeSeconds()); + * }
*/ @NullMarked public class IdTokenCredentials extends OAuth2Credentials { diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java index 821bbb61a76b..83a8288d0f60 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java @@ -94,7 +94,7 @@ * ServiceAccountCredentials sourceCredentials = ServiceAccountCredentials * .fromStream(new FileInputStream(credPath)); * sourceCredentials = (ServiceAccountCredentials) sourceCredentials - * .createScoped(Arrays.asList("https://www.googleapis.com/auth/iam")); + * .createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")); * * ImpersonatedCredentials targetCredentials = ImpersonatedCredentials.create(sourceCredentials, * "impersonated-account@project.iam.gserviceaccount.com", null, diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtClaims.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtClaims.java index cb165b76850c..a3fca26b5f87 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtClaims.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtClaims.java @@ -44,7 +44,7 @@ *

To create and customize claims, use the builder: * *


- * Claims claims = Claims.newBuilder()
+ * JwtClaims claims = JwtClaims.newBuilder()
  *     .setAudience("https://example.com/some-audience")
  *     .setIssuer("some-issuer@example.com")
  *     .setSubject("some-subject@example.com")
@@ -77,7 +77,7 @@ public static Builder newBuilder() {
   }
 
   /**
-   * Returns a new Claims instance with overridden fields.
+   * Returns a new {@link JwtClaims} instance with overridden fields.
    *
    * 

Any non-null field will overwrite the value from the original claims instance. * @@ -100,8 +100,9 @@ public JwtClaims merge(JwtClaims other) { /** * Returns whether or not this set of claims is complete. * - *

Audience, issuer, and subject are required to be set in order to use the claim set for a JWT - * token. An incomplete Claims instance is useful for overriding claims when using {@link + *

Audience (or a non-empty {@code scope} additional claim), issuer, and subject are required + * to be set in order to use the claim set for a JWT token. An incomplete {@link JwtClaims} + * instance is useful for overriding claims when using {@link * ServiceAccountJwtAccessCredentials#jwtWithClaims(JwtClaims)} or {@link * JwtCredentials#jwtWithClaims(JwtClaims)}. * diff --git a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtCredentials.java b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtCredentials.java index 7997d5fa6b32..9eae766df513 100644 --- a/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtCredentials.java +++ b/google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtCredentials.java @@ -63,7 +63,7 @@ * .setIssuer("some-issuer@example.com") * .setSubject("some-subject@example.com") * .build(); - * Credentials = JwtCredentials.newBuilder() + * Credentials credentials = JwtCredentials.newBuilder() * .setPrivateKey(privateKey) * .setPrivateKeyId("private-key-id") * .setJwtClaims(claims)