Skip to main content

bitwarden_shared_unlock/
drivers.rs

1//! Drivers that need to be implemented per platform for the shared unlock system.
2
3use bitwarden_core::UserId;
4use bitwarden_crypto::SymmetricCryptoKey;
5
6use crate::PeerLockState;
7
8/// Trait that implmeents the device's shared unlock driver. These functions need to be implemented
9/// in order to allow the shared unlock system to function.
10#[async_trait::async_trait]
11pub trait SharedUnlockDriver {
12    /// Lock the user with the given ID.
13    async fn lock_user(&self, user_id: UserId) -> Result<(), ()>;
14    /// Unlock the user with the given ID.
15    async fn unlock_user(&self, user_id: UserId, user_key: SymmetricCryptoKey) -> Result<(), ()>;
16    /// List all users this device has an account for, locked or unlocked.
17    async fn list_users(&self) -> Vec<UserId>;
18    /// Get vault_url for the user with the given ID, if available. This is used to verify IPC
19    /// message sources
20    async fn get_vault_url(&self, user_id: UserId) -> Option<String>;
21    /// Suppress the vault timeout for the given user for the specified duration.
22    /// Called when a sync is received from this device's leader, keeping the shared session active.
23    async fn suppress_vault_timeout(
24        &self,
25        user_id: UserId,
26        suppression_duration: std::time::Duration,
27    );
28    /// Called when a peer state is reported to the shared unlock system (locked or unlocked).
29    /// This can be used when client needs to be informed about establishment of a shared session
30    /// with a peer device.
31    async fn on_peer_state(&self, user_id: UserId, lock_state: PeerLockState);
32
33    /// Persist the date (milliseconds since the Unix epoch) of the user's last manual lock. Must
34    /// survive a process reload, so a lock that was not synced before the reload still wins
35    /// against an older unlock held by a peer.
36    async fn set_last_manual_lock(&self, user_id: UserId, locked_at: u64);
37    /// The date persisted by [`SharedUnlockDriver::set_last_manual_lock`], if any.
38    async fn get_last_manual_lock(&self, user_id: UserId) -> Option<u64>;
39
40    /// Discovers the endpoint of the peer above this one in the device hierarchy or none
41    /// if this device is at the top of the hierarchy. The local peer disables vault timeout
42    /// if it is not at the top of the hierarchy.
43    async fn discover_leader(&self) -> Option<bitwarden_ipc::Endpoint>;
44}