Move JoseJson creation logic into new() method

This commit is contained in:
Dirkjan Ochtman 2023-05-18 10:49:31 +02:00
parent c4a1c29534
commit c4f715216f
2 changed files with 13 additions and 10 deletions

View File

@ -23,7 +23,8 @@ pub use types::{
Identifier, LetsEncrypt, NewAccount, NewOrder, OrderState, OrderStatus, Problem,
};
use types::{
DirectoryUrls, Empty, FinalizeRequest, Header, Jwk, KeyOrKeyId, Signer, SigningAlgorithm,
DirectoryUrls, Empty, FinalizeRequest, Header, JoseJson, Jwk, KeyOrKeyId, Signer,
SigningAlgorithm,
};
/// An ACME order as described in RFC 8555 (section 7.1.3)
@ -361,7 +362,7 @@ impl Client {
};
let nonce = nonce.ok_or("no nonce found")?;
let body = signer.signed_json(payload, signer.header(&nonce, url))?;
let body = JoseJson::new(payload, signer.header(&nonce, url), signer)?;
let request = Request::builder()
.method(Method::POST)
.uri(url)

View File

@ -261,14 +261,12 @@ pub(crate) struct JoseJson {
pub(crate) signature: String,
}
pub(crate) trait Signer {
type Signature: AsRef<[u8]>;
fn signed_json(
&self,
impl JoseJson {
pub(crate) fn new(
payload: Option<&impl Serialize>,
protected: Header<'_>,
) -> Result<JoseJson, Error> {
signer: &impl Signer,
) -> Result<Self, Error> {
let protected = base64(&protected)?;
let payload = match payload {
Some(data) => base64(&data)?,
@ -276,13 +274,17 @@ pub(crate) trait Signer {
};
let combined = format!("{protected}.{payload}");
let signature = self.sign(combined.as_bytes())?;
Ok(JoseJson {
let signature = signer.sign(combined.as_bytes())?;
Ok(Self {
protected,
payload,
signature: BASE64_URL_SAFE_NO_PAD.encode(signature.as_ref()),
})
}
}
pub(crate) trait Signer {
type Signature: AsRef<[u8]>;
fn header<'n, 'u: 'n, 's: 'u>(&'s self, nonce: &'n str, url: &'u str) -> Header<'n>;