example_cross_signing_bootstrap/
main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::{
    env, io,
    process::exit,
    sync::atomic::{AtomicBool, Ordering},
};

use anyhow::Result;
use matrix_sdk::{
    config::SyncSettings,
    encryption::CrossSigningResetAuthType,
    ruma::{api::client::uiaa, OwnedUserId},
    Client, LoopCtrl,
};
use url::Url;

async fn bootstrap(client: Client, user_id: OwnedUserId, password: String) -> Result<()> {
    println!("Bootstrapping a new cross signing identity, press enter to continue.");

    let mut input = String::new();

    io::stdin().read_line(&mut input).expect("error: unable to read user input");

    if let Some(handle) = client.encryption().reset_cross_signing().await? {
        match handle.auth_type() {
            CrossSigningResetAuthType::Uiaa(uiaa) => {
                let mut password = uiaa::Password::new(user_id.into(), password);
                password.session = uiaa.session.clone();
                handle.auth(Some(uiaa::AuthData::Password(password))).await?;
            }
            CrossSigningResetAuthType::Oidc(oidc) => {
                println!(
                    "To reset your end-to-end encryption cross-signing identity, \
                    you first need to approve it at {}",
                    oidc.approval_url
                );
                handle.auth(None).await?;
            }
        }
    }

    Ok(())
}

async fn login(homeserver_url: String, username: &str, password: &str) -> matrix_sdk::Result<()> {
    let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
    let client = Client::new(homeserver_url).await.unwrap();

    let response = client
        .matrix_auth()
        .login_username(username, password)
        .initial_device_display_name("rust-sdk")
        .await?;

    let user_id = &response.user_id;
    let client_ref = &client;
    let asked = AtomicBool::new(false);
    let asked_ref = &asked;

    client
        .sync_with_callback(SyncSettings::new(), |_| async move {
            let asked = asked_ref;
            let client = &client_ref;
            let user_id = &user_id;

            // Wait for sync to be done then ask the user to bootstrap.
            if !asked.load(Ordering::SeqCst) {
                tokio::spawn(bootstrap((*client).clone(), (*user_id).clone(), password.to_owned()));
            }

            asked.store(true, Ordering::SeqCst);
            LoopCtrl::Continue
        })
        .await?;

    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt::init();

    let (homeserver_url, username, password) =
        match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) {
            (Some(a), Some(b), Some(c)) => (a, b, c),
            _ => {
                eprintln!(
                    "Usage: {} <homeserver_url> <username> <password>",
                    env::args().next().unwrap()
                );
                exit(1)
            }
        };

    login(homeserver_url, &username, &password).await?;

    Ok(())
}