Skip to content

Commit 62d7b08

Browse files
committed
fix: make calendar field private and modify it through builder
1 parent 5a6937c commit 62d7b08

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class ImpersonatedCredentials extends GoogleCredentials
111111

112112
private transient HttpTransportFactory transportFactory;
113113

114-
@VisibleForTesting transient Calendar calendar = Calendar.getInstance();
114+
private transient Calendar calendar;
115115

116116
/**
117117
* @param sourceCredentials the source credential used to acquire the impersonated credentials. It
@@ -432,6 +432,25 @@ public GoogleCredentials createScoped(Collection<String> scopes) {
432432
.build();
433433
}
434434

435+
/**
436+
* Clones the impersonated credentials with a new calendar.
437+
*
438+
* @param calendar the calendar that will be used by the new ImpersonatedCredentials instance when
439+
* parsing the received expiration time of the refreshed access token
440+
* @return the cloned impersonated credentials with the given custom calendar
441+
*/
442+
public ImpersonatedCredentials createWithCustomCalendar(Calendar calendar) {
443+
return toBuilder()
444+
.setScopes(this.scopes)
445+
.setLifetime(this.lifetime)
446+
.setDelegates(this.delegates)
447+
.setHttpTransportFactory(this.transportFactory)
448+
.setQuotaProjectId(this.quotaProjectId)
449+
.setIamEndpointOverride(this.iamEndpointOverride)
450+
.setCalendar(calendar)
451+
.build();
452+
}
453+
435454
@Override
436455
protected Map<String, List<String>> getAdditionalHeaders() {
437456
Map<String, List<String>> headers = super.getAdditionalHeaders();
@@ -454,6 +473,7 @@ private ImpersonatedCredentials(Builder builder) {
454473
this.quotaProjectId = builder.quotaProjectId;
455474
this.iamEndpointOverride = builder.iamEndpointOverride;
456475
this.transportFactoryClassName = this.transportFactory.getClass().getName();
476+
this.calendar = builder.getCalendar();
457477
if (this.delegates == null) {
458478
this.delegates = new ArrayList<String>();
459479
}
@@ -610,6 +630,7 @@ public static class Builder extends GoogleCredentials.Builder {
610630
private HttpTransportFactory transportFactory;
611631
private String quotaProjectId;
612632
private String iamEndpointOverride;
633+
private Calendar calendar = Calendar.getInstance();
613634

614635
protected Builder() {}
615636

@@ -682,6 +703,15 @@ public Builder setIamEndpointOverride(String iamEndpointOverride) {
682703
return this;
683704
}
684705

706+
public Builder setCalendar(Calendar calendar) {
707+
this.calendar = calendar;
708+
return this;
709+
}
710+
711+
public Calendar getCalendar() {
712+
return this.calendar;
713+
}
714+
685715
public ImpersonatedCredentials build() {
686716
return new ImpersonatedCredentials(this);
687717
}

oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ void refreshAccessToken_delegates_success() throws IOException, IllegalStateExce
567567

568568
@Test
569569
void refreshAccessToken_GMT_dateParsedCorrectly() throws IOException, IllegalStateException {
570-
571570
Calendar c = Calendar.getInstance();
572571
c.add(Calendar.SECOND, VALID_LIFETIME);
573572

@@ -576,14 +575,15 @@ void refreshAccessToken_GMT_dateParsedCorrectly() throws IOException, IllegalSta
576575
mockTransportFactory.transport.setExpireTime(getFormattedTime(c.getTime()));
577576
ImpersonatedCredentials targetCredentials =
578577
ImpersonatedCredentials.create(
579-
sourceCredentials,
580-
IMPERSONATED_CLIENT_EMAIL,
581-
null,
582-
IMMUTABLE_SCOPES_LIST,
583-
VALID_LIFETIME,
584-
mockTransportFactory);
585-
// Set system timezone to GMT
586-
targetCredentials.calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
578+
sourceCredentials,
579+
IMPERSONATED_CLIENT_EMAIL,
580+
null,
581+
IMMUTABLE_SCOPES_LIST,
582+
VALID_LIFETIME,
583+
mockTransportFactory)
584+
.createWithCustomCalendar(
585+
// Set system timezone to GMT
586+
Calendar.getInstance(TimeZone.getTimeZone("GMT")));
587587

588588
assertEquals(
589589
c.getTime().toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli(),
@@ -592,7 +592,6 @@ void refreshAccessToken_GMT_dateParsedCorrectly() throws IOException, IllegalSta
592592

593593
@Test
594594
void refreshAccessToken_nonGMT_dateParsedCorrectly() throws IOException, IllegalStateException {
595-
596595
Calendar c = Calendar.getInstance();
597596
c.add(Calendar.SECOND, VALID_LIFETIME);
598597

@@ -601,14 +600,15 @@ void refreshAccessToken_nonGMT_dateParsedCorrectly() throws IOException, Illegal
601600
mockTransportFactory.transport.setExpireTime(getFormattedTime(c.getTime()));
602601
ImpersonatedCredentials targetCredentials =
603602
ImpersonatedCredentials.create(
604-
sourceCredentials,
605-
IMPERSONATED_CLIENT_EMAIL,
606-
null,
607-
IMMUTABLE_SCOPES_LIST,
608-
VALID_LIFETIME,
609-
mockTransportFactory);
610-
// Set system timezone to one different than GMT
611-
targetCredentials.calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
603+
sourceCredentials,
604+
IMPERSONATED_CLIENT_EMAIL,
605+
null,
606+
IMMUTABLE_SCOPES_LIST,
607+
VALID_LIFETIME,
608+
mockTransportFactory)
609+
.createWithCustomCalendar(
610+
// Set system timezone to one different than GMT
611+
Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles")));
612612

613613
assertEquals(
614614
c.getTime().toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli(),

0 commit comments

Comments
 (0)