Skip to content

Commit

Permalink
First pass at retrying bad nonce errors
Browse files Browse the repository at this point in the history
I'm not sure I'm super happy with this solution but it does minimise
the amount of interface I have to change. I think the copying is okay
because it only happens when we get a bad nonce error which should be
pretty uncommon (unless you're running against pebble but that'll only
be for testing).
  • Loading branch information
Nick Spain committed Sep 16, 2024
1 parent 643c585 commit cc72a26
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,44 @@ impl Client {
nonce: Option<String>,
signer: &impl Signer,
url: &str,
) -> Result<BytesResponse, Error> {
let mut retries = 3;

let mut nonce = nonce;
loop {
let response = self.inner_post(payload, nonce.clone(), signer, url).await?;
if response.parts.status == StatusCode::BAD_REQUEST {
let parts = response.parts.clone();
let body = response.body().await.map_err(Error::Other)?;
let problem: Problem = serde_json::from_slice(&body)?;
if problem
.r#type
.map(|t| t == "urn:ietf:params:acme:error:badNonce")
.unwrap_or_default()
{
retries -= 1;
if retries != 0 {
nonce = Some(self.nonce(None).await?);
continue;
}
}

let response = BytesResponse {
parts,
body: Box::new(StaticBody { bytes: body }),
};
return Ok(response);
}
return Ok(response);
}
}

async fn inner_post(
&self,
payload: Option<&impl Serialize>,
nonce: Option<String>,
signer: &impl Signer,
url: &str,
) -> Result<BytesResponse, Error> {
let nonce = self.nonce(nonce).await?;
let body = JoseJson::new(payload, signer.header(Some(&nonce), url), signer)?;
Expand Down Expand Up @@ -789,6 +827,17 @@ where
}
}

struct StaticBody {
bytes: Bytes,
}

#[async_trait]
impl BytesBody for StaticBody {
async fn into_bytes(&mut self) -> Result<Bytes, Box<dyn StdError + Send + Sync + 'static>> {
Ok(self.bytes.to_owned())
}
}

/// Object safe body trait
#[async_trait]
pub trait BytesBody: Send {
Expand Down

0 comments on commit cc72a26

Please sign in to comment.