diff --git a/packages/secrets/src/keyring/src/os/unix.rs b/packages/secrets/src/keyring/src/os/unix.rs index db121368c0..723817a7d3 100644 --- a/packages/secrets/src/keyring/src/os/unix.rs +++ b/packages/secrets/src/keyring/src/os/unix.rs @@ -1,10 +1,9 @@ -extern crate libsecret; extern crate glib_sys; +extern crate libsecret; use glib::translate::{FromGlibPtrContainer, ToGlibPtr}; -use glib_sys::{g_hash_table_unref}; +use glib_sys::g_hash_table_unref; use libsecret::{ prelude::CollectionExtManual, traits::ItemExt, SearchFlags, Service, ServiceFlags, - password_free }; use std::collections::HashMap; @@ -21,7 +20,7 @@ impl From for KeyringError { /// /// Returns the libsecret schema that corresponds to service and account attributes. -/// +/// fn get_schema() -> libsecret::Schema { libsecret::Schema::new( "org.freedesktop.Secret.Generic", @@ -35,20 +34,20 @@ fn get_schema() -> libsecret::Schema { /// /// Builds an attribute map with the given service and account names. -/// +/// /// Returns: /// - A `HashMap` built with the `service` and `account` values provided. Used for attribute functions. -/// +/// fn get_attribute_map<'a>(service: &'a str, account: &'a str) -> HashMap<&'a str, &'a str> { HashMap::from([("service", service), ("account", account)]) } -/// +/// /// Attempts to set a password for a given service and account. -/// +/// /// - `service`: The service name for the new credential /// - `account`: The account name for the new credential -/// +/// /// Returns: /// - `true` if the credential was stored successfully /// - A `KeyringError` if there were any issues interacting with the credential vault @@ -74,16 +73,16 @@ pub fn set_password( } } -/// +/// /// Returns a password contained in the given service and account, if found. -/// +/// /// - `service`: The service name that matches the credential of interest /// - `account`: The account name that matches the credential of interest -/// +/// /// Returns: /// - `Some(password)` if a matching credential was found; `None` otherwise /// - A `KeyringError` if there were any issues interacting with the credential vault -/// +/// pub fn get_password(service: &String, account: &String) -> Result, KeyringError> { let attributes = get_attribute_map(service.as_str(), account.as_str()); @@ -96,15 +95,15 @@ pub fn get_password(service: &String, account: &String) -> Result } } -/// +/// /// Returns the first password (if any) that matches the given service pattern. -/// +/// /// - `service`: The service pattern that matches the credential of interest -/// +/// /// Returns: /// - `Some(password)` if a matching credential was found; `None` otherwise /// - A `KeyringError` if there were any issues interacting with the credential vault -/// +/// pub fn find_password(service: &String) -> Result, KeyringError> { let attributes = if service.contains("/") && service.len() > 1 { // In format "service/account" @@ -123,16 +122,16 @@ pub fn find_password(service: &String) -> Result, KeyringError> { } } -/// +/// /// Attempts to delete the password associated with a given service and account. -/// +/// /// - `service`: The service name of the credential to delete /// - `account`: The account name of the credential to delete -/// +/// /// Returns: /// - `true` if a matching credential was deleted; `false` otherwise /// - A `KeyringError` if there were any issues interacting with the credential vault -/// +/// pub fn delete_password(service: &String, account: &String) -> Result { match libsecret::password_clear_sync( Some(&get_schema()), @@ -147,16 +146,16 @@ pub fn delete_password(service: &String, account: &String) -> Result, @@ -188,30 +187,26 @@ pub fn find_credentials( Ok(vec) => { let valid_creds: Vec<(String, String)> = vec .iter() - .filter_map(|item| { - match item.secret() { - Some(secret) => { - let attrs: HashMap = unsafe { - let attrs = libsecret_sys::secret_item_get_attributes(item.to_glib_none().0); - FromGlibPtrContainer::from_glib_full(attrs) - }; - let bytes = secret.get(); - let pw = String::from_utf8(bytes).unwrap_or("".to_string()); + .filter_map(|item| match item.secret() { + Some(secret) => { + let attrs: HashMap = unsafe { + let attrs = + libsecret_sys::secret_item_get_attributes(item.to_glib_none().0); + FromGlibPtrContainer::from_glib_full(attrs) + }; + let bytes = secret.get(); + let pw = String::from_utf8(bytes).unwrap_or("".to_string()); - let acc = attrs.get("account").unwrap().clone(); - unsafe { - g_hash_table_unref(attrs.to_glib_full()); - } - Some((acc, pw)) + let acc = attrs.get("account").unwrap().clone(); + unsafe { + g_hash_table_unref(attrs.to_glib_full()); } - None => { - None - }, + Some((acc, pw)) } + None => None, }) .collect(); *credentials = valid_creds; - Ok(true) }