2020-08-17 13:34:05 +00:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-08-17 15:36:07 +00:00
|
|
|
use std::{ops::Deref, result::Result as StdResult};
|
2020-08-17 13:54:54 +00:00
|
|
|
|
2020-09-11 14:34:39 +00:00
|
|
|
use matrix_sdk_base::crypto::{
|
|
|
|
store::CryptoStoreError, Device as BaseDevice, LocalTrust, ReadOnlyDevice,
|
2020-08-17 15:36:07 +00:00
|
|
|
UserDevices as BaseUserDevices,
|
|
|
|
};
|
2020-12-15 15:35:54 +00:00
|
|
|
use matrix_sdk_common::identifiers::{DeviceId, DeviceIdBox};
|
2020-08-17 13:34:05 +00:00
|
|
|
|
2020-12-15 15:35:54 +00:00
|
|
|
use crate::{error::Result, Client, Sas};
|
2020-08-17 13:34:05 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
/// A device represents a E2EE capable client of an user.
|
|
|
|
pub struct Device {
|
2020-08-17 14:36:50 +00:00
|
|
|
pub(crate) inner: BaseDevice,
|
2020-12-15 15:35:54 +00:00
|
|
|
pub(crate) client: Client,
|
2020-08-17 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 13:54:54 +00:00
|
|
|
impl Deref for Device {
|
|
|
|
type Target = ReadOnlyDevice;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:34:05 +00:00
|
|
|
impl Device {
|
|
|
|
/// Start a interactive verification with this `Device`
|
|
|
|
///
|
2021-05-12 17:20:52 +00:00
|
|
|
/// Returns a `Sas` object that represents the interactive verification
|
|
|
|
/// flow.
|
2020-08-17 14:47:24 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// # use std::convert::TryFrom;
|
|
|
|
/// # use matrix_sdk::{Client, identifiers::UserId};
|
|
|
|
/// # use url::Url;
|
|
|
|
/// # use futures::executor::block_on;
|
|
|
|
/// # let alice = UserId::try_from("@alice:example.org").unwrap();
|
|
|
|
/// # let homeserver = Url::parse("http://example.com").unwrap();
|
|
|
|
/// # let client = Client::new(homeserver).unwrap();
|
|
|
|
/// # block_on(async {
|
2020-09-29 12:18:03 +00:00
|
|
|
/// let device = client.get_device(&alice, "DEVICEID".into())
|
|
|
|
/// .await
|
|
|
|
/// .unwrap()
|
|
|
|
/// .unwrap();
|
2020-08-17 14:47:24 +00:00
|
|
|
///
|
|
|
|
/// let verification = device.start_verification().await.unwrap();
|
|
|
|
/// # });
|
|
|
|
/// ```
|
2020-08-17 13:34:05 +00:00
|
|
|
pub async fn start_verification(&self) -> Result<Sas> {
|
2020-08-18 11:37:02 +00:00
|
|
|
let (sas, request) = self.inner.start_verification().await?;
|
2020-12-15 15:35:54 +00:00
|
|
|
self.client.send_to_device(&request).await?;
|
2020-08-17 13:34:05 +00:00
|
|
|
|
2021-05-12 15:00:47 +00:00
|
|
|
Ok(Sas { inner: sas, client: self.client.clone() })
|
2020-08-17 13:34:05 +00:00
|
|
|
}
|
2020-08-17 15:36:07 +00:00
|
|
|
|
2020-08-19 12:50:35 +00:00
|
|
|
/// Is the device trusted.
|
|
|
|
pub fn is_trusted(&self) -> bool {
|
|
|
|
self.inner.trust_state()
|
|
|
|
}
|
|
|
|
|
2020-08-20 16:01:34 +00:00
|
|
|
/// Set the local trust state of the device to the given state.
|
|
|
|
///
|
|
|
|
/// This won't affect any cross signing trust state, this only sets a flag
|
|
|
|
/// marking to have the given trust state.
|
2020-08-17 15:36:07 +00:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `trust_state` - The new trust state that should be set for the device.
|
2020-08-20 16:01:34 +00:00
|
|
|
pub async fn set_local_trust(
|
2020-08-17 15:36:07 +00:00
|
|
|
&self,
|
2020-08-19 09:20:08 +00:00
|
|
|
trust_state: LocalTrust,
|
2020-08-17 15:36:07 +00:00
|
|
|
) -> StdResult<(), CryptoStoreError> {
|
2020-08-20 16:01:34 +00:00
|
|
|
self.inner.set_local_trust(trust_state).await
|
2020-08-17 15:36:07 +00:00
|
|
|
}
|
2020-08-17 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A read only view over all devices belonging to a user.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct UserDevices {
|
2020-08-17 15:12:39 +00:00
|
|
|
pub(crate) inner: BaseUserDevices,
|
2020-12-15 15:35:54 +00:00
|
|
|
pub(crate) client: Client,
|
2020-08-17 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserDevices {
|
|
|
|
/// Get the specific device with the given device id.
|
|
|
|
pub fn get(&self, device_id: &DeviceId) -> Option<Device> {
|
2021-05-12 15:00:47 +00:00
|
|
|
self.inner.get(device_id).map(|d| Device { inner: d, client: self.client.clone() })
|
2020-08-17 13:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator over all the device ids of the user devices.
|
2020-10-16 14:53:10 +00:00
|
|
|
pub fn keys(&self) -> impl Iterator<Item = &DeviceIdBox> {
|
2020-08-17 13:34:05 +00:00
|
|
|
self.inner.keys()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterator over all the devices of the user devices.
|
|
|
|
pub fn devices(&self) -> impl Iterator<Item = Device> + '_ {
|
2020-12-15 15:35:54 +00:00
|
|
|
let client = self.client.clone();
|
2020-08-17 13:34:05 +00:00
|
|
|
|
2021-05-12 15:00:47 +00:00
|
|
|
self.inner.devices().map(move |d| Device { inner: d, client: client.clone() })
|
2020-08-17 13:34:05 +00:00
|
|
|
}
|
|
|
|
}
|