move filter_scope to auth level, filter on apply action, rename constructors

This commit is contained in:
yggverse 2025-01-23 12:30:22 +02:00
parent 089a91d5a2
commit 24adba9065
4 changed files with 31 additions and 29 deletions

View File

@ -34,7 +34,7 @@ impl Identity {
profile_identity_id: &Rc<i64>,
) -> Result<Self, Error> {
// 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)),
};

View File

@ -21,10 +21,10 @@ impl Auth {
// Constructors
/// Create new `Self`
pub fn new(connection: &Rc<RwLock<Connection>>) -> Result<Self, Error> {
pub fn build(connection: &Rc<RwLock<Connection>>) -> Result<Self, Error> {
// 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<i64, Error> {
pub fn apply(&self, profile_identity_id: i64, auth_url: &str) -> Result<i64, Error> {
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()
}

View File

@ -16,7 +16,7 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn new(connection: &Rc<RwLock<Connection>>) -> Self {
pub fn build(connection: &Rc<RwLock<Connection>>) -> Self {
Self {
connection: connection.clone(),
}

View File

@ -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()
}