sos_login/
delegated_access.rs

1//! Trait for delegated password access.
2use async_trait::async_trait;
3use secrecy::SecretString;
4use sos_core::{crypto::AccessKey, VaultId};
5use sos_password::diceware::generate_passphrase_words;
6
7/// Number of words to use when generating passphrases for vaults.
8const VAULT_PASSPHRASE_WORDS: usize = 12;
9
10/// Delegated access to folder keys.
11#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
12#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
13pub trait DelegatedAccess {
14    /// Error type.
15    type Error: std::error::Error
16        + From<crate::Error>
17        + From<sos_password::Error>
18        + std::fmt::Debug
19        + Send
20        + Sync
21        + 'static;
22
23    /// Find a folder password.
24    async fn find_folder_password(
25        &self,
26        folder_id: &VaultId,
27    ) -> Result<Option<AccessKey>, Self::Error>;
28
29    /// Remove a folder password.
30    async fn remove_folder_password(
31        &mut self,
32        folder_id: &VaultId,
33    ) -> Result<(), Self::Error>;
34
35    /// Generate a folder password.
36    fn generate_folder_password(&self) -> Result<SecretString, Self::Error> {
37        let (vault_passphrase, _) =
38            generate_passphrase_words(VAULT_PASSPHRASE_WORDS)?;
39        Ok(vault_passphrase)
40    }
41
42    /// Save a folder password.
43    ///
44    /// If a password already exists it is overwritten.
45    async fn save_folder_password(
46        &mut self,
47        folder_id: &VaultId,
48        key: AccessKey,
49    ) -> Result<(), Self::Error>;
50}