Skip to content

Commit 97cc4e8

Browse files
authored
docs(auth): fix broken Javadoc snippets in IdToken, Jwt, and Impersonated credentials (#15425)
1 parent 65b8a11 commit 97cc4e8

4 files changed

Lines changed: 77 additions & 52 deletions

File tree

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/IdTokenCredentials.java‎

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,67 +42,91 @@
4242
import org.jspecify.annotations.Nullable;
4343

4444
/**
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.
5046
*
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}.
5351
*
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
5558
* String credPath = "/path/to/svc_account.json";
5659
* String targetAudience = "https://example.com";
60+
* IdTokenCredentials tokenCredential;
5761
*
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)
5965
* // export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc.json
6066
* 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.");
6369
* }
6470
*
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
95114
* GenericUrl genericUrl = new GenericUrl("https://example.com");
96115
* HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(tokenCredential);
97116
* HttpTransport transport = new NetHttpTransport();
98117
* HttpRequest request = transport.createRequestFactory(adapter).buildGetRequest(genericUrl);
99118
* HttpResponse response = request.execute();
100119
*
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>
106130
*/
107131
@NullMarked
108132
public class IdTokenCredentials extends OAuth2Credentials {

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
* ServiceAccountCredentials sourceCredentials = ServiceAccountCredentials
9595
* .fromStream(new FileInputStream(credPath));
9696
* sourceCredentials = (ServiceAccountCredentials) sourceCredentials
97-
* .createScoped(Arrays.asList("https://www.googleapis.com/auth/iam"));
97+
* .createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
9898
*
9999
* ImpersonatedCredentials targetCredentials = ImpersonatedCredentials.create(sourceCredentials,
100100
* "impersonated-account@project.iam.gserviceaccount.com", null,

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtClaims.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* <p>To create and customize claims, use the builder:
4545
*
4646
* <pre><code>
47-
* Claims claims = Claims.newBuilder()
47+
* JwtClaims claims = JwtClaims.newBuilder()
4848
* .setAudience("https://example.com/some-audience")
4949
* .setIssuer("some-issuer@example.com")
5050
* .setSubject("some-subject@example.com")
@@ -77,7 +77,7 @@ public static Builder newBuilder() {
7777
}
7878

7979
/**
80-
* Returns a new Claims instance with overridden fields.
80+
* Returns a new {@link JwtClaims} instance with overridden fields.
8181
*
8282
* <p>Any non-null field will overwrite the value from the original claims instance.
8383
*
@@ -100,8 +100,9 @@ public JwtClaims merge(JwtClaims other) {
100100
/**
101101
* Returns whether or not this set of claims is complete.
102102
*
103-
* <p>Audience, issuer, and subject are required to be set in order to use the claim set for a JWT
104-
* token. An incomplete Claims instance is useful for overriding claims when using {@link
103+
* <p>Audience (or a non-empty {@code scope} additional claim), issuer, and subject are required
104+
* to be set in order to use the claim set for a JWT token. An incomplete {@link JwtClaims}
105+
* instance is useful for overriding claims when using {@link
105106
* ServiceAccountJwtAccessCredentials#jwtWithClaims(JwtClaims)} or {@link
106107
* JwtCredentials#jwtWithClaims(JwtClaims)}.
107108
*

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/JwtCredentials.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
* .setIssuer("some-issuer@example.com")
6464
* .setSubject("some-subject@example.com")
6565
* .build();
66-
* Credentials = JwtCredentials.newBuilder()
66+
* JwtCredentials credentials = JwtCredentials.newBuilder()
6767
* .setPrivateKey(privateKey)
6868
* .setPrivateKeyId("private-key-id")
6969
* .setJwtClaims(claims)

0 commit comments

Comments
 (0)