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, Eq)]
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, Eq)]
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}
50
51impl FollowerMessage {
52    /// Returns the user ID associated with the message.
53    pub fn user_id(&self) -> UserId {
54        match self {
55            FollowerMessage::LockStateUpdate { user_id, .. }
56            | FollowerMessage::StartSession { user_id, .. }
57            | FollowerMessage::HeartBeat { user_id } => *user_id,
58        }
59    }
60}
61
62impl PayloadTypeName for FollowerMessage {
63    const PAYLOAD_TYPE_NAME: &'static str = "password-manager.shared-unlock.follower-to-leader";
64}
65
66impl LeaderMessage {
67    /// Returns the user ID associated with the message.
68    pub fn user_id(&self) -> UserId {
69        match self {
70            LeaderMessage::LockStateUpdate { user_id, .. }
71            | LeaderMessage::HeartBeat { user_id } => *user_id,
72        }
73    }
74}
75
76impl PayloadTypeName for LeaderMessage {
77    const PAYLOAD_TYPE_NAME: &'static str = "password-manager.shared-unlock.leader-to-follower";
78}