Skip to content

Commit

Permalink
chore: some clippy and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Jul 27, 2023
1 parent 229894c commit 8804bda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
20 changes: 11 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()),
Expand Down
16 changes: 4 additions & 12 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ impl Repo {

pub async fn admit_pubkeys(&mut self, pubkeys: &HashSet<XOnlyPublicKey>) -> 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()
Expand Down Expand Up @@ -118,12 +114,8 @@ impl Repo {

pub async fn deny_pubkeys(&mut self, pubkeys: &HashSet<XOnlyPublicKey>) -> 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()
Expand Down

0 comments on commit 8804bda

Please sign in to comment.