auth.rs

 1use futures::{sink, Async, Future, Poll, Stream};
 2use std::mem::replace;
 3use tokio_io::{AsyncRead, AsyncWrite};
 4use xmpp_parsers::component::Handshake;
 5
 6use crate::xmpp_codec::Packet;
 7use crate::xmpp_stream::XMPPStream;
 8use crate::{AuthError, Error};
 9
10const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
11
12pub struct ComponentAuth<S: AsyncWrite> {
13    state: ComponentAuthState<S>,
14}
15
16enum ComponentAuthState<S: AsyncWrite> {
17    WaitSend(sink::Send<XMPPStream<S>>),
18    WaitRecv(XMPPStream<S>),
19    Invalid,
20}
21
22impl<S: AsyncWrite> ComponentAuth<S> {
23    // TODO: doesn't have to be a Result<> actually
24    pub fn new(stream: XMPPStream<S>, password: String) -> Result<Self, Error> {
25        // FIXME: huge hack, shouldn’t be an element!
26        let sid = stream.stream_features.name().to_owned();
27        let mut this = ComponentAuth {
28            state: ComponentAuthState::Invalid,
29        };
30        this.send(
31            stream,
32            Handshake::from_password_and_stream_id(&password, &sid),
33        );
34        Ok(this)
35    }
36
37    fn send(&mut self, stream: XMPPStream<S>, handshake: Handshake) {
38        let nonza = handshake;
39        let send = stream.send_stanza(nonza);
40
41        self.state = ComponentAuthState::WaitSend(send);
42    }
43}
44
45impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
46    type Item = XMPPStream<S>;
47    type Error = Error;
48
49    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
50        let state = replace(&mut self.state, ComponentAuthState::Invalid);
51
52        match state {
53            ComponentAuthState::WaitSend(mut send) => match send.poll() {
54                Ok(Async::Ready(stream)) => {
55                    self.state = ComponentAuthState::WaitRecv(stream);
56                    self.poll()
57                }
58                Ok(Async::NotReady) => {
59                    self.state = ComponentAuthState::WaitSend(send);
60                    Ok(Async::NotReady)
61                }
62                Err(e) => Err(e)?,
63            },
64            ComponentAuthState::WaitRecv(mut stream) => match stream.poll() {
65                Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
66                    if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
67                {
68                    self.state = ComponentAuthState::Invalid;
69                    Ok(Async::Ready(stream))
70                }
71                Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
72                    if stanza.is("error", "http://etherx.jabber.org/streams") =>
73                {
74                    Err(AuthError::ComponentFail.into())
75                }
76                Ok(Async::Ready(_event)) => {
77                    // println!("ComponentAuth ignore {:?}", _event);
78                    Ok(Async::NotReady)
79                }
80                Ok(_) => {
81                    self.state = ComponentAuthState::WaitRecv(stream);
82                    Ok(Async::NotReady)
83                }
84                Err(e) => Err(e)?,
85            },
86            ComponentAuthState::Invalid => unreachable!(),
87        }
88    }
89}