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: Option<bool>. 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 committed Oct 29, 2024
1 parent 6456c60 commit 6362983
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
38 changes: 29 additions & 9 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 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: Option<bool>,
manager: Arc<Mutex<ServiceManager>>,
keyring: Arc<Keyring>,
) -> Self {
Expand All @@ -130,7 +126,7 @@ impl Collection {
Self {
items: Default::default(),
label: Mutex::new(label.to_owned()),
locked: AtomicBool::new(true),
locked: AtomicBool::new(locked.unwrap_or(true)),
modified: Mutex::new(created),
alias: Mutex::new(alias.to_owned()),
n_items: RwLock::new(0),
Expand All @@ -156,4 +152,28 @@ impl Collection {
pub async fn alias(&self) -> String {
self.alias.lock().await.clone()
}

pub async fn dispatch_items(&self, object_server: &zbus::ObjectServer) -> Result<(), Error> {
for item in self.keyring.items().await {
match item {
Ok(item) => {
let n_items = *self.n_items.read().await + 1;
let item = item::Item::new(
item,
self.is_locked().await,
Arc::clone(&self.manager),
self.path.clone(),
n_items,
);
*self.n_items.write().await = n_items;

self.items.lock().await.push(item.path().clone());
object_server.at(item.path().clone(), item).await?;
}
Err(err) => return Err(Error::InvalidItem(err)),
}
}

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,
n_items: i32,
) -> Self {
Self {
locked: AtomicBool::new(locked),
inner: Mutex::new(item),
path: OwnedObjectPath::try_from(format!("{}/{}", collection_path, n_items)).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 @@ -223,10 +223,12 @@ impl Service {
let collection = Collection::new(
"login",
"default",
Some(false),
Arc::clone(&service.manager),
Arc::new(Keyring::open("login", secret).await?),
);
collections.push(collection.path().clone());
collection.dispatch_items(object_server).await?;
object_server
.at(collection.path().clone(), collection)
.await?;
Expand All @@ -235,6 +237,7 @@ impl Service {
let collection = Collection::new(
"session",
"session",
Some(false),
Arc::clone(&service.manager),
Arc::new(Keyring::temporary(Secret::random()).await?),
);
Expand All @@ -243,8 +246,6 @@ impl Service {
.at(collection.path().clone(), collection)
.await?;

drop(collections);

Ok(())
}
}

0 comments on commit 6362983

Please sign in to comment.