Skip to content

Commit

Permalink
Fix: Not loosing the access token when calling UserCredentials#ToBuil… (
Browse files Browse the repository at this point in the history
#993)

* Update oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java

* lint fixes

Co-authored-by: Timur Sadykov <stim@google.com>
  • Loading branch information
qcastel and TimurSadykov committed Dec 2, 2022
1 parent 040acef commit 84afdb8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions oauth2_http/java/com/google/auth/oauth2/UserCredentials.java
Expand Up @@ -53,6 +53,7 @@
import java.io.ObjectInputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -410,6 +411,16 @@ public Builder setAccessToken(AccessToken token) {
return this;
}

public Builder setExpirationMargin(Duration expirationMargin) {
super.setExpirationMargin(expirationMargin);
return this;
}

public Builder setRefreshMargin(Duration refreshMargin) {
super.setRefreshMargin(refreshMargin);
return this;
}

public Builder setQuotaProjectId(String quotaProjectId) {
super.setQuotaProjectId(quotaProjectId);
return this;
Expand Down
Expand Up @@ -55,8 +55,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -830,6 +833,26 @@ public void IdTokenCredentials_NoUserEmailScope_throws() throws IOException {
}
}

@Test
public void userCredentials_toBuilder_copyEveryAttribute() {
MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory();
UserCredentials credentials =
UserCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setRefreshToken(REFRESH_TOKEN)
.setAccessToken(new AccessToken(ACCESS_TOKEN, new Date()))
.setHttpTransportFactory(httpTransportFactory)
.setTokenServerUri(URI.create("https://foo1.com/bar"))
.setQuotaProjectId(QUOTA_PROJECT)
.setExpirationMargin(Duration.of(10, ChronoUnit.SECONDS))
.setRefreshMargin(Duration.of(12, ChronoUnit.MINUTES))
.build();

UserCredentials otherCredentials = credentials.toBuilder().build();
assertEquals(credentials, otherCredentials);
}

static GenericJson writeUserJson(
String clientId, String clientSecret, String refreshToken, String quotaProjectId) {
GenericJson json = new GenericJson();
Expand Down

0 comments on commit 84afdb8

Please sign in to comment.