From e89659b69d30abbc165fad490ee4c839dbe7e60d 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 | 162 +++++++++--------- 1 file changed, 82 insertions(+), 80 deletions(-) diff --git a/bindings/matrix-sdk-ffi/src/authentication.rs b/bindings/matrix-sdk-ffi/src/authentication.rs index 91986c551..3674ea5a0 100644 --- a/bindings/matrix-sdk-ffi/src/authentication.rs +++ b/bindings/matrix-sdk-ffi/src/authentication.rs @@ -15,86 +15,6 @@ 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 }, -} - -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: 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: Error) -> OidcError { - match e { - Error::Oidc(e) => e.into(), - _ => OidcError::Generic { message: e.to_string() }, - } - } -} - -/// The configuration to use when authenticating with OIDC. -#[derive(uniffi::Record)] -pub struct OidcConfiguration { - /// The name of the client that will be shown during OIDC authentication. - pub client_name: Option, - /// The redirect URI that will be used when OIDC authentication is - /// successful. - pub redirect_uri: String, - /// A URI that contains information about the client. - pub client_uri: Option, - /// A URI that contains the client's logo. - pub logo_uri: Option, - /// A URI that contains the client's terms of service. - pub tos_uri: Option, - /// A URI that contains the client's privacy policy. - pub policy_uri: Option, - /// An array of e-mail addresses of people responsible for this client. - pub contacts: Option>, - - /// Pre-configured registrations for use with issuers that don't support - /// dynamic client registration. - pub static_registrations: HashMap, - - /// A file path where any dynamic registrations should be stored. - /// - /// Suggested value: `{base_path}/oidc/registrations.json` - pub dynamic_registrations_file: String, -} - #[derive(uniffi::Object)] pub struct HomeserverLoginDetails { pub(crate) url: String, @@ -127,6 +47,35 @@ impl HomeserverLoginDetails { } } +/// The configuration to use when authenticating with OIDC. +#[derive(uniffi::Record)] +pub struct OidcConfiguration { + /// The name of the client that will be shown during OIDC authentication. + pub client_name: Option, + /// The redirect URI that will be used when OIDC authentication is + /// successful. + pub redirect_uri: String, + /// A URI that contains information about the client. + pub client_uri: Option, + /// A URI that contains the client's logo. + pub logo_uri: Option, + /// A URI that contains the client's terms of service. + pub tos_uri: Option, + /// A URI that contains the client's privacy policy. + pub policy_uri: Option, + /// An array of e-mail addresses of people responsible for this client. + pub contacts: Option>, + + /// Pre-configured registrations for use with issuers that don't support + /// dynamic client registration. + pub static_registrations: HashMap, + + /// A file path where any dynamic registrations should be stored. + /// + /// Suggested value: `{base_path}/oidc/registrations.json` + pub dynamic_registrations_file: String, +} + 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.