Skip to content

Commit

Permalink
Adds a way to store a session
Browse files Browse the repository at this point in the history
This introduces ServiceManager struct.

Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura committed Feb 19, 2024
1 parent d759844 commit 1f012ae
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
25 changes: 22 additions & 3 deletions server/src/daemon/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ use tokio::sync::RwLock;
use zbus::{interface, zvariant, ObjectServer, SignalContext};
use zvariant::{ObjectPath, OwnedObjectPath};

use super::{error::ServiceError, prompt::Prompt, Result, Service};
use super::{
error::ServiceError,
prompt::Prompt,
service::{Service, ServiceManager},
Result,
};

const SECRET_COLLECTION_OBJECTPATH: &str = "/org/freedesktop/secrets.Devel/collection/";

Expand All @@ -27,6 +32,7 @@ pub struct Collection {
locked: AtomicBool,
created: Duration,
modified: Duration,
manager: Arc<ServiceManager>,
path: OwnedObjectPath,
}

Expand Down Expand Up @@ -76,7 +82,13 @@ impl Collection {

let prompt = Prompt::default(); // temp Prompt

let item = super::item::Item::new(item, self.path(), Arc::clone(&self.keyring)).await;
let item = super::item::Item::new(
item,
self.path(),
Arc::clone(&self.keyring),
Arc::clone(&self.manager),
)
.await;
let path = OwnedObjectPath::from(item.path());
self.items.write().await.push(item);

Expand Down Expand Up @@ -125,7 +137,13 @@ impl Collection {
}

impl Collection {
pub fn new(label: &str, alias: &str, created: Duration, keyring: Arc<Keyring>) -> Self {
pub fn new(
label: &str,
alias: &str,
created: Duration,
keyring: Arc<Keyring>,
manager: Arc<ServiceManager>,
) -> Self {
Self {
items: Default::default(),
label: label.to_owned(),
Expand All @@ -136,6 +154,7 @@ impl Collection {
path: OwnedObjectPath::try_from(format!("{}{}", SECRET_COLLECTION_OBJECTPATH, alias))
.unwrap(),
keyring,
manager,
}
}

Expand Down
7 changes: 6 additions & 1 deletion server/src/daemon/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ use zbus::{
ObjectServer,
};

use super::{collection::Collection, error::ServiceError, prompt::Prompt, Result};
use super::{
collection::Collection, error::ServiceError, prompt::Prompt, service::ServiceManager, Result,
};

#[derive(Debug)]
pub struct Item {
inner: RwLock<portal::Item>,
path: OwnedObjectPath,
keyring: Arc<Keyring>,
locked: bool,
manager: Arc<ServiceManager>,

Check failure on line 26 in server/src/daemon/item.rs

View workflow job for this annotation

GitHub Actions / Clippy

field `manager` is never read

Check warning on line 26 in server/src/daemon/item.rs

View workflow job for this annotation

GitHub Actions / Check

field `manager` is never read
}

#[zbus::interface(name = "org.freedesktop.Secret.Item")]
Expand Down Expand Up @@ -106,12 +109,14 @@ impl Item {
item: portal::Item,
collection_path: ObjectPath<'_>,
keyring: Arc<Keyring>,
manager: Arc<ServiceManager>,
) -> Self {
Self {
path: OwnedObjectPath::try_from(format!("{}/items/{}", collection_path, item.label(),))
.unwrap(),
inner: RwLock::new(item),
keyring,
manager,
locked: true,
}
}
Expand Down
26 changes: 26 additions & 0 deletions server/src/daemon/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,33 @@ use super::{
collection::Collection, error::ServiceError, prompt::Prompt, session::Session, Result,
};

#[derive(Debug)]
pub struct ServiceManager {
sessions: HashMap<OwnedObjectPath, Session>,

Check failure on line 29 in server/src/daemon/service.rs

View workflow job for this annotation

GitHub Actions / Clippy

field `sessions` is never read

Check warning on line 29 in server/src/daemon/service.rs

View workflow job for this annotation

GitHub Actions / Check

field `sessions` is never read
}

impl ServiceManager {
pub fn new() -> Self {
Self {
sessions: HashMap::new(),
}
}

pub fn sessions(&self) -> &HashMap<OwnedObjectPath, Session> {

Check failure on line 39 in server/src/daemon/service.rs

View workflow job for this annotation

GitHub Actions / Clippy

methods `sessions` and `set_sessions` are never used

Check warning on line 39 in server/src/daemon/service.rs

View workflow job for this annotation

GitHub Actions / Check

methods `sessions` and `set_sessions` are never used
&self.sessions
}

pub fn set_sessions(&mut self, path: OwnedObjectPath, session: Session) {
self.sessions.insert(path, session);
}
}

#[derive(Debug)]
pub struct Service {
collections: Vec<Collection>,
keyring: Arc<Keyring>,
cnx: Mutex<Option<zbus::Connection>>,
manager: Arc<ServiceManager>,
}

#[zbus::interface(name = "org.freedesktop.Secret.Service")]
Expand All @@ -44,6 +66,7 @@ impl Service {
};
let (session, key) = Session::new(client_public_key);
// TODO: clean up the default generated key
// TODO call self.manager.set_sessions();
let key = key
.map(|k| OwnedValue::from(&k))
.unwrap_or_else(|| Value::new::<Vec<u8>>(vec![]).try_to_owned().unwrap());
Expand All @@ -65,12 +88,14 @@ impl Service {
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap(),
Arc::clone(&self.keyring),
Arc::clone(&self.manager),
);
self.set_collections(Collection::new(
collection.label(),
alias,
Duration::from_secs(collection.created()),
Arc::clone(&self.keyring),
Arc::clone(&self.manager),
));

let path = OwnedObjectPath::from(collection.path());
Expand Down Expand Up @@ -253,6 +278,7 @@ impl Service {
collections: Vec::new(),
keyring: Arc::new(Keyring::load_default().await.unwrap()),
cnx: Default::default(),
manager: Arc::new(ServiceManager::new()),
}
}

Expand Down

0 comments on commit 1f012ae

Please sign in to comment.