use crate::{
crypto::{
KeyMatchStrength,
verifier::{JwsVerifier, KeyMatch, VerifyError},
},
platform::MaybeSendBoxFuture,
};
#[derive(Debug, Clone)]
pub struct RetryingVerifier<V> {
inner: V,
}
impl<V: JwsVerifier> RetryingVerifier<V> {
pub fn new(inner: V) -> Self {
Self { inner }
}
}
impl<V: JwsVerifier> JwsVerifier for RetryingVerifier<V> {
fn key_match(&self, key_match: &KeyMatch<'_>) -> Option<KeyMatchStrength> {
self.inner.key_match(key_match)
}
fn verify<'a>(
&'a self,
input: &'a [u8],
signature: &'a [u8],
key_match: &'a KeyMatch<'a>,
) -> MaybeSendBoxFuture<'a, Result<(), VerifyError>> {
Box::pin(async move {
match self.inner.verify(input, signature, key_match).await {
Err(VerifyError::NoMatchingKey) => {
if self.inner.try_refresh().await {
self.inner.verify(input, signature, key_match).await
} else {
Err(VerifyError::NoMatchingKey)
}
}
other => other,
}
})
}
fn try_refresh(&self) -> MaybeSendBoxFuture<'_, bool> {
self.inner.try_refresh()
}
}