Skip to content

Commit 30b6981

Browse files
warusadurabilelmoussaoui
authored andcommitted
server: Dispatch default collection items
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>
1 parent 0140b5b commit 30b6981

File tree

5 files changed

+73
-20
lines changed

5 files changed

+73
-20
lines changed

server/src/collection.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ use tokio::sync::{Mutex, RwLock};
1717
use zbus::{interface, object_server::SignalEmitter, zvariant};
1818
use zvariant::{ObjectPath, OwnedObjectPath};
1919

20-
use crate::{item, service_manager::ServiceManager};
20+
use crate::{error::Error, item, service_manager::ServiceManager};
2121

2222
#[derive(Debug)]
2323
#[allow(unused)]
2424
pub struct Collection {
2525
// Properties
26-
items: Mutex<Vec<item::Item>>,
26+
items: Mutex<Vec<OwnedObjectPath>>,
2727
label: Mutex<String>,
2828
locked: AtomicBool,
2929
created: Duration,
@@ -33,7 +33,7 @@ pub struct Collection {
3333
#[allow(unused)]
3434
keyring: Arc<Keyring>,
3535
manager: Arc<Mutex<ServiceManager>>,
36-
n_items: RwLock<i32>,
36+
item_index: RwLock<u32>,
3737
path: OwnedObjectPath,
3838
}
3939

@@ -64,12 +64,7 @@ impl Collection {
6464

6565
#[zbus(property, name = "Items")]
6666
pub async fn items(&self) -> Vec<OwnedObjectPath> {
67-
self.items
68-
.lock()
69-
.await
70-
.iter()
71-
.map(|item| OwnedObjectPath::from(item.path()))
72-
.collect()
67+
self.items.lock().await.clone()
7368
}
7469

7570
#[zbus(property, name = "Label")]
@@ -120,6 +115,7 @@ impl Collection {
120115
pub fn new(
121116
label: &str,
122117
alias: &str,
118+
locked: bool,
123119
manager: Arc<Mutex<ServiceManager>>,
124120
keyring: Arc<Keyring>,
125121
) -> Self {
@@ -130,10 +126,10 @@ impl Collection {
130126
Self {
131127
items: Default::default(),
132128
label: Mutex::new(label.to_owned()),
133-
locked: AtomicBool::new(true),
129+
locked: AtomicBool::new(locked),
134130
modified: Mutex::new(created),
135131
alias: Mutex::new(alias.to_owned()),
136-
n_items: RwLock::new(0),
132+
item_index: RwLock::new(0),
137133
path: OwnedObjectPath::try_from(format!(
138134
"/org/freedesktop/secrets/collection/{}",
139135
label
@@ -156,4 +152,30 @@ impl Collection {
156152
pub async fn alias(&self) -> String {
157153
self.alias.lock().await.clone()
158154
}
155+
156+
pub async fn dispatch_items(&self) -> Result<(), Error> {
157+
let keyring_items = self.keyring.items().await;
158+
let mut items = self.items.lock().await;
159+
let service_manager = self.manager.lock().await;
160+
let object_server = service_manager.object_server();
161+
let mut n_items = 1;
162+
163+
for keyring_item in keyring_items {
164+
let item = item::Item::new(
165+
keyring_item.map_err(Error::InvalidItem)?,
166+
self.is_locked().await,
167+
Arc::clone(&self.manager),
168+
self.path.clone(),
169+
n_items,
170+
);
171+
n_items += 1;
172+
173+
items.push(item.path().clone());
174+
object_server.at(item.path().clone(), item).await?;
175+
}
176+
177+
*self.item_index.write().await = n_items;
178+
179+
Ok(())
180+
}
159181
}

server/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub enum Error {
1010
IO(std::io::Error),
1111
// Empty password error
1212
EmptyPassword,
13+
// Invalid item error
14+
InvalidItem(oo7::portal::InvalidItemError),
1315
}
1416

1517
impl From<zbus::Error> for Error {
@@ -37,6 +39,7 @@ impl fmt::Display for Error {
3739
Self::Zbus(err) => write!(f, "Zbus error {err}"),
3840
Self::IO(err) => write!(f, "IO error {err}"),
3941
Self::EmptyPassword => write!(f, "Login password can't be empty"),
42+
Self::InvalidItem(err) => write!(f, "Item cannot be decrypted {err}"),
4043
}
4144
}
4245
}

server/src/item.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
// org.freedesktop.Secret.Item
22

3-
use std::{collections::HashMap, sync::atomic::AtomicBool};
3+
use std::{
4+
collections::HashMap,
5+
sync::{atomic::AtomicBool, Arc},
6+
};
47

5-
use oo7::dbus::{api::SecretInner, ServiceError};
8+
use oo7::{
9+
dbus::{api::SecretInner, ServiceError},
10+
portal,
11+
};
612
use tokio::sync::Mutex;
713
use zbus::zvariant::{ObjectPath, OwnedObjectPath};
814

15+
use crate::service_manager::ServiceManager;
16+
917
#[derive(Debug)]
18+
#[allow(unused)]
1019
pub struct Item {
20+
// Properties
1121
locked: AtomicBool,
1222
inner: Mutex<oo7::portal::Item>,
23+
// Other attributes
24+
manager: Arc<Mutex<ServiceManager>>,
1325
path: OwnedObjectPath,
1426
}
1527

@@ -72,7 +84,22 @@ impl Item {
7284
}
7385

7486
impl Item {
75-
pub fn path(&self) -> ObjectPath<'_> {
76-
self.path.as_ref()
87+
pub fn new(
88+
item: portal::Item,
89+
locked: bool,
90+
manager: Arc<Mutex<ServiceManager>>,
91+
collection_path: OwnedObjectPath,
92+
item_index: u32,
93+
) -> Self {
94+
Self {
95+
locked: AtomicBool::new(locked),
96+
inner: Mutex::new(item),
97+
path: OwnedObjectPath::try_from(format!("{}/{}", collection_path, item_index)).unwrap(),
98+
manager,
99+
}
100+
}
101+
102+
pub fn path(&self) -> &OwnedObjectPath {
103+
&self.path
77104
}
78105
}

server/src/service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,12 @@ impl Service {
220220
let collection = Collection::new(
221221
"login",
222222
"default",
223+
false,
223224
Arc::clone(&service.manager),
224225
Arc::new(Keyring::open("login", secret).await?),
225226
);
226227
collections.push(collection.path().clone());
228+
collection.dispatch_items().await?;
227229
object_server
228230
.at(collection.path().clone(), collection)
229231
.await?;
@@ -232,6 +234,7 @@ impl Service {
232234
let collection = Collection::new(
233235
"session",
234236
"session",
237+
false,
235238
Arc::clone(&service.manager),
236239
Arc::new(Keyring::temporary(Secret::random()).await?),
237240
);
@@ -240,8 +243,6 @@ impl Service {
240243
.at(collection.path().clone(), collection)
241244
.await?;
242245

243-
drop(collections);
244-
245246
Ok(())
246247
}
247248
}

server/src/service_manager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

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

5-
use zbus::{zvariant::OwnedObjectPath, Connection};
5+
use zbus::zvariant::OwnedObjectPath;
66

77
use crate::session::Session;
88

99
#[derive(Debug)]
1010
pub struct ServiceManager {
11-
connection: Connection,
11+
connection: zbus::Connection,
1212
// sessions mapped to their corresponding object path on the bus
1313
sessions: HashMap<OwnedObjectPath, Arc<Session>>,
1414
}
1515

1616
impl ServiceManager {
17-
pub fn new(connection: Connection) -> Self {
17+
pub fn new(connection: zbus::Connection) -> Self {
1818
Self {
1919
sessions: Default::default(),
2020
connection,

0 commit comments

Comments
 (0)