Skip to content

Commit 1be703a

Browse files
committed
Add SSO login support
Changes: 1. Updated Cargo.toml Added "sso-login" feature to the matrix-sdk dependency 2. Updated connection.rs Added a local HTTP server to automatically capture the SSO callback Added SsoLoginUrl(String) variant to ClientMessage enum Added LoginType import from ruma::api::client::session::get_login_types::v3 Updated response_receiver to handle SsoLoginUrl messages Modified sync_loop to: Query available login flows using get_login_types() Check if SSO is available Generate and display SSO URL when SSO is available and credentials are empty Fall back to password login when credentials are provided Added login_with_token() method to complete SSO login with a token 3. Updated server.rs Added receive_sso_url() method to display SSO URL and optionally open browser Added complete_sso_login() method to complete SSO login with a token Added use_sso parameter (defaults to False) 4. Updated commands/matrix.rs Added sso-complete command to the command structure Added handler method sso_complete_command() Added subcommand to argparse How to use: * Connect with empty username/password: /matrix connect myserver The plugin will: Check if SSO is available on matrix.org Display the SSO login URL Optionally open it in your browser * After completing SSO in the browser: Copy the loginToken from the callback URL (the parameter after loginToken=) * Run: /matrix sso-complete m <login-token> The plugin will complete the login and you'll be connected. The implementation follows the same pattern as the matrix-rust-sdk examples and should work with matrix.org and other homeservers that support SSO. Assisted-by: Cursor AI agent (Claude 4.5 Opus) Signed-off-by: Bogdan Dobrelya <bogdando@yahoo.com>
1 parent 4cc5777 commit 1be703a

5 files changed

Lines changed: 527 additions & 38 deletions

File tree

Cargo.lock

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ features = ["async", "config_macro"]
3838
version = "0.14.0"
3939
# git = "https://github.com/matrix-org/matrix-rust-sdk.git"
4040
# rev = "92192c549b3f53889c23954386743867a73b31b1"
41-
features = ["markdown", "socks", "qrcode"]
41+
features = ["markdown", "socks", "qrcode", "sso-login"]
4242

4343
[profile.dev.package]
4444
sha2 = { opt-level = 2 }

src/commands/matrix.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ impl MatrixCommand {
3636
.add_argument("keys import|export <file> <passphrase>")
3737
.add_argument("disconnect <server-name>")
3838
.add_argument("reconnect <server-name>")
39+
.add_argument("sso-complete <server-name> <login-token>")
3940
.add_argument("help <matrix-command> [<matrix-subcommand>]")
4041
.arguments_description(format!(
4142
" server: List, add, or remove Matrix servers.
4243
connect: Connect to Matrix servers.
4344
disconnect: Disconnect from one or all Matrix servers.
4445
reconnect: Reconnect to server(s).
46+
sso-complete: Complete SSO login with a token from the browser.
4547
devices: {}
4648
keys: {}
4749
verification: {}
@@ -61,8 +63,9 @@ Use /matrix [command] help to find out more.\n",
6163
.add_completion("connect %(matrix_servers)")
6264
.add_completion("disconnect %(matrix_servers)")
6365
.add_completion("reconnect %(matrix_servers)")
66+
.add_completion("sso-complete %(matrix_servers)")
6467
.add_completion(
65-
"help server|connect|disconnect|reconnect|keys|devices",
68+
"help server|connect|disconnect|reconnect|sso-complete|keys|devices",
6669
);
6770

6871
Command::new(
@@ -219,10 +222,31 @@ Use /matrix [command] help to find out more.\n",
219222
}
220223
}
221224

225+
fn sso_complete_command(&self, args: &ArgMatches) {
226+
let server_name = args
227+
.value_of("name")
228+
.expect("Server name not set but was required");
229+
let login_token = args
230+
.value_of("token")
231+
.expect("Login token not set but was required")
232+
.to_string();
233+
234+
if let Some(s) = self.servers.get(server_name) {
235+
let server = s.clone();
236+
Weechat::spawn(async move {
237+
server.complete_sso_login(login_token).await;
238+
})
239+
.detach();
240+
} else {
241+
self.server_not_found(server_name)
242+
}
243+
}
244+
222245
fn run(&self, buffer: &Buffer, args: &ArgMatches) {
223246
match args.subcommand() {
224247
("connect", Some(subargs)) => self.connect_command(subargs),
225248
("disconnect", Some(subargs)) => self.disconnect_command(subargs),
249+
("sso-complete", Some(subargs)) => self.sso_complete_command(subargs),
226250
("server", Some(subargs)) => self.server_command(subargs),
227251
("devices", Some(subargs)) => {
228252
DevicesCommand::run(buffer, &self.servers, subargs)
@@ -323,6 +347,22 @@ impl CommandCallback for MatrixCommand {
323347
.value_name("server-name")
324348
.required(true),
325349
),
350+
)
351+
.subcommand(
352+
SubCommand::with_name("sso-complete")
353+
.about("Complete SSO login with a token from the browser.")
354+
.arg(
355+
Arg::with_name("name")
356+
.value_name("server-name")
357+
.required(true)
358+
.index(1),
359+
)
360+
.arg(
361+
Arg::with_name("token")
362+
.value_name("login-token")
363+
.required(true)
364+
.index(2),
365+
),
326366
);
327367

328368
parse_and_run(argparse, arguments, |args| self.run(buffer, args));

0 commit comments

Comments
 (0)