Skip to content

Commit 84afdb8

Browse files
qcastelTimur Sadykov
andauthored
Fix: Not loosing the access token when calling UserCredentials#ToBuil… (#993)
* Update oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java * lint fixes Co-authored-by: Timur Sadykov <stim@google.com>
1 parent 040acef commit 84afdb8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.io.ObjectInputStream;
5454
import java.net.URI;
5555
import java.nio.charset.StandardCharsets;
56+
import java.time.Duration;
5657
import java.util.Date;
5758
import java.util.List;
5859
import java.util.Map;
@@ -410,6 +411,16 @@ public Builder setAccessToken(AccessToken token) {
410411
return this;
411412
}
412413

414+
public Builder setExpirationMargin(Duration expirationMargin) {
415+
super.setExpirationMargin(expirationMargin);
416+
return this;
417+
}
418+
419+
public Builder setRefreshMargin(Duration refreshMargin) {
420+
super.setRefreshMargin(refreshMargin);
421+
return this;
422+
}
423+
413424
public Builder setQuotaProjectId(String quotaProjectId) {
414425
super.setQuotaProjectId(quotaProjectId);
415426
return this;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@
5555
import java.io.IOException;
5656
import java.io.InputStream;
5757
import java.net.URI;
58+
import java.time.Duration;
59+
import java.time.temporal.ChronoUnit;
5860
import java.util.Collection;
5961
import java.util.Collections;
62+
import java.util.Date;
6063
import java.util.List;
6164
import java.util.Map;
6265
import java.util.concurrent.atomic.AtomicBoolean;
@@ -830,6 +833,26 @@ public void IdTokenCredentials_NoUserEmailScope_throws() throws IOException {
830833
}
831834
}
832835

836+
@Test
837+
public void userCredentials_toBuilder_copyEveryAttribute() {
838+
MockHttpTransportFactory httpTransportFactory = new MockHttpTransportFactory();
839+
UserCredentials credentials =
840+
UserCredentials.newBuilder()
841+
.setClientId(CLIENT_ID)
842+
.setClientSecret(CLIENT_SECRET)
843+
.setRefreshToken(REFRESH_TOKEN)
844+
.setAccessToken(new AccessToken(ACCESS_TOKEN, new Date()))
845+
.setHttpTransportFactory(httpTransportFactory)
846+
.setTokenServerUri(URI.create("https://foo1.com/bar"))
847+
.setQuotaProjectId(QUOTA_PROJECT)
848+
.setExpirationMargin(Duration.of(10, ChronoUnit.SECONDS))
849+
.setRefreshMargin(Duration.of(12, ChronoUnit.MINUTES))
850+
.build();
851+
852+
UserCredentials otherCredentials = credentials.toBuilder().build();
853+
assertEquals(credentials, otherCredentials);
854+
}
855+
833856
static GenericJson writeUserJson(
834857
String clientId, String clientSecret, String refreshToken, String quotaProjectId) {
835858
GenericJson json = new GenericJson();

0 commit comments

Comments
 (0)