Skip to content

Commit

Permalink
Use Duration in JwkProviderBuilder (#130)
Browse files Browse the repository at this point in the history
Use Duration in JwkProviderBuilder

Co-authored-by: Vincent Ricard <vricard@oui.sncf>
Co-authored-by: Luciano Balmaceda <balmacedaluciano@gmail.com>
  • Loading branch information
3 people committed Oct 11, 2021
1 parent 9750abf commit afb7b0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/auth0/jwk/GuavaCachedJwkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand All @@ -24,7 +25,7 @@ public class GuavaCachedJwkProvider implements JwkProvider {
* @param provider fallback provider to use when jwk is not cached
*/
public GuavaCachedJwkProvider(final JwkProvider provider) {
this(provider, 5, 10, TimeUnit.MINUTES);
this(provider, 5, Duration.ofMinutes(10));
}

/**
Expand All @@ -36,10 +37,21 @@ public GuavaCachedJwkProvider(final JwkProvider provider) {
* @param expiresUnit unit of the expiresIn parameter
*/
public GuavaCachedJwkProvider(final JwkProvider provider, long size, long expiresIn, TimeUnit expiresUnit) {
this(provider, size, Duration.ofSeconds(expiresUnit.toSeconds(expiresIn)));
}

/**
* Creates a new cached provider specifying cache size and ttl
*
* @param provider fallback provider to use when jwk is not cached
* @param size number of jwt to cache
* @param expiresIn amount of time a jwk will live in the cache
*/
public GuavaCachedJwkProvider(final JwkProvider provider, long size, Duration expiresIn) {
this.provider = provider;
this.cache = CacheBuilder.newBuilder()
.maximumSize(size)
.expireAfterWrite(expiresIn, expiresUnit)
.expireAfterWrite(expiresIn)
.build();
}

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/com/auth0/jwk/JwkProviderBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.Proxy;
import java.net.URL;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.TimeUnit;

Expand All @@ -15,10 +16,9 @@ public class JwkProviderBuilder {

private final URL url;
private Proxy proxy;
private TimeUnit expiresUnit;
private Duration expiresIn;
private Integer connectTimeout;
private Integer readTimeout;
private long expiresIn;
private long cacheSize;
private boolean cached;
private BucketImpl bucket;
Expand All @@ -37,8 +37,7 @@ public JwkProviderBuilder(URL url) {
}
this.url = url;
this.cached = true;
this.expiresIn = 10;
this.expiresUnit = TimeUnit.HOURS;
this.expiresIn = Duration.ofHours(10);
this.cacheSize = 5;
this.rateLimited = true;
this.bucket = new BucketImpl(10, 1, TimeUnit.MINUTES);
Expand Down Expand Up @@ -84,17 +83,27 @@ public JwkProviderBuilder cached(boolean cached) {
*
* @param cacheSize number of jwk to cache
* @param expiresIn amount of time the jwk will be cached
* @param unit unit of time for the expire of jwk
* @return the builder
*/
public JwkProviderBuilder cached(long cacheSize, long expiresIn, TimeUnit unit) {
public JwkProviderBuilder cached(long cacheSize, Duration expiresIn) {
this.cached = true;
this.cacheSize = cacheSize;
this.expiresIn = expiresIn;
this.expiresUnit = unit;
return this;
}

/**
* Enable the cache specifying size and expire time.
*
* @param cacheSize number of jwk to cache
* @param expiresIn amount of time the jwk will be cached
* @param unit unit of time for the expire of jwk
* @return the builder
*/
public JwkProviderBuilder cached(long cacheSize, long expiresIn, TimeUnit unit) {
return this.cached(cacheSize, Duration.ofSeconds(unit.toSeconds(expiresIn)));
}

/**
* Toggle the rate limit of Jwk. By default the Provider will use rate limit.
*
Expand Down Expand Up @@ -168,7 +177,7 @@ public JwkProvider build() {
urlProvider = new RateLimitedJwkProvider(urlProvider, bucket);
}
if (this.cached) {
urlProvider = new GuavaCachedJwkProvider(urlProvider, cacheSize, expiresIn, expiresUnit);
urlProvider = new GuavaCachedJwkProvider(urlProvider, cacheSize, expiresIn);
}
return urlProvider;
}
Expand Down

0 comments on commit afb7b0f

Please sign in to comment.