Skip to content

Commit

Permalink
server: Load temporary keyring when dispatching the session collection
Browse files Browse the repository at this point in the history
And add Collection::new()
Also store a Keyring in Collection struct.

Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura committed Oct 22, 2024
1 parent 12ff720 commit 34154e1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true

[dependencies]
oo7 = { workspace = true, features = ["unstable"] }
rand = "0.8"
serde.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing = "0.1"
Expand Down
15 changes: 13 additions & 2 deletions server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{
time::{Duration, SystemTime},
};

use oo7::dbus::api::{Properties, SecretInner};
use oo7::{
dbus::api::{Properties, SecretInner},
portal::Keyring,
};
use tokio::sync::{Mutex, RwLock};
use zbus::{interface, zvariant};
use zvariant::{ObjectPath, OwnedObjectPath};
Expand All @@ -25,6 +28,8 @@ pub struct Collection {
modified: Mutex<Duration>,
// Other attributes
alias: Mutex<String>,
#[allow(unused)]
keyring: Arc<Keyring>,
manager: Arc<Mutex<ServiceManager>>,
n_items: RwLock<i32>,
path: OwnedObjectPath,
Expand Down Expand Up @@ -54,7 +59,12 @@ impl Collection {
}

impl Collection {
pub fn new(label: &str, alias: &str, manager: Arc<Mutex<ServiceManager>>) -> Self {
pub fn new(
label: &str,
alias: &str,
manager: Arc<Mutex<ServiceManager>>,
keyring: Arc<Keyring>,
) -> Self {
let created = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
Expand All @@ -73,6 +83,7 @@ impl Collection {
.unwrap(),
created,
manager,
keyring,
}
}

Expand Down
5 changes: 4 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ mod service;
mod service_manager;
mod session;

use oo7::portal::Secret;
use service::{Result, Service};

const BINARY_NAME: &str = env!("CARGO_BIN_NAME");

#[tokio::main]
async fn main() -> Result<()> {
let secret: Option<Secret> = None;
tracing_subscriber::fmt::init();
tracing::info!("Starting {}", BINARY_NAME);

Service::run().await?;
let connection = Service::run().await?;
Service::dispatch_collections(connection, secret).await?;

std::future::pending::<()>().await;

Expand Down
66 changes: 44 additions & 22 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use oo7::{
api::{Properties, SecretInner},
Algorithm, ServiceError,
},
portal::{Keyring, Secret},
Key,
};
use rand::Rng;
use tokio::sync::Mutex;
use zbus::{
proxy::Defaults,
Expand Down Expand Up @@ -123,46 +125,66 @@ impl Service {
}

impl Service {
pub async fn run() -> Result<()> {
pub async fn run() -> Result<zbus::Connection> {
let connection = zbus::connection::Builder::session()?
.name(oo7::dbus::api::Service::DESTINATION.as_deref().unwrap())?
.build()
.await?;
let object_server = connection.object_server();
let service = Self {
collections: Default::default(),
manager: Default::default(),
connection: connection.clone(),
};
let collections = service.fetch_collections().await;

object_server
connection
.object_server()
.at(oo7::dbus::api::Service::PATH.as_deref().unwrap(), service)
.await?;

for collection in collections {
object_server
.at(collection.path().clone(), collection)
.await?;
Ok(connection)
}

pub async fn dispatch_collections(
connection: zbus::Connection,
secret: Option<Secret>,
) -> Result<()> {
let service_ifce_ref = connection
.object_server()
.interface::<_, Service>(oo7::dbus::api::Service::PATH.as_deref().unwrap())
.await?;
let service = service_ifce_ref.get_mut().await;
let manager = service.manager();

if secret.is_some() {
// this is for the login collection
}

// create and dispatch temporary session collection
let secret = Secret::from(rand::thread_rng().gen::<[u8; 8]>().to_vec());
let session = Collection::new(
"session",
"session",
manager,
Arc::new(match Keyring::temporary(secret).await {
Ok(keyring) => keyring,
Err(err) => panic!("Failed to open temporary keyring: {}", err),
}),
);
service.set_collections(session.path().clone()).await;

connection
.object_server()
.at(session.path().clone(), session)
.await?;

Ok(())
}

async fn fetch_collections(&self) -> Vec<Collection> {
let mut collections = Vec::new();
// todo: create default collection

// create temporary session collection
let session_collection = Collection::new("session", "session", Arc::clone(&self.manager));
collections.push(session_collection);

let mut lock = self.collections.lock().await;
for collection in &collections {
lock.push(collection.path().clone());
}
drop(lock);
pub fn manager(&self) -> Arc<Mutex<ServiceManager>> {
Arc::clone(&self.manager)
}

collections
pub async fn set_collections(&self, path: OwnedObjectPath) {
self.collections.lock().await.push(path);
}
}

0 comments on commit 34154e1

Please sign in to comment.