Skip to main content

example_cross_signing_bootstrap/
main.rs

1use std::{
2    env, io,
3    process::exit,
4    sync::atomic::{AtomicBool, Ordering},
5};
6
7use anyhow::Result;
8use matrix_sdk::{
9    Client, LoopCtrl,
10    config::SyncSettings,
11    encryption::CrossSigningResetAuthType,
12    ruma::{OwnedUserId, api::client::uiaa},
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::OAuth(oauth) => {
31                println!(
32                    "To reset your end-to-end encryption cross-signing identity, \
33                    you first need to approve it at {}",
34                    oauth.approval_url
35                );
36
37                let mut oauth_data = uiaa::OAuth::new();
38                oauth_data.session = oauth.session.clone();
39                handle.auth(Some(uiaa::AuthData::OAuth(oauth_data))).await?;
40            }
41        }
42    }
43
44    Ok(())
45}
46
47async fn login(homeserver_url: String, username: &str, password: &str) -> matrix_sdk::Result<()> {
48    let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
49    let client = Client::new(homeserver_url).await.unwrap();
50
51    let response = client
52        .matrix_auth()
53        .login_username(username, password)
54        .initial_device_display_name("rust-sdk")
55        .await?;
56
57    let user_id = &response.user_id;
58    let client_ref = &client;
59    let asked = AtomicBool::new(false);
60    let asked_ref = &asked;
61
62    client
63        .sync_with_callback(SyncSettings::new(), |_| async move {
64            let asked = asked_ref;
65            let client = &client_ref;
66            let user_id = &user_id;
67
68            // Wait for sync to be done then ask the user to bootstrap.
69            if !asked.load(Ordering::SeqCst) {
70                tokio::spawn(bootstrap((*client).clone(), (*user_id).clone(), password.to_owned()));
71            }
72
73            asked.store(true, Ordering::SeqCst);
74            LoopCtrl::Continue
75        })
76        .await?;
77
78    Ok(())
79}
80
81#[tokio::main]
82async fn main() -> Result<()> {
83    tracing_subscriber::fmt::init();
84
85    let (Some(homeserver_url), Some(username), Some(password)) =
86        (env::args().nth(1), env::args().nth(2), env::args().nth(3))
87    else {
88        eprintln!("Usage: {} <homeserver_url> <username> <password>", env::args().next().unwrap());
89        exit(1)
90    };
91
92    login(homeserver_url, &username, &password).await?;
93
94    Ok(())
95}