diff --git a/crates/in-web/src/oidc.rs b/crates/in-web/src/oidc.rs index 9c15e40..f94c559 100644 --- a/crates/in-web/src/oidc.rs +++ b/crates/in-web/src/oidc.rs @@ -590,7 +590,7 @@ struct Cache { discovery_failed_at: Option, /// The key set and when it was fetched. keys: Option<(Instant, Arc)>, - /// When the keys were last fetched for an unknown `kid`. Limits those fetches. + /// When the keys were last fetched for an unknown `kid`, or a fetch failed. Limits those fetches. keys_refresh_at: Option, /// The client-credentials token for `im`, and the instant it stops being used. service: Option<(String, Instant)>, @@ -773,9 +773,16 @@ impl OidcClient { return answer; } let endpoints = self.endpoints().await; - self.cache.lock().await.keys_refresh_at = Some(Instant::now()); + // Only a forced refetch or a failure starts the gap. A scheduled fetch does not, so the first token + // signed by a newly rotated key is verified at once. + if force { + self.cache.lock().await.keys_refresh_at = Some(Instant::now()); + } let fetched = self.get_json::(&endpoints.jwks).await; let mut cache = self.cache.lock().await; + if fetched.is_err() { + cache.keys_refresh_at = Some(Instant::now()); + } match fetched { Ok(jwks) => { let keys = Arc::new(KeySet::from_jwks(&jwks));