mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-26 19:36:08 +03:00
crypto: Send out to-device requests to share the room key
This commit is contained in:
parent
4c44a5e108
commit
5f848093b9
5 changed files with 53 additions and 4 deletions
|
@ -91,6 +91,7 @@ import org.matrix.android.sdk.internal.crypto.tasks.GetDeviceInfoTask
|
||||||
import org.matrix.android.sdk.internal.crypto.tasks.GetDevicesTask
|
import org.matrix.android.sdk.internal.crypto.tasks.GetDevicesTask
|
||||||
import org.matrix.android.sdk.internal.crypto.tasks.NewUploadKeysTask
|
import org.matrix.android.sdk.internal.crypto.tasks.NewUploadKeysTask
|
||||||
import org.matrix.android.sdk.internal.crypto.tasks.SetDeviceNameTask
|
import org.matrix.android.sdk.internal.crypto.tasks.SetDeviceNameTask
|
||||||
|
import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask
|
||||||
import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
|
import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask
|
||||||
import org.matrix.android.sdk.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask
|
import org.matrix.android.sdk.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask
|
||||||
import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService
|
import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService
|
||||||
|
@ -174,6 +175,7 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
// Tasks
|
// Tasks
|
||||||
private val getDevicesTask: GetDevicesTask,
|
private val getDevicesTask: GetDevicesTask,
|
||||||
private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask,
|
private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask,
|
||||||
|
private val sendToDeviceTask: SendToDeviceTask,
|
||||||
private val downloadKeysForUsersTask: DownloadKeysForUsersTask,
|
private val downloadKeysForUsersTask: DownloadKeysForUsersTask,
|
||||||
private val getDeviceInfoTask: GetDeviceInfoTask,
|
private val getDeviceInfoTask: GetDeviceInfoTask,
|
||||||
private val setDeviceNameTask: SetDeviceNameTask,
|
private val setDeviceNameTask: SetDeviceNameTask,
|
||||||
|
@ -962,9 +964,9 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
|
|
||||||
private suspend fun preshareGroupSession(roomId: String, roomMembers: List<String>) {
|
private suspend fun preshareGroupSession(roomId: String, roomMembers: List<String>) {
|
||||||
val request = olmMachine!!.getMissingSessions(roomMembers)
|
val request = olmMachine!!.getMissingSessions(roomMembers)
|
||||||
roomId == "est"
|
|
||||||
|
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
|
// This request can only be a keys claim request.
|
||||||
when (request) {
|
when (request) {
|
||||||
is Request.KeysClaim -> {
|
is Request.KeysClaim -> {
|
||||||
val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys)
|
val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys)
|
||||||
|
@ -975,6 +977,23 @@ internal class DefaultCryptoService @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (toDeviceRequest in olmMachine!!.shareGroupSession(roomId, roomMembers)) {
|
||||||
|
// This request can only be a to-device request.
|
||||||
|
when (toDeviceRequest) {
|
||||||
|
is Request.ToDevice -> {
|
||||||
|
val adapter = MoshiProvider.providesMoshi().adapter<Map<String, HashMap<String, Any>>>(Map::class.java)
|
||||||
|
val body = adapter.fromJson(toDeviceRequest.body)!!
|
||||||
|
|
||||||
|
val userMap = MXUsersDevicesMap<Any>()
|
||||||
|
userMap.join(body)
|
||||||
|
|
||||||
|
val sendToDeviceParams = SendToDeviceTask.Params(toDeviceRequest.eventType, userMap)
|
||||||
|
sendToDeviceTask.execute(sendToDeviceParams)
|
||||||
|
olmMachine!!.markRequestAsSent(toDeviceRequest.requestId, RequestType.TO_DEVICE, "{}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// private suspend fun encrypt(roomId: String, eventType: String, content: Content) {
|
// private suspend fun encrypt(roomId: String, eventType: String, content: Content) {
|
||||||
|
|
|
@ -115,6 +115,10 @@ class MXUsersDevicesMap<E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun join(other: Map<out String, HashMap<String, E>>) {
|
||||||
|
map.putAll(other)
|
||||||
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "MXUsersDevicesMap $map"
|
return "MXUsersDevicesMap $map"
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,6 +87,10 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
|
||||||
inner.outgoingRequests()
|
inner.outgoingRequests()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun shareGroupSession(roomId: String, users: List<String>): List<Request> = withContext(Dispatchers.IO) {
|
||||||
|
inner.shareGroupSession(roomId, users)
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun getMissingSessions(users: List<String>): Request? = withContext(Dispatchers.IO) {
|
suspend fun getMissingSessions(users: List<String>): Request? = withContext(Dispatchers.IO) {
|
||||||
inner.getMissingSessions(users)
|
inner.getMissingSessions(users)
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,8 @@ use matrix_sdk_common::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use matrix_sdk_crypto::{
|
use matrix_sdk_crypto::{
|
||||||
IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, ToDeviceRequest,
|
EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest,
|
||||||
|
ToDeviceRequest,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError};
|
use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError};
|
||||||
|
@ -358,6 +359,25 @@ impl OlmMachine {
|
||||||
.block_on(self.inner.update_tracked_users(users.iter()));
|
.block_on(self.inner.update_tracked_users(users.iter()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn share_group_session(&self, room_id: &str, users: Vec<String>) -> Vec<Request> {
|
||||||
|
let users: Vec<UserId> = users
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|u| UserId::try_from(u).ok())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let room_id = RoomId::try_from(room_id).unwrap();
|
||||||
|
let requests = self
|
||||||
|
.runtime
|
||||||
|
.block_on(self.inner.share_group_session(
|
||||||
|
&room_id,
|
||||||
|
users.iter(),
|
||||||
|
EncryptionSettings::default(),
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
requests.into_iter().map(|r| (&*r).into()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_missing_sessions(
|
pub fn get_missing_sessions(
|
||||||
&self,
|
&self,
|
||||||
users: Vec<String>,
|
users: Vec<String>,
|
||||||
|
|
|
@ -84,11 +84,13 @@ interface OlmMachine {
|
||||||
|
|
||||||
Device? get_device([ByRef] string user_id, [ByRef] string device_id);
|
Device? get_device([ByRef] string user_id, [ByRef] string device_id);
|
||||||
sequence<Device> get_user_devices([ByRef] string user_id);
|
sequence<Device> get_user_devices([ByRef] string user_id);
|
||||||
sequence<Request> outgoing_requests();
|
|
||||||
void update_tracked_users(sequence<string> users);
|
|
||||||
|
|
||||||
|
sequence<Request> outgoing_requests();
|
||||||
|
|
||||||
|
void update_tracked_users(sequence<string> users);
|
||||||
[Throws=CryptoStoreError]
|
[Throws=CryptoStoreError]
|
||||||
Request? get_missing_sessions(sequence<string> users);
|
Request? get_missing_sessions(sequence<string> users);
|
||||||
|
sequence<Request> share_group_session([ByRef] string room_id, sequence<string> users);
|
||||||
|
|
||||||
[Throws=CryptoStoreError]
|
[Throws=CryptoStoreError]
|
||||||
void mark_request_as_sent(
|
void mark_request_as_sent(
|
||||||
|
|
Loading…
Reference in a new issue