examples: Update the emoji verification example tho show a list of devices.

This may showcase that cross signing verification works if the other
device uploads valid signatures.
master
Damir Jelić 2020-08-19 14:50:35 +02:00
parent 7f23cbbeb5
commit 9fe0717cee
2 changed files with 39 additions and 8 deletions

View File

@ -1,10 +1,12 @@
use std::{env, io, process::exit}; use std::{env, io, process::exit};
use url::Url; use url::Url;
use matrix_sdk::{self, events::AnyToDeviceEvent, Client, ClientConfig, Sas, SyncSettings}; use matrix_sdk::{
self, events::AnyToDeviceEvent, identifiers::UserId, Client, ClientConfig, Sas, SyncSettings,
};
async fn wait_for_confirmation(sas: Sas) { async fn wait_for_confirmation(client: Client, sas: Sas) {
println!("Emoji: {:?}", sas.emoji()); println!("Does the emoji match: {:?}", sas.emoji());
let mut input = String::new(); let mut input = String::new();
io::stdin() io::stdin()
@ -16,14 +18,15 @@ async fn wait_for_confirmation(sas: Sas) {
sas.confirm().await.unwrap(); sas.confirm().await.unwrap();
if sas.is_done() { if sas.is_done() {
print_result(sas); print_result(&sas);
print_devices(sas.other_device().user_id(), &client).await;
} }
} }
_ => sas.cancel().await.unwrap(), _ => sas.cancel().await.unwrap(),
} }
} }
fn print_result(sas: Sas) { fn print_result(sas: &Sas) {
let device = sas.other_device(); let device = sas.other_device();
println!( println!(
@ -34,12 +37,28 @@ fn print_result(sas: Sas) {
); );
} }
async fn print_devices(user_id: &UserId, client: &Client) {
println!("Devices of user {}", user_id);
for device in client.get_user_devices(user_id).await.unwrap().devices() {
println!(
" {:<10} {:<30} {:<}",
device.device_id(),
device.display_name().as_deref().unwrap_or_default(),
device.is_trusted()
);
}
}
async fn login( async fn login(
homeserver_url: String, homeserver_url: String,
username: &str, username: &str,
password: &str, password: &str,
) -> Result<(), matrix_sdk::Error> { ) -> Result<(), matrix_sdk::Error> {
let client_config = ClientConfig::new(); let client_config = ClientConfig::new()
.disable_ssl_verification()
.proxy("http://localhost:8080")
.unwrap();
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new_with_config(homeserver_url, client_config).unwrap(); let client = Client::new_with_config(homeserver_url, client_config).unwrap();
@ -64,6 +83,12 @@ async fn login(
.get_verification(&e.content.transaction_id) .get_verification(&e.content.transaction_id)
.await .await
.expect("Sas object wasn't created"); .expect("Sas object wasn't created");
println!(
"Starting verification with {} {}",
&sas.other_device().user_id(),
&sas.other_device().device_id()
);
print_devices(&e.sender, &client).await;
sas.accept().await.unwrap(); sas.accept().await.unwrap();
} }
@ -73,7 +98,7 @@ async fn login(
.await .await
.expect("Sas object wasn't created"); .expect("Sas object wasn't created");
tokio::spawn(wait_for_confirmation(sas)); tokio::spawn(wait_for_confirmation((*client).clone(), sas));
} }
AnyToDeviceEvent::KeyVerificationMac(e) => { AnyToDeviceEvent::KeyVerificationMac(e) => {
@ -83,7 +108,8 @@ async fn login(
.expect("Sas object wasn't created"); .expect("Sas object wasn't created");
if sas.is_done() { if sas.is_done() {
print_result(sas); print_result(&sas);
print_devices(&e.sender, &client).await;
} }
} }

View File

@ -76,6 +76,11 @@ impl Device {
}) })
} }
/// Is the device trusted.
pub fn is_trusted(&self) -> bool {
self.inner.trust_state()
}
/// Set the trust state of the device to the given state. /// Set the trust state of the device to the given state.
/// ///
/// # Arguments /// # Arguments