Skip to main content

core_crypto/proteus/
session.rs

1use core_crypto_keystore::{Database, entities::ProteusSession};
2use proteus_wasm::{keys::PreKeyBundle, message::Envelope, session::Session};
3
4use super::{ProteusCentral, ProteusConversationSession};
5use crate::{KeystoreError, LeafError, ProteusError, Result};
6
7impl ProteusCentral {
8    /// Creates a new session from a prekey
9    pub async fn session_from_prekey(
10        &mut self,
11        session_id: &str,
12        key: &[u8],
13    ) -> Result<&mut ProteusConversationSession> {
14        let prekey = PreKeyBundle::deserialise(key).map_err(ProteusError::wrap("deserializing prekey bundle"))?;
15        // Note on the `::<>` turbofish below:
16        //
17        // `init_from_prekey` returns an error type which is parametric over some wrapped `E`,
18        // because one variant (not relevant to this particular operation) wraps an error type based
19        // on a parameter of a different function entirely.
20        //
21        // Rust complains here, because it can't figure out what type that `E` should be. After all, it's
22        // not inferrable from this function call! It is also entirely irrelevant in this case.
23        //
24        // We can derive two general rules about error-handling in Rust from this example:
25        //
26        // 1. It's better to make smaller error types where possible, encapsulating fallible operations with their own
27        //    error variants, and then wrapping those errors where required, as opposed to creating giant catch-all
28        //    errors. Doing so also has knock-on benefits with regard to tracing the precise origin of the error.
29        // 2. One should never make an error wrapper parametric. If you need to wrap an unknown error, it's always
30        //    better to wrap a `Box<dyn std::error::Error>` than to make your error type parametric. The allocation cost
31        //    of creating the `Box` is utterly trivial in an error-handling path, and it avoids parametric virality.
32        //    (`init_from_prekey` is itself only generic because it returns this error type with a type-parametric
33        //    variant, which the function never returns.)
34        //
35        // In this case, we have the out of band knowledge that `ProteusErrorKind` has a `#[from]` implementation
36        // for `proteus_wasm::session::Error<core_crypto_keystore::CryptoKeystoreError>` and for no other kinds
37        // of session error. So we can safely say that the type of error we are meant to catch here, and
38        // therefore pass in that otherwise-irrelevant type, to ensure that error handling works properly.
39        //
40        // Some people say that if it's stupid but it works, it's not stupid. I disagree. If it's stupid but
41        // it works, that's our cue to seek out even better, non-stupid ways to get things done. I reiterate:
42        // the actual type referred to in this turbofish is nothing but a magic incantation to make error
43        // handling work; it has no bearing on the error retured from this function. How much better would it
44        // have been if `session::Error` were not parametric and we could have avoided the turbofish entirely?
45        let proteus_session = Session::init_from_prekey::<core_crypto_keystore::CryptoKeystoreError>(
46            self.proteus_identity.clone(),
47            prekey,
48        )
49        .map_err(ProteusError::wrap("initializing session from prekey"))?;
50
51        let conversation = ProteusConversationSession {
52            identifier: session_id.into(),
53            session: proteus_session,
54        };
55
56        Ok(self.proteus_sessions.insert(conversation))
57    }
58
59    /// Creates a new proteus Session from a received message
60    pub(crate) async fn session_from_message(
61        &mut self,
62        keystore: &mut Database,
63        session_id: &str,
64        envelope: &[u8],
65    ) -> Result<(&mut ProteusConversationSession, Vec<u8>)> {
66        let message = Envelope::deserialise(envelope).map_err(ProteusError::wrap("deserialising envelope"))?;
67        let (session, payload) = Session::init_from_message(self.proteus_identity.clone(), keystore, &message)
68            .await
69            .map_err(ProteusError::wrap("initializing session from message"))?;
70
71        let conversation = ProteusConversationSession {
72            identifier: session_id.into(),
73            session,
74        };
75
76        Ok((self.proteus_sessions.insert(conversation), payload))
77    }
78
79    /// Persists a session in store
80    ///
81    /// **Note**: This isn't usually needed as persisting sessions happens automatically when decrypting/encrypting
82    /// messages and initializing Sessions
83    pub(crate) async fn session_save(&mut self, keystore: &Database, session_id: &str) -> Result<()> {
84        if let Some(session) = self.proteus_sessions.get_or_fetch(session_id, keystore).await? {
85            Self::session_save_by_ref(keystore, session).await?;
86        }
87        Ok(())
88    }
89
90    pub(crate) async fn session_save_by_ref(keystore: &Database, session: &ProteusConversationSession) -> Result<()> {
91        let db_session = ProteusSession {
92            id: session.identifier().to_string(),
93            session: session
94                .session
95                .serialise()
96                .map_err(ProteusError::wrap("serializing session"))?,
97        };
98        keystore
99            .save(db_session)
100            .await
101            .map_err(KeystoreError::wrap("saving proteus session"))?;
102        Ok(())
103    }
104
105    /// Deletes a session in the store
106    pub(crate) async fn session_delete(&mut self, keystore: &Database, session_id: &str) -> Result<()> {
107        if keystore.remove_borrowed::<ProteusSession>(session_id).await.is_ok() {
108            let _ = self.proteus_sessions.remove(session_id);
109        }
110        Ok(())
111    }
112
113    /// Session accessor
114    pub(crate) async fn session(
115        &mut self,
116        session_id: &str,
117        keystore: &Database,
118    ) -> Result<Option<&mut ProteusConversationSession>> {
119        self.proteus_sessions.get_or_fetch(session_id, keystore).await
120    }
121
122    /// Session exists
123    pub(crate) async fn session_exists(&mut self, session_id: &str, keystore: &Database) -> bool {
124        self.session(session_id, keystore).await.ok().flatten().is_some()
125    }
126
127    /// Proteus Session local hex-encoded fingerprint
128    ///
129    /// # Errors
130    /// When the session is not found
131    pub(crate) async fn fingerprint_local(&mut self, session_id: &str, keystore: &Database) -> Result<String> {
132        let session = self
133            .session(session_id, keystore)
134            .await?
135            .ok_or(LeafError::ConversationNotFound(session_id.as_bytes().into()))
136            .map_err(ProteusError::wrap("getting session"))?;
137        Ok(session.fingerprint_local())
138    }
139
140    /// Proteus Session remote hex-encoded fingerprint
141    ///
142    /// # Errors
143    /// When the session is not found
144    pub(crate) async fn fingerprint_remote(&mut self, session_id: &str, keystore: &Database) -> Result<String> {
145        let session = self
146            .session(session_id, keystore)
147            .await?
148            .ok_or(LeafError::ConversationNotFound(session_id.as_bytes().into()))
149            .map_err(ProteusError::wrap("getting session"))?;
150        Ok(session.fingerprint_remote())
151    }
152}