From 053c078e11e151543f46e45bcec182ff255d50ce Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 27 Nov 2024 21:40:44 +0200 Subject: [PATCH] delegate certificate init to ggemini wrapper, add structs for some, tuple entities, move match_priority to gemini impl area --- src/app/browser/window/tab/item/page.rs | 18 ++++++++------- src/profile/identity.rs | 13 ----------- src/profile/identity/gemini.rs | 20 +++++++++++++++++ src/profile/identity/gemini/auth/memory.rs | 22 ++++++++++++------- .../identity/gemini/auth/memory/auth.rs | 5 +++++ src/profile/identity/gemini/identity.rs | 4 ++++ 6 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 src/profile/identity/gemini/auth/memory/auth.rs create mode 100644 src/profile/identity/gemini/identity.rs diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index d021885e..0385c3e4 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -23,7 +23,7 @@ use crate::Profile; use gtk::{ gdk::Texture, gdk_pixbuf::Pixbuf, - gio::{Cancellable, SocketClientEvent, TlsCertificate}, + gio::{Cancellable, SocketClientEvent}, glib::{ gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags, UriHideFlags, @@ -417,17 +417,19 @@ impl Page { let input = self.input.clone(); let meta = self.meta.clone(); - // Return PEM string match request + // Find identity match request let certificate = match self .profile .identity - .gemini(&self.navigation.request().widget().gobject().text()) + .gemini + .match_priority(&self.navigation.request().widget().gobject().text()) { - // @TODO delegate to client - Some(pem) => match TlsCertificate::from_pem(&pem) { - Ok(certificate) => Some(certificate), - Err(reason) => todo!("{reason}"), - }, + Some(identity) => { + match gemini::client::Certificate::from_pem(&identity.pem, &identity.scope) { + Ok(certificate) => Some(certificate), + Err(reason) => todo!("{reason}"), + } + } None => None, }; diff --git a/src/profile/identity.rs b/src/profile/identity.rs index 7dc20b31..0422b27a 100644 --- a/src/profile/identity.rs +++ b/src/profile/identity.rs @@ -47,19 +47,6 @@ impl Identity { gemini, }) } - - /// Get `pem` record match `request` - /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) - /// * this function work with memory cache collected (not database) - pub fn gemini(&self, request: &str) -> Option { - 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.to_string()), - } - } - None - } } // Tools diff --git a/src/profile/identity/gemini.rs b/src/profile/identity/gemini.rs index 61c8c36e..f6711dbf 100644 --- a/src/profile/identity/gemini.rs +++ b/src/profile/identity/gemini.rs @@ -2,11 +2,13 @@ mod auth; mod certificate; mod database; mod error; +mod identity; mod memory; use auth::Auth; use database::Database; pub use error::Error; +use identity::Identity; use memory::Memory; @@ -106,6 +108,24 @@ impl Gemini { Ok(()) } + + /// Get `pem` record match `request` + /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) + /// * this function work with memory cache collected (not database) + pub fn match_priority(&self, request: &str) -> Option { + if let Some(auth) = self.auth.memory.match_priority(request) { + match self.memory.get(auth.profile_identity_gemini_id) { + Ok(pem) => { + return Some(Identity { + scope: auth.scope, + pem, + }) + } + Err(reason) => todo!("{:?}", reason.to_string()), + } + } + None + } } // Tools diff --git a/src/profile/identity/gemini/auth/memory.rs b/src/profile/identity/gemini/auth/memory.rs index 2d2ca372..1a176a52 100644 --- a/src/profile/identity/gemini/auth/memory.rs +++ b/src/profile/identity/gemini/auth/memory.rs @@ -1,4 +1,7 @@ +pub mod auth; pub mod error; + +pub use auth::Auth; pub use error::Error; use std::{cell::RefCell, collections::HashMap}; @@ -49,23 +52,26 @@ impl Memory { } } - /// Get `profile_identity_gemini_id` vector match given `request` + /// Get identity match `request` /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) /// * contain unspecified length priority implementation @TODO - pub fn match_priority(&self, request: &str) -> Option { + pub fn match_priority(&self, request: &str) -> Option { let mut result = Vec::new(); // Get all records starts with URL cached, collect length for priority - for (url, &profile_identity_gemini_id) in self.index.borrow().iter() { - if request.starts_with(url) { - result.push((profile_identity_gemini_id, url.len())) + for (scope, &profile_identity_gemini_id) in self.index.borrow().iter() { + if request.starts_with(scope) { + result.push(Auth { + profile_identity_gemini_id, + scope: scope.clone(), + }) } } // Sort by length desc @TODO - result.sort_by(|a, b| b.1.cmp(&a.1)); + result.sort_by(|a, b| b.scope.len().cmp(&a.scope.len())); - // Get first match ID - result.first().map(|value| value.0) + // Get first copy + result.first().cloned() } } diff --git a/src/profile/identity/gemini/auth/memory/auth.rs b/src/profile/identity/gemini/auth/memory/auth.rs new file mode 100644 index 00000000..39dfc848 --- /dev/null +++ b/src/profile/identity/gemini/auth/memory/auth.rs @@ -0,0 +1,5 @@ +#[derive(Clone)] +pub struct Auth { + pub profile_identity_gemini_id: i64, + pub scope: String, +} diff --git a/src/profile/identity/gemini/identity.rs b/src/profile/identity/gemini/identity.rs new file mode 100644 index 00000000..3fec1002 --- /dev/null +++ b/src/profile/identity/gemini/identity.rs @@ -0,0 +1,4 @@ +pub struct Identity { + pub pem: String, + pub scope: String, +}