-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathGdchCredentials.java
More file actions
812 lines (722 loc) · 30.8 KB
/
GdchCredentials.java
File metadata and controls
812 lines (722 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
* Copyright 2022, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.auth.oauth2;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.json.webtoken.JsonWebToken;
import com.google.api.client.util.Clock;
import com.google.api.client.util.GenericData;
import com.google.api.client.util.SecurityUtils;
import com.google.api.client.util.StringUtils;
import com.google.api.core.ObsoleteApi;
import com.google.auth.http.HttpTransportFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
public class GdchCredentials extends GoogleCredentials {
private static final LoggerProvider LOGGER_PROVIDER =
LoggerProvider.forClazz(GdchCredentials.class);
private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. ";
@VisibleForTesting static final String SUPPORTED_FORMAT_VERSION = "1";
private static final String SERVICE_ACCOUNT_TOKEN_TYPE =
"urn:k8s:params:oauth:token-type:serviceaccount";
private static final int DEFAULT_LIFETIME_IN_SECONDS = 3600;
private final PrivateKey privateKey;
private final String privateKeyId;
private final String projectId;
private final String serviceIdentityName;
private final URI tokenServerUri;
private final String apiAudience;
private final int lifetime;
private final String transportFactoryClassName;
private final String caCertPath;
private transient HttpTransportFactory transportFactory;
/**
* Internal constructor.
*
* @param builder A builder for {@link GdchCredentials} See {@link GdchCredentials.Builder}.
*/
@VisibleForTesting
GdchCredentials(GdchCredentials.Builder builder) {
this.projectId = Preconditions.checkNotNull(builder.projectId);
this.privateKeyId = Preconditions.checkNotNull(builder.privateKeyId);
this.privateKey = Preconditions.checkNotNull(builder.privateKey);
this.serviceIdentityName = Preconditions.checkNotNull(builder.serviceIdentityName);
this.tokenServerUri = Preconditions.checkNotNull(builder.tokenServerUri);
this.transportFactory = Preconditions.checkNotNull(builder.transportFactory);
this.transportFactoryClassName = this.transportFactory.getClass().getName();
this.caCertPath = builder.caCertPath;
this.apiAudience = builder.apiAudience;
this.lifetime = builder.lifetime;
this.name = GoogleCredentialsInfo.GDCH_CREDENTIALS.getCredentialName();
}
/**
* Returns credentials defined by a GdchCredentials key file in JSON format from the Google
* Developers Console.
*
* <p>Important: If you accept a credential configuration (credential JSON/File/Stream) from an
* external source for authentication to Google Cloud Platform, you must validate it before
* providing it to any Google API or library. Providing an unvalidated credential configuration to
* Google APIs can compromise the security of your systems and data. For more information, refer
* to {@see <a
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}.
*
* @param credentialsStream the stream with the credential definition.
* @return the credential defined by the credentialsStream.
* @throws IOException if the credential cannot be created from the stream.
*/
public static GdchCredentials fromStream(InputStream credentialsStream) throws IOException {
return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY);
}
/**
* Returns credentials defined by a GdchCredentials key file in JSON format from the Google
* Developers Console.
*
* <p>Important: If you accept a credential configuration (credential JSON/File/Stream) from an
* external source for authentication to Google Cloud Platform, you must validate it before
* providing it to any Google API or library. Providing an unvalidated credential configuration to
* Google APIs can compromise the security of your systems and data. For more information, refer
* to {@see <a
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}.
*
* @param credentialsStream the stream with the credential definition.
* @param transportFactory HTTP transport factory, creates the transport used to get access
* tokens.
* @return the credential defined by the credentialsStream.
* @throws IOException if the credential cannot be created from the stream.
*/
public static GdchCredentials fromStream(
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
Preconditions.checkNotNull(transportFactory);
GenericJson fileContents = parseJsonInputStream(credentialsStream);
String fileType = extractFromJson(fileContents, "type");
if (fileType.equals(GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType())) {
return fromJson(fileContents, transportFactory);
}
throw new IOException(
String.format(
"Error reading credentials from stream, 'type' value '%s' not recognized."
+ " Expecting '%s'.",
fileType, GoogleCredentialsInfo.GDCH_CREDENTIALS.getFileType()));
}
/**
* Create GDCH service account credentials defined by JSON.
*
* @param json a map from the JSON representing the credentials.
* @return the GDCH service account credentials defined by the JSON.
* @throws IOException if the credential cannot be created from the JSON.
*/
static GdchCredentials fromJson(Map<String, Object> json) throws IOException {
String caCertPath = (String) json.get("ca_cert_path");
return fromJson(json, new TransportFactoryForGdch(caCertPath));
}
/**
* Create GDCH service account credentials defined by JSON.
*
* @param json a map from the JSON representing the credentials.
* @param transportFactory HTTP transport factory, creates the transport used to get access
* tokens.
* @return the GDCH service account credentials defined by the JSON.
* @throws IOException if the credential cannot be created from the JSON.
*/
@VisibleForTesting
static GdchCredentials fromJson(Map<String, Object> json, HttpTransportFactory transportFactory)
throws IOException {
String formatVersion = validateField((String) json.get("format_version"), "format_version");
String projectId = validateField((String) json.get("project"), "project");
String privateKeyId = validateField((String) json.get("private_key_id"), "private_key_id");
String privateKeyPkcs8 = validateField((String) json.get("private_key"), "private_key");
String serviceIdentityName = validateField((String) json.get("name"), "name");
String tokenServerUriStringFromCreds =
validateField((String) json.get("token_uri"), "token_uri");
String caCertPath = (String) json.get("ca_cert_path");
if (!SUPPORTED_FORMAT_VERSION.equals(formatVersion)) {
throw new IOException(
String.format("Only format version %s is supported.", SUPPORTED_FORMAT_VERSION));
}
URI tokenServerUriFromCreds = null;
try {
tokenServerUriFromCreds = new URI(tokenServerUriStringFromCreds);
} catch (URISyntaxException e) {
throw new IOException("Token server URI specified in 'token_uri' could not be parsed.");
}
GdchCredentials.Builder builder =
GdchCredentials.newBuilder()
.setProjectId(projectId)
.setPrivateKeyId(privateKeyId)
.setTokenServerUri(tokenServerUriFromCreds)
.setServiceIdentityName(serviceIdentityName)
.setCaCertPath(caCertPath)
.setHttpTransportFactory(transportFactory);
return fromPkcs8(privateKeyPkcs8, builder);
}
/**
* Internal constructor.
*
* @param privateKeyPkcs8 EC private key object for the service account in PKCS#8 format.
* @param builder A builder for GdchCredentials.
* @return an instance of GdchCredentials.
*/
static GdchCredentials fromPkcs8(String privateKeyPkcs8, GdchCredentials.Builder builder)
throws IOException {
// GDCH key generation natively only supports the EC algorithm.
PrivateKey privateKey =
OAuth2Utils.privateKeyFromPkcs8(privateKeyPkcs8, OAuth2Utils.Pkcs8Algorithm.EC);
builder.setPrivateKey(privateKey);
return new GdchCredentials(builder);
}
/**
* This method is obsolete. Please use {@link #createWithGdchAudience(String)}} instead. Create a
* copy of GDCH credentials with the specified audience.
*
* @param apiAudience The intended audience for GDCH credentials.
*/
@ObsoleteApi("Use createWithGdchAudience(String) instead.")
public GdchCredentials createWithGdchAudience(URI apiAudience) {
if (apiAudience == null) {
throw new IllegalArgumentException(
"Audience cannot be null or empty for GDCH service account credentials.");
}
return this.toBuilder().setGdchAudience(apiAudience.toString()).build();
}
/**
* Create a copy of GDCH credentials with the specified audience.
*
* @param apiAudience The intended audience for GDCH credentials.
*/
public GdchCredentials createWithGdchAudience(String apiAudience) {
if (Strings.isNullOrEmpty(apiAudience)) {
throw new IllegalArgumentException(
"Audience cannot be null or empty for GDCH service account credentials.");
}
return this.toBuilder().setGdchAudience(apiAudience).build();
}
/**
* Refresh the OAuth2 access token by getting a new access token using a JSON Web Token (JWT).
*
* <p>For GDCH credentials, this class creates a self-signed JWT, and sends to the GDCH
* authentication endpoint (tokenServerUri) to exchange an access token for the intended api
* audience (apiAudience).
*/
@Override
public AccessToken refreshAccessToken() throws IOException {
Preconditions.checkNotNull(
this.apiAudience,
"Audience cannot be null or empty for GDCH service account credentials. "
+ "Specify the audience by calling createWithGdchAudience.");
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
long currentTime = Clock.SYSTEM.currentTimeMillis();
String assertion = createAssertion(jsonFactory, currentTime);
GenericData tokenRequest = new GenericData();
tokenRequest.set("audience", apiAudience);
tokenRequest.set("grant_type", OAuth2Utils.TOKEN_TYPE_TOKEN_EXCHANGE);
tokenRequest.set("requested_token_type", OAuth2Utils.TOKEN_TYPE_ACCESS_TOKEN);
tokenRequest.set("subject_token", assertion);
tokenRequest.set("subject_token_type", SERVICE_ACCOUNT_TOKEN_TYPE);
JsonHttpContent content = new JsonHttpContent(jsonFactory, tokenRequest);
HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory();
HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), content);
// Disable automatic logging by google-http-java-client to prevent leakage of sensitive tokens.
// Client Library Debug Logging via LoggingUtils is used instead.
request.setLoggingEnabled(false);
request.setParser(new JsonObjectParser(jsonFactory));
HttpResponse response;
String errorTemplate = "Error getting access token for GDCH service account: %s, iss: %s";
try {
LoggingUtils.logRequest(request, LOGGER_PROVIDER, "Sending request to get GDCH access token");
response = request.execute();
LoggingUtils.logResponse(
response, LOGGER_PROVIDER, "Received response for GDCH access token");
} catch (HttpResponseException re) {
String message = String.format(errorTemplate, re.getMessage(), getServiceIdentityName());
throw GoogleAuthException.createWithTokenEndpointResponseException(re, message);
} catch (IOException e) {
String message = String.format(errorTemplate, e.getMessage(), getServiceIdentityName());
throw GoogleAuthException.createWithTokenEndpointIOException(e, message);
}
GenericData responseData = response.parseAs(GenericData.class);
LoggingUtils.logResponsePayload(
responseData, LOGGER_PROVIDER, "Response payload for GDCH access token");
String accessToken =
OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX);
int expiresInSeconds =
OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L;
return new AccessToken(accessToken, new Date(expiresAtMilliseconds));
}
/**
* Create a self-signed JWT for GDCH authentication flow.
*
* <p>The self-signed JWT is used to exchange access token from GDCH authentication
* (tokenServerUri), not for API call. It uses the serviceIdentityName as the `iss` and `sub`
* claim, and the tokenServerUri as the `aud` claim. The JWT is signed with the privateKey.
*/
String createAssertion(JsonFactory jsonFactory, long currentTime) throws IOException {
JsonWebSignature.Header header = new JsonWebSignature.Header();
header.setAlgorithm("ES256");
header.setType("JWT");
header.setKeyId(privateKeyId);
JsonWebToken.Payload payload = new JsonWebToken.Payload();
payload.setIssuer(getIssuerSubjectValue(projectId, serviceIdentityName));
payload.setSubject(getIssuerSubjectValue(projectId, serviceIdentityName));
payload.setIssuedAtTimeSeconds(currentTime / 1000);
payload.setExpirationTimeSeconds(currentTime / 1000 + this.lifetime);
payload.setAudience(tokenServerUri.toString());
String assertion;
try {
assertion = signUsingEsSha256(privateKey, jsonFactory, header, payload);
} catch (GeneralSecurityException e) {
throw new GoogleAuthException(
false, 0, "Error signing service account access token request with private key.", e);
}
return assertion;
}
/**
* Get the issuer and subject value in the format GDCH token server required.
*
* <p>This value is specific to GDCH and combined parameter used for both `iss` and `sub` fields
* in JWT claim.
*/
@VisibleForTesting
static String getIssuerSubjectValue(String projectId, String serviceIdentityName) {
return String.format("system:serviceaccount:%s:%s", projectId, serviceIdentityName);
}
@Override
public final String getProjectId() {
return projectId;
}
public final String getPrivateKeyId() {
return privateKeyId;
}
public final PrivateKey getPrivateKey() {
return privateKey;
}
public final String getServiceIdentityName() {
return serviceIdentityName;
}
public final URI getTokenServerUri() {
return tokenServerUri;
}
/**
* Returns the underlying audience string set for this credentials object.
*
* @return the audience string, or null if no audience has been set.
*/
public final String getGdchAudience() {
return apiAudience;
}
/**
* NOTE: This method is obsolete, please use {@link #getGdchAudience()} instead. Returns a URI
* representation of the underlying audience string set for this credentials object. This method
* may fail if the underlying audience string does not conform to a URI format.
*
* @return a URI object representing the audience of the credentials, or null if no audience has
* been set or if the audience string is not a valid URI.
*/
@ObsoleteApi("Use getGdchAudience() instead.")
public final URI getApiAudience() {
if (Strings.isNullOrEmpty(apiAudience)) {
return null;
}
try {
return new URI(apiAudience);
} catch (URISyntaxException e) {
return null;
}
}
public final HttpTransportFactory getTransportFactory() {
return transportFactory;
}
public final String getCaCertPath() {
return caCertPath;
}
public static Builder newBuilder() {
return new Builder();
}
@Override
public Builder toBuilder() {
return new Builder(this);
}
@SuppressWarnings("unused")
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
// properly deserialize the transient transportFactory.
input.defaultReadObject();
transportFactory = newInstance(transportFactoryClassName);
}
@Override
public int hashCode() {
return Objects.hash(
projectId,
privateKeyId,
privateKey,
serviceIdentityName,
tokenServerUri,
transportFactoryClassName,
apiAudience,
caCertPath,
lifetime);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("projectId", projectId)
.add("privateKeyId", privateKeyId)
.add("serviceIdentityName", serviceIdentityName)
.add("tokenServerUri", tokenServerUri)
.add("transportFactoryClassName", transportFactoryClassName)
.add("caCertPath", caCertPath)
.add("apiAudience", apiAudience)
.add("lifetime", lifetime)
.toString();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof GdchCredentials)) {
return false;
}
GdchCredentials other = (GdchCredentials) obj;
return Objects.equals(this.projectId, other.projectId)
&& Objects.equals(this.privateKeyId, other.privateKeyId)
&& Objects.equals(this.privateKey, other.privateKey)
&& Objects.equals(this.serviceIdentityName, other.serviceIdentityName)
&& Objects.equals(this.tokenServerUri, other.tokenServerUri)
&& Objects.equals(this.transportFactoryClassName, other.transportFactoryClassName)
&& Objects.equals(this.apiAudience, other.apiAudience)
&& Objects.equals(this.caCertPath, other.caCertPath)
&& Objects.equals(this.lifetime, other.lifetime);
}
static InputStream readStream(File file) throws FileNotFoundException {
return new FileInputStream(file);
}
public static class Builder extends GoogleCredentials.Builder {
private String projectId;
private String privateKeyId;
private PrivateKey privateKey;
private String serviceIdentityName;
private URI tokenServerUri;
private String apiAudience;
private HttpTransportFactory transportFactory;
private String caCertPath;
private int lifetime = DEFAULT_LIFETIME_IN_SECONDS;
protected Builder() {}
protected Builder(GdchCredentials credentials) {
this.projectId = credentials.projectId;
this.privateKeyId = credentials.privateKeyId;
this.privateKey = credentials.privateKey;
this.serviceIdentityName = credentials.serviceIdentityName;
this.tokenServerUri = credentials.tokenServerUri;
this.transportFactory = credentials.transportFactory;
this.caCertPath = credentials.caCertPath;
this.lifetime = credentials.lifetime;
}
@CanIgnoreReturnValue
public Builder setProjectId(String projectId) {
this.projectId = projectId;
return this;
}
@CanIgnoreReturnValue
public Builder setPrivateKeyId(String privateKeyId) {
this.privateKeyId = privateKeyId;
return this;
}
@CanIgnoreReturnValue
public Builder setPrivateKey(PrivateKey privateKey) {
this.privateKey = privateKey;
return this;
}
@CanIgnoreReturnValue
public Builder setServiceIdentityName(String name) {
this.serviceIdentityName = name;
return this;
}
@CanIgnoreReturnValue
public Builder setTokenServerUri(URI tokenServerUri) {
this.tokenServerUri = tokenServerUri;
return this;
}
@CanIgnoreReturnValue
public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) {
this.transportFactory = transportFactory;
return this;
}
@CanIgnoreReturnValue
public Builder setCaCertPath(String caCertPath) {
this.caCertPath = caCertPath;
return this;
}
@CanIgnoreReturnValue
public Builder setGdchAudience(String apiAudience) {
if (Strings.isNullOrEmpty(apiAudience)) {
throw new IllegalArgumentException(
"Audience cannot be null or empty for GDCH service account credentials.");
}
this.apiAudience = apiAudience;
return this;
}
@CanIgnoreReturnValue
@ObsoleteApi("Use setGdchAudience(String) instead")
public Builder setGdchAudience(URI apiAudience) {
if (apiAudience == null) {
throw new IllegalArgumentException(
"Audience cannot be null for GDCH service account credentials.");
}
this.apiAudience = apiAudience.toString();
return this;
}
public String getProjectId() {
return projectId;
}
public String getPrivateKeyId() {
return privateKeyId;
}
public PrivateKey getPrivateKey() {
return privateKey;
}
public String getServiceIdentityName() {
return serviceIdentityName;
}
public URI getTokenServerUri() {
return tokenServerUri;
}
public HttpTransportFactory getHttpTransportFactory() {
return transportFactory;
}
public String getCaCertPath() {
return caCertPath;
}
public int getLifetime() {
return lifetime;
}
@Override
public GdchCredentials build() {
return new GdchCredentials(this);
}
}
private static String validateField(String field, String fieldName) throws IOException {
if (field == null || field.isEmpty()) {
throw new IOException(
String.format(
"Error reading GDCH service account credential from JSON, %s is misconfigured.",
fieldName));
}
return field;
}
/*
* Internal HttpTransportFactory for GDCH credentials.
*
* <p> GDCH authentication server could use a self-signed certificate, thus the
* client could
* provide the CA certificate path through the `ca_cert_path` in GDCH JSON file.
*
* <p> The TransportFactoryForGdch subclass would read the certificate and
* create a trust store,
* then use the trust store to create a transport.
*
* <p> If the GDCH authentication server uses well known CA certificate, then a
* regular transport
* would be set.
*/
static class TransportFactoryForGdch implements HttpTransportFactory {
HttpTransport transport;
public TransportFactoryForGdch(String caCertPath) throws IOException {
setTransport(caCertPath);
}
@Override
public HttpTransport create() {
return transport;
}
private void setTransport(String caCertPath) throws IOException {
if (caCertPath == null || caCertPath.isEmpty()) {
this.transport = new NetHttpTransport();
return;
}
try {
InputStream certificateStream = readStream(new File(caCertPath));
this.transport =
new NetHttpTransport.Builder().trustCertificatesFromStream(certificateStream).build();
} catch (IOException e) {
throw new IOException(
String.format(
"Error reading certificate file from CA cert path, value '%s': %s",
caCertPath, e.getMessage()),
e);
} catch (GeneralSecurityException e) {
throw new IOException("Error initiating transport with certificate stream.", e);
}
}
}
/**
* Signs the JWS header and payload using the ES256 algorithm (ECDSA with SHA-256).
*
* <p>The ES256 algorithm is defined in <a
* href="https://tools.ietf.org/html/rfc7518#section-3.4">RFC 7518 Section 3.4</a>. This method
* follows the JWS Compact Serialization format described in <a
* href="https://tools.ietf.org/html/rfc7515#section-3.1">RFC 7515 Section 3.1</a>.
*
* <p>Unlike RSA signatures, ECDSA signatures produced by the Java Cryptography Architecture (JCA)
* are DER-encoded. This method transcodes the DER-encoded signature into the concatenated R|S
* format required by the JWS standard, as specified in <a
* href="https://tools.ietf.org/html/rfc7515#appendix-A.3">RFC 7515 Appendix A.3</a>.
*
* @param privateKey The Elliptic Curve private key used for signing.
* @param jsonFactory The JSON factory to serialize header and payload.
* @param header The JWS header (e.g., containing "alg": "ES256").
* @param payload The JWS payload containing claims like "iss", "sub", and "aud".
* @return A complete, signed JWS string in the format {@code [header].[payload].[signature]}.
* @throws GeneralSecurityException If signing fails due to cryptographic errors.
* @throws IOException If serialization or transcoding fails.
*/
@VisibleForTesting
static String signUsingEsSha256(
PrivateKey privateKey,
JsonFactory jsonFactory,
JsonWebSignature.Header header,
JsonWebToken.Payload payload)
throws GeneralSecurityException, GoogleAuthException {
try {
// 1. Construct the JWS Signing Input: Base64URL(UTF8(Header)) + '.' +
// Base64URL(UTF8(Payload))
String content =
Base64.getUrlEncoder().withoutPadding().encodeToString(jsonFactory.toByteArray(header))
+ "."
+ Base64.getUrlEncoder()
.withoutPadding()
.encodeToString(jsonFactory.toByteArray(payload));
byte[] contentBytes = StringUtils.getBytesUtf8(content);
// 2. Create the digital signature using SHA256withECDSA.
byte[] signature =
SecurityUtils.sign(SecurityUtils.getEs256SignatureAlgorithm(), privateKey, contentBytes);
// 3. Transcode the signature from DER to Concatenated R|S.
byte[] jwsSignature = transcodeDerToConcat(signature, 64);
// 4. Return final JWS: [Signing Input] + '.' + Base64URL(Signature)
return content + "." + Base64.getUrlEncoder().withoutPadding().encodeToString(jwsSignature);
} catch (IOException e) {
throw new GoogleAuthException(false, 0, "Error serializing or transcoding JWT.", e);
}
}
/**
* Transcodes a DER-encoded ECDSA signature into the concatenated R|S format.
*
* <p>DER format (ASN.1): {@code SEQUENCE { r INTEGER, s INTEGER }}
*
* <p>Concatenated format: {@code r | s} (where {@code |} is concatenation).
*
* @param derSignature The raw bytes of the DER-encoded signature.
* @param outputLength The total expected length of the concatenated signature (64 bytes for
* ES256).
* @return The signature in concatenated R|S format.
* @throws IOException If the DER format is invalid.
*/
@VisibleForTesting
static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength)
throws GoogleAuthException {
// Validate basic ASN.1 DER structure (0x30 = SEQUENCE)
if (derSignature.length < 8 || derSignature[0] != 0x30) {
throw new GoogleAuthException(false, 0, "Invalid DER signature format.", null);
}
int offset = 2;
int seqLength = derSignature[1] & 0xFF;
// Handle long-form length encoding for the sequence
if (seqLength == 0x81) {
offset = 3;
seqLength = derSignature[2] & 0xFF;
}
if (derSignature.length - offset != seqLength) {
throw new GoogleAuthException(false, 0, "Invalid DER signature length.", null);
}
// Parse Integer R (0x02 = INTEGER)
if (derSignature[offset++] != 0x02) {
throw new GoogleAuthException(false, 0, "Expected INTEGER for R.", null);
}
int rLength = derSignature[offset++];
// Skip leading zero byte if it exists (DER integers are signed; zero is added to stay positive)
if (derSignature[offset] == 0x00 && rLength > 1 && (derSignature[offset + 1] & 0x80) != 0) {
offset++;
rLength--;
}
byte[] r = new byte[rLength];
System.arraycopy(derSignature, offset, r, 0, rLength);
offset += rLength;
// Parse Integer S
if (derSignature[offset++] != 0x02) {
throw new GoogleAuthException(false, 0, "Expected INTEGER for S.", null);
}
int sLength = derSignature[offset++];
if (derSignature[offset] == 0x00 && sLength > 1 && (derSignature[offset + 1] & 0x80) != 0) {
offset++;
sLength--;
}
byte[] s = new byte[sLength];
System.arraycopy(derSignature, offset, s, 0, sLength);
// Concatenate r and s into fixed-length segments (32 bytes each for ES256)
int keySizeBytes = outputLength / 2;
if (r.length > keySizeBytes || s.length > keySizeBytes) {
throw new GoogleAuthException(
false,
0,
String.format(
"Invalid R or S length. R: %d, S: %d, Expected: %d",
r.length, s.length, keySizeBytes),
null);
}
byte[] result = new byte[outputLength];
System.arraycopy(r, 0, result, keySizeBytes - r.length, r.length);
System.arraycopy(s, 0, result, outputLength - s.length, s.length);
return result;
}
}