From 73fbc8750da015a05951f9a9035b04689f7e6306 Mon Sep 17 00:00:00 2001 From: rasul Date: Tue, 28 Apr 2020 08:28:02 -0500 Subject: [PATCH] handle errors with passwords --- src/command/set/player/password.rs | 2 +- src/game/state/login.rs | 162 +++++++++++++++++------------ 2 files changed, 95 insertions(+), 69 deletions(-) diff --git a/src/command/set/player/password.rs b/src/command/set/player/password.rs index 86291fe..babc570 100644 --- a/src/command/set/player/password.rs +++ b/src/command/set/player/password.rs @@ -15,7 +15,7 @@ impl CommandSetPlayer { return (token, "Password can't be empty").into(); } - let password = Password::new(args); + let password = try_send_error!(token, Password::new(args)); let _ = try_send_error!(token, db.save_password(player.id, &password)); SendQueue::ok(token) diff --git a/src/game/state/login.rs b/src/game/state/login.rs index 3d939c2..4c616e1 100644 --- a/src/game/state/login.rs +++ b/src/game/state/login.rs @@ -83,15 +83,27 @@ impl Game { } else { match self.check_player_password(message) { PlayerCheck::Ok(pass) => { - send_queue.push( - token, - "\nNew password again: ", - false, - Some(State::Login(Login::CreatePassword2(( - username.to_owned(), - Password::new(pass), - )))), - ); + match Password::new(pass) { + Ok(password) => send_queue.push( + token, + "\nNew password again: ", + false, + Some(State::Login(Login::CreatePassword2(( + username.to_owned(), + password, + )))), + ), + Err(e) => { + log_error!("Hash error checking password :: {}", e); + send_queue.push(token, "\nError\n", false, None); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); + }, + }; } PlayerCheck::Err(err) => { send_queue.push(token, "\nInvalid password:\n", false, None); @@ -105,48 +117,55 @@ impl Game { } Login::CreatePassword2((username, pass)) => { - if !pass.check(message) { - send_queue.push(token, "\n\nPasswords don't match", false, None); - send_queue.push( - token, - "\n\nUsername: ", - false, - Some(State::Login(Login::Username)), - ); - } else { - if let Ok(id) = self.db.new_player_id() { - let player = Player { - id, - name: username.clone(), - created: Utc::now(), - location: self.config.player.starting_location.clone(), - }; + match pass.check(message) { + Ok(true) => { + if let Ok(id) = self.db.new_player_id() { + let player = Player { + id, + name: username.clone(), + created: Utc::now(), + location: self.config.player.starting_location.clone(), + }; - if self.db.single_save_player(token, &player).is_ok() - && self.db.save_password(player.id, pass).is_ok() - { - send_queue.push( - token, - format!("Welcome, {}\n", username), - false, - Some(State::Action), - ); - send_queue.push(token, "", true, None); + if self.db.single_save_player(token, &player).is_ok() + && self.db.save_password(player.id, pass).is_ok() + { + send_queue.push( + token, + format!("Welcome, {}\n", username), + false, + Some(State::Action), + ); + send_queue.push(token, "", true, None); - send_queue.append(&mut Command::dispatch_look( - &Command::default(), - String::new(), - token, - &mut self.db, - )); + send_queue.append(&mut Command::dispatch_look( + &Command::default(), + String::new(), + token, + &mut self.db, + )); + } else { + send_queue.push(token, "Error", true, None); + } } else { send_queue.push(token, "Error", true, None); } - } else { - send_queue.push(token, "Error", true, None); + }, + Ok(false) => { + send_queue.push(token, "\n\nPasswords don't match", false, None); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); + } + Err(e) => { + log_error!("Error creating password :: {}", e); + send_queue.push(token, "\nError\n\n", false, None); } } - } + }, Login::Password(username) => { if message.is_empty() { @@ -160,23 +179,34 @@ impl Game { match self.db.find_player_by_name(username) { Ok(Some(player)) => match self.db.get_password(player.id) { Ok(Some(password)) => { - if password.check(message) { - if self.db.save_connected_player(token, &player).is_ok() { - send_queue.push( - token, - format!("Welcome back, {}\n\n", username), - false, - Some(State::Action), - ); + match password.check(message) { + Ok(true) => { + if self.db.save_connected_player(token, &player).is_ok() { + send_queue.push( + token, + format!("Welcome back, {}\n\n", username), + false, + Some(State::Action), + ); - send_queue.append(&mut Command::dispatch_look( - &Command::default(), - String::new(), - token, - &mut self.db, - )); - } else { - send_queue.push(token, "Unable to login\n", false, None); + send_queue.append(&mut Command::dispatch_look( + &Command::default(), + String::new(), + token, + &mut self.db, + )); + } else { + send_queue.push(token, "Unable to login\n", false, None); + send_queue.push( + token, + "\n\nUsername: ", + false, + Some(State::Login(Login::Username)), + ); + } + } + Ok(false) => { + send_queue.push(token, "Incorrect password\n", false, None); send_queue.push( token, "\n\nUsername: ", @@ -184,14 +214,10 @@ impl Game { Some(State::Login(Login::Username)), ); } - } else { - send_queue.push(token, "Incorrect password\n", false, None); - send_queue.push( - token, - "\n\nUsername: ", - false, - Some(State::Login(Login::Username)), - ); + Err(e) => { + log_error!("Error creating password :: {}", e); + send_queue.push(token, "\nError\n\n", false, None); + } } } Ok(None) => {