From 93d879f356a26b52975a86214e9d49b3b60d6972 Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 19 Jul 2022 12:22:14 +0100 Subject: [PATCH] Add homeserver_details property. --- bindings/matrix-sdk-ffi/src/api.udl | 4 +++- .../src/authentication_service.rs | 23 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/api.udl b/bindings/matrix-sdk-ffi/src/api.udl index 5ccb81863..2c705695f 100644 --- a/bindings/matrix-sdk-ffi/src/api.udl +++ b/bindings/matrix-sdk-ffi/src/api.udl @@ -169,8 +169,10 @@ interface HomeserverLoginDetails { interface AuthenticationService { constructor(string base_path); + HomeserverLoginDetails? homeserver_details(); + [Throws=AuthenticationError] - HomeserverLoginDetails configure_homeserver(string server_name); + void configure_homeserver(string server_name); [Throws=AuthenticationError] Client login(string username, string password); diff --git a/bindings/matrix-sdk-ffi/src/authentication_service.rs b/bindings/matrix-sdk-ffi/src/authentication_service.rs index da52f9fc1..afda831d3 100644 --- a/bindings/matrix-sdk-ffi/src/authentication_service.rs +++ b/bindings/matrix-sdk-ffi/src/authentication_service.rs @@ -7,6 +7,7 @@ use super::{client::Client, client_builder::ClientBuilder}; pub struct AuthenticationService { base_path: String, client: RwLock>>, + homeserver_details: RwLock>>, } #[derive(Debug, thiserror::Error)] @@ -50,15 +51,20 @@ impl HomeserverLoginDetails { impl AuthenticationService { /// Creates a new service to authenticate a user with. pub fn new(base_path: String) -> Self { - AuthenticationService { base_path, client: RwLock::new(None) } + AuthenticationService { + base_path, + client: RwLock::new(None), + homeserver_details: RwLock::new(None), + } + } + + pub fn homeserver_details(&self) -> Option> { + self.homeserver_details.read().clone() } /// Updates the service to authenticate with the homeserver for the /// specified address. - pub fn configure_homeserver( - &self, - server_name: String, - ) -> Result, AuthenticationError> { + pub fn configure_homeserver(&self, server_name: String) -> Result<(), AuthenticationError> { // Construct a username as the builder currently requires one. let username = format!("@auth:{}", server_name); let client = Arc::new(ClientBuilder::new()) @@ -67,10 +73,12 @@ impl AuthenticationService { .build() .map_err(AuthenticationError::from)?; - let details = self.details_from_client(&client)?; + let details = Arc::new(self.details_from_client(&client)?); *self.client.write() = Some(client); - Ok(Arc::new(details)) + *self.homeserver_details.write() = Some(details); + + Ok(()) } /// Performs a password login using the current homeserver. @@ -100,6 +108,7 @@ impl AuthenticationService { } } + /// Get the homeserver login details from a client. fn details_from_client( &self, client: &Arc,