From 24adba9065c89f45d0e4a82b5002feb24bc40182 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 23 Jan 2025 12:30:22 +0200 Subject: [PATCH] move `filter_scope` to auth level, filter on apply action, rename constructors --- src/profile/identity.rs | 2 +- src/profile/identity/auth.rs | 33 +++++++++++++++++++++++---- src/profile/identity/auth/database.rs | 2 +- src/profile/identity/auth/memory.rs | 23 +------------------ 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/profile/identity.rs b/src/profile/identity.rs index 12755399..3cc10ea5 100644 --- a/src/profile/identity.rs +++ b/src/profile/identity.rs @@ -34,7 +34,7 @@ impl Identity { profile_identity_id: &Rc, ) -> Result { // Init components - let auth = match Auth::new(connection) { + let auth = match Auth::build(connection) { Ok(auth) => Rc::new(auth), Err(e) => return Err(Error::Auth(e)), }; diff --git a/src/profile/identity/auth.rs b/src/profile/identity/auth.rs index ce3dda01..e47d1681 100644 --- a/src/profile/identity/auth.rs +++ b/src/profile/identity/auth.rs @@ -21,10 +21,10 @@ impl Auth { // Constructors /// Create new `Self` - pub fn new(connection: &Rc>) -> Result { + pub fn build(connection: &Rc>) -> Result { // Init `Self` let this = Self { - database: Rc::new(Database::new(connection)), + database: Rc::new(Database::build(connection)), memory: Rc::new(Memory::new()), }; @@ -41,12 +41,14 @@ impl Auth { /// * deactivate active auth by remove previous records from `Self` database /// * reindex `Self` memory index on success /// * return last insert `profile_identity_auth_id` on success - pub fn apply(&self, profile_identity_id: i64, scope: &str) -> Result { + pub fn apply(&self, profile_identity_id: i64, auth_url: &str) -> Result { + let scope = filter_scope(auth_url); + // Cleanup records match `scope` (unauthorize) - self.remove_scope(scope)?; + self.remove_scope(&scope)?; // Create new record (auth) - let profile_identity_auth_id = match self.database.add(profile_identity_id, scope) { + let profile_identity_auth_id = match self.database.add(profile_identity_id, &scope) { Ok(id) => id, Err(e) => return Err(Error::Database(e)), }; @@ -126,3 +128,24 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> { Ok(()) } + +/// Get valid identity scope for given URL +/// * helper function for different protocol drivers implementation +fn filter_scope(url: &str) -> String { + use gtk::glib::{Regex, RegexCompileFlags, RegexMatchFlags}; + + match Regex::split_simple( + r"^\w+://(.*)", + url, + RegexCompileFlags::DEFAULT, + RegexMatchFlags::DEFAULT, + ) + .get(1) + { + Some(postfix) => postfix.to_string(), + None => url.to_string(), + } + .trim() + .trim_end_matches("/") + .to_lowercase() +} diff --git a/src/profile/identity/auth/database.rs b/src/profile/identity/auth/database.rs index ca6416a7..df8351c8 100644 --- a/src/profile/identity/auth/database.rs +++ b/src/profile/identity/auth/database.rs @@ -16,7 +16,7 @@ impl Database { // Constructors /// Create new `Self` - pub fn new(connection: &Rc>) -> Self { + pub fn build(connection: &Rc>) -> Self { Self { connection: connection.clone(), } diff --git a/src/profile/identity/auth/memory.rs b/src/profile/identity/auth/memory.rs index af503225..737a3c85 100644 --- a/src/profile/identity/auth/memory.rs +++ b/src/profile/identity/auth/memory.rs @@ -65,7 +65,7 @@ impl Memory { let mut result = Vec::new(); // Get all records starts with `scope` - let query = filter_scope(request); + let query = super::filter_scope(request); for (scope, &profile_identity_id) in self.index.borrow().iter() { if query.starts_with(scope) { @@ -83,24 +83,3 @@ impl Memory { result.first().cloned() } } - -/// Get valid identity scope for given URL -/// * helper function for different protocol drivers implementation -fn filter_scope(url: &str) -> String { - use gtk::glib::{Regex, RegexCompileFlags, RegexMatchFlags}; - - match Regex::split_simple( - r"^\w+://(.*)", - url, - RegexCompileFlags::DEFAULT, - RegexMatchFlags::DEFAULT, - ) - .get(1) - { - Some(postfix) => postfix.to_string(), - None => url.to_string(), - } - .trim() - .trim_end_matches("/") - .to_lowercase() -}