example_cross_signing_bootstrap/
main.rs1use std::{
2 env, io,
3 process::exit,
4 sync::atomic::{AtomicBool, Ordering},
5};
6
7use anyhow::Result;
8use matrix_sdk::{
9 config::SyncSettings,
10 encryption::CrossSigningResetAuthType,
11 ruma::{api::client::uiaa, OwnedUserId},
12 Client, LoopCtrl,
13};
14use url::Url;
15
16async fn bootstrap(client: Client, user_id: OwnedUserId, password: String) -> Result<()> {
17 println!("Bootstrapping a new cross signing identity, press enter to continue.");
18
19 let mut input = String::new();
20
21 io::stdin().read_line(&mut input).expect("error: unable to read user input");
22
23 if let Some(handle) = client.encryption().reset_cross_signing().await? {
24 match handle.auth_type() {
25 CrossSigningResetAuthType::Uiaa(uiaa) => {
26 let mut password = uiaa::Password::new(user_id.into(), password);
27 password.session = uiaa.session.clone();
28 handle.auth(Some(uiaa::AuthData::Password(password))).await?;
29 }
30 CrossSigningResetAuthType::Oidc(oidc) => {
31 println!(
32 "To reset your end-to-end encryption cross-signing identity, \
33 you first need to approve it at {}",
34 oidc.approval_url
35 );
36 handle.auth(None).await?;
37 }
38 }
39 }
40
41 Ok(())
42}
43
44async fn login(homeserver_url: String, username: &str, password: &str) -> matrix_sdk::Result<()> {
45 let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
46 let client = Client::new(homeserver_url).await.unwrap();
47
48 let response = client
49 .matrix_auth()
50 .login_username(username, password)
51 .initial_device_display_name("rust-sdk")
52 .await?;
53
54 let user_id = &response.user_id;
55 let client_ref = &client;
56 let asked = AtomicBool::new(false);
57 let asked_ref = &asked;
58
59 client
60 .sync_with_callback(SyncSettings::new(), |_| async move {
61 let asked = asked_ref;
62 let client = &client_ref;
63 let user_id = &user_id;
64
65 if !asked.load(Ordering::SeqCst) {
67 tokio::spawn(bootstrap((*client).clone(), (*user_id).clone(), password.to_owned()));
68 }
69
70 asked.store(true, Ordering::SeqCst);
71 LoopCtrl::Continue
72 })
73 .await?;
74
75 Ok(())
76}
77
78#[tokio::main]
79async fn main() -> Result<()> {
80 tracing_subscriber::fmt::init();
81
82 let (homeserver_url, username, password) =
83 match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) {
84 (Some(a), Some(b), Some(c)) => (a, b, c),
85 _ => {
86 eprintln!(
87 "Usage: {} <homeserver_url> <username> <password>",
88 env::args().next().unwrap()
89 );
90 exit(1)
91 }
92 };
93
94 login(homeserver_url, &username, &password).await?;
95
96 Ok(())
97}