libsignal_service/
session_store.rs

1use async_trait::async_trait;
2use libsignal_core::DeviceId;
3use libsignal_protocol::{
4    ProtocolAddress, ServiceId, SessionStore, SignalProtocolError,
5};
6
7use crate::push_service::DEFAULT_DEVICE_ID;
8
9/// This is additional functions required to handle
10/// session deletion. It might be a candidate for inclusion into
11/// the bigger `SessionStore` trait.
12#[async_trait(?Send)]
13pub trait SessionStoreExt: SessionStore {
14    /// Get the IDs of all known sub devices with active sessions for a recipient.
15    ///
16    /// This should return every device except for the main device [DEFAULT_DEVICE_ID].
17    async fn get_sub_device_sessions(
18        &self,
19        name: &ServiceId,
20    ) -> Result<Vec<DeviceId>, SignalProtocolError>;
21
22    /// Remove a session record for a recipient ID + device ID tuple.
23    async fn delete_session(
24        &self,
25        address: &ProtocolAddress,
26    ) -> Result<(), SignalProtocolError>;
27
28    /// Remove the session records corresponding to all devices of a recipient
29    /// ID.
30    ///
31    /// Returns the number of deleted sessions.
32    async fn delete_all_sessions(
33        &self,
34        address: &ServiceId,
35    ) -> Result<usize, SignalProtocolError>;
36
37    /// Remove a session record for a recipient ID + device ID tuple.
38    async fn delete_service_addr_device_session(
39        &self,
40        address: &ProtocolAddress,
41    ) -> Result<usize, SignalProtocolError> {
42        let mut count = 0;
43        match self.delete_session(address).await {
44            Ok(()) => {
45                count += 1;
46            },
47            Err(SignalProtocolError::SessionNotFound(_)) => (),
48            Err(e) => return Err(e),
49        }
50
51        Ok(count)
52    }
53
54    async fn compute_safety_number(
55        &self,
56        local_address: &ServiceId,
57        address: &ServiceId,
58    ) -> Result<String, SignalProtocolError>
59    where
60        Self: Sized + libsignal_protocol::IdentityKeyStore,
61    {
62        let addr = crate::cipher::get_preferred_protocol_address(
63            self,
64            address,
65            *DEFAULT_DEVICE_ID,
66        )
67        .await?;
68        let ident = self
69            .get_identity(&addr)
70            .await?
71            .ok_or(SignalProtocolError::UntrustedIdentity(addr))?;
72        let local = self
73            .get_identity_key_pair()
74            .await
75            .expect("valid local identity");
76        let fp = libsignal_protocol::Fingerprint::new(
77            2,
78            5200,
79            local_address.raw_uuid().as_bytes(),
80            local.identity_key(),
81            address.raw_uuid().as_bytes(),
82            &ident,
83        )?;
84        fp.display_string()
85    }
86}