Skip to content

Commit

Permalink
fix: make calendar field private and modify it through builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-f-n committed Jul 14, 2022
1 parent 2fa1c26 commit a3a3138
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Expand Up @@ -111,7 +111,7 @@ public class ImpersonatedCredentials extends GoogleCredentials

private transient HttpTransportFactory transportFactory;

@VisibleForTesting transient Calendar calendar = Calendar.getInstance();
private transient Calendar calendar;

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

/**
* Clones the impersonated credentials with a new calendar.
*
* @param calendar the calendar that will be used by the new ImpersonatedCredentials instance when
* parsing the received expiration time of the refreshed access token
* @return the cloned impersonated credentials with the given custom calendar
*/
public ImpersonatedCredentials createWithCustomCalendar(Calendar calendar) {
return toBuilder()
.setScopes(this.scopes)
.setLifetime(this.lifetime)
.setDelegates(this.delegates)
.setHttpTransportFactory(this.transportFactory)
.setQuotaProjectId(this.quotaProjectId)
.setIamEndpointOverride(this.iamEndpointOverride)
.setCalendar(calendar)
.build();
}

@Override
protected Map<String, List<String>> getAdditionalHeaders() {
Map<String, List<String>> headers = super.getAdditionalHeaders();
Expand All @@ -454,6 +473,7 @@ private ImpersonatedCredentials(Builder builder) {
this.quotaProjectId = builder.quotaProjectId;
this.iamEndpointOverride = builder.iamEndpointOverride;
this.transportFactoryClassName = this.transportFactory.getClass().getName();
this.calendar = builder.getCalendar();
if (this.delegates == null) {
this.delegates = new ArrayList<String>();
}
Expand Down Expand Up @@ -610,6 +630,7 @@ public static class Builder extends GoogleCredentials.Builder {
private HttpTransportFactory transportFactory;
private String quotaProjectId;
private String iamEndpointOverride;
private Calendar calendar = Calendar.getInstance();

protected Builder() {}

Expand Down Expand Up @@ -682,6 +703,15 @@ public Builder setIamEndpointOverride(String iamEndpointOverride) {
return this;
}

public Builder setCalendar(Calendar calendar) {
this.calendar = calendar;
return this;
}

public Calendar getCalendar() {
return this.calendar;
}

public ImpersonatedCredentials build() {
return new ImpersonatedCredentials(this);
}
Expand Down
Expand Up @@ -567,7 +567,6 @@ void refreshAccessToken_delegates_success() throws IOException, IllegalStateExce

@Test
void refreshAccessToken_GMT_dateParsedCorrectly() throws IOException, IllegalStateException {

Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, VALID_LIFETIME);

Expand All @@ -576,14 +575,15 @@ void refreshAccessToken_GMT_dateParsedCorrectly() throws IOException, IllegalSta
mockTransportFactory.transport.setExpireTime(getFormattedTime(c.getTime()));
ImpersonatedCredentials targetCredentials =
ImpersonatedCredentials.create(
sourceCredentials,
IMPERSONATED_CLIENT_EMAIL,
null,
IMMUTABLE_SCOPES_LIST,
VALID_LIFETIME,
mockTransportFactory);
// Set system timezone to GMT
targetCredentials.calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
sourceCredentials,
IMPERSONATED_CLIENT_EMAIL,
null,
IMMUTABLE_SCOPES_LIST,
VALID_LIFETIME,
mockTransportFactory)
.createWithCustomCalendar(
// Set system timezone to GMT
Calendar.getInstance(TimeZone.getTimeZone("GMT")));

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

@Test
void refreshAccessToken_nonGMT_dateParsedCorrectly() throws IOException, IllegalStateException {

Calendar c = Calendar.getInstance();
c.add(Calendar.SECOND, VALID_LIFETIME);

Expand All @@ -601,14 +600,15 @@ void refreshAccessToken_nonGMT_dateParsedCorrectly() throws IOException, Illegal
mockTransportFactory.transport.setExpireTime(getFormattedTime(c.getTime()));
ImpersonatedCredentials targetCredentials =
ImpersonatedCredentials.create(
sourceCredentials,
IMPERSONATED_CLIENT_EMAIL,
null,
IMMUTABLE_SCOPES_LIST,
VALID_LIFETIME,
mockTransportFactory);
// Set system timezone to one different than GMT
targetCredentials.calendar = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
sourceCredentials,
IMPERSONATED_CLIENT_EMAIL,
null,
IMMUTABLE_SCOPES_LIST,
VALID_LIFETIME,
mockTransportFactory)
.createWithCustomCalendar(
// Set system timezone to one different than GMT
Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles")));

assertEquals(
c.getTime().toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli(),
Expand Down

0 comments on commit a3a3138

Please sign in to comment.