Move JSON signing logic from Key to Signer

This commit is contained in:
Dirkjan Ochtman 2023-05-18 10:45:44 +02:00
parent 48cd0a0ded
commit 92864b03bf
1 changed files with 22 additions and 34 deletions

View File

@ -323,10 +323,6 @@ impl Signer for AccountInner {
fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error> { fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error> {
self.key.sign(payload) self.key.sign(payload)
} }
fn key(&self) -> &Key {
&self.key
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -365,9 +361,7 @@ impl Client {
}; };
let nonce = nonce.ok_or("no nonce found")?; let nonce = nonce.ok_or("no nonce found")?;
let body = signer let body = signer.signed_json(payload, signer.header(&nonce, url))?;
.key()
.signed_json(payload, signer.header(&nonce, url))?;
let request = Request::builder() let request = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(url) .uri(url)
@ -415,6 +409,27 @@ impl Key {
thumb, thumb,
}) })
} }
}
impl Signer for Key {
type Signature = ring::signature::Signature;
fn header<'n, 'u: 'n, 's: 'u>(&'s self, nonce: &'n str, url: &'u str) -> Header<'n> {
Header {
alg: self.signing_algorithm,
key: KeyOrKeyId::from_key(&self.inner),
nonce,
url,
}
}
fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error> {
Ok(self.inner.sign(&self.rng, payload)?)
}
}
trait Signer {
type Signature: AsRef<[u8]>;
fn signed_json( fn signed_json(
&self, &self,
@ -435,37 +450,10 @@ impl Key {
signature: BASE64_URL_SAFE_NO_PAD.encode(signature.as_ref()), signature: BASE64_URL_SAFE_NO_PAD.encode(signature.as_ref()),
}) })
} }
}
impl Signer for Key {
type Signature = ring::signature::Signature;
fn header<'n, 'u: 'n, 's: 'u>(&'s self, nonce: &'n str, url: &'u str) -> Header<'n> {
Header {
alg: self.signing_algorithm,
key: KeyOrKeyId::from_key(&self.inner),
nonce,
url,
}
}
fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error> {
Ok(self.inner.sign(&self.rng, payload)?)
}
fn key(&self) -> &Key {
self
}
}
trait Signer {
type Signature: AsRef<[u8]>;
fn header<'n, 'u: 'n, 's: 'u>(&'s self, nonce: &'n str, url: &'u str) -> Header<'n>; fn header<'n, 'u: 'n, 's: 'u>(&'s self, nonce: &'n str, url: &'u str) -> Header<'n>;
fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error>; fn sign(&self, payload: &[u8]) -> Result<Self::Signature, Error>;
fn key(&self) -> &Key;
} }
/// The response value to use for challenge responses /// The response value to use for challenge responses