1use std::{
2 env, fmt,
3 io::{self, Write},
4 process::exit,
5};
67use anyhow::anyhow;
8use matrix_sdk::{
9 config::SyncSettings,
10 ruma::{
11 api::client::session::get_login_types::v3::{IdentityProvider, LoginType},
12 events::room::message::{MessageType, OriginalSyncRoomMessageEvent},
13 },
14 Client, Room, RoomState,
15};
16use url::Url;
1718/// The initial device name when logging in with a device for the first time.
19const INITIAL_DEVICE_DISPLAY_NAME: &str = "login client";
2021/// A simple program that adapts to the different login methods offered by a
22/// Matrix homeserver.
23///
24/// Homeservers usually offer to login either via password, Single Sign-On (SSO)
25/// or both.
26#[tokio::main]
27async fn main() -> anyhow::Result<()> {
28 tracing_subscriber::fmt::init();
2930let Some(homeserver_url) = env::args().nth(1) else {
31eprintln!("Usage: {} <homeserver_url>", env::args().next().unwrap());
32 exit(1)
33 };
3435 login_and_sync(homeserver_url).await?;
3637Ok(())
38}
3940/// Log in to the given homeserver and sync.
41async fn login_and_sync(homeserver_url: String) -> anyhow::Result<()> {
42let homeserver_url = Url::parse(&homeserver_url)?;
43let client = Client::new(homeserver_url).await?;
4445// First, let's figure out what login types are supported by the homeserver.
46let mut choices = Vec::new();
47let login_types = client.matrix_auth().get_login_types().await?.flows;
4849for login_type in login_types {
50match login_type {
51 LoginType::Password(_) => {
52 choices.push(LoginChoice::Password)
53 }
54 LoginType::Sso(sso) => {
55if sso.identity_providers.is_empty() {
56 choices.push(LoginChoice::Sso)
57 } else {
58 choices.extend(sso.identity_providers.into_iter().map(LoginChoice::SsoIdp))
59 }
60 }
61// This is used for SSO, so it's not a separate choice.
62LoginType::Token(_) |
63// This is only for application services, ignore it here.
64LoginType::ApplicationService(_) => {},
65// We don't support unknown login types.
66_ => {},
67 }
68 }
6970match choices.len() {
710 => return Err(anyhow!("Homeserver login types incompatible with this client")),
721 => choices[0].login(&client).await?,
73_ => offer_choices_and_login(&client, choices).await?,
74 }
7576// Now that we are logged in, we can sync and listen to new messages.
77client.add_event_handler(on_room_message);
78// This will sync until an error happens or the program is killed.
79client.sync(SyncSettings::new()).await?;
8081Ok(())
82}
8384#[derive(Debug)]
85enum LoginChoice {
86/// Login with username and password.
87Password,
8889/// Login with SSO.
90Sso,
9192/// Login with a specific SSO identity provider.
93SsoIdp(IdentityProvider),
94}
9596impl LoginChoice {
97/// Login with this login choice.
98async fn login(&self, client: &Client) -> anyhow::Result<()> {
99match self {
100 LoginChoice::Password => login_with_password(client).await,
101 LoginChoice::Sso => login_with_sso(client, None).await,
102 LoginChoice::SsoIdp(idp) => login_with_sso(client, Some(idp)).await,
103 }
104 }
105}
106107impl fmt::Display for LoginChoice {
108fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109match self {
110 LoginChoice::Password => write!(f, "Username and password"),
111 LoginChoice::Sso => write!(f, "SSO"),
112 LoginChoice::SsoIdp(idp) => write!(f, "SSO via {}", idp.name),
113 }
114 }
115}
116117/// Offer the given choices to the user and login with the selected option.
118async fn offer_choices_and_login(client: &Client, choices: Vec<LoginChoice>) -> anyhow::Result<()> {
119println!("Several options are available to login with this homeserver:\n");
120121let choice = loop {
122for (idx, login_choice) in choices.iter().enumerate() {
123println!("{idx}) {login_choice}");
124 }
125126print!("\nEnter your choice: ");
127 io::stdout().flush().expect("Unable to write to stdout");
128let mut choice_str = String::new();
129 io::stdin().read_line(&mut choice_str).expect("Unable to read user input");
130131match choice_str.trim().parse::<usize>() {
132Ok(choice) => {
133if choice >= choices.len() {
134eprintln!("This is not a valid choice");
135 } else {
136break choice;
137 }
138 }
139Err(_) => eprintln!("This is not a valid choice. Try again.\n"),
140 };
141 };
142143 choices[choice].login(client).await?;
144145Ok(())
146}
147148/// Login with a username and password.
149async fn login_with_password(client: &Client) -> anyhow::Result<()> {
150println!("Logging in with username and password…");
151152loop {
153print!("\nUsername: ");
154 io::stdout().flush().expect("Unable to write to stdout");
155let mut username = String::new();
156 io::stdin().read_line(&mut username).expect("Unable to read user input");
157 username = username.trim().to_owned();
158159print!("Password: ");
160 io::stdout().flush().expect("Unable to write to stdout");
161let mut password = String::new();
162 io::stdin().read_line(&mut password).expect("Unable to read user input");
163 password = password.trim().to_owned();
164165match client
166 .matrix_auth()
167 .login_username(&username, &password)
168 .initial_device_display_name(INITIAL_DEVICE_DISPLAY_NAME)
169 .await
170{
171Ok(_) => {
172println!("Logged in as {username}");
173break;
174 }
175Err(error) => {
176println!("Error logging in: {error}");
177println!("Please try again\n");
178 }
179 }
180 }
181182Ok(())
183}
184185/// Login with SSO.
186async fn login_with_sso(client: &Client, idp: Option<&IdentityProvider>) -> anyhow::Result<()> {
187println!("Logging in with SSO…");
188189let mut login_builder = client.matrix_auth().login_sso(|url| async move {
190// Usually we would want to use a library to open the URL in the browser, but
191 // let's keep it simple.
192println!("\nOpen this URL in your browser: {url}\n");
193println!("Waiting for login token…");
194Ok(())
195 });
196197if let Some(idp) = idp {
198 login_builder = login_builder.identity_provider_id(&idp.id);
199 }
200201 login_builder.await?;
202203println!("Logged in as {}", client.user_id().unwrap());
204205Ok(())
206}
207208/// Handle room messages by logging them.
209async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
210// We only want to listen to joined rooms.
211if room.state() != RoomState::Joined {
212return;
213 }
214215// We only want to log text messages.
216let MessageType::Text(msgtype) = &event.content.msgtype else {
217return;
218 };
219220let member = room
221 .get_member(&event.sender)
222 .await
223.expect("Couldn't get the room member")
224 .expect("The room member doesn't exist");
225let name = member.name();
226227println!("{name}: {}", msgtype.body);
228}