t_rust_less_lib/service/
mod.rs

1use chrono::{DateTime, Utc};
2
3use crate::api::{ClipboardProviding, Event, PasswordGeneratorParam, StoreConfig};
4use std::sync::Arc;
5
6mod config;
7mod error;
8pub mod local;
9pub mod pw_generator;
10mod remote;
11pub mod secrets_provider;
12mod synchronizer;
13
14#[cfg(unix)]
15pub mod unix;
16#[cfg(windows)]
17pub mod windows;
18
19pub use self::config::config_file;
20pub use self::error::*;
21
22use crate::secrets_store::{SecretStoreResult, SecretsStore};
23
24pub trait ClipboardControl: Send + Sync {
25  fn is_done(&self) -> ServiceResult<bool>;
26
27  fn currently_providing(&self) -> ServiceResult<Option<ClipboardProviding>>;
28
29  fn provide_next(&self) -> ServiceResult<()>;
30
31  fn destroy(&self) -> ServiceResult<()>;
32}
33
34/// Main entrypoint for all interactions with the t-rust-less system
35pub trait TrustlessService: std::fmt::Debug + Send + Sync {
36  /// List all store configurations
37  fn list_stores(&self) -> ServiceResult<Vec<StoreConfig>>;
38
39  /// Create or update a store configuration
40  fn upsert_store_config(&self, store_config: StoreConfig) -> ServiceResult<()>;
41
42  /// Delete a store configuration
43  /// (This will only delete the configuration, the store itself will be left untouched)
44  fn delete_store_config(&self, name: &str) -> ServiceResult<()>;
45
46  /// Open a store
47  fn open_store(&self, name: &str) -> SecretStoreResult<Arc<dyn SecretsStore>>;
48
49  /// Get the name of the store that should be opened by default
50  fn get_default_store(&self) -> ServiceResult<Option<String>>;
51
52  /// Set the name of the store that should be opened by default
53  fn set_default_store(&self, name: &str) -> ServiceResult<()>;
54
55  fn secret_to_clipboard(
56    &self,
57    store_name: &str,
58    block_id: &str,
59    properties: &[&str],
60  ) -> ServiceResult<Arc<dyn ClipboardControl>>;
61
62  fn poll_events(&self, last_id: u64) -> ServiceResult<Vec<Event>>;
63
64  fn generate_id(&self) -> ServiceResult<String>;
65
66  fn generate_password(&self, param: PasswordGeneratorParam) -> ServiceResult<String>;
67
68  fn check_autolock(&self);
69
70  fn needs_synchronization(&self) -> bool;
71
72  fn synchronize(&self) -> Option<DateTime<Utc>>;
73}
74
75pub fn create_service() -> ServiceResult<Arc<dyn TrustlessService>> {
76  #[cfg(unix)]
77  {
78    if let Some(remote) = self::unix::try_remote_service()? {
79      return Ok(Arc::new(remote));
80    }
81  }
82  #[cfg(windows)]
83  {
84    if let Some(remote) = self::windows::try_remote_service()? {
85      return Ok(Arc::new(remote));
86    }
87  }
88  Ok(Arc::new(self::local::LocalTrustlessService::new()?))
89}