From 87bc891c53f1f9298c799c1625ac0dfb4b313354 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 15 Feb 2021 16:24:25 +0100 Subject: [PATCH] client-api: Make UIAA types non-exhaustive --- ruma-client-api/src/r0/uiaa.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ruma-client-api/src/r0/uiaa.rs b/ruma-client-api/src/r0/uiaa.rs index b192e03b..5a70c90d 100644 --- a/ruma-client-api/src/r0/uiaa.rs +++ b/ruma-client-api/src/r0/uiaa.rs @@ -17,9 +17,12 @@ use crate::error::{Error as MatrixError, ErrorBody}; /// Additional authentication information for the user-interactive authentication API. #[derive(Clone, Debug, Outgoing, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[serde(untagged)] pub enum AuthData<'a> { /// Used for sending UIAA authentication requests to the homeserver directly from the client. + // Could be made non-exhaustive by creating a separate struct and making auth_parameters + // private, but probably not worth the hassle. DirectRequest { /// The login type that the client is attempting to complete. #[serde(rename = "type")] @@ -30,13 +33,15 @@ pub enum AuthData<'a> { session: Option<&'a str>, /// Parameters submitted for a particular authentication stage. - // FIXME: RawJsonValue doesn't work here, is that a bug? #[serde(flatten)] auth_parameters: BTreeMap, }, /// Used by the client to acknowledge that the user has completed a UIAA stage through the /// fallback method. + // Exhaustiveness is correct here since this variant is a fallback that explicitly only has a + // single field. TODO: #[serde(deny_unknown_fields)] not supported on enum variants + // https://github.com/serde-rs/serde/issues/1982 FallbackAcknowledgement { /// The value of the session key given by the homeserver. session: &'a str, @@ -46,6 +51,7 @@ pub enum AuthData<'a> { /// Information about available authentication flows and status for User-Interactive Authenticiation /// API. #[derive(Clone, Debug, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct UiaaInfo { /// List of authentication flows available for this endpoint. pub flows: Vec, @@ -68,8 +74,16 @@ pub struct UiaaInfo { pub auth_error: Option, } +impl UiaaInfo { + /// Creates a new `UiaaInfo` with the given flows and parameters. + pub fn new(flows: Vec, params: Box) -> Self { + Self { flows, completed: Vec::new(), params, session: None, auth_error: None } + } +} + /// Description of steps required to authenticate via the User-Interactive Authentication API. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(test, derive(PartialEq))] pub struct AuthFlow { /// Ordered list of stages required to complete authentication. @@ -77,6 +91,13 @@ pub struct AuthFlow { pub stages: Vec, } +impl AuthFlow { + /// Creates an empty `AuthFlow`. + pub fn new() -> Self { + Self { stages: Vec::new() } + } +} + /// Contains either a User-Interactive Authentication API response body or a Matrix error. #[derive(Clone, Debug)] pub enum UiaaResponse {