diff --git a/src/app.rs b/src/app.rs index 51c120e..05833b4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,7 +10,7 @@ use futures_signals::signal::{Mutable, Signal, MutableSignalCloned, MutableLockM use termion::event::Key; use crate::input::Inputs; -use crate::schema::API; +use crate::schema::{API, Authentication}; use crate::commands::{CommandParser, Commands}; use smol::future::FutureExt; @@ -62,30 +62,37 @@ impl<'a, S: Unpin> Sute<'a, S> { } fn run_cmd(&mut self, cmdline: String) { - let mut words = cmdline.split_ascii_whitespace().map(|s| s.to_string()); + let words = cmdline.split_ascii_whitespace().map(|s| s.to_string()); match self.cmds.parse(words) { Ok(matches) => { match matches.subcommand() { Some(("quit", _)) => self.state.lock_mut().running = false, Some(("authenticate", m)) => { - if let Some(mut api) = self.api.clone() { - let log2 = self.log.clone(); - info!(self.log, "Going for an auth trip"); - let f = api.authentication().then(move |mut auth| { - info!(log2, "Hey, an auth step!"); - auth.mechanisms().map(move |mechs| { - info!(log2, "Oh yeah about `authenticate` of yours:"); - for mech in mechs { - info!(log2, "Mech: {}", mech); - } - }) - }); - let old_f = self.future.replace(Box::pin(f)); - if !old_f.is_none() { - warn!(self.log, "Dropping a future"); - } + let u = m.value_of("user"); + let p = m.value_of("pass"); + if u.is_none() || p.is_none() { + error!(self.log, "authenticate "); } else { - error!(self.log, "No connection, can't authenticate!"); + if let Some(mut api) = self.api.clone() { + let log2 = self.log.clone(); + info!(self.log, "Going for an auth trip"); + + let u = u.unwrap().to_string(); + let p = p.unwrap().to_string(); + + let f = api.authentication().then(move |mut auth| { + auth.authenticate(u, p).then(move |res| { + info!(log2, "Authentication result: {}", res); + futures::future::ready(()) + }) + }); + let old_f = self.future.replace(Box::pin(f)); + if !old_f.is_none() { + warn!(self.log, "Dropping a future"); + } + } else { + error!(self.log, "No connection, can't authenticate!"); + } } } Some((s, m)) => info!(self.log, "Got Command {} with params {:?}", s, m), diff --git a/src/commands.rs b/src/commands.rs index 2f033ba..cddd962 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -27,7 +27,7 @@ impl<'help> CommandParser<'help> { .subcommand(App::new("authenticate") .arg(Arg::new("user") .takes_value(true)) - .arg(Arg::new("password") + .arg(Arg::new("pass") .takes_value(true)) ) , diff --git a/src/schema/authentication.rs b/src/schema/authentication.rs index 59b4f07..9577c05 100644 --- a/src/schema/authentication.rs +++ b/src/schema/authentication.rs @@ -31,4 +31,27 @@ impl Authentication { tmp.get_mechs().unwrap().iter().map(|x| x.unwrap().to_string()).collect() }) } + + pub fn authenticate(&mut self, username: String, password: String) -> impl Future { + let mut req = self.inner.start_request(); + let mut builder = req.get().init_request(); + builder.set_mechanism("PLAIN"); + let mut init_data = builder.init_initial_response(); + init_data.set_initial(format!("\0{}\0{}", username, password).as_ref()); + let response = req.send().promise; + response.map(|res| { + let res = res.unwrap(); + let tmp = res.get().unwrap().get_response().unwrap(); + match tmp.which().unwrap() { + super::auth_capnp::response::Which::Challence(_) => false, + super::auth_capnp::response::Which::Outcome(outcome) => { + if outcome.get_result().unwrap() == super::auth_capnp::response::Result::Successful { + return true; + } else { + return false; + } + } + } + }) + } }