client::auth: update xmpp-parsers usage

This commit is contained in:
Astro 2018-08-03 01:11:55 +02:00
parent b82237f422
commit a3db744e46
2 changed files with 11 additions and 16 deletions

View file

@ -22,7 +22,7 @@ tokio-tls = "0.1"
sasl = "0.4" sasl = "0.4"
jid = { version = "0.5", features = ["minidom"] } jid = { version = "0.5", features = ["minidom"] }
domain = "0.2" domain = "0.2"
xmpp-parsers = "0.10" xmpp-parsers = "0.11"
idna = "0.1" idna = "0.1"
try_from = "0.2" try_from = "0.2"
quick-xml = "0.12" quick-xml = "0.12"

View file

@ -1,4 +1,5 @@
use std::mem::replace; use std::mem::replace;
use std::str::FromStr;
use futures::{Future, Poll, Async, sink, Stream}; use futures::{Future, Poll, Async, sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use sasl::common::Credentials; use sasl::common::Credentials;
@ -29,19 +30,11 @@ enum ClientAuthState<S: AsyncWrite> {
impl<S: AsyncWrite> ClientAuth<S> { impl<S: AsyncWrite> ClientAuth<S> {
pub fn new(stream: XMPPStream<S>, creds: Credentials) -> Result<Self, String> { pub fn new(stream: XMPPStream<S>, creds: Credentials) -> Result<Self, String> {
let mechs: Vec<(Box<Mechanism>, XMPPMechanism)> = vec![ let mechs: Vec<Box<Mechanism>> = vec![
(Box::new(Scram::<Sha256>::from_credentials(creds.clone()).unwrap()), Box::new(Scram::<Sha256>::from_credentials(creds.clone()).unwrap()),
XMPPMechanism::ScramSha256 Box::new(Scram::<Sha1>::from_credentials(creds.clone()).unwrap()),
), Box::new(Plain::from_credentials(creds).unwrap()),
(Box::new(Scram::<Sha1>::from_credentials(creds.clone()).unwrap()), Box::new(Anonymous::new()),
XMPPMechanism::ScramSha1
),
(Box::new(Plain::from_credentials(creds).unwrap()),
XMPPMechanism::Plain
),
(Box::new(Anonymous::new()),
XMPPMechanism::Anonymous
),
]; ];
let mech_names: Vec<String> = let mech_names: Vec<String> =
@ -56,7 +49,7 @@ impl<S: AsyncWrite> ClientAuth<S> {
}; };
println!("SASL mechanisms offered: {:?}", mech_names); println!("SASL mechanisms offered: {:?}", mech_names);
for (mut mech, mechanism) in mechs { for mut mech in mechs {
let name = mech.name().to_owned(); let name = mech.name().to_owned();
if mech_names.iter().any(|name1| *name1 == name) { if mech_names.iter().any(|name1| *name1 == name) {
println!("SASL mechanism selected: {:?}", name); println!("SASL mechanism selected: {:?}", name);
@ -65,6 +58,8 @@ impl<S: AsyncWrite> ClientAuth<S> {
state: ClientAuthState::Invalid, state: ClientAuthState::Invalid,
mechanism: mech, mechanism: mech,
}; };
let mechanism = XMPPMechanism::from_str(&name)
.map_err(|e| format!("{:?}", e))?;
this.send( this.send(
stream, stream,
Auth { Auth {
@ -119,7 +114,7 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientAuth<S> {
self.state = ClientAuthState::Start(start); self.state = ClientAuthState::Start(start);
self.poll() self.poll()
} else if let Ok(failure) = Failure::try_from(stanza) { } else if let Ok(failure) = Failure::try_from(stanza) {
let e = failure.data; let e = format!("{:?}", failure.defined_condition);
Err(e) Err(e)
} else { } else {
Ok(Async::NotReady) Ok(Async::NotReady)