forge
Explore
Log in

Don't you dare go Hollow

🔑 oidc: a scheduled key fetch no longer delays the refetch for a newly rotated key

Browse files at this commit

d423462ee0c1c7ed845a734f42f80e601110a99aAhmet Kaan GümüşParent: 1788d7f
crates/iz-web/src/oidc.rsmodified+10 -3
@@ -229,7 +229,7 @@ pub struct OidcClient {
229229 struct KeyCache {
230230 /// The last key set fetched, and when it was fetched.
231231 current: Option<(Instant, Arc<KeySet>)>,
232- /// When a fetch was last attempted, whether it succeeded or not.
232+ /// When the keys were last fetched for an unknown key, or a fetch failed. Limits those fetches.
233233 last_fetch: Option<Instant>,
234234 }
235235
@@ -330,9 +330,16 @@ impl OidcClient {
330330 if let Some(keys) = self.keys_without_fetch(refetch_unknown).await? {
331331 return Ok(keys);
332332 }
333- self.keys.lock().await.last_fetch = Some(Instant::now());
333+ // Only a refetch for an unknown key or a failure starts the gap. A scheduled fetch does not, so the
334+ // first token signed by a newly rotated key is verified at once.
335+ if refetch_unknown {
336+ self.keys.lock().await.last_fetch = Some(Instant::now());
337+ }
334338 let fetched = self.fetch_keys().await;
335339 let mut cache = self.keys.lock().await;
340+ if fetched.is_err() {
341+ cache.last_fetch = Some(Instant::now());
342+ }
336343 match fetched {
337344 Ok(keys) => {
338345 cache.current = Some((Instant::now(), keys.clone()));
@@ -349,7 +356,7 @@ impl OidcClient {
349356 }
350357
351358 /// The cached key set, when no fetch is due: it is fresh, or a fetch was attempted less than
352- /// [`UNKNOWN_KEY_REFETCH`] ago. `None` when a fetch is due. With no cached set and a fetch attempted
359+ /// [`UNKNOWN_KEY_REFETCH`] ago (see `last_fetch`). `None` when a fetch is due. With no cached set and a fetch attempted
353360 /// that recently, the answer is an error at once, without a network call.
354361 async fn keys_without_fetch(&self, refetch_unknown: bool) -> ImResult<Option<Arc<KeySet>>> {
355362 let cache = self.keys.lock().await;