2017-06-05 23:29:20 +00:00
|
|
|
use std::mem::replace;
|
2018-08-01 23:01:41 +00:00
|
|
|
use futures::{Future, Poll, Async, sink, Stream};
|
2017-06-05 23:29:20 +00:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
use sasl::common::Credentials;
|
2017-07-18 20:54:10 +00:00
|
|
|
use sasl::common::scram::{Sha1, Sha256};
|
2017-06-05 23:29:20 +00:00
|
|
|
use sasl::client::Mechanism;
|
2017-07-18 20:54:10 +00:00
|
|
|
use sasl::client::mechanisms::{Scram, Plain, Anonymous};
|
2018-08-01 23:44:48 +00:00
|
|
|
use serialize::base64::FromBase64;
|
2018-08-01 23:01:41 +00:00
|
|
|
use minidom::Element;
|
2018-08-01 23:44:48 +00:00
|
|
|
use xmpp_parsers::sasl::{Auth, Response, Mechanism as XMPPMechanism};
|
2017-06-05 23:29:20 +00:00
|
|
|
|
2017-07-18 20:54:10 +00:00
|
|
|
use xmpp_codec::Packet;
|
|
|
|
use xmpp_stream::XMPPStream;
|
|
|
|
use stream_start::StreamStart;
|
2017-06-05 23:29:20 +00:00
|
|
|
|
|
|
|
const NS_XMPP_SASL: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
|
|
|
|
|
|
|
|
pub struct ClientAuth<S: AsyncWrite> {
|
|
|
|
state: ClientAuthState<S>,
|
|
|
|
mechanism: Box<Mechanism>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ClientAuthState<S: AsyncWrite> {
|
|
|
|
WaitSend(sink::Send<XMPPStream<S>>),
|
|
|
|
WaitRecv(XMPPStream<S>),
|
2017-06-05 23:38:48 +00:00
|
|
|
Start(StreamStart<S>),
|
2017-06-05 23:29:20 +00:00
|
|
|
Invalid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsyncWrite> ClientAuth<S> {
|
|
|
|
pub fn new(stream: XMPPStream<S>, creds: Credentials) -> Result<Self, String> {
|
2018-08-01 23:44:48 +00:00
|
|
|
let mechs: Vec<(Box<Mechanism>, XMPPMechanism)> = vec![
|
|
|
|
// (Box::new(Scram::<Sha256>::from_credentials(creds.clone()).unwrap()),
|
|
|
|
// XMPPMechanism::ScramSha256
|
|
|
|
// ),
|
|
|
|
(Box::new(Scram::<Sha1>::from_credentials(creds.clone()).unwrap()),
|
|
|
|
XMPPMechanism::ScramSha1
|
|
|
|
),
|
|
|
|
(Box::new(Plain::from_credentials(creds).unwrap()),
|
|
|
|
XMPPMechanism::Plain
|
|
|
|
),
|
|
|
|
(Box::new(Anonymous::new()),
|
|
|
|
XMPPMechanism::Anonymous
|
|
|
|
),
|
2017-06-05 23:29:20 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let mech_names: Vec<String> =
|
2017-07-17 18:53:00 +00:00
|
|
|
match stream.stream_features.get_child("mechanisms", NS_XMPP_SASL) {
|
2017-06-05 23:29:20 +00:00
|
|
|
None =>
|
|
|
|
return Err("No auth mechanisms".to_owned()),
|
|
|
|
Some(mechs) =>
|
2017-07-17 18:53:00 +00:00
|
|
|
mechs.children()
|
|
|
|
.filter(|child| child.is("mechanism", NS_XMPP_SASL))
|
|
|
|
.map(|mech_el| mech_el.text())
|
2017-06-05 23:29:20 +00:00
|
|
|
.collect(),
|
|
|
|
};
|
2017-06-19 00:34:16 +00:00
|
|
|
println!("SASL mechanisms offered: {:?}", mech_names);
|
2017-06-05 23:29:20 +00:00
|
|
|
|
2018-08-01 23:44:48 +00:00
|
|
|
for (mut mech, mechanism) in mechs {
|
2017-06-05 23:29:20 +00:00
|
|
|
let name = mech.name().to_owned();
|
|
|
|
if mech_names.iter().any(|name1| *name1 == name) {
|
2017-06-19 00:34:16 +00:00
|
|
|
println!("SASL mechanism selected: {:?}", name);
|
2017-06-05 23:29:20 +00:00
|
|
|
let initial = try!(mech.initial());
|
|
|
|
let mut this = ClientAuth {
|
|
|
|
state: ClientAuthState::Invalid,
|
|
|
|
mechanism: mech,
|
|
|
|
};
|
|
|
|
this.send(
|
|
|
|
stream,
|
2018-08-01 23:44:48 +00:00
|
|
|
Auth {
|
|
|
|
mechanism,
|
|
|
|
data: initial,
|
|
|
|
}
|
2017-06-05 23:29:20 +00:00
|
|
|
);
|
|
|
|
return Ok(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err("No supported SASL mechanism available".to_owned())
|
|
|
|
}
|
|
|
|
|
2018-08-01 23:44:48 +00:00
|
|
|
fn send<N: Into<Element>>(&mut self, stream: XMPPStream<S>, nonza: N) {
|
2018-08-01 22:19:06 +00:00
|
|
|
let send = stream.send_stanza(nonza);
|
2017-06-05 23:29:20 +00:00
|
|
|
|
|
|
|
self.state = ClientAuthState::WaitSend(send);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
|
|
|
|
type Item = XMPPStream<S>;
|
|
|
|
type Error = String;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
let state = replace(&mut self.state, ClientAuthState::Invalid);
|
|
|
|
|
|
|
|
match state {
|
|
|
|
ClientAuthState::WaitSend(mut send) =>
|
|
|
|
match send.poll() {
|
|
|
|
Ok(Async::Ready(stream)) => {
|
|
|
|
self.state = ClientAuthState::WaitRecv(stream);
|
|
|
|
self.poll()
|
|
|
|
},
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
self.state = ClientAuthState::WaitSend(send);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(format!("{}", e)),
|
|
|
|
},
|
|
|
|
ClientAuthState::WaitRecv(mut stream) =>
|
|
|
|
match stream.poll() {
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
|
2017-08-14 01:56:08 +00:00
|
|
|
if stanza.is("challenge", NS_XMPP_SASL) =>
|
2017-06-05 23:29:20 +00:00
|
|
|
{
|
|
|
|
let content = try!(
|
2017-07-17 18:53:00 +00:00
|
|
|
stanza.text()
|
2017-06-05 23:29:20 +00:00
|
|
|
.from_base64()
|
|
|
|
.map_err(|e| format!("{}", e))
|
|
|
|
);
|
|
|
|
let response = try!(self.mechanism.response(&content));
|
2018-08-01 23:44:48 +00:00
|
|
|
self.send(stream, Response { data: response });
|
2017-06-05 23:29:20 +00:00
|
|
|
self.poll()
|
|
|
|
},
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
|
2017-08-14 01:56:08 +00:00
|
|
|
if stanza.is("success", NS_XMPP_SASL) =>
|
2017-06-05 23:38:48 +00:00
|
|
|
{
|
|
|
|
let start = stream.restart();
|
|
|
|
self.state = ClientAuthState::Start(start);
|
|
|
|
self.poll()
|
|
|
|
},
|
2017-06-05 23:29:20 +00:00
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(ref stanza))))
|
2017-08-14 01:56:08 +00:00
|
|
|
if stanza.is("failure", NS_XMPP_SASL) =>
|
2017-06-05 23:29:20 +00:00
|
|
|
{
|
2017-07-20 22:19:08 +00:00
|
|
|
let e = stanza.children().next()
|
|
|
|
.map(|child| child.name())
|
|
|
|
.unwrap_or("Authentication failure");
|
2017-07-17 18:53:00 +00:00
|
|
|
Err(e.to_owned())
|
2017-06-05 23:29:20 +00:00
|
|
|
},
|
|
|
|
Ok(Async::Ready(event)) => {
|
|
|
|
println!("ClientAuth ignore {:?}", event);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Ok(_) => {
|
|
|
|
self.state = ClientAuthState::WaitRecv(stream);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(format!("{}", e)),
|
|
|
|
},
|
2017-06-05 23:38:48 +00:00
|
|
|
ClientAuthState::Start(mut start) =>
|
|
|
|
match start.poll() {
|
|
|
|
Ok(Async::Ready(stream)) =>
|
|
|
|
Ok(Async::Ready(stream)),
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
self.state = ClientAuthState::Start(start);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(format!("{}", e)),
|
|
|
|
},
|
2017-06-05 23:29:20 +00:00
|
|
|
ClientAuthState::Invalid =>
|
|
|
|
unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|