diff --git a/Cargo.toml b/Cargo.toml index fab462b..53c0a28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,6 @@ minidom = "0.9" native-tls = "0.1" tokio-tls = "0.1" sasl = "0.4" -rustc-serialize = "0.3" jid = { version = "0.5", features = ["minidom"] } domain = "0.2" xmpp-parsers = "0.10" diff --git a/src/client/auth.rs b/src/client/auth.rs index 575b1d4..1953dad 100644 --- a/src/client/auth.rs +++ b/src/client/auth.rs @@ -5,9 +5,9 @@ use sasl::common::Credentials; use sasl::common::scram::{Sha1, Sha256}; use sasl::client::Mechanism; use sasl::client::mechanisms::{Scram, Plain, Anonymous}; -use serialize::base64::FromBase64; use minidom::Element; -use xmpp_parsers::sasl::{Auth, Response, Mechanism as XMPPMechanism}; +use xmpp_parsers::sasl::{Auth, Challenge, Response, Success, Failure, Mechanism as XMPPMechanism}; +use try_from::TryFrom; use xmpp_codec::Packet; use xmpp_stream::XMPPStream; @@ -30,9 +30,9 @@ enum ClientAuthState { impl ClientAuth { pub fn new(stream: XMPPStream, creds: Credentials) -> Result { let mechs: Vec<(Box, XMPPMechanism)> = vec![ - // (Box::new(Scram::::from_credentials(creds.clone()).unwrap()), - // XMPPMechanism::ScramSha256 - // ), + (Box::new(Scram::::from_credentials(creds.clone()).unwrap()), + XMPPMechanism::ScramSha256 + ), (Box::new(Scram::::from_credentials(creds.clone()).unwrap()), XMPPMechanism::ScramSha1 ), @@ -109,33 +109,22 @@ impl Future for ClientAuth { }, ClientAuthState::WaitRecv(mut stream) => match stream.poll() { - Ok(Async::Ready(Some(Packet::Stanza(ref stanza)))) - if stanza.is("challenge", NS_XMPP_SASL) => - { - let content = try!( - stanza.text() - .from_base64() - .map_err(|e| format!("{}", e)) - ); - let response = try!(self.mechanism.response(&content)); - self.send(stream, Response { data: response }); - self.poll() - }, - Ok(Async::Ready(Some(Packet::Stanza(ref stanza)))) - if stanza.is("success", NS_XMPP_SASL) => - { - let start = stream.restart(); - self.state = ClientAuthState::Start(start); - self.poll() - }, - Ok(Async::Ready(Some(Packet::Stanza(ref stanza)))) - if stanza.is("failure", NS_XMPP_SASL) => - { - let e = stanza.children().next() - .map(|child| child.name()) - .unwrap_or("Authentication failure"); - Err(e.to_owned()) - }, + Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => { + if let Ok(challenge) = Challenge::try_from(stanza.clone()) { + let response = try!(self.mechanism.response(&challenge.data)); + self.send(stream, Response { data: response }); + self.poll() + } else if let Ok(_) = Success::try_from(stanza.clone()) { + let start = stream.restart(); + self.state = ClientAuthState::Start(start); + self.poll() + } else if let Ok(failure) = Failure::try_from(stanza) { + let e = failure.data; + Err(e) + } else { + Ok(Async::NotReady) + } + } Ok(Async::Ready(event)) => { println!("ClientAuth ignore {:?}", event); Ok(Async::NotReady) diff --git a/src/lib.rs b/src/lib.rs index 6ee15d1..3ae6405 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,6 @@ extern crate minidom; extern crate native_tls; extern crate tokio_tls; extern crate sasl; -extern crate rustc_serialize as serialize; extern crate jid; extern crate domain; extern crate idna;