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 {
229 struct KeyCache {229 struct KeyCache {
230 /// The last key set fetched, and when it was fetched.230 /// The last key set fetched, and when it was fetched.
231 current: Option<(Instant, Arc<KeySet>)>,231 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.
233 last_fetch: Option<Instant>,233 last_fetch: Option<Instant>,
234 }234 }
235 235
@@ -330,9 +330,16 @@ impl OidcClient {
330 if let Some(keys) = self.keys_without_fetch(refetch_unknown).await? {330 if let Some(keys) = self.keys_without_fetch(refetch_unknown).await? {
331 return Ok(keys);331 return Ok(keys);
332 }332 }
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+ }
334 let fetched = self.fetch_keys().await;338 let fetched = self.fetch_keys().await;
335 let mut cache = self.keys.lock().await;339 let mut cache = self.keys.lock().await;
340+ if fetched.is_err() {
341+ cache.last_fetch = Some(Instant::now());
342+ }
336 match fetched {343 match fetched {
337 Ok(keys) => {344 Ok(keys) => {
338 cache.current = Some((Instant::now(), keys.clone()));345 cache.current = Some((Instant::now(), keys.clone()));
@@ -349,7 +356,7 @@ impl OidcClient {
349 }356 }
350 357
351 /// The cached key set, when no fetch is due: it is fresh, or a fetch was attempted less than358 /// 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 attempted359+ /// [`UNKNOWN_KEY_REFETCH`] ago (see `last_fetch`). `None` when a fetch is due. With no cached set and a fetch attempted
353 /// that recently, the answer is an error at once, without a network call.360 /// that recently, the answer is an error at once, without a network call.
354 async fn keys_without_fetch(&self, refetch_unknown: bool) -> ImResult<Option<Arc<KeySet>>> {361 async fn keys_without_fetch(&self, refetch_unknown: bool) -> ImResult<Option<Arc<KeySet>>> {
355 let cache = self.keys.lock().await;362 let cache = self.keys.lock().await;