From f210f91611b0fd098dbdcc38f012e9e05fd4061d Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 13 Aug 2020 21:11:28 +0200 Subject: [PATCH] Borrow strings and identifiers in uiaa::AuthData and dependent endpoints --- .../src/r0/account/change_password.rs | 4 +-- ruma-client-api/src/r0/account/deactivate.rs | 6 ++-- ruma-client-api/src/r0/account/register.rs | 12 ++++---- .../src/r0/device/delete_device.rs | 6 ++-- .../src/r0/device/delete_devices.rs | 6 ++-- ruma-client-api/src/r0/uiaa.rs | 28 +++++++++---------- ruma-client/src/lib.rs | 4 +-- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/ruma-client-api/src/r0/account/change_password.rs b/ruma-client-api/src/r0/account/change_password.rs index a8e0694d..882ad4b6 100644 --- a/ruma-client-api/src/r0/account/change_password.rs +++ b/ruma-client-api/src/r0/account/change_password.rs @@ -2,7 +2,7 @@ use ruma_api::ruma_api; -use crate::r0::uiaa::{AuthData, UiaaResponse}; +use crate::r0::uiaa::{AuthData, IncomingAuthData, UiaaResponse}; ruma_api! { metadata: { @@ -29,7 +29,7 @@ ruma_api! { pub logout_devices: bool, /// Additional authentication information for the user-interactive authentication API. - pub auth: Option, + pub auth: Option>, } response: {} diff --git a/ruma-client-api/src/r0/account/deactivate.rs b/ruma-client-api/src/r0/account/deactivate.rs index 0f220aa5..182c6137 100644 --- a/ruma-client-api/src/r0/account/deactivate.rs +++ b/ruma-client-api/src/r0/account/deactivate.rs @@ -2,7 +2,7 @@ use ruma_api::ruma_api; -use crate::r0::uiaa::{AuthData, UiaaResponse}; +use crate::r0::uiaa::{AuthData, IncomingAuthData, UiaaResponse}; use super::ThirdPartyIdRemovalStatus; @@ -19,12 +19,12 @@ ruma_api! { request: { /// Additional authentication information for the user-interactive authentication API. #[serde(skip_serializing_if = "Option::is_none")] - pub auth: Option, + pub auth: Option>, /// Identity server from which to unbind the user's third party /// identifier. #[serde(skip_serializing_if = "Option::is_none")] - pub id_server: Option, + pub id_server: Option<&'a str>, } response: { diff --git a/ruma-client-api/src/r0/account/register.rs b/ruma-client-api/src/r0/account/register.rs index ece32331..54c63d3b 100644 --- a/ruma-client-api/src/r0/account/register.rs +++ b/ruma-client-api/src/r0/account/register.rs @@ -4,7 +4,7 @@ use ruma_api::ruma_api; use ruma_identifiers::{DeviceId, UserId}; use serde::{Deserialize, Serialize}; -use crate::r0::uiaa::{AuthData, UiaaResponse}; +use crate::r0::uiaa::{AuthData, IncomingAuthData, UiaaResponse}; ruma_api! { metadata: { @@ -22,26 +22,26 @@ ruma_api! { /// May be empty for accounts that should not be able to log in again /// with a password, e.g., for guest or application service accounts. #[serde(skip_serializing_if = "Option::is_none")] - pub password: Option, + pub password: Option<&'a str>, /// local part of the desired Matrix ID. /// /// If omitted, the homeserver MUST generate a Matrix ID local part. #[serde(skip_serializing_if = "Option::is_none")] - pub username: Option, + pub username: Option<&'a str>, /// ID of the client device. /// /// If this does not correspond to a known client device, a new device will be created. /// The server will auto-generate a device_id if this is not specified. #[serde(skip_serializing_if = "Option::is_none")] - pub device_id: Option>, + pub device_id: Option<&'a DeviceId>, /// A display name to assign to the newly-created device. /// /// Ignored if `device_id` corresponds to a known device. #[serde(skip_serializing_if = "Option::is_none")] - pub initial_device_display_name: Option, + pub initial_device_display_name: Option<&'a str>, /// Additional authentication information for the user-interactive authentication API. /// @@ -50,7 +50,7 @@ ruma_api! { /// It should be left empty, or omitted, unless an earlier call returned an response /// with status code 401. #[serde(skip_serializing_if = "Option::is_none")] - pub auth: Option, + pub auth: Option>, /// Kind of account to register /// diff --git a/ruma-client-api/src/r0/device/delete_device.rs b/ruma-client-api/src/r0/device/delete_device.rs index 9f4084b2..c2098991 100644 --- a/ruma-client-api/src/r0/device/delete_device.rs +++ b/ruma-client-api/src/r0/device/delete_device.rs @@ -3,7 +3,7 @@ use ruma_api::ruma_api; use ruma_identifiers::DeviceId; -use crate::r0::uiaa::{AuthData, UiaaResponse}; +use crate::r0::uiaa::{AuthData, IncomingAuthData, UiaaResponse}; ruma_api! { metadata: { @@ -18,11 +18,11 @@ ruma_api! { request: { /// The device to delete. #[ruma_api(path)] - pub device_id: Box, + pub device_id: &'a DeviceId, /// Additional authentication information for the user-interactive authentication API. #[serde(skip_serializing_if = "Option::is_none")] - pub auth: Option, + pub auth: Option>, } response: {} diff --git a/ruma-client-api/src/r0/device/delete_devices.rs b/ruma-client-api/src/r0/device/delete_devices.rs index df3cd7fc..cb743e5a 100644 --- a/ruma-client-api/src/r0/device/delete_devices.rs +++ b/ruma-client-api/src/r0/device/delete_devices.rs @@ -3,7 +3,7 @@ use ruma_api::ruma_api; use ruma_identifiers::DeviceId; -use crate::r0::uiaa::{AuthData, UiaaResponse}; +use crate::r0::uiaa::{AuthData, IncomingAuthData, UiaaResponse}; ruma_api! { metadata: { @@ -17,11 +17,11 @@ ruma_api! { request: { /// List of devices to delete. - pub devices: Vec>, + pub devices: &'a [Box], /// Additional authentication information for the user-interactive authentication API. #[serde(skip_serializing_if = "Option::is_none")] - pub auth: Option, + pub auth: Option>, } response: {} diff --git a/ruma-client-api/src/r0/uiaa.rs b/ruma-client-api/src/r0/uiaa.rs index 9b1bcb31..4b617f51 100644 --- a/ruma-client-api/src/r0/uiaa.rs +++ b/ruma-client-api/src/r0/uiaa.rs @@ -5,7 +5,7 @@ use std::{ fmt::{self, Display, Formatter}, }; -use ruma_api::{error::ResponseDeserializationError, EndpointError}; +use ruma_api::{error::ResponseDeserializationError, EndpointError, Outgoing}; use serde::{Deserialize, Serialize}; use serde_json::{ from_slice as from_json_slice, to_vec as to_json_vec, value::RawValue as RawJsonValue, @@ -15,19 +15,19 @@ use serde_json::{ use crate::error::{Error as MatrixError, ErrorBody}; /// Additional authentication information for the user-interactive authentication API. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Outgoing, Serialize)] #[serde(untagged)] -pub enum AuthData { +pub enum AuthData<'a> { /// Used for sending UIAA authentication requests to the homeserver directly /// from the client. DirectRequest { /// The login type that the client is attempting to complete. #[serde(rename = "type")] - kind: String, + kind: &'a str, /// The value of the session key given by the homeserver. #[serde(skip_serializing_if = "Option::is_none")] - session: Option, + session: Option<&'a str>, /// Parameters submitted for a particular authentication stage. // FIXME: RawJsonValue doesn't work here, is that a bug? @@ -39,7 +39,7 @@ pub enum AuthData { /// stage through the fallback method. FallbackAcknowledgement { /// The value of the session key given by the homeserver. - session: String, + session: &'a str, }, } @@ -145,14 +145,14 @@ mod tests { Value as JsonValue, }; - use super::{AuthData, AuthFlow, UiaaInfo, UiaaResponse}; + use super::{AuthData, AuthFlow, IncomingAuthData, UiaaInfo, UiaaResponse}; use crate::error::{ErrorBody, ErrorKind}; #[test] fn test_serialize_authentication_data_direct_request() { let authentication_data = AuthData::DirectRequest { - kind: "example.type.foo".into(), - session: Some("ZXY000".into()), + kind: "example.type.foo", + session: Some("ZXY000"), auth_parameters: btreemap! { "example_credential".to_owned() => json!("verypoorsharedsecret") }, @@ -177,8 +177,8 @@ mod tests { }); assert_matches!( - from_json_value::(json).unwrap(), - AuthData::DirectRequest { kind, session: Some(session), auth_parameters } + from_json_value(json).unwrap(), + IncomingAuthData::DirectRequest { kind, session: Some(session), auth_parameters } if kind == "example.type.foo" && session == "opaque_session_id" && auth_parameters == btreemap!{ @@ -189,7 +189,7 @@ mod tests { #[test] fn test_serialize_authentication_data_fallback() { - let authentication_data = AuthData::FallbackAcknowledgement { session: "ZXY000".into() }; + let authentication_data = AuthData::FallbackAcknowledgement { session: "ZXY000" }; assert_eq!(json!({ "session": "ZXY000" }), to_json_value(authentication_data).unwrap()); } @@ -199,8 +199,8 @@ mod tests { let json = json!({ "session": "opaque_session_id" }); assert_matches!( - from_json_value::(json).unwrap(), - AuthData::FallbackAcknowledgement { session } + from_json_value(json).unwrap(), + IncomingAuthData::FallbackAcknowledgement { session } if session == "opaque_session_id" ); } diff --git a/ruma-client/src/lib.rs b/ruma-client/src/lib.rs index 2a02f809..91af4265 100644 --- a/ruma-client/src/lib.rs +++ b/ruma-client/src/lib.rs @@ -282,8 +282,8 @@ where /// omitted from this request, the server will generate one. pub async fn register_user( &self, - username: Option, - password: String, + username: Option<&str>, + password: &str, ) -> Result> { use ruma_client_api::r0::account::register;