From 77f7dbbbc805e69c13e64dab1e93e01bbb12c7cb Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 19 Jul 2022 16:16:03 +0100 Subject: [PATCH] Support server URLs. Join homeserver details futures. --- .../src/authentication_service.rs | 50 +++++++++++-------- bindings/matrix-sdk-ffi/src/client.rs | 27 +++++----- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/authentication_service.rs b/bindings/matrix-sdk-ffi/src/authentication_service.rs index afda831d3..0c3df8f12 100644 --- a/bindings/matrix-sdk-ffi/src/authentication_service.rs +++ b/bindings/matrix-sdk-ffi/src/authentication_service.rs @@ -1,8 +1,9 @@ use std::sync::Arc; +use futures_util::future::join3; use parking_lot::RwLock; -use super::{client::Client, client_builder::ClientBuilder}; +use super::{client::Client, client_builder::ClientBuilder, RUNTIME}; pub struct AuthenticationService { base_path: String, @@ -67,18 +68,24 @@ impl AuthenticationService { 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()) - .base_path(self.base_path.clone()) - .username(username) - .build() - .map_err(AuthenticationError::from)?; - let details = Arc::new(self.details_from_client(&client)?); + let mut builder = + Arc::new(ClientBuilder::new()).base_path(self.base_path.clone()).username(username); - *self.client.write() = Some(client); - *self.homeserver_details.write() = Some(details); + if server_name.starts_with("http://") || server_name.starts_with("https://") { + builder = builder.homeserver_url(server_name) + } - Ok(()) + let client = builder.build().map_err(AuthenticationError::from)?; + + RUNTIME.block_on(async move { + let details = Arc::new(self.details_from_client(&client).await?); + + *self.client.write() = Some(client); + *self.homeserver_details.write() = Some(details); + + Ok(()) + }) } /// Performs a password login using the current homeserver. @@ -109,18 +116,21 @@ impl AuthenticationService { } /// Get the homeserver login details from a client. - fn details_from_client( + async fn details_from_client( &self, client: &Arc, ) -> Result { - let homeserver = client.homeserver(); - let authentication_issuer = client.authentication_issuer(); - let supports_password_login = - client.supports_password_login().map_err(AuthenticationError::from)?; - Ok(HomeserverLoginDetails { - url: homeserver, - authentication_issuer, - supports_password_login, - }) + let login_details = join3( + client.async_homeserver(), + client.authentication_issuer(), + client.supports_password_login(), + ) + .await; + + let url = login_details.0; + let authentication_issuer = login_details.1; + let supports_password_login = login_details.2.map_err(AuthenticationError::from)?; + + Ok(HomeserverLoginDetails { url, authentication_issuer, supports_password_login }) } } diff --git a/bindings/matrix-sdk-ffi/src/client.rs b/bindings/matrix-sdk-ffi/src/client.rs index 0dda08d72..35538cb84 100644 --- a/bindings/matrix-sdk-ffi/src/client.rs +++ b/bindings/matrix-sdk-ffi/src/client.rs @@ -74,26 +74,27 @@ impl Client { /// The homeserver this client is configured to use. pub fn homeserver(&self) -> String { - RUNTIME.block_on(async move { self.client.homeserver().await.to_string() }) + RUNTIME.block_on(async move { self.async_homeserver().await }) + } + + pub async fn async_homeserver(&self) -> String { + self.client.homeserver().await.to_string() } /// The OIDC Provider that is trusted by the homeserver. `None` when /// not configured. - pub fn authentication_issuer(&self) -> Option { - RUNTIME.block_on(async move { - self.client.authentication_issuer().await.map(|server| server.to_string()) - }) + pub async fn authentication_issuer(&self) -> Option { + self.client.authentication_issuer().await.map(|server| server.to_string()) } /// Whether or not the client's homeserver supports the password login flow. - pub fn supports_password_login(&self) -> anyhow::Result { - RUNTIME.block_on(async move { - let login_types = self.client.get_login_types().await?; - let supports_password = login_types.flows.iter().any(|login_type| { - matches!(login_type, get_login_types::v3::LoginType::Password(_)) - }); - Ok(supports_password) - }) + pub async fn supports_password_login(&self) -> anyhow::Result { + let login_types = self.client.get_login_types().await?; + let supports_password = login_types + .flows + .iter() + .any(|login_type| matches!(login_type, get_login_types::v3::LoginType::Password(_))); + Ok(supports_password) } pub fn start_sync(&self) {