crypto: Add a method to directly verify a device.

This commit is contained in:
Damir Jelić 2020-08-17 17:36:07 +02:00
parent 84c0311d80
commit 6db7eb0694
3 changed files with 33 additions and 4 deletions

View file

@ -12,9 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::ops::Deref;
use std::{ops::Deref, result::Result as StdResult};
use matrix_sdk_base::{Device as BaseDevice, ReadOnlyDevice, UserDevices as BaseUserDevices};
use matrix_sdk_base::{
CryptoStoreError, Device as BaseDevice, ReadOnlyDevice, TrustState,
UserDevices as BaseUserDevices,
};
use matrix_sdk_common::{
api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, identifiers::DeviceId,
};
@ -72,6 +75,18 @@ impl Device {
http_client: self.http_client.clone(),
})
}
/// Set the trust state of the device to the given state.
///
/// # Arguments
///
/// * `trust_state` - The new trust state that should be set for the device.
pub async fn set_trust_state(
&self,
trust_state: TrustState,
) -> StdResult<(), CryptoStoreError> {
self.inner.set_trust_state(trust_state).await.into()
}
}
/// A read only view over all devices belonging to a user.

View file

@ -36,7 +36,8 @@ use serde_json::{json, Value};
use super::{Account, OlmMachine};
use crate::{
error::SignatureError, verification::VerificationMachine, verify_json, ReadOnlyUserDevices, Sas,
error::SignatureError, store::Result as StoreResult, verification::VerificationMachine,
verify_json, ReadOnlyUserDevices, Sas,
};
/// A read-only version of a `Device`.
@ -74,6 +75,19 @@ impl Device {
pub fn start_verification(&self) -> (Sas, OwnedToDeviceRequest) {
self.verification_machine.start_sas(self.inner.clone())
}
/// Set the trust state of the device to the given state.
///
/// # Arguments
///
/// * `trust_state` - The new trust state that should be set for the device.
pub async fn set_trust_state(&self, trust_state: TrustState) -> StoreResult<()> {
self.inner.set_trust_state(trust_state);
self.verification_machine
.store
.save_devices(&[self.inner.clone()])
.await
}
}
/// A read only view over all devices belonging to a user.

View file

@ -30,7 +30,7 @@ use crate::{Account, CryptoStore, CryptoStoreError, ReadOnlyDevice};
#[derive(Clone, Debug)]
pub struct VerificationMachine {
account: Account,
store: Arc<Box<dyn CryptoStore>>,
pub(crate) store: Arc<Box<dyn CryptoStore>>,
verifications: Arc<DashMap<String, Sas>>,
outgoing_to_device_messages: Arc<DashMap<String, OwnedToDeviceRequest>>,
}