From 18af01cf54256a7eb0832e9f18148fd0655c0457 Mon Sep 17 00:00:00 2001 From: nazeh Date: Sat, 7 Sep 2024 16:46:51 +0300 Subject: [PATCH] feat(common): rename capabilities::Ability to Action --- examples/authz/authenticator/src/main.rs | 2 +- pubky-common/src/capabilities.rs | 44 ++++++++++++------------ pubky-homeserver/src/routes/public.rs | 4 +-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/examples/authz/authenticator/src/main.rs b/examples/authz/authenticator/src/main.rs index 31da8d6..410b8f5 100644 --- a/examples/authz/authenticator/src/main.rs +++ b/examples/authz/authenticator/src/main.rs @@ -49,7 +49,7 @@ async fn main() -> Result<()> { } for cap in &caps { - println!(" {} : {:?}", cap.scope, cap.abilities); + println!(" {} : {:?}", cap.scope, cap.actions); } // === Consent form === diff --git a/pubky-common/src/capabilities.rs b/pubky-common/src/capabilities.rs index 3dfe19f..7929860 100644 --- a/pubky-common/src/capabilities.rs +++ b/pubky-common/src/capabilities.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Capability { pub scope: String, - pub abilities: Vec, + pub actions: Vec, } impl Capability { @@ -13,13 +13,13 @@ impl Capability { pub fn root() -> Self { Capability { scope: "/".to_string(), - abilities: vec![Ability::Read, Ability::Write], + actions: vec![Action::Read, Action::Write], } } } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum Ability { +pub enum Action { /// Can read the scope at the specified path (GET requests). Read, /// Can write to the scope at the specified path (PUT/POST/DELETE requests). @@ -28,24 +28,24 @@ pub enum Ability { Unknown(char), } -impl From<&Ability> for char { - fn from(value: &Ability) -> Self { +impl From<&Action> for char { + fn from(value: &Action) -> Self { match value { - Ability::Read => 'r', - Ability::Write => 'w', - Ability::Unknown(char) => char.to_owned(), + Action::Read => 'r', + Action::Write => 'w', + Action::Unknown(char) => char.to_owned(), } } } -impl TryFrom for Ability { +impl TryFrom for Action { type Error = Error; fn try_from(value: char) -> Result { match value { 'r' => Ok(Self::Read), 'w' => Ok(Self::Write), - _ => Err(Error::InvalidAbility), + _ => Err(Error::InvalidAction), } } } @@ -56,7 +56,7 @@ impl Display for Capability { f, "{}:{}", self.scope, - self.abilities.iter().map(char::from).collect::() + self.actions.iter().map(char::from).collect::() ) } } @@ -81,24 +81,24 @@ impl TryFrom<&str> for Capability { return Err(Error::InvalidScope); } - let abilities_str = value.rsplit(':').next().unwrap_or(""); + let actions_str = value.rsplit(':').next().unwrap_or(""); - let mut abilities = Vec::new(); + let mut actions = Vec::new(); - for char in abilities_str.chars() { - let ability = Ability::try_from(char)?; + for char in actions_str.chars() { + let ability = Action::try_from(char)?; - match abilities.binary_search_by(|element| char::from(element).cmp(&char)) { + match actions.binary_search_by(|element| char::from(element).cmp(&char)) { Ok(_) => {} Err(index) => { - abilities.insert(index, ability); + actions.insert(index, ability); } } } - let scope = value[0..value.len() - abilities_str.len() - 1].to_string(); + let scope = value[0..value.len() - actions_str.len() - 1].to_string(); - Ok(Capability { scope, abilities }) + Ok(Capability { scope, actions }) } } @@ -130,8 +130,8 @@ pub enum Error { InvalidScope, #[error("Capability: Invalid format should be :")] InvalidFormat, - #[error("Capability: Invalid Ability")] - InvalidAbility, + #[error("Capability: Invalid Action")] + InvalidAction, #[error("Capabilities: Invalid capabilities format")] InvalidCapabilities, } @@ -224,7 +224,7 @@ mod tests { fn pubky_caps() { let cap = Capability { scope: "/pub/pubky.app/".to_string(), - abilities: vec![Ability::Read, Ability::Write], + actions: vec![Action::Read, Action::Write], }; // Read and write withing directory `/pub/pubky.app/`. diff --git a/pubky-homeserver/src/routes/public.rs b/pubky-homeserver/src/routes/public.rs index 5c5da84..4cf2eed 100644 --- a/pubky-homeserver/src/routes/public.rs +++ b/pubky-homeserver/src/routes/public.rs @@ -152,8 +152,8 @@ fn authorize( && session.capabilities().iter().any(|cap| { path.starts_with(&cap.scope[1..]) && cap - .abilities - .contains(&pubky_common::capabilities::Ability::Write) + .actions + .contains(&pubky_common::capabilities::Action::Write) }) { return Ok(());