From 4abe876c9dc7051825df9a0ea04f7555b8fbb0e7 Mon Sep 17 00:00:00 2001 From: Doug Date: Fri, 21 Jun 2024 10:49:26 +0100 Subject: [PATCH] ffi: Tidy up authentication.rs file. (Nothing changed, just moving things around) --- bindings/matrix-sdk-ffi/src/authentication.rs | 150 +++++++++--------- 1 file changed, 76 insertions(+), 74 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/authentication.rs b/bindings/matrix-sdk-ffi/src/authentication.rs index 91986c55130..3674ea5a00d 100644 --- a/bindings/matrix-sdk-ffi/src/authentication.rs +++ b/bindings/matrix-sdk-ffi/src/authentication.rs @@ -15,54 +15,35 @@ use matrix_sdk::{ }; use url::Url; -#[derive(Debug, thiserror::Error, uniffi::Error)] -#[uniffi(flat_error)] -pub enum OidcError { - #[error( - "The homeserver doesn't provide an authentication issuer in its well-known configuration." - )] - NotSupported, - #[error("Unable to use OIDC as the supplied client metadata is invalid.")] - MetadataInvalid, - #[error("Failed to use the supplied registrations file path.")] - RegistrationsPathInvalid, - #[error("The supplied callback URL used to complete OIDC is invalid.")] - CallbackUrlInvalid, - #[error("The OIDC login was cancelled by the user.")] - Cancelled, - - #[error("An error occurred: {message}")] - Generic { message: String }, +#[derive(uniffi::Object)] +pub struct HomeserverLoginDetails { + pub(crate) url: String, + pub(crate) sliding_sync_proxy: Option, + pub(crate) supports_oidc_login: bool, + pub(crate) supports_password_login: bool, } -impl From for OidcError { - fn from(e: OidcRegistrationsError) -> OidcError { - match e { - OidcRegistrationsError::InvalidFilePath => OidcError::RegistrationsPathInvalid, - _ => OidcError::Generic { message: e.to_string() }, - } +#[uniffi::export] +impl HomeserverLoginDetails { + /// The URL of the currently configured homeserver. + pub fn url(&self) -> String { + self.url.clone() } -} -impl From for OidcError { - fn from(e: SdkOidcError) -> OidcError { - match e { - SdkOidcError::MissingAuthenticationIssuer => OidcError::NotSupported, - SdkOidcError::MissingRedirectUri => OidcError::MetadataInvalid, - SdkOidcError::InvalidCallbackUrl => OidcError::CallbackUrlInvalid, - SdkOidcError::InvalidState => OidcError::CallbackUrlInvalid, - SdkOidcError::CancelledAuthorization => OidcError::Cancelled, - _ => OidcError::Generic { message: e.to_string() }, - } + /// The URL of the discovered or manually set sliding sync proxy, + /// if any. + pub fn sliding_sync_proxy(&self) -> Option { + self.sliding_sync_proxy.clone() } -} -impl From for OidcError { - fn from(e: Error) -> OidcError { - match e { - Error::Oidc(e) => e.into(), - _ => OidcError::Generic { message: e.to_string() }, - } + /// Whether the current homeserver supports login using OIDC. + pub fn supports_oidc_login(&self) -> bool { + self.supports_oidc_login + } + + /// Whether the current homeserver supports the password login flow. + pub fn supports_password_login(&self) -> bool { + self.supports_password_login } } @@ -95,38 +76,6 @@ pub struct OidcConfiguration { pub dynamic_registrations_file: String, } -#[derive(uniffi::Object)] -pub struct HomeserverLoginDetails { - pub(crate) url: String, - pub(crate) sliding_sync_proxy: Option, - pub(crate) supports_oidc_login: bool, - pub(crate) supports_password_login: bool, -} - -#[uniffi::export] -impl HomeserverLoginDetails { - /// The URL of the currently configured homeserver. - pub fn url(&self) -> String { - self.url.clone() - } - - /// The URL of the discovered or manually set sliding sync proxy, - /// if any. - pub fn sliding_sync_proxy(&self) -> Option { - self.sliding_sync_proxy.clone() - } - - /// Whether the current homeserver supports login using OIDC. - pub fn supports_oidc_login(&self) -> bool { - self.supports_oidc_login - } - - /// Whether the current homeserver supports the password login flow. - pub fn supports_password_login(&self) -> bool { - self.supports_password_login - } -} - impl TryInto for &OidcConfiguration { type Error = OidcError; @@ -164,6 +113,59 @@ impl TryInto for &OidcConfiguration { } } +#[derive(Debug, thiserror::Error, uniffi::Error)] +#[uniffi(flat_error)] +pub enum OidcError { + #[error( + "The homeserver doesn't provide an authentication issuer in its well-known configuration." + )] + NotSupported, + #[error("Unable to use OIDC as the supplied client metadata is invalid.")] + MetadataInvalid, + #[error("Failed to use the supplied registrations file path.")] + RegistrationsPathInvalid, + #[error("The supplied callback URL used to complete OIDC is invalid.")] + CallbackUrlInvalid, + #[error("The OIDC login was cancelled by the user.")] + Cancelled, + + #[error("An error occurred: {message}")] + Generic { message: String }, +} + +impl From for OidcError { + fn from(e: SdkOidcError) -> OidcError { + match e { + SdkOidcError::MissingAuthenticationIssuer => OidcError::NotSupported, + SdkOidcError::MissingRedirectUri => OidcError::MetadataInvalid, + SdkOidcError::InvalidCallbackUrl => OidcError::CallbackUrlInvalid, + SdkOidcError::InvalidState => OidcError::CallbackUrlInvalid, + SdkOidcError::CancelledAuthorization => OidcError::Cancelled, + _ => OidcError::Generic { message: e.to_string() }, + } + } +} + +impl From for OidcError { + fn from(e: OidcRegistrationsError) -> OidcError { + match e { + OidcRegistrationsError::InvalidFilePath => OidcError::RegistrationsPathInvalid, + _ => OidcError::Generic { message: e.to_string() }, + } + } +} + +impl From for OidcError { + fn from(e: Error) -> OidcError { + match e { + Error::Oidc(e) => e.into(), + _ => OidcError::Generic { message: e.to_string() }, + } + } +} + +/* Helpers */ + trait OptionExt { /// Convenience method to convert a string to a URL and returns it as a /// Localized URL. No localization is actually performed.