master
rasul 4 years ago
parent 2d19be25d9
commit cea795b99d

@ -64,9 +64,15 @@ pub fn player_name<S: Into<String>>(name: S) -> PlayerCheck {
let mut err_vec = Vec::<String>::new();
if name.len() < NAME_MIN.with(|n| *n.borrow()) {
err_vec.push(format!("{} character minimum", NAME_MIN.with(|n| *n.borrow())));
err_vec.push(format!(
"{} character minimum",
NAME_MIN.with(|n| *n.borrow())
));
} else if name.len() > NAME_MAX.with(|n| *n.borrow()) {
err_vec.push(format!("{} character maximum", NAME_MAX.with(|n| *n.borrow())));
err_vec.push(format!(
"{} character maximum",
NAME_MAX.with(|n| *n.borrow())
));
}
for c in name.chars() {
@ -92,9 +98,15 @@ pub fn player_password<S: Into<String>>(password: S) -> PlayerCheck {
let mut err_vec = Vec::<String>::new();
if password.len() < PASS_MIN.with(|p| *p.borrow()) {
err_vec.push(format!("{} character minimum", PASS_MIN.with(|p| *p.borrow())));
err_vec.push(format!(
"{} character minimum",
PASS_MIN.with(|p| *p.borrow())
));
} else if password.len() > PASS_MAX.with(|p| *p.borrow()) {
err_vec.push(format!("{} character maximum", PASS_MAX.with(|p| *p.borrow())));
err_vec.push(format!(
"{} character maximum",
PASS_MAX.with(|p| *p.borrow())
));
}
for c in password.chars() {

@ -5,12 +5,7 @@ use crate::database::Db;
use crate::queue::SendQueue;
impl Command {
pub fn dispatch_help(
command: &Command,
args: String,
token: Token,
_db: &mut Db,
) -> SendQueue {
pub fn dispatch_help(command: &Command, args: String, token: Token, _db: &mut Db) -> SendQueue {
if args.is_empty() {
return (token, command.commands()).into();
}

@ -103,7 +103,7 @@ impl Game {
false,
Some(State::Login(Login::Username)),
);
},
}
};
}
check::PlayerCheck::Err(err) => {
@ -117,55 +117,53 @@ impl Game {
}
}
Login::CreatePassword2((username, pass)) => {
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(),
};
Login::CreatePassword2((username, pass)) => 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,
));
} else {
send_queue.push(token, "Error", true, None);
}
send_queue.append(&mut Command::dispatch_look(
&Command::default(),
String::new(),
token,
&mut self.db,
));
} 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);
} 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) => {
@ -179,35 +177,24 @@ impl Game {
} else {
match self.db.find_player_by_name(username) {
Ok(Some(player)) => match self.db.get_password(player.id) {
Ok(Some(password)) => {
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),
);
Ok(Some(password)) => 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.push(
token,
"\n\nUsername: ",
false,
Some(State::Login(Login::Username)),
);
}
}
Ok(false) => {
send_queue.push(token, "Incorrect password\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: ",
@ -215,12 +202,21 @@ impl Game {
Some(State::Login(Login::Username)),
);
}
Err(e) => {
log_error!("Error creating password :: {}", e);
send_queue.push(token, "\nError\n\n", false, None);
}
}
}
Ok(false) => {
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) => {
log_error!("Player has no password: {}", player.id);
send_queue.push(token, "Error\n", false, None);

@ -63,6 +63,9 @@ mod tests {
let salt = "cucumbers";
let hashed = hash(password, salt).unwrap();
assert_eq!(hashed, "$argon2i$v=19$m=4096,t=3,p=1$Y3VjdW1iZXJz$r5IYZSVjTOQoW9bGSv3GseB6pcOSEK8btQ8ftJX5wmE");
assert_eq!(
hashed,
"$argon2i$v=19$m=4096,t=3,p=1$Y3VjdW1iZXJz$r5IYZSVjTOQoW9bGSv3GseB6pcOSEK8btQ8ftJX5wmE"
);
}
}

@ -33,10 +33,7 @@ impl Password {
/// Check the password against the provided password.
pub fn check<S: Into<String>>(&self, s: S) -> RudeResult<bool> {
let s = s.into();
let hash = try_log!(
hash(s, &self.salt),
"Unable to check hash",
);
let hash = try_log!(hash(s, &self.salt), "Unable to check hash",);
Ok(self.hash == hash)
}

Loading…
Cancel
Save