Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sdk/src/main/java/io/opentdf/platform/sdk/DPoPMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public enum DPoPMode {
* {@code /.well-known/oauth-protected-resource}. Servers that advertise neither get
* {@link #LEGACY}. A discovery that gets no usable answer (a network error, timeout, redirect,
* or a status other than 200, 404, 405 and 501) uses legacy for 30 seconds before the server is
* asked again. Answers are cached per origin for a few minutes, then rediscovered, so a server
* that is upgraded or reconfigured is picked up without restarting the client. This is the
* default.
* asked again. Answers are cached per origin for a few minutes (see
* {@link SDKBuilder#dpopDiscoveryCacheTtl}), then rediscovered, so a server that is upgraded or
* reconfigured is picked up without restarting the client. This is the default.
*/
AUTO,
/**
Expand Down
24 changes: 23 additions & 1 deletion sdk/src/main/java/io/opentdf/platform/sdk/SDKBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
Expand All @@ -71,6 +72,7 @@ public class SDKBuilder {
private JWK dpopKey;
private JWSAlgorithm dpopAlg;
private DPoPMode dpopMode = DPoPMode.AUTO;
private Duration dpopDiscoveryCacheTtl = DPoPModeResolver.DEFAULT_CACHE_TTL;

private static final Logger logger = LoggerFactory.getLogger(SDKBuilder.class);

Expand Down Expand Up @@ -268,6 +270,25 @@ public SDKBuilder dpopMode(DPoPMode dpopMode) {
return this;
}

/**
* How long a server's DPoP style discovered in {@link DPoPMode#AUTO} is reused before it is
* discovered again. Defaults to five minutes. The legacy fallback after a discovery that got no
* usable answer is reused for 30 seconds, or for this TTL if shorter. {@link Duration#ZERO}
* disables the cache, so every request first queries the server's well-known configuration.
* Ignored for the fixed modes.
*
* @param ttl how long to cache each server's discovered style
* @return this builder instance for method chaining
* @throws IllegalArgumentException if ttl is null or negative
*/
public SDKBuilder dpopDiscoveryCacheTtl(Duration ttl) {
if (ttl == null || ttl.isNegative()) {
throw new IllegalArgumentException("DPoP discovery cache TTL must be zero or positive");
}
this.dpopDiscoveryCacheTtl = ttl;
return this;
}

private AuthInterceptor getAuthInterceptor(JWK dpopJwk, JWSAlgorithm dpopAlgorithm) {
if (platformEndpoint == null) {
throw new SDKException("cannot build an SDK without specifying the platform endpoint");
Expand Down Expand Up @@ -326,7 +347,8 @@ private AuthInterceptor getAuthInterceptor(JWK dpopJwk, JWSAlgorithm dpopAlgorit
this.authzGrant = new ClientCredentialsGrant();
}
// Discovery reuses the unauthenticated bootstrap client so it never sends credentials.
var dpopModeResolver = new DPoPModeResolver(dpopMode, httpClient);
var dpopModeResolver = new DPoPModeResolver(dpopMode, httpClient, dpopDiscoveryCacheTtl, System::nanoTime,
DPoPModeResolver.DISCOVERY_TIMEOUT);
// Reuse the configuration fetched above rather than discovering the platform again. Only
// a positive is seeded: RFC 9728 metadata may still advertise.
if (DPoPModeResolver.advertisesRfc9449(config.getConfiguration())) {
Expand Down
17 changes: 17 additions & 0 deletions sdk/src/test/java/io/opentdf/platform/sdk/DPoPModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,18 @@ void seededAnswerExpires() {
assertThat(discoveredPaths()).containsExactly(WELL_KNOWN_CONFIGURATION_PROCEDURE, PROTECTED_RESOURCE_PATH);
}

@Test
void zeroTtlDisablesTheCache() {
DPoPModeResolver resolver = new DPoPModeResolver(DPoPMode.AUTO, new OkHttpClient(), Duration.ZERO, nanos::get,
DPoPModeResolver.DISCOVERY_TIMEOUT);
assertThat(resolver.modeFor(rpcUrl())).isEqualTo(DPoPMode.LEGACY);
resolver.recordRfc9449(resourceServer.url("/").url());
assertThat(resolver.modeFor(rpcUrl())).isEqualTo(DPoPMode.LEGACY);
assertThat(discoveredPaths()).containsExactly(
WELL_KNOWN_CONFIGURATION_PROCEDURE, PROTECTED_RESOURCE_PATH,
WELL_KNOWN_CONFIGURATION_PROCEDURE, PROTECTED_RESOURCE_PATH);
}

@Test
void bootstrapAdvertisementSeedsPlatformWithoutDiscovery() {
Struct algs = Struct.newBuilder().putFields("dpop_signing_alg_values_supported",
Expand Down Expand Up @@ -553,6 +565,11 @@ void autoRequiresHttpClientAndBuilderRejectsNull() {
SDKBuilder builder = SDKBuilder.newBuilder();
assertThatThrownBy(() -> builder.dpopMode(null))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> builder.dpopDiscoveryCacheTtl(null))
.isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> builder.dpopDiscoveryCacheTtl(Duration.ofSeconds(-1)))
.isInstanceOf(IllegalArgumentException.class);
assertThat(builder.dpopDiscoveryCacheTtl(Duration.ZERO)).isSameAs(builder);
}

private DPoPModeResolver autoResolver() {
Expand Down
Loading