2018-12-18 18:04:31 +00:00
|
|
|
|
use futures::{sink, Async, Future, Poll, Stream};
|
2017-07-22 00:59:51 +00:00
|
|
|
|
use std::mem::replace;
|
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2018-07-31 23:07:10 +00:00
|
|
|
|
use xmpp_parsers::component::Handshake;
|
2017-07-22 00:59:51 +00:00
|
|
|
|
|
2018-12-18 17:29:31 +00:00
|
|
|
|
use crate::xmpp_codec::Packet;
|
|
|
|
|
use crate::xmpp_stream::XMPPStream;
|
2018-12-18 18:04:31 +00:00
|
|
|
|
use crate::{AuthError, Error};
|
2017-07-22 00:59:51 +00:00
|
|
|
|
|
|
|
|
|
const NS_JABBER_COMPONENT_ACCEPT: &str = "jabber:component:accept";
|
|
|
|
|
|
|
|
|
|
pub struct ComponentAuth<S: AsyncWrite> {
|
|
|
|
|
state: ComponentAuthState<S>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ComponentAuthState<S: AsyncWrite> {
|
|
|
|
|
WaitSend(sink::Send<XMPPStream<S>>),
|
|
|
|
|
WaitRecv(XMPPStream<S>),
|
|
|
|
|
Invalid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: AsyncWrite> ComponentAuth<S> {
|
2018-09-06 15:46:06 +00:00
|
|
|
|
// TODO: doesn't have to be a Result<> actually
|
|
|
|
|
pub fn new(stream: XMPPStream<S>, password: String) -> Result<Self, Error> {
|
2017-07-22 00:59:51 +00:00
|
|
|
|
// FIXME: huge hack, shouldn’t be an element!
|
|
|
|
|
let sid = stream.stream_features.name().to_owned();
|
|
|
|
|
let mut this = ComponentAuth {
|
|
|
|
|
state: ComponentAuthState::Invalid,
|
|
|
|
|
};
|
|
|
|
|
this.send(
|
|
|
|
|
stream,
|
2018-12-18 18:04:31 +00:00
|
|
|
|
Handshake::from_password_and_stream_id(&password, &sid),
|
2017-07-22 00:59:51 +00:00
|
|
|
|
);
|
2018-09-06 21:57:42 +00:00
|
|
|
|
Ok(this)
|
2017-07-22 00:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 23:07:10 +00:00
|
|
|
|
fn send(&mut self, stream: XMPPStream<S>, handshake: Handshake) {
|
2018-08-01 22:19:06 +00:00
|
|
|
|
let nonza = handshake;
|
|
|
|
|
let send = stream.send_stanza(nonza);
|
2017-07-22 00:59:51 +00:00
|
|
|
|
|
|
|
|
|
self.state = ComponentAuthState::WaitSend(send);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<S: AsyncRead + AsyncWrite> Future for ComponentAuth<S> {
|
|
|
|
|
type Item = XMPPStream<S>;
|
2018-09-06 15:46:06 +00:00
|
|
|
|
type Error = Error;
|
2017-07-22 00:59:51 +00:00
|
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
|
let state = replace(&mut self.state, ComponentAuthState::Invalid);
|
|
|
|
|
|
|
|
|
|
match state {
|
2018-12-18 18:04:31 +00:00
|
|
|
|
ComponentAuthState::WaitSend(mut send) => match send.poll() {
|
|
|
|
|
Ok(Async::Ready(stream)) => {
|
|
|
|
|
self.state = ComponentAuthState::WaitRecv(stream);
|
|
|
|
|
self.poll()
|
|
|
|
|
}
|
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
|
self.state = ComponentAuthState::WaitSend(send);
|
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(e)?,
|
|
|
|
|
},
|
|
|
|
|
ComponentAuthState::WaitRecv(mut stream) => match stream.poll() {
|
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
|
|
|
|
|
if stanza.is("handshake", NS_JABBER_COMPONENT_ACCEPT) =>
|
|
|
|
|
{
|
|
|
|
|
self.state = ComponentAuthState::Invalid;
|
|
|
|
|
Ok(Async::Ready(stream))
|
|
|
|
|
}
|
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
|
|
|
|
|
if stanza.is("error", "http://etherx.jabber.org/streams") =>
|
|
|
|
|
{
|
|
|
|
|
Err(AuthError::ComponentFail.into())
|
|
|
|
|
}
|
2019-01-26 18:28:57 +00:00
|
|
|
|
Ok(Async::Ready(_event)) => {
|
|
|
|
|
// println!("ComponentAuth ignore {:?}", _event);
|
2018-12-18 18:04:31 +00:00
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
|
}
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
self.state = ComponentAuthState::WaitRecv(stream);
|
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
|
}
|
|
|
|
|
Err(e) => Err(e)?,
|
|
|
|
|
},
|
|
|
|
|
ComponentAuthState::Invalid => unreachable!(),
|
2017-07-22 00:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|