From c86dca53bda22028bc897586f8530e9a75156261 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 23 Nov 2024 18:04:57 +0200 Subject: [PATCH] implement to_string method, prevent memory index overwrite on validation step --- .../window/tab/item/identity/gemini.rs | 14 ++++++++++---- src/profile.rs | 2 +- src/profile/bookmark.rs | 6 +++--- src/profile/bookmark/memory.rs | 19 ++++++++++++++----- src/profile/bookmark/memory/error.rs | 15 +++++++++++++-- src/profile/identity.rs | 2 +- src/profile/identity/error.rs | 13 +++++++++++++ src/profile/identity/gemini/error.rs | 17 ++++++++++++++++- src/profile/identity/gemini/memory.rs | 15 ++++++++++++--- src/profile/identity/gemini/memory/error.rs | 18 +++++++++++++++++- 10 files changed, 100 insertions(+), 21 deletions(-) diff --git a/src/app/browser/window/tab/item/identity/gemini.rs b/src/app/browser/window/tab/item/identity/gemini.rs index fca6f0ee..5e2eea8f 100644 --- a/src/app/browser/window/tab/item/identity/gemini.rs +++ b/src/app/browser/window/tab/item/identity/gemini.rs @@ -119,18 +119,24 @@ impl Gemini { Value::ProfileIdentityGeminiId(value) => Some(value), Value::UseGuestSession => None, Value::GenerateNewAuth => Some( - profile + match profile .identity .gemini .make(None, &widget.form.name.value().unwrap()) - .unwrap(), + { + Ok(profile_identity_gemini_id) => profile_identity_gemini_id, + Err(reason) => todo!("{}", reason.to_string()), + }, ), Value::ImportPem => Some( - profile + match profile .identity .gemini .add(&widget.form.file.pem.take().unwrap()) - .unwrap(), + { + Ok(profile_identity_gemini_id) => profile_identity_gemini_id, + Err(reason) => todo!("{}", reason.to_string()), + }, ), }; diff --git a/src/profile.rs b/src/profile.rs index 3cd6993f..0d4a7b20 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -98,7 +98,7 @@ impl Profile { // Init identity component let identity = Rc::new(match Identity::new(connection, profile_id) { Ok(result) => result, - Err(reason) => todo!("{:?}", reason), + Err(reason) => todo!("{:?}", reason.to_string()), }); // Result diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index 59c4a3b5..e67ddc58 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -28,12 +28,12 @@ impl Bookmark { match database.records(None) { Ok(records) => { for record in records { - if memory.add(record.request, record.id).is_err() { - todo!() + if let Err(reason) = memory.add(record.request, record.id) { + todo!("{}", reason.to_string()) } } } - Err(reason) => todo!("{reason}"), + Err(reason) => todo!("{}", reason.to_string()), } // Return new `Self` diff --git a/src/profile/bookmark/memory.rs b/src/profile/bookmark/memory.rs index 51878a5a..d0e960ab 100644 --- a/src/profile/bookmark/memory.rs +++ b/src/profile/bookmark/memory.rs @@ -23,8 +23,17 @@ impl Memory { /// Add new record with `request` as key and `id` as value /// * validate record with same key does not exist yet pub fn add(&self, request: String, id: i64) -> Result<(), Error> { - match self.index.borrow_mut().insert(request, id) { - Some(key) => Err(Error::Overwrite(key)), // @TODO prevent? + // Borrow shared index access + let mut index = self.index.borrow_mut(); + + // Prevent existing key overwrite + if index.contains_key(&request) { + return Err(Error::Overwrite(request)); + } + + // Slot should be free, let check it twice + match index.insert(request, id) { + Some(_) => return Err(Error::Unexpected), None => Ok(()), } } @@ -34,15 +43,15 @@ impl Memory { pub fn delete(&self, request: &str) -> Result<(), Error> { match self.index.borrow_mut().remove(request) { Some(_) => Ok(()), - None => Err(Error::NotFound), + None => Err(Error::Unexpected), // @TODO } } /// Get `id` by `request` from memory index pub fn get(&self, request: &str) -> Result { match self.index.borrow().get(request) { - Some(&id) => Ok(id), - None => Err(Error::NotFound), + Some(&value) => Ok(value), + None => Err(Error::Unexpected), // @TODO } } } diff --git a/src/profile/bookmark/memory/error.rs b/src/profile/bookmark/memory/error.rs index ed0b40ff..8ade67e5 100644 --- a/src/profile/bookmark/memory/error.rs +++ b/src/profile/bookmark/memory/error.rs @@ -1,5 +1,16 @@ #[derive(Debug)] pub enum Error { - NotFound, - Overwrite(i64), + Overwrite(String), + Unexpected, +} + +impl Error { + pub fn to_string(&self) -> String { + match self { + Self::Overwrite(key) => { + format!("Overwrite attempt for existing record `{key}`") + } + Self::Unexpected => format!("Unexpected error"), + } + } } diff --git a/src/profile/identity.rs b/src/profile/identity.rs index 23c3ca02..7dc20b31 100644 --- a/src/profile/identity.rs +++ b/src/profile/identity.rs @@ -55,7 +55,7 @@ impl Identity { if let Some(id) = self.gemini.auth.memory.match_priority(request) { match self.gemini.memory.get(id) { Ok(pem) => return Some(pem), - Err(reason) => todo!("{:?}", reason), + Err(reason) => todo!("{:?}", reason.to_string()), } } None diff --git a/src/profile/identity/error.rs b/src/profile/identity/error.rs index 338d50ac..45199566 100644 --- a/src/profile/identity/error.rs +++ b/src/profile/identity/error.rs @@ -3,3 +3,16 @@ pub enum Error { Database(sqlite::Error), Gemini(super::gemini::Error), } + +impl Error { + pub fn to_string(&self) -> String { + match self { + Self::Database(reason) => { + format!("Database error: {}", reason.to_string()) + } + Self::Gemini(reason) => { + format!("Could not init Gemini identity: {}", reason.to_string()) + } + } + } +} diff --git a/src/profile/identity/gemini/error.rs b/src/profile/identity/gemini/error.rs index c004ce0b..d06452a8 100644 --- a/src/profile/identity/gemini/error.rs +++ b/src/profile/identity/gemini/error.rs @@ -1,7 +1,22 @@ #[derive(Debug)] pub enum Error { Auth(super::auth::Error), - Certificate(Box), // @TODO + Certificate(Box), Database(sqlite::Error), Memory(super::memory::Error), } + +impl Error { + pub fn to_string(&self) -> String { + match self { + Self::Auth(reason) => format!("Could not create auth: {}", reason.to_string()), + Self::Certificate(reason) => { + format!("Could not create certificate: {}", reason.to_string()) + } + Self::Database(reason) => { + format!("Database error: {}", reason.to_string()) + } + Self::Memory(reason) => format!("Memory error: {}", reason.to_string()), + } + } +} diff --git a/src/profile/identity/gemini/memory.rs b/src/profile/identity/gemini/memory.rs index aa3709e6..dfe16e4c 100644 --- a/src/profile/identity/gemini/memory.rs +++ b/src/profile/identity/gemini/memory.rs @@ -22,9 +22,18 @@ impl Memory { /// Add new record with `id` as key and `pem` as value /// * validate record with same key does not exist yet - pub fn add(&self, id: i64, pem: String) -> Result<(), Error> { - match self.index.borrow_mut().insert(id, pem) { - Some(key) => Err(Error::Overwrite(key)), // @TODO prevent? + pub fn add(&self, profile_identity_gemini_id: i64, pem: String) -> Result<(), Error> { + // Borrow shared index access + let mut index = self.index.borrow_mut(); + + // Prevent existing key overwrite + if index.contains_key(&profile_identity_gemini_id) { + return Err(Error::Overwrite(profile_identity_gemini_id)); + } + + // Slot should be free, let check it twice + match index.insert(profile_identity_gemini_id, pem) { + Some(_) => return Err(Error::Unexpected), None => Ok(()), } } diff --git a/src/profile/identity/gemini/memory/error.rs b/src/profile/identity/gemini/memory/error.rs index 3187386b..3592df71 100644 --- a/src/profile/identity/gemini/memory/error.rs +++ b/src/profile/identity/gemini/memory/error.rs @@ -2,5 +2,21 @@ pub enum Error { Clear, NotFound(i64), - Overwrite(String), + Overwrite(i64), + Unexpected, +} + +impl Error { + pub fn to_string(&self) -> String { + match self { + Self::Clear => format!("Could not cleanup memory index"), + Self::NotFound(key) => { + format!("Record `{key}` not found in memory index") + } + Self::Overwrite(key) => { + format!("Overwrite attempt for existing record `{key}`") + } + Self::Unexpected => format!("Unexpected error"), + } + } }