summaryrefslogtreecommitdiffstats
path: root/src/stanza/sasl.rs
blob: 20cd0631e75aa692f083dd193c8d9f0d078caeb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use quick_xml::{
    events::{BytesStart, BytesText, Event},
    name::QName,
};

use crate::error::SASLError;
use crate::JabberError;

use super::Element;

const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-sasl";

#[derive(Debug)]
pub struct Auth<'e> {
    pub mechanism: &'e str,
    pub sasl_data: &'e str,
}

impl<'e> IntoElement<'e> for Auth<'e> {
    fn event(&self) -> Event<'e> {
        let mut start = BytesStart::new("auth");
        start.push_attribute(("xmlns", XMLNS));
        start.push_attribute(("mechanism", self.mechanism));
        Event::Start(start)
    }

    fn children(&self) -> Option<Vec<Element<'e>>> {
        let sasl = BytesText::from_escaped(self.sasl_data);
        let sasl = Element {
            event: Event::Text(sasl),
            children: None,
        };
        Some(vec![sasl])
    }
}

#[derive(Debug)]
pub struct Challenge {
    pub sasl_data: Vec<u8>,
}

impl<'e> TryFrom<&Element<'e>> for Challenge {
    type Error = JabberError;

    fn try_from(element: &Element<'e>) -> Result<Challenge, Self::Error> {
        if let Event::Start(start) = &element.event {
            if start.name() == QName(b"challenge") {
                let sasl_data: &Element<'_> = element.child()?;
                if let Event::Text(sasl_data) = &sasl_data.event {
                    let s = sasl_data.clone();
                    let s = s.into_inner();
                    let s = s.to_vec();
                    return Ok(Challenge { sasl_data: s });
                }
            }
        }
        Err(SASLError::NoChallenge.into())
    }
}

// impl<'e> TryFrom<Element<'e>> for Challenge {
//     type Error = JabberError;

//     fn try_from(element: Element<'e>) -> Result<Challenge, Self::Error> {
//         if let Event::Start(start) = &element.event {
//             if start.name() == QName(b"challenge") {
//                 println!("one");
//                 if let Some(children) = element.children.as_deref() {
//                     if children.len() == 1 {
//                         let sasl_data = children.first().unwrap();
//                         if let Event::Text(sasl_data) = &sasl_data.event {
//                             return Ok(Challenge {
//                                 sasl_data: sasl_data.clone().into_inner().to_vec(),
//                             });
//                         } else {
//                             return Err(SASLError::NoChallenge.into());
//                         }
//                     } else {
//                         return Err(SASLError::NoChallenge.into());
//                     }
//                 } else {
//                     return Err(SASLError::NoChallenge.into());
//                 }
//             }
//         }
//         Err(SASLError::NoChallenge.into())
//     }
// }

#[derive(Debug)]
pub struct Response<'e> {
    pub sasl_data: &'e str,
}

impl<'e> IntoElement<'e> for Response<'e> {
    fn event(&self) -> Event<'e> {
        let mut start = BytesStart::new("response");
        start.push_attribute(("xmlns", XMLNS));
        Event::Start(start)
    }

    fn children(&self) -> Option<Vec<Element<'e>>> {
        let sasl = BytesText::from_escaped(self.sasl_data);
        let sasl = Element {
            event: Event::Text(sasl),
            children: None,
        };
        Some(vec![sasl])
    }
}

#[derive(Debug)]
pub struct Success {
    pub sasl_data: Option<Vec<u8>>,
}

impl<'e> TryFrom<&Element<'e>> for Success {
    type Error = JabberError;

    fn try_from(element: &Element<'e>) -> Result<Success, Self::Error> {
        match &element.event {
            Event::Start(start) => {
                if start.name() == QName(b"success") {
                    match element.child() {
                        Ok(sasl_data) => {
                            if let Event::Text(sasl_data) = &sasl_data.event {
                                return Ok(Success {
                                    sasl_data: Some(sasl_data.clone().into_inner().to_vec()),
                                });
                            }
                        }
                        Err(_) => return Ok(Success { sasl_data: None }),
                    };
                }
            }
            Event::Empty(empty) => {
                if empty.name() == QName(b"success") {
                    return Ok(Success { sasl_data: None });
                }
            }
            _ => {}
        }
        Err(SASLError::NoSuccess.into())
    }
}