Skip to content

Commit

Permalink
server: Dispatch default collection items
Browse files Browse the repository at this point in the history
And, Collection::new() now takes an additional parameter: locked. Based
on this the locked status of a collection is determined. When the daemon is
executed with the -l option, default collection and its items will be in
unlocked status.

Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura authored and bilelmoussaoui committed Oct 29, 2024
1 parent 0140b5b commit 30b6981
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
44 changes: 33 additions & 11 deletions server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use tokio::sync::{Mutex, RwLock};
use zbus::{interface, object_server::SignalEmitter, zvariant};
use zvariant::{ObjectPath, OwnedObjectPath};

use crate::{item, service_manager::ServiceManager};
use crate::{error::Error, item, service_manager::ServiceManager};

#[derive(Debug)]
#[allow(unused)]
pub struct Collection {
// Properties
items: Mutex<Vec<item::Item>>,
items: Mutex<Vec<OwnedObjectPath>>,
label: Mutex<String>,
locked: AtomicBool,
created: Duration,
Expand All @@ -33,7 +33,7 @@ pub struct Collection {
#[allow(unused)]
keyring: Arc<Keyring>,
manager: Arc<Mutex<ServiceManager>>,
n_items: RwLock<i32>,
item_index: RwLock<u32>,
path: OwnedObjectPath,
}

Expand Down Expand Up @@ -64,12 +64,7 @@ impl Collection {

#[zbus(property, name = "Items")]
pub async fn items(&self) -> Vec<OwnedObjectPath> {
self.items
.lock()
.await
.iter()
.map(|item| OwnedObjectPath::from(item.path()))
.collect()
self.items.lock().await.clone()
}

#[zbus(property, name = "Label")]
Expand Down Expand Up @@ -120,6 +115,7 @@ impl Collection {
pub fn new(
label: &str,
alias: &str,
locked: bool,
manager: Arc<Mutex<ServiceManager>>,
keyring: Arc<Keyring>,
) -> Self {
Expand All @@ -130,10 +126,10 @@ impl Collection {
Self {
items: Default::default(),
label: Mutex::new(label.to_owned()),
locked: AtomicBool::new(true),
locked: AtomicBool::new(locked),
modified: Mutex::new(created),
alias: Mutex::new(alias.to_owned()),
n_items: RwLock::new(0),
item_index: RwLock::new(0),
path: OwnedObjectPath::try_from(format!(
"/org/freedesktop/secrets/collection/{}",
label
Expand All @@ -156,4 +152,30 @@ impl Collection {
pub async fn alias(&self) -> String {
self.alias.lock().await.clone()
}

pub async fn dispatch_items(&self) -> Result<(), Error> {
let keyring_items = self.keyring.items().await;
let mut items = self.items.lock().await;
let service_manager = self.manager.lock().await;
let object_server = service_manager.object_server();
let mut n_items = 1;

for keyring_item in keyring_items {
let item = item::Item::new(
keyring_item.map_err(Error::InvalidItem)?,
self.is_locked().await,
Arc::clone(&self.manager),
self.path.clone(),
n_items,
);
n_items += 1;

items.push(item.path().clone());
object_server.at(item.path().clone(), item).await?;
}

*self.item_index.write().await = n_items;

Ok(())
}
}
3 changes: 3 additions & 0 deletions server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum Error {
IO(std::io::Error),
// Empty password error
EmptyPassword,
// Invalid item error
InvalidItem(oo7::portal::InvalidItemError),
}

impl From<zbus::Error> for Error {
Expand Down Expand Up @@ -37,6 +39,7 @@ impl fmt::Display for Error {
Self::Zbus(err) => write!(f, "Zbus error {err}"),
Self::IO(err) => write!(f, "IO error {err}"),
Self::EmptyPassword => write!(f, "Login password can't be empty"),
Self::InvalidItem(err) => write!(f, "Item cannot be decrypted {err}"),
}
}
}
35 changes: 31 additions & 4 deletions server/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
// org.freedesktop.Secret.Item

use std::{collections::HashMap, sync::atomic::AtomicBool};
use std::{
collections::HashMap,
sync::{atomic::AtomicBool, Arc},
};

use oo7::dbus::{api::SecretInner, ServiceError};
use oo7::{
dbus::{api::SecretInner, ServiceError},
portal,
};
use tokio::sync::Mutex;
use zbus::zvariant::{ObjectPath, OwnedObjectPath};

use crate::service_manager::ServiceManager;

#[derive(Debug)]
#[allow(unused)]
pub struct Item {
// Properties
locked: AtomicBool,
inner: Mutex<oo7::portal::Item>,
// Other attributes
manager: Arc<Mutex<ServiceManager>>,
path: OwnedObjectPath,
}

Expand Down Expand Up @@ -72,7 +84,22 @@ impl Item {
}

impl Item {
pub fn path(&self) -> ObjectPath<'_> {
self.path.as_ref()
pub fn new(
item: portal::Item,
locked: bool,
manager: Arc<Mutex<ServiceManager>>,
collection_path: OwnedObjectPath,
item_index: u32,
) -> Self {
Self {
locked: AtomicBool::new(locked),
inner: Mutex::new(item),
path: OwnedObjectPath::try_from(format!("{}/{}", collection_path, item_index)).unwrap(),
manager,
}
}

pub fn path(&self) -> &OwnedObjectPath {
&self.path
}
}
5 changes: 3 additions & 2 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,12 @@ impl Service {
let collection = Collection::new(
"login",
"default",
false,
Arc::clone(&service.manager),
Arc::new(Keyring::open("login", secret).await?),
);
collections.push(collection.path().clone());
collection.dispatch_items().await?;
object_server
.at(collection.path().clone(), collection)
.await?;
Expand All @@ -232,6 +234,7 @@ impl Service {
let collection = Collection::new(
"session",
"session",
false,
Arc::clone(&service.manager),
Arc::new(Keyring::temporary(Secret::random()).await?),
);
Expand All @@ -240,8 +243,6 @@ impl Service {
.at(collection.path().clone(), collection)
.await?;

drop(collections);

Ok(())
}
}
6 changes: 3 additions & 3 deletions server/src/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

use std::{collections::HashMap, sync::Arc};

use zbus::{zvariant::OwnedObjectPath, Connection};
use zbus::zvariant::OwnedObjectPath;

use crate::session::Session;

#[derive(Debug)]
pub struct ServiceManager {
connection: Connection,
connection: zbus::Connection,
// sessions mapped to their corresponding object path on the bus
sessions: HashMap<OwnedObjectPath, Arc<Session>>,
}

impl ServiceManager {
pub fn new(connection: Connection) -> Self {
pub fn new(connection: zbus::Connection) -> Self {
Self {
sessions: Default::default(),
connection,
Expand Down

0 comments on commit 30b6981

Please sign in to comment.