From f7d99cc506d5704083ea56e1bf947b836e9ed4e7 Mon Sep 17 00:00:00 2001 From: Pratik Deshpande Date: Tue, 1 Oct 2024 10:51:45 +0530 Subject: [PATCH] Added a binding for custom login using JWT --- bindings/matrix-sdk-ffi/src/client.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index 2414d32c4..d5f0f0702 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -60,7 +60,7 @@ use ruma::{ OwnedServerName, RoomAliasId, RoomOrAliasId, ServerName, }; use serde::{Deserialize, Serialize}; -use serde_json::Value; +use serde_json::{json, Value}; use tokio::sync::broadcast::error::RecvError; use tracing::{debug, error}; use url::Url; @@ -295,6 +295,31 @@ impl Client { Ok(()) } + /// Login using JWT + /// This is an implementation of the custom_login https://docs.rs/matrix-sdk/latest/matrix_sdk/matrix_auth/struct.MatrixAuth.html#method.login_custom + /// For more information on logging in with JWT: https://element-hq.github.io/synapse/latest/jwt.html + pub async fn custom_login_with_jwt( + &self, + jwt: String, + initial_device_name: Option, + device_id: Option, + ) -> Result<(), ClientError> { + let data = json!({ "token": jwt }).as_object().unwrap().clone(); + + let mut builder = self.inner.matrix_auth().login_custom("org.matrix.login.jwt", data)?; + + if let Some(initial_device_name) = initial_device_name.as_ref() { + builder = builder.initial_device_display_name(initial_device_name); + } + + if let Some(device_id) = device_id.as_ref() { + builder = builder.device_id(device_id); + } + + builder.send().await?; + Ok(()) + } + /// Login using an email and password. pub async fn login_with_email( &self,