Commit 8b9feba502

8b9feba50243ed4cca4e11c7ccf4c5c58b0cdab2

parent: a8d1ab80cd

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-22 03:00 UTC
committer: <strk@kbt.io>

feat: add manual SSO token completion

Layout: unified · split

src/commands/matrix.rs +37 −1
@@ -42,6 +42,7 @@ impl MatrixCommand {
42 .add_argument("media download <mxc-uri> [file]") 42 .add_argument("media download <mxc-uri> [file]")
43 .add_argument("disconnect <server-name>") 43 .add_argument("disconnect <server-name>")
44 .add_argument("reconnect <server-name>") 44 .add_argument("reconnect <server-name>")
45 .add_argument("sso-complete <server-name> <login-token>")
45 .add_argument("read") 46 .add_argument("read")
46 .add_argument("help <matrix-command> [<matrix-subcommand>]") 47 .add_argument("help <matrix-command> [<matrix-subcommand>]")
47 .arguments_description(format!( 48 .arguments_description(format!(
@@ -50,6 +51,7 @@ impl MatrixCommand {
50 disconnect: Disconnect from one or all Matrix servers. 51 disconnect: Disconnect from one or all Matrix servers.
51 reconnect: Reconnect to server(s). 52 reconnect: Reconnect to server(s).
52 join: Join a Matrix room by ID or alias. 53 join: Join a Matrix room by ID or alias.
54sso-complete: Finish SSO login with a copied loginToken.
53 read: Mark the current room as read. 55 read: Mark the current room as read.
54 devices: {} 56 devices: {}
55 keys: {} 57 keys: {}
@@ -73,8 +75,9 @@ Use /matrix [command] help to find out more.\n",
73 .add_completion("connect %(matrix_servers)") 75 .add_completion("connect %(matrix_servers)")
74 .add_completion("disconnect %(matrix_servers)") 76 .add_completion("disconnect %(matrix_servers)")
75 .add_completion("reconnect %(matrix_servers)") 77 .add_completion("reconnect %(matrix_servers)")
78 .add_completion("sso-complete %(matrix_servers)")
76 .add_completion( 79 .add_completion(
77 "help server|connect|disconnect|reconnect|join|read|keys|devices|media", 80 "help server|connect|disconnect|reconnect|join|sso-complete|read|keys|devices|media",
78 ); 81 );
79 82
80 Command::new( 83 Command::new(
@@ -231,6 +234,22 @@ Use /matrix [command] help to find out more.\n",
231 } 234 }
232 } 235 }
233 236
237 fn sso_complete_command(&self, args: &ArgMatches) {
238 let server_name = args
239 .value_of("name")
240 .expect("Server name not set but was required");
241 let login_token = args
242 .value_of("login-token")
243 .expect("Login token not set but was required")
244 .to_owned();
245
246 if let Some(s) = self.servers.get(server_name) {
247 s.complete_sso_login(login_token);
248 } else {
249 self.server_not_found(server_name)
250 }
251 }
252
234 fn join_command(&self, buffer: &Buffer, args: &ArgMatches) { 253 fn join_command(&self, buffer: &Buffer, args: &ArgMatches) {
235 let room_id_or_alias = args 254 let room_id_or_alias = args
236 .value_of("room") 255 .value_of("room")
@@ -251,6 +270,9 @@ Use /matrix [command] help to find out more.\n",
251 match args.subcommand() { 270 match args.subcommand() {
252 ("connect", Some(subargs)) => self.connect_command(subargs), 271 ("connect", Some(subargs)) => self.connect_command(subargs),
253 ("disconnect", Some(subargs)) => self.disconnect_command(subargs), 272 ("disconnect", Some(subargs)) => self.disconnect_command(subargs),
273 ("sso-complete", Some(subargs)) => {
274 self.sso_complete_command(subargs)
275 }
254 ("join", Some(subargs)) => self.join_command(buffer, subargs), 276 ("join", Some(subargs)) => self.join_command(buffer, subargs),
255 ("server", Some(subargs)) => self.server_command(subargs), 277 ("server", Some(subargs)) => self.server_command(subargs),
256 ("devices", Some(subargs)) => { 278 ("devices", Some(subargs)) => {
@@ -368,6 +390,20 @@ impl CommandCallback for MatrixCommand {
368 .required(true), 390 .required(true),
369 ), 391 ),
370 ) 392 )
393 .subcommand(
394 SubCommand::with_name("sso-complete")
395 .about("Finish SSO login with a copied loginToken")
396 .arg(
397 Arg::with_name("name")
398 .value_name("server-name")
399 .required(true),
400 )
401 .arg(
402 Arg::with_name("login-token")
403 .value_name("login-token")
404 .required(true),
405 ),
406 )
371 .subcommand( 407 .subcommand(
372 SubCommand::with_name("join") 408 SubCommand::with_name("join")
373 .about("Join a Matrix room by ID or alias") 409 .about("Join a Matrix room by ID or alias")
src/connection.rs +1 −1
@@ -293,7 +293,7 @@ impl Connection {
293 .await 293 .await
294 } 294 }
295 295
296 fn save_device_id( 296 pub(crate) fn save_device_id(
297 user_name: &str, 297 user_name: &str,
298 mut server_path: PathBuf, 298 mut server_path: PathBuf,
299 response: &LoginResponse, 299 response: &LoginResponse,
src/server.rs +78
@@ -327,6 +327,84 @@ impl MatrixServer {
327 Ok(()) 327 Ok(())
328 } 328 }
329 329
330 pub fn complete_sso_login(&self, login_token: String) {
331 if self
332 .get_client()
333 .map(|client| client.matrix_auth().logged_in())
334 .unwrap_or(false)
335 {
336 self.print_error(&format!(
337 "Already connected to {}{}{}",
338 Weechat::color("chat_server"),
339 self.name(),
340 Weechat::color("reset")
341 ));
342 return;
343 }
344
345 if self.connected() {
346 self.connection.borrow_mut().take();
347 }
348
349 let client = match self.get_or_create_client() {
350 Ok(client) => client,
351 Err(e) => {
352 self.print_error(&format!(
353 "Failed to create Matrix client: {:?}",
354 e
355 ));
356 return;
357 }
358 };
359
360 let server_name = self.name().to_owned();
361 let server_path = self.get_server_path();
362
363 let response = self.servers.runtime().block_on(async {
364 client
365 .matrix_auth()
366 .login_token(&login_token)
367 .initial_device_display_name("WeeChat-Matrix-rs")
368 .send()
369 .await
370 });
371
372 match response {
373 Ok(response) => {
374 if let Err(e) = Connection::save_device_id(
375 &server_name,
376 server_path,
377 &response,
378 ) {
379 self.print_error(&format!(
380 "Error while writing the device id for server {}{}{}: {:?}",
381 Weechat::color("chat_server"),
382 self.name(),
383 Weechat::color("reset"),
384 e
385 ));
386 return;
387 }
388
389 self.receive_login(response);
390 let connection = Connection::new(self, &client);
391 self.set_connection(connection);
392 self.print_network(&format!(
393 "Completed SSO login for {}{}{}",
394 Weechat::color("chat_server"),
395 self.name(),
396 Weechat::color("reset")
397 ));
398 }
399 Err(e) => {
400 self.print_error(&format!(
401 "Failed to complete SSO login: {:?}",
402 e
403 ));
404 }
405 }
406 }
407
330 fn inner(&self) -> Rc<InnerServer> { 408 fn inner(&self) -> Rc<InnerServer> {
331 self.inner.clone() 409 self.inner.clone()
332 } 410 }