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,8 +117,7 @@ impl Game {
}
}
Login::CreatePassword2((username, pass)) => {
match pass.check(message) {
Login::CreatePassword2((username, pass)) => match pass.check(message) {
Ok(true) => {
if let Ok(id) = self.db.new_player_id() {
let player = Player {
@ -151,7 +150,7 @@ impl Game {
} else {
send_queue.push(token, "Error", true, None);
}
},
}
Ok(false) => {
send_queue.push(token, "\n\nPasswords don't match", false, None);
send_queue.push(
@ -165,7 +164,6 @@ impl Game {
log_error!("Error creating password :: {}", e);
send_queue.push(token, "\nError\n\n", false, None);
}
}
},
Login::Password(username) => {
@ -179,8 +177,7 @@ 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(Some(password)) => match password.check(message) {
Ok(true) => {
if self.db.save_connected_player(token, &player).is_ok() {
send_queue.push(
@ -219,8 +216,7 @@ impl Game {
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