|
31 | 31 |
|
32 | 32 | package com.google.auth.oauth2;
|
33 | 33 |
|
34 |
| -import static java.util.concurrent.TimeUnit.MINUTES; |
35 |
| - |
36 | 34 | import com.google.api.client.util.Clock;
|
37 | 35 | import com.google.auth.Credentials;
|
38 | 36 | import com.google.auth.RequestMetadataCallback;
|
39 | 37 | import com.google.auth.http.AuthHttpConstants;
|
40 | 38 | import com.google.common.annotations.VisibleForTesting;
|
41 | 39 | import com.google.common.base.MoreObjects;
|
| 40 | +import com.google.common.base.Preconditions; |
42 | 41 | import com.google.common.collect.ImmutableList;
|
43 | 42 | import com.google.common.collect.ImmutableMap;
|
44 | 43 | import com.google.common.collect.Iterables;
|
|
51 | 50 | import java.io.ObjectInputStream;
|
52 | 51 | import java.io.Serializable;
|
53 | 52 | import java.net.URI;
|
| 53 | +import java.time.Duration; |
54 | 54 | import java.util.ArrayList;
|
55 | 55 | import java.util.Date;
|
56 | 56 | import java.util.List;
|
|
67 | 67 | public class OAuth2Credentials extends Credentials {
|
68 | 68 |
|
69 | 69 | private static final long serialVersionUID = 4556936364828217687L;
|
70 |
| - static final long MINIMUM_TOKEN_MILLISECONDS = MINUTES.toMillis(5); |
71 |
| - static final long REFRESH_MARGIN_MILLISECONDS = MINIMUM_TOKEN_MILLISECONDS + MINUTES.toMillis(1); |
| 70 | + static final Duration DEFAULT_EXPIRATION_MARGIN = Duration.ofMinutes(5); |
| 71 | + static final Duration DEFAULT_REFRESH_MARGIN = Duration.ofMinutes(6); |
72 | 72 | private static final ImmutableMap<String, List<String>> EMPTY_EXTRA_HEADERS = ImmutableMap.of();
|
73 | 73 |
|
| 74 | + private final Duration expirationMargin; |
| 75 | + private final Duration refreshMargin; |
| 76 | + |
74 | 77 | // byte[] is serializable, so the lock variable can be final
|
75 | 78 | @VisibleForTesting final Object lock = new byte[0];
|
76 | 79 | private volatile OAuthValue value = null;
|
@@ -102,9 +105,20 @@ protected OAuth2Credentials() {
|
102 | 105 | * @param accessToken initial or temporary access token
|
103 | 106 | */
|
104 | 107 | protected OAuth2Credentials(AccessToken accessToken) {
|
| 108 | + this(accessToken, DEFAULT_REFRESH_MARGIN, DEFAULT_EXPIRATION_MARGIN); |
| 109 | + } |
| 110 | + |
| 111 | + protected OAuth2Credentials( |
| 112 | + AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) { |
105 | 113 | if (accessToken != null) {
|
106 | 114 | this.value = OAuthValue.create(accessToken, EMPTY_EXTRA_HEADERS);
|
107 | 115 | }
|
| 116 | + |
| 117 | + this.refreshMargin = Preconditions.checkNotNull(refreshMargin, "refreshMargin"); |
| 118 | + Preconditions.checkArgument(!refreshMargin.isNegative(), "refreshMargin can't be negative"); |
| 119 | + this.expirationMargin = Preconditions.checkNotNull(expirationMargin, "expirationMargin"); |
| 120 | + Preconditions.checkArgument( |
| 121 | + !expirationMargin.isNegative(), "expirationMargin can't be negative"); |
108 | 122 | }
|
109 | 123 |
|
110 | 124 | @Override
|
@@ -324,13 +338,12 @@ private CacheState getState() {
|
324 | 338 | return CacheState.FRESH;
|
325 | 339 | }
|
326 | 340 |
|
327 |
| - long remainingMillis = expirationTime.getTime() - clock.currentTimeMillis(); |
328 |
| - |
329 |
| - if (remainingMillis <= MINIMUM_TOKEN_MILLISECONDS) { |
| 341 | + Duration remaining = Duration.ofMillis(expirationTime.getTime() - clock.currentTimeMillis()); |
| 342 | + if (remaining.compareTo(expirationMargin) <= 0) { |
330 | 343 | return CacheState.EXPIRED;
|
331 | 344 | }
|
332 | 345 |
|
333 |
| - if (remainingMillis <= REFRESH_MARGIN_MILLISECONDS) { |
| 346 | + if (remaining.compareTo(refreshMargin) <= 0) { |
334 | 347 | return CacheState.STALE;
|
335 | 348 | }
|
336 | 349 |
|
@@ -572,24 +585,46 @@ void executeIfNew(Executor executor) {
|
572 | 585 | public static class Builder {
|
573 | 586 |
|
574 | 587 | private AccessToken accessToken;
|
| 588 | + private Duration refreshMargin = DEFAULT_REFRESH_MARGIN; |
| 589 | + private Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN; |
575 | 590 |
|
576 | 591 | protected Builder() {}
|
577 | 592 |
|
578 | 593 | protected Builder(OAuth2Credentials credentials) {
|
579 | 594 | this.accessToken = credentials.getAccessToken();
|
| 595 | + this.refreshMargin = credentials.refreshMargin; |
| 596 | + this.expirationMargin = credentials.expirationMargin; |
580 | 597 | }
|
581 | 598 |
|
582 | 599 | public Builder setAccessToken(AccessToken token) {
|
583 | 600 | this.accessToken = token;
|
584 | 601 | return this;
|
585 | 602 | }
|
586 | 603 |
|
| 604 | + public Builder setRefreshMargin(Duration refreshMargin) { |
| 605 | + this.refreshMargin = refreshMargin; |
| 606 | + return this; |
| 607 | + } |
| 608 | + |
| 609 | + public Duration getRefreshMargin() { |
| 610 | + return refreshMargin; |
| 611 | + } |
| 612 | + |
| 613 | + public Builder setExpirationMargin(Duration expirationMargin) { |
| 614 | + this.expirationMargin = expirationMargin; |
| 615 | + return this; |
| 616 | + } |
| 617 | + |
| 618 | + public Duration getExpirationMargin() { |
| 619 | + return expirationMargin; |
| 620 | + } |
| 621 | + |
587 | 622 | public AccessToken getAccessToken() {
|
588 | 623 | return accessToken;
|
589 | 624 | }
|
590 | 625 |
|
591 | 626 | public OAuth2Credentials build() {
|
592 |
| - return new OAuth2Credentials(accessToken); |
| 627 | + return new OAuth2Credentials(accessToken, refreshMargin, expirationMargin); |
593 | 628 | }
|
594 | 629 | }
|
595 | 630 | }
|
0 commit comments