diff --git a/crates/iz-web/src/oidc.rs b/crates/iz-web/src/oidc.rs index ea36a28..77bfb76 100644 --- a/crates/iz-web/src/oidc.rs +++ b/crates/iz-web/src/oidc.rs @@ -229,7 +229,7 @@ pub struct OidcClient { struct KeyCache { /// The last key set fetched, and when it was fetched. current: Option<(Instant, Arc)>, - /// When a fetch was last attempted, whether it succeeded or not. + /// When the keys were last fetched for an unknown key, or a fetch failed. Limits those fetches. last_fetch: Option, } @@ -330,9 +330,16 @@ impl OidcClient { if let Some(keys) = self.keys_without_fetch(refetch_unknown).await? { return Ok(keys); } - self.keys.lock().await.last_fetch = Some(Instant::now()); + // Only a refetch for an unknown key 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 refetch_unknown { + self.keys.lock().await.last_fetch = Some(Instant::now()); + } let fetched = self.fetch_keys().await; let mut cache = self.keys.lock().await; + if fetched.is_err() { + cache.last_fetch = Some(Instant::now()); + } match fetched { Ok(keys) => { cache.current = Some((Instant::now(), keys.clone())); @@ -349,7 +356,7 @@ impl OidcClient { } /// The cached key set, when no fetch is due: it is fresh, or a fetch was attempted less than - /// [`UNKNOWN_KEY_REFETCH`] ago. `None` when a fetch is due. With no cached set and a fetch attempted + /// [`UNKNOWN_KEY_REFETCH`] ago (see `last_fetch`). `None` when a fetch is due. With no cached set and a fetch attempted /// that recently, the answer is an error at once, without a network call. async fn keys_without_fetch(&self, refetch_unknown: bool) -> ImResult>> { let cache = self.keys.lock().await;