xmpp-parsers: Convert component to xso

This commit is contained in:
Emmanuel Gil Peyrot 2024-07-24 17:35:56 +02:00 committed by Maxime “pep” Buquet
parent e9b6fd5d8b
commit 4be297de11

View file

@ -4,24 +4,25 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::util::text_node_codecs::{Codec, OptionalCodec, Text};
use xso::{text::FixedHex, AsXml, FromXml};
use crate::ns;
use digest::Digest;
use sha1::Sha1;
generate_element!(
/// The main authentication mechanism for components.
#[derive(Default)]
Handshake, "handshake", COMPONENT,
text: (
/// If Some, contains the hex-encoded SHA-1 of the concatenation of the
/// stream id and the password, and is used to authenticate against the
/// server.
///
/// If None, it is the successful reply from the server, the stream is now
/// fully established and both sides can now exchange stanzas.
data: OptionalCodec<Text>
)
);
/// The main authentication mechanism for components.
#[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)]
#[xml(namespace = ns::COMPONENT, name = "handshake")]
pub struct Handshake {
/// If Some, contains the hex-encoded SHA-1 of the concatenation of the
/// stream id and the password, and is used to authenticate against the
/// server.
///
/// If None, it is the successful reply from the server, the stream is now
/// fully established and both sides can now exchange stanzas.
#[xml(text(codec = FixedHex<20>))]
pub data: Option<[u8; 20]>,
}
impl Handshake {
/// Creates a successful reply from a server.
@ -33,9 +34,8 @@ impl Handshake {
pub fn from_password_and_stream_id(password: &str, stream_id: &str) -> Handshake {
let input = String::from(stream_id) + password;
let hash = Sha1::digest(input.as_bytes());
let content = format!("{:x}", hash);
Handshake {
data: Some(content),
data: Some(hash.into()),
}
}
}
@ -45,16 +45,9 @@ mod tests {
use super::*;
use minidom::Element;
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Handshake, 12);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Handshake, 24);
assert_size!(Handshake, 21);
}
#[test]
@ -65,11 +58,17 @@ mod tests {
let handshake = Handshake::try_from(elem).unwrap();
assert_eq!(handshake.data, None);
let elem: Element = "<handshake xmlns='jabber:component:accept'>Coucou</handshake>"
let elem: Element = "<handshake xmlns='jabber:component:accept'>9accec263ab84a43c6037ccf7cd48cb1d3f6df8e</handshake>"
.parse()
.unwrap();
let handshake = Handshake::try_from(elem).unwrap();
assert_eq!(handshake.data, Some(String::from("Coucou")));
assert_eq!(
handshake.data,
Some([
0x9a, 0xcc, 0xec, 0x26, 0x3a, 0xb8, 0x4a, 0x43, 0xc6, 0x03, 0x7c, 0xcf, 0x7c, 0xd4,
0x8c, 0xb1, 0xd3, 0xf6, 0xdf, 0x8e
])
);
}
#[test]
@ -80,7 +79,10 @@ mod tests {
let handshake = Handshake::from_password_and_stream_id("123456", "sid");
assert_eq!(
handshake.data,
Some(String::from("9accec263ab84a43c6037ccf7cd48cb1d3f6df8e"))
Some([
0x9a, 0xcc, 0xec, 0x26, 0x3a, 0xb8, 0x4a, 0x43, 0xc6, 0x03, 0x7c, 0xcf, 0x7c, 0xd4,
0x8c, 0xb1, 0xd3, 0xf6, 0xdf, 0x8e
])
);
}
}