From 858806149a30f815eb7ff3dce642978195f7644f Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Wed, 24 Nov 2021 21:41:44 +0900 Subject: [PATCH] Use concrete Error for Signer --- plume-common/src/activity_pub/sign.rs | 17 +++++++++++------ plume-models/src/blogs.rs | 12 +++++------- plume-models/src/lib.rs | 8 +++++++- plume-models/src/users.rs | 14 ++++++-------- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/plume-common/src/activity_pub/sign.rs b/plume-common/src/activity_pub/sign.rs index c97cb5dd..22aaf9d6 100644 --- a/plume-common/src/activity_pub/sign.rs +++ b/plume-common/src/activity_pub/sign.rs @@ -19,20 +19,25 @@ pub fn gen_keypair() -> (Vec, Vec) { #[derive(Debug)] pub struct Error(); +pub type Result = std::result::Result; + +impl From for Error { + fn from(_: openssl::error::ErrorStack) -> Self { + Self() + } +} pub trait Signer { - type Error; - fn get_key_id(&self) -> String; /// Sign some data with the signer keypair - fn sign(&self, to_sign: &str) -> Result, Self::Error>; + fn sign(&self, to_sign: &str) -> Result>; /// Verify if the signature is valid - fn verify(&self, data: &str, signature: &[u8]) -> Result; + fn verify(&self, data: &str, signature: &[u8]) -> Result; } pub trait Signable { - fn sign(&mut self, creator: &T) -> Result<&mut Self, Error> + fn sign(&mut self, creator: &T) -> Result<&mut Self> where T: Signer; fn verify(self, creator: &T) -> bool @@ -46,7 +51,7 @@ pub trait Signable { } impl Signable for serde_json::Value { - fn sign(&mut self, creator: &T) -> Result<&mut serde_json::Value, Error> { + fn sign(&mut self, creator: &T) -> Result<&mut serde_json::Value> { let creation_date = Utc::now().to_rfc3339(); let mut options = json!({ "type": "RsaSignature2017", diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 4043c85e..da556225 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -462,24 +462,22 @@ impl AsActor<&PlumeRocket> for Blog { } impl sign::Signer for Blog { - type Error = Error; - fn get_key_id(&self) -> String { format!("{}#main-key", self.ap_url) } - fn sign(&self, to_sign: &str) -> Result> { - let key = self.get_keypair()?; + fn sign(&self, to_sign: &str) -> sign::Result> { + let key = self.get_keypair().map_err(|_| sign::Error())?; let mut signer = Signer::new(MessageDigest::sha256(), &key)?; signer.update(to_sign.as_bytes())?; - signer.sign_to_vec().map_err(Error::from) + signer.sign_to_vec().map_err(sign::Error::from) } - fn verify(&self, data: &str, signature: &[u8]) -> Result { + fn verify(&self, data: &str, signature: &[u8]) -> sign::Result { let key = PKey::from_rsa(Rsa::public_key_from_pem(self.public_key.as_ref())?)?; let mut verifier = Verifier::new(MessageDigest::sha256(), &key)?; verifier.update(data.as_bytes())?; - verifier.verify(signature).map_err(Error::from) + verifier.verify(signature).map_err(sign::Error::from) } } diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index bd1474ec..33a5ec2b 100755 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -17,7 +17,7 @@ extern crate serde_json; extern crate tantivy; use once_cell::sync::Lazy; -use plume_common::activity_pub::inbox::InboxError; +use plume_common::activity_pub::{inbox::InboxError, sign}; use posts::PostEvent; use riker::actors::{channel, ActorSystem, ChannelRef, SystemBuilder}; use users::UserEvent; @@ -79,6 +79,12 @@ impl From for Error { } } +impl From for Error { + fn from(_: sign::Error) -> Self { + Error::Signature + } +} + impl From for Error { fn from(err: diesel::result::Error) -> Self { Error::Db(err) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index d0325afd..04e27d5f 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -24,7 +24,7 @@ use plume_common::{ activity_pub::{ ap_accept_header, inbox::{AsActor, AsObject, FromId}, - sign::{gen_keypair, Signer}, + sign::{gen_keypair, Error as SignError, Result as SignResult, Signer}, ActivityStream, ApSignature, Id, IntoId, PublicKey, PUBLIC_VISIBILITY, }, utils, @@ -1071,24 +1071,22 @@ impl AsObject for User { } impl Signer for User { - type Error = Error; - fn get_key_id(&self) -> String { format!("{}#main-key", self.ap_url) } - fn sign(&self, to_sign: &str) -> Result> { - let key = self.get_keypair()?; + fn sign(&self, to_sign: &str) -> SignResult> { + let key = self.get_keypair().map_err(|_| SignError())?; let mut signer = sign::Signer::new(MessageDigest::sha256(), &key)?; signer.update(to_sign.as_bytes())?; - signer.sign_to_vec().map_err(Error::from) + signer.sign_to_vec().map_err(SignError::from) } - fn verify(&self, data: &str, signature: &[u8]) -> Result { + fn verify(&self, data: &str, signature: &[u8]) -> SignResult { let key = PKey::from_rsa(Rsa::public_key_from_pem(self.public_key.as_ref())?)?; let mut verifier = sign::Verifier::new(MessageDigest::sha256(), &key)?; verifier.update(data.as_bytes())?; - verifier.verify(signature).map_err(Error::from) + verifier.verify(signature).map_err(SignError::from) } }