2018-03-01 09:57:01 +00: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/.
|
|
|
|
|
2018-08-02 16:39:39 +00:00
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
2018-03-01 16:31:49 +00:00
|
|
|
use helpers::PlainText;
|
2018-03-01 16:32:50 +00:00
|
|
|
use sha1::Sha1;
|
|
|
|
use digest::Digest;
|
|
|
|
|
2018-08-02 16:39:39 +00:00
|
|
|
generate_element_with_text!(
|
|
|
|
/// The main authentication mechanism for components.
|
|
|
|
Handshake, "handshake", COMPONENT,
|
|
|
|
|
|
|
|
/// 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.
|
2018-03-01 16:31:49 +00:00
|
|
|
data: PlainText<Option<String>>
|
2018-03-01 09:57:01 +00:00
|
|
|
);
|
|
|
|
|
2018-03-01 16:32:50 +00:00
|
|
|
impl Handshake {
|
2018-08-02 16:39:39 +00:00
|
|
|
/// Creates a successful reply from a server.
|
2018-03-01 16:32:50 +00:00
|
|
|
pub fn new() -> Handshake {
|
|
|
|
Handshake {
|
|
|
|
data: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:39:39 +00:00
|
|
|
/// Creates an authentication request from the component.
|
2018-03-01 16:32:50 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 09:57:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-05-14 14:07:15 +00:00
|
|
|
use try_from::TryFrom;
|
2018-05-14 14:17:21 +00:00
|
|
|
use minidom::Element;
|
2018-03-01 09:57:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<handshake xmlns='jabber:component:accept'/>".parse().unwrap();
|
|
|
|
let handshake = Handshake::try_from(elem).unwrap();
|
2018-03-01 16:31:49 +00:00
|
|
|
assert_eq!(handshake.data, None);
|
2018-03-01 09:57:01 +00:00
|
|
|
|
2018-03-01 16:31:49 +00:00
|
|
|
let elem: Element = "<handshake xmlns='jabber:component:accept'>Coucou</handshake>".parse().unwrap();
|
2018-03-01 09:57:01 +00:00
|
|
|
let handshake = Handshake::try_from(elem).unwrap();
|
2018-03-01 16:31:49 +00:00
|
|
|
assert_eq!(handshake.data, Some(String::from("Coucou")));
|
2018-03-01 09:57:01 +00:00
|
|
|
}
|
2018-03-01 16:32:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_constructors() {
|
|
|
|
let handshake = Handshake::new();
|
|
|
|
assert_eq!(handshake.data, None);
|
|
|
|
|
|
|
|
let handshake = Handshake::from_password_and_stream_id("123456", "sid");
|
|
|
|
assert_eq!(handshake.data, Some(String::from("9accec263ab84a43c6037ccf7cd48cb1d3f6df8e")));
|
|
|
|
}
|
2018-03-01 09:57:01 +00:00
|
|
|
}
|