From 8804bda4b02f623f9c714a74e3ef28cd71c1ea11 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Wed, 26 Jul 2023 23:43:47 -0400 Subject: [PATCH] chore: some clippy and error handling --- src/main.rs | 20 +++++++++++--------- src/repo.rs | 16 ++++------------ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8dbade8..c770b3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,11 +62,19 @@ impl Authorization for EventAuthz { None => &event.pubkey, }; + let author = XOnlyPublicKey::from_slice(author) + .map_err(|_| Status::internal("Invalid Author Key"))?; + // If author is trusted pubkey decode event and update account(s) // admit event - if self.pubkey.eq(&XOnlyPublicKey::from_slice(author).unwrap()) { + if self.pubkey.eq(&author) { if event.kind.eq(&300000) { - self.repo.lock().await.update_people(event).await.unwrap(); + self.repo + .lock() + .await + .update_people(event) + .await + .map_err(|_| Status::internal("Could not update users"))?; } return Ok(Response::new(nauthz_grpc::EventReply { @@ -75,13 +83,7 @@ impl Authorization for EventAuthz { })); } - let response = match self - .repo - .lock() - .await - .get_user_status(XOnlyPublicKey::from_slice(author).unwrap()) - .await - { + let response = match self.repo.lock().await.get_user_status(author).await { UserStatus::Allowed => Response::new(nauthz_grpc::EventReply { decision: Decision::Permit as i32, message: Some("Ok".to_string()), diff --git a/src/repo.rs b/src/repo.rs index c241899..67f93eb 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -82,12 +82,8 @@ impl Repo { pub async fn admit_pubkeys(&mut self, pubkeys: &HashSet) -> Result<()> { self.allowed_pubkeys.extend(pubkeys); - self.denied_pubkeys = self - .denied_pubkeys - .iter() - .filter(|p| !self.allowed_pubkeys.contains(p)) - .cloned() - .collect(); + self.denied_pubkeys + .retain(|p| !self.allowed_pubkeys.contains(p)); let allowed: Vec<_> = pubkeys .iter() @@ -118,12 +114,8 @@ impl Repo { pub async fn deny_pubkeys(&mut self, pubkeys: &HashSet) -> Result<()> { self.denied_pubkeys.extend(pubkeys); - self.allowed_pubkeys = self - .allowed_pubkeys - .iter() - .filter(|p| !self.denied_pubkeys.contains(p)) - .cloned() - .collect(); + self.allowed_pubkeys + .retain(|p| !self.denied_pubkeys.contains(p)); let denied: Vec<_> = pubkeys .iter()