sasl.rs

 1// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7use try_from::TryFrom;
 8use std::str::FromStr;
 9
10use minidom::{Element, IntoAttributeValue};
11
12use error::Error;
13
14use ns;
15use helpers::Base64;
16
17generate_attribute!(Mechanism, "mechanism", {
18    Plain => "PLAIN",
19    ScramSha1 => "SCRAM-SHA-1",
20    Anonymous => "ANONYMOUS",
21});
22
23generate_element_with_text!(Auth, "auth", ns::SASL,
24    [
25        mechanism: Mechanism = "mechanism" => required
26    ],
27    data: Base64<Vec<u8>>
28);
29
30generate_element_with_text!(Challenge, "challenge", ns::SASL,
31    data: Base64<Vec<u8>>
32);
33
34generate_element_with_text!(Response, "response", ns::SASL,
35    data: Base64<Vec<u8>>
36);
37
38generate_element_with_text!(Success, "success", ns::SASL,
39    data: Base64<Vec<u8>>
40);
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_simple() {
48        let elem: Element = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'/>".parse().unwrap();
49        let auth = Auth::try_from(elem).unwrap();
50        assert_eq!(auth.mechanism, Mechanism::Plain);
51        assert!(auth.data.is_empty());
52    }
53}