crypto: Add the automatic key claim users to the key claim request.

master
Damir Jelić 2020-10-07 14:02:50 +02:00
parent 06b9c71dbc
commit 8ea0035cd0
2 changed files with 37 additions and 11 deletions

View File

@ -214,6 +214,11 @@ impl KeyRequestMachine {
&self.user_id &self.user_id
} }
/// Get the map of user/devices which we need to claim one-time for.
pub fn users_for_key_claim(&self) -> &DashMap<UserId, DashSet<DeviceIdBox>> {
&self.users_for_key_claim
}
pub fn outgoing_to_device_requests(&self) -> Vec<OutgoingRequest> { pub fn outgoing_to_device_requests(&self) -> Vec<OutgoingRequest> {
#[allow(clippy::map_clone)] #[allow(clippy::map_clone)]
self.outgoing_to_device_requests self.outgoing_to_device_requests

View File

@ -358,7 +358,14 @@ impl OlmMachine {
/// Sessions need to be established between devices so group sessions for a /// Sessions need to be established between devices so group sessions for a
/// room can be shared with them. /// room can be shared with them.
/// ///
/// This should be called every time a group session needs to be shared. /// This should be called every time a group session needs to be shared as
/// well as between sync calls. After a sync some devices may request room
/// keys without us having a valid Olm session with them, making it
/// impossible to server the room key request, thus it's necessary to check
/// for missing sessions between sync as well.
///
/// **Note**: Care should be taken that only one such request at a time is
/// in flight, e.g. using a lock.
/// ///
/// The response of a successful key claiming requests needs to be passed to /// The response of a successful key claiming requests needs to be passed to
/// the `OlmMachine` with the [`mark_request_as_sent`]. /// the `OlmMachine` with the [`mark_request_as_sent`].
@ -366,7 +373,8 @@ impl OlmMachine {
/// # Arguments /// # Arguments
/// ///
/// `users` - The list of users that we should check if we lack a session /// `users` - The list of users that we should check if we lack a session
/// with one of their devices. /// with one of their devices. This can be an empty iterator when calling
/// this method between sync requests.
/// ///
/// [`mark_request_as_sent`]: #method.mark_request_as_sent /// [`mark_request_as_sent`]: #method.mark_request_as_sent
pub async fn get_missing_sessions( pub async fn get_missing_sessions(
@ -375,6 +383,8 @@ impl OlmMachine {
) -> OlmResult<Option<(Uuid, KeysClaimRequest)>> { ) -> OlmResult<Option<(Uuid, KeysClaimRequest)>> {
let mut missing = BTreeMap::new(); let mut missing = BTreeMap::new();
// Add the list of devices that the user wishes to establish sessions
// right now.
for user_id in users { for user_id in users {
let user_devices = self.store.get_user_devices(user_id).await?; let user_devices = self.store.get_user_devices(user_id).await?;
@ -394,19 +404,30 @@ impl OlmMachine {
}; };
if is_missing { if is_missing {
if !missing.contains_key(user_id) { missing
let _ = missing.insert(user_id.clone(), BTreeMap::new()); .entry(user_id.to_owned())
} .or_insert_with(BTreeMap::new)
.insert(
let user_map = missing.get_mut(user_id).unwrap(); device.device_id().into(),
let _ = user_map.insert( DeviceKeyAlgorithm::SignedCurve25519,
device.device_id().into(), );
DeviceKeyAlgorithm::SignedCurve25519,
);
} }
} }
} }
// Add the list of sessions that for some reason automatically need to
// create an Olm session.
for item in self.key_request_machine.users_for_key_claim().iter() {
let user = item.key();
for device_id in item.value().iter() {
missing
.entry(user.to_owned())
.or_insert_with(BTreeMap::new)
.insert(device_id.to_owned(), DeviceKeyAlgorithm::SignedCurve25519);
}
}
if missing.is_empty() { if missing.is_empty() {
Ok(None) Ok(None)
} else { } else {