Skip to main content

bitwarden_shared_unlock/
message.rs

1use bitwarden_core::UserId;
2use bitwarden_ipc::PayloadTypeName;
3use serde::{Deserialize, Serialize};
4
5use crate::LockState;
6
7/// The messages sent from followers to the leader
8#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
9pub enum FollowerMessage {
10    /// Synchronizes a user's lock state between participants.
11    LockStateUpdate {
12        /// User whose lock state is being synchronized.
13        user_id: UserId,
14        /// New lock state for the user.
15        lock_state: LockState,
16    },
17    /// A follower, upon startup should send the `StartSession` message to the leader to
18    /// announce its presence. It also sends the lock state. The leader then should unlock
19    /// if it is locked and the follower sent an unlocked state, otherwise it should not change
20    /// the lock state. Subsequently, it should respond with a lockstate update.
21    StartSession {
22        /// User whose session is starting.
23        user_id: UserId,
24        /// Current lock state for the user.
25        lock_state: LockState,
26    },
27    /// A heartbeat request to the leader every `HEARTBEAT_INTERVAL`.
28    HeartBeat {
29        /// User whose session liveness is being reported.
30        user_id: UserId,
31    },
32}
33
34/// The messages sent from the leader to followers
35#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
36pub enum LeaderMessage {
37    /// Synchronizes a user's lock state between participants.
38    LockStateUpdate {
39        /// User whose lock state is being synchronized.
40        user_id: UserId,
41        /// New lock state for the user.
42        lock_state: LockState,
43    },
44    /// The leader response to the follower's heartbeat request.
45    HeartBeat {
46        /// User whose session liveness is being reported.
47        user_id: UserId,
48    },
49    /// The leader requests a lock state from a follower
50    RequestSessionStart {
51        /// User whose lock state is being requested.
52        user_id: UserId,
53    },
54}
55
56impl FollowerMessage {
57    /// Returns the user ID associated with the message.
58    pub fn user_id(&self) -> UserId {
59        match self {
60            FollowerMessage::LockStateUpdate { user_id, .. }
61            | FollowerMessage::StartSession { user_id, .. }
62            | FollowerMessage::HeartBeat { user_id } => *user_id,
63        }
64    }
65}
66
67impl PayloadTypeName for FollowerMessage {
68    const PAYLOAD_TYPE_NAME: &'static str = "password-manager.shared-unlock.follower-to-leader";
69}
70
71impl LeaderMessage {
72    /// Returns the user ID associated with the message.
73    pub fn user_id(&self) -> UserId {
74        match self {
75            LeaderMessage::LockStateUpdate { user_id, .. }
76            | LeaderMessage::RequestSessionStart { user_id }
77            | LeaderMessage::HeartBeat { user_id } => *user_id,
78        }
79    }
80}
81
82impl PayloadTypeName for LeaderMessage {
83    const PAYLOAD_TYPE_NAME: &'static str = "password-manager.shared-unlock.leader-to-follower";
84}