From c4f715216ff97d88186056fe17ae93549ab26948 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 18 May 2023 10:49:31 +0200 Subject: [PATCH] Move JoseJson creation logic into new() method --- src/lib.rs | 5 +++-- src/types.rs | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index faaf7ac..b5cac49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) diff --git a/src/types.rs b/src/types.rs index c811588..6793190 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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 { + signer: &impl Signer, + ) -> Result { 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>;