ruma-events: Rename MSasV1ContentOptions to MSasV1ContentInit.

This commit is contained in:
Damir Jelić 2020-07-30 11:01:03 +02:00 committed by Jonas Platte
parent 21837d6b8d
commit 6513ebd9b0

View File

@ -86,9 +86,9 @@ pub struct MSasV1Content {
pub short_authentication_string: Vec<ShortAuthenticationString>, pub short_authentication_string: Vec<ShortAuthenticationString>,
} }
/// Options for creating an `MSasV1Content` with `MSasV1Content::new`. /// Mandatory initial set of fields for creating an `MSasV1Content`.
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
pub struct MSasV1ContentOptions { pub struct MSasV1ContentInit {
/// The key agreement protocols the sending device understands. /// The key agreement protocols the sending device understands.
/// ///
/// Must include at least `curve25519`. /// Must include at least `curve25519`.
@ -122,20 +122,18 @@ impl MSasV1Content {
/// * `message_authentication_codes` does not include /// * `message_authentication_codes` does not include
/// `MessageAuthenticationCode::HkdfHmacSha256`. /// `MessageAuthenticationCode::HkdfHmacSha256`.
/// * `short_authentication_string` does not include `ShortAuthenticationString::Decimal`. /// * `short_authentication_string` does not include `ShortAuthenticationString::Decimal`.
pub fn new(options: MSasV1ContentOptions) -> Result<Self, InvalidInput> { pub fn new(options: MSasV1ContentInit) -> Result<Self, InvalidInput> {
MSasV1Content::try_from(options) MSasV1Content::try_from(options)
} }
} }
impl TryFrom<MSasV1ContentOptions> for MSasV1Content { impl TryFrom<MSasV1ContentInit> for MSasV1Content {
type Error = InvalidInput; type Error = InvalidInput;
/// Creates a new `MSasV1Content` from the given init struct. /// Creates a new `MSasV1Content` from the given init struct.
fn try_from(options: MSasV1ContentOptions) -> Result<Self, Self::Error> { fn try_from(init: MSasV1ContentInit) -> Result<Self, Self::Error> {
if !options.key_agreement_protocols.contains(&KeyAgreementProtocol::Curve25519) if !init.key_agreement_protocols.contains(&KeyAgreementProtocol::Curve25519)
&& !options && !init.key_agreement_protocols.contains(&KeyAgreementProtocol::Curve25519HkdfSha256)
.key_agreement_protocols
.contains(&KeyAgreementProtocol::Curve25519HkdfSha256)
{ {
return Err(InvalidInput( return Err(InvalidInput(
"`key_agreement_protocols` must contain at \ "`key_agreement_protocols` must contain at \
@ -145,16 +143,13 @@ impl TryFrom<MSasV1ContentOptions> for MSasV1Content {
)); ));
} }
if !options.hashes.contains(&HashAlgorithm::Sha256) { if !init.hashes.contains(&HashAlgorithm::Sha256) {
return Err(InvalidInput( return Err(InvalidInput(
"`hashes` must contain at least `HashAlgorithm::Sha256`".into(), "`hashes` must contain at least `HashAlgorithm::Sha256`".into(),
)); ));
} }
if !options if !init.message_authentication_codes.contains(&MessageAuthenticationCode::HkdfHmacSha256) {
.message_authentication_codes
.contains(&MessageAuthenticationCode::HkdfHmacSha256)
{
return Err(InvalidInput( return Err(InvalidInput(
"`message_authentication_codes` must contain \ "`message_authentication_codes` must contain \
at least `MessageAuthenticationCode::HkdfHmacSha256`" at least `MessageAuthenticationCode::HkdfHmacSha256`"
@ -162,7 +157,7 @@ impl TryFrom<MSasV1ContentOptions> for MSasV1Content {
)); ));
} }
if !options.short_authentication_string.contains(&ShortAuthenticationString::Decimal) { if !init.short_authentication_string.contains(&ShortAuthenticationString::Decimal) {
return Err(InvalidInput( return Err(InvalidInput(
"`short_authentication_string` must contain \ "`short_authentication_string` must contain \
at least `ShortAuthenticationString::Decimal`" at least `ShortAuthenticationString::Decimal`"
@ -171,10 +166,10 @@ impl TryFrom<MSasV1ContentOptions> for MSasV1Content {
} }
Ok(Self { Ok(Self {
key_agreement_protocols: options.key_agreement_protocols, key_agreement_protocols: init.key_agreement_protocols,
hashes: options.hashes, hashes: init.hashes,
message_authentication_codes: options.message_authentication_codes, message_authentication_codes: init.message_authentication_codes,
short_authentication_string: options.short_authentication_string, short_authentication_string: init.short_authentication_string,
}) })
} }
} }
@ -189,7 +184,7 @@ mod tests {
}; };
use super::{ use super::{
CustomContent, HashAlgorithm, KeyAgreementProtocol, MSasV1Content, MSasV1ContentOptions, CustomContent, HashAlgorithm, KeyAgreementProtocol, MSasV1Content, MSasV1ContentInit,
MessageAuthenticationCode, ShortAuthenticationString, StartEvent, StartEventContent, MessageAuthenticationCode, ShortAuthenticationString, StartEvent, StartEventContent,
StartMethod, StartMethod,
}; };
@ -197,7 +192,7 @@ mod tests {
#[test] #[test]
fn invalid_m_sas_v1_content_missing_required_key_agreement_protocols() { fn invalid_m_sas_v1_content_missing_required_key_agreement_protocols() {
let error = MSasV1Content::new(MSasV1ContentOptions { let error = MSasV1Content::new(MSasV1ContentInit {
hashes: vec![HashAlgorithm::Sha256], hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![], key_agreement_protocols: vec![],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256], message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
@ -211,7 +206,7 @@ mod tests {
#[test] #[test]
fn invalid_m_sas_v1_content_missing_required_hashes() { fn invalid_m_sas_v1_content_missing_required_hashes() {
let error = MSasV1Content::new(MSasV1ContentOptions { let error = MSasV1Content::new(MSasV1ContentInit {
hashes: vec![], hashes: vec![],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519], key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256], message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
@ -225,7 +220,7 @@ mod tests {
#[test] #[test]
fn invalid_m_sas_v1_content_missing_required_message_authentication_codes() { fn invalid_m_sas_v1_content_missing_required_message_authentication_codes() {
let error = MSasV1Content::new(MSasV1ContentOptions { let error = MSasV1Content::new(MSasV1ContentInit {
hashes: vec![HashAlgorithm::Sha256], hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519], key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![], message_authentication_codes: vec![],
@ -239,7 +234,7 @@ mod tests {
#[test] #[test]
fn invalid_m_sas_v1_content_missing_required_short_authentication_string() { fn invalid_m_sas_v1_content_missing_required_short_authentication_string() {
let error = MSasV1Content::new(MSasV1ContentOptions { let error = MSasV1Content::new(MSasV1ContentInit {
hashes: vec![HashAlgorithm::Sha256], hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519], key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256], message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
@ -257,7 +252,7 @@ mod tests {
from_device: "123".into(), from_device: "123".into(),
transaction_id: "456".into(), transaction_id: "456".into(),
method: StartMethod::MSasV1( method: StartMethod::MSasV1(
MSasV1Content::new(MSasV1ContentOptions { MSasV1Content::new(MSasV1ContentInit {
hashes: vec![HashAlgorithm::Sha256], hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519], key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256], message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],