core_crypto/proteus/
session.rs1use core_crypto_keystore::{
2 Transaction,
3 entities::ProteusSession,
4 traits::{EntityDatabaseMutation as _, EntityDeleteBorrowed, FetchFromDatabase},
5};
6use proteus_wasm::{keys::PreKeyBundle, message::Envelope, session::Session};
7
8use super::{ProteusCentral, ProteusConversationSession};
9use crate::{KeystoreError, ProteusError, ProteusErrorKind, Result};
10
11impl ProteusCentral {
12 pub fn session_from_prekey(&mut self, session_id: &str, key: &[u8]) -> Result<&mut ProteusConversationSession> {
14 let prekey = PreKeyBundle::deserialise(key).map_err(ProteusError::wrap("deserializing prekey bundle"))?;
15 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 pub(crate) async fn session_from_message(
61 &mut self,
62 transaction: &Transaction,
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(), transaction, &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 pub(crate) async fn session_save(&mut self, transaction: &Transaction, session_id: &str) -> Result<()> {
84 if let Some(session) = self.proteus_sessions.get_or_fetch(session_id, transaction).await? {
85 Self::session_save_by_ref(transaction, session)?;
86 }
87 Ok(())
88 }
89
90 pub(crate) fn session_save_by_ref(transaction: &Transaction, session: &ProteusConversationSession) -> Result<()> {
91 ProteusSession {
92 id: session.identifier().to_string(),
93 session: session
94 .session
95 .serialise()
96 .map_err(ProteusError::wrap("serializing session"))?,
97 }
98 .save(transaction)
99 .map_err(KeystoreError::wrap("saving proteus session"))?;
100 Ok(())
101 }
102
103 pub(crate) fn session_delete(&mut self, transaction: &Transaction, session_id: &str) -> Result<()> {
105 ProteusSession::delete_borrowed(transaction, session_id)
106 .map_err(KeystoreError::wrap("deleting proteus session"))?;
107 self.proteus_sessions.remove(session_id);
108
109 Ok(())
110 }
111
112 pub(crate) fn clear_session_cache(&mut self) {
114 self.proteus_sessions.clear();
115 }
116
117 pub(crate) async fn session(
119 &mut self,
120 session_id: &str,
121 keystore: &impl FetchFromDatabase,
122 ) -> Result<Option<&mut ProteusConversationSession>> {
123 self.proteus_sessions.get_or_fetch(session_id, keystore).await
124 }
125
126 pub(crate) async fn session_exists(&mut self, session_id: &str, keystore: &impl FetchFromDatabase) -> Result<bool> {
128 self.session(session_id, keystore)
129 .await
130 .map(|maybe_session| maybe_session.is_some())
131 }
132
133 pub(crate) async fn fingerprint_local(
138 &mut self,
139 session_id: &str,
140 keystore: &impl FetchFromDatabase,
141 ) -> Result<String> {
142 let session = self
143 .session(session_id, keystore)
144 .await?
145 .ok_or_else(|| ProteusErrorKind::SessionNotFound(session_id.to_owned()))
146 .map_err(ProteusError::wrap("getting session"))?;
147 Ok(session.fingerprint_local())
148 }
149
150 pub(crate) async fn fingerprint_remote(
155 &mut self,
156 session_id: &str,
157 keystore: &impl FetchFromDatabase,
158 ) -> Result<String> {
159 let session = self
160 .session(session_id, keystore)
161 .await?
162 .ok_or_else(|| ProteusErrorKind::SessionNotFound(session_id.to_owned()))
163 .map_err(ProteusError::wrap("getting session"))?;
164 Ok(session.fingerprint_remote())
165 }
166}