xmpp-rs/parsers/src/component.rs

90 lines
2.8 KiB
Rust
Raw Normal View History

2018-03-01 10:57:01 +01:00
// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.
2024-07-24 17:35:56 +02:00
use xso::{text::FixedHex, AsXml, FromXml};
use crate::ns;
2018-03-01 17:32:50 +01:00
use digest::Digest;
2018-12-18 15:32:05 +01:00
use sha1::Sha1;
2018-03-01 17:32:50 +01:00
2024-07-24 17:35:56 +02:00
/// 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>))]
2024-07-24 17:35:56 +02:00
pub data: Option<[u8; 20]>,
}
2018-03-01 10:57:01 +01:00
2018-03-01 17:32:50 +01:00
impl Handshake {
2018-08-02 18:39:39 +02:00
/// Creates a successful reply from a server.
2018-03-01 17:32:50 +01:00
pub fn new() -> Handshake {
2019-02-21 21:00:58 +01:00
Handshake::default()
2018-03-01 17:32:50 +01:00
}
2018-08-02 18:39:39 +02:00
/// Creates an authentication request from the component.
pub fn from_stream_id_and_password(stream_id: String, password: &str) -> Handshake {
let input = stream_id + password;
2018-03-01 17:32:50 +01:00
let hash = Sha1::digest(input.as_bytes());
Handshake {
2024-07-24 17:35:56 +02:00
data: Some(hash.into()),
2018-03-01 17:32:50 +01:00
}
}
}
2018-03-01 10:57:01 +01:00
#[cfg(test)]
mod tests {
use super::*;
use minidom::Element;
2018-03-01 10:57:01 +01:00
2018-10-28 13:10:48 +01:00
#[test]
fn test_size() {
2024-07-24 17:35:56 +02:00
assert_size!(Handshake, 21);
}
2018-03-01 10:57:01 +01:00
#[test]
fn test_simple() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<handshake xmlns='jabber:component:accept'/>"
.parse()
.unwrap();
2018-03-01 10:57:01 +01:00
let handshake = Handshake::try_from(elem).unwrap();
assert_eq!(handshake.data, None);
2018-03-01 10:57:01 +01:00
2024-07-24 17:35:56 +02:00
let elem: Element = "<handshake xmlns='jabber:component:accept'>9accec263ab84a43c6037ccf7cd48cb1d3f6df8e</handshake>"
2018-12-18 15:32:05 +01:00
.parse()
.unwrap();
2018-03-01 10:57:01 +01:00
let handshake = Handshake::try_from(elem).unwrap();
2024-07-24 17:35:56 +02:00
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
])
);
2018-03-01 10:57:01 +01:00
}
2018-03-01 17:32:50 +01:00
#[test]
fn test_constructors() {
let handshake = Handshake::new();
assert_eq!(handshake.data, None);
let stream_id = String::from("sid");
let password = "123456";
let handshake = Handshake::from_stream_id_and_password(stream_id, password);
2018-12-18 15:32:05 +01:00
assert_eq!(
handshake.data,
2024-07-24 17:35:56 +02:00
Some([
0x9a, 0xcc, 0xec, 0x26, 0x3a, 0xb8, 0x4a, 0x43, 0xc6, 0x03, 0x7c, 0xcf, 0x7c, 0xd4,
0x8c, 0xb1, 0xd3, 0xf6, 0xdf, 0x8e
])
2018-12-18 15:32:05 +01:00
);
2018-03-01 17:32:50 +01:00
}
2018-03-01 10:57:01 +01:00
}