From 157c4816b3ce823f4c4c25de8a1ca9092b7a9d53 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Wed, 7 Aug 2019 15:42:44 -0400 Subject: [PATCH 01/11] laying the groundwork --- client/src/error.rs | 1 + voxygen/src/menu/main/client_init.rs | 4 +++ voxygen/src/menu/main/mod.rs | 5 +++- voxygen/src/menu/main/ui.rs | 40 +++++++++++++++++++++++++++- voxygen/src/settings.rs | 2 ++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/client/src/error.rs b/client/src/error.rs index 20f3319649..27ff6b9142 100644 --- a/client/src/error.rs +++ b/client/src/error.rs @@ -7,6 +7,7 @@ pub enum Error { ServerTimeout, ServerShutdown, TooManyPlayers, + InvalidAuth, //TODO: InvalidAlias, Other(String), } diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index ee07025a55..a4dc1ba7da 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -19,6 +19,7 @@ pub enum Error { NoAddress, // Parsing/host name resolution successful but could not connect. ConnectionFailed(ClientError), + InvalidAuth, ClientCrashed, ServerIsFull, } @@ -81,6 +82,9 @@ impl ClientInit { last_err = Some(Error::ServerIsFull); break; } + ClientError::InvalidAuth => { + last_err = Some(Error::InvalidAuth); + } // TODO: Handle errors? _ => panic!( "Unexpected non-network error when creating client: {:?}", diff --git a/voxygen/src/menu/main/mod.rs b/voxygen/src/menu/main/mod.rs index 13f24e53d3..33f7d22493 100644 --- a/voxygen/src/menu/main/mod.rs +++ b/voxygen/src/menu/main/mod.rs @@ -64,6 +64,7 @@ impl PlayState for MainMenuState { self.main_menu_ui.login_error( match err { InitError::BadAddress(_) | InitError::NoAddress => "Server not found", + InitError::InvalidAuth => "Invalid username or password", InitError::ServerIsFull => "Server is Full!", InitError::ConnectionFailed(_) => "Connection failed", InitError::ClientCrashed => "Client crashed", @@ -82,10 +83,12 @@ impl PlayState for MainMenuState { match event { MainMenuEvent::LoginAttempt { username, + password, server_address, } => { let mut net_settings = &mut global_state.settings.networking; net_settings.username = username.clone(); + net_settings.password = password.clone(); if !net_settings.servers.contains(&server_address) { net_settings.servers.push(server_address.clone()); } @@ -107,7 +110,7 @@ impl PlayState for MainMenuState { ))); } else { self.main_menu_ui - .login_error("Invalid username".to_string()); + .login_error("Invalid username or password".to_string()); } } MainMenuEvent::StartSingleplayer => { diff --git a/voxygen/src/menu/main/ui.rs b/voxygen/src/menu/main/ui.rs index 3b7941d24b..b1602adc72 100644 --- a/voxygen/src/menu/main/ui.rs +++ b/voxygen/src/menu/main/ui.rs @@ -38,10 +38,14 @@ widget_ids! { username_text, username_bg, username_field, + password_text, + password_bg, + password_field, singleplayer_button, singleplayer_text, usrnm_bg, srvr_bg, + passwd_bg, // Server list servers_button, servers_frame, @@ -87,6 +91,7 @@ font_ids! { pub enum Event { LoginAttempt { username: String, + password: String, server_address: String, }, StartSingleplayer, @@ -101,6 +106,7 @@ pub struct MainMenuUi { imgs: Imgs, fonts: Fonts, username: String, + password: String, server_address: String, login_error: Option, connecting: Option, @@ -129,6 +135,7 @@ impl MainMenuUi { imgs, fonts, username: networking.username.clone(), + password: "".to_owned(), server_address: networking.servers[networking.default_server].clone(), login_error: None, connecting: None, @@ -227,6 +234,7 @@ impl MainMenuUi { self.connecting = Some(std::time::Instant::now()); events.push(Event::LoginAttempt { username: self.username.clone(), + password: self.password.clone(), server_address: self.server_address.clone(), }); }; @@ -240,6 +248,7 @@ impl MainMenuUi { events.push(Event::StartSingleplayer); events.push(Event::LoginAttempt { username: "singleplayer".to_string(), + password: String::default(), server_address: "localhost".to_string(), }); }; @@ -274,6 +283,35 @@ impl MainMenuUi { } } } + // Password + Rectangle::fill_with([320.0, 50.0], color::rgba(0.0, 0.0, 0.0, 0.97)) + .down_from(self.ids.usrnm_bg, 30.0) + .set(self.ids.passwd_bg, ui_widgets); + Image::new(self.imgs.input_bg) + .w_h(337.0, 67.0) + .middle_of(self.ids.passwd_bg) + .set(self.ids.password_bg, ui_widgets); + for event in TextBox::new(&self.password) + .w_h(290.0, 30.0) + .mid_bottom_with_margin_on(self.ids.password_bg, 44.0 / 2.0) + .font_size(22) + .font_id(self.fonts.opensans) + .text_color(TEXT_COLOR) + // transparent background + .color(TRANSPARENT) + .border_color(TRANSPARENT) + .set(self.ids.password_field, ui_widgets) + { + match event { + TextBoxEvent::Update(password) => { + // Note: TextBox limits the input string length to what fits in it + self.password = password; + } + TextBoxEvent::Enter => { + login!(); + } + } + } // Login error if let Some(msg) = &self.login_error { let text = Text::new(&msg) @@ -369,7 +407,7 @@ impl MainMenuUi { } // Server address Rectangle::fill_with([320.0, 50.0], color::rgba(0.0, 0.0, 0.0, 0.97)) - .down_from(self.ids.usrnm_bg, 30.0) + .down_from(self.ids.passwd_bg, 30.0) .set(self.ids.srvr_bg, ui_widgets); Image::new(self.imgs.input_bg) .w_h(337.0, 67.0) diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 8495bde99a..46b11a77c1 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -100,6 +100,7 @@ impl Default for GameplaySettings { #[serde(default)] pub struct NetworkingSettings { pub username: String, + pub password: String, pub servers: Vec, pub default_server: usize, } @@ -108,6 +109,7 @@ impl Default for NetworkingSettings { fn default() -> Self { Self { username: "Username".to_string(), + password: String::default(), servers: vec!["server.veloren.net".to_string()], default_server: 0, } From 6d94d430215b6e5f7979875b833f13fdc8da2e91 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Wed, 7 Aug 2019 23:56:02 -0400 Subject: [PATCH 02/11] still compiles --- chat-cli/src/main.rs | 5 ++++- client/src/lib.rs | 11 ++++++++--- common/src/msg/client.rs | 1 + common/src/msg/server.rs | 1 + server/src/lib.rs | 19 +++++++++++++++++-- voxygen/src/menu/main/client_init.rs | 10 ++++++++-- voxygen/src/menu/main/mod.rs | 1 + voxygen/src/menu/main/start_singleplayer.rs | 1 + 8 files changed, 41 insertions(+), 8 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index e6920e8dee..dea1044b75 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -30,6 +30,9 @@ fn main() { println!("Enter the server address"); let server_addr = read_input(); + println!("Enter your password"); + let password = read_input(); + // Create a client. let mut client = Client::new( server_addr @@ -45,7 +48,7 @@ fn main() { println!("Players online: {:?}", client.get_players()); - client.register(comp::Player::new(username, None)); + client.register(comp::Player::new(username, None), password); let (tx, rx) = mpsc::channel(); thread::spawn(move || loop { diff --git a/client/src/lib.rs b/client/src/lib.rs index a296cb567b..82e162d1d3 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -132,9 +132,13 @@ impl Client { } /// Request a state transition to `ClientState::Registered`. - pub fn register(&mut self, player: comp::Player) { - self.postbox.send_message(ClientMsg::Register { player }); - self.client_state = ClientState::Pending; + pub fn register(&mut self, player: comp::Player, password: String) -> Result<(), Error> { + self.postbox.send_message(ClientMsg::Register { player, password }); + /*match self.postbox.next_message() { + + } + self.client_state = ClientState::Pending;*/ + Err(Error::InvalidAuth) } /// Request a state transition to `ClientState::Character`. @@ -397,6 +401,7 @@ impl Client { match msg { ServerMsg::Error(e) => match e { ServerError::TooManyPlayers => return Err(Error::ServerWentMad), + ServerError::InvalidAuth => return Err(Error::InvalidAuth), //TODO: ServerError::InvalidAlias => return Err(Error::InvalidAlias), }, ServerMsg::Shutdown => return Err(Error::ServerShutdown), diff --git a/common/src/msg/client.rs b/common/src/msg/client.rs index cc325415d8..f5c7bf9f7b 100644 --- a/common/src/msg/client.rs +++ b/common/src/msg/client.rs @@ -7,6 +7,7 @@ use vek::*; pub enum ClientMsg { Register { player: comp::Player, + password: String, }, Character { name: String, diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index 8686e6f089..5d5b8a83a3 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -69,6 +69,7 @@ pub enum ServerMsg { #[derive(Debug, Clone, Serialize, Deserialize)] pub enum ServerError { TooManyPlayers, + InvalidAuth, //TODO: InvalidAlias, } diff --git a/server/src/lib.rs b/server/src/lib.rs index f0cbf76fd5..e4d2de309b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -26,7 +26,7 @@ use log::debug; use rand::Rng; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; use std::{ - collections::HashSet, + collections::{HashSet, HashMap}, i32, net::SocketAddr, sync::{mpsc, Arc}, @@ -68,6 +68,9 @@ pub struct Server { server_settings: ServerSettings, server_info: ServerInfo, + + // TODO: anything but this + accounts: HashMap, } impl Server { @@ -107,6 +110,7 @@ impl Server { description: settings.server_description.clone(), git_hash: common::util::GIT_HASH.to_string(), }, + accounts: HashMap::new(), server_settings: settings, }; @@ -468,6 +472,8 @@ impl Server { fn handle_new_messages(&mut self) -> Result, Error> { let mut frontend_events = Vec::new(); + let accounts = &mut self.accounts; + let state = &mut self.state; let mut new_chat_msgs = Vec::new(); let mut disconnected_clients = Vec::new(); @@ -522,7 +528,16 @@ impl Server { ClientState::Pending => {} }, // Valid player - ClientMsg::Register { player } if player.is_valid() => { + ClientMsg::Register { player, password } if player.is_valid() => { + if let Some(pass) = accounts.get(&player.alias) { + if pass != &password { + client.error_state(RequestStateError::Denied); + break; + } + } else { + accounts.insert(player.alias.clone(), password); + } + println!("{:?}", accounts); match client.client_state { ClientState::Connected => { Self::initialize_player(state, entity, client, player); diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index a4dc1ba7da..3b43d5a3d6 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -30,7 +30,7 @@ pub struct ClientInit { rx: Receiver>, } impl ClientInit { - pub fn new(connection_args: (String, u16, bool), player: comp::Player, wait: bool) -> Self { + pub fn new(connection_args: (String, u16, bool), player: comp::Player, password: String, wait: bool) -> Self { let (server_address, default_port, prefer_ipv6) = connection_args; let (tx, rx) = channel(); @@ -57,7 +57,13 @@ impl ClientInit { for socket_addr in first_addrs.into_iter().chain(second_addrs) { match Client::new(socket_addr, player.view_distance) { Ok(mut client) => { - client.register(player); + match client.register(player, password) { + Err(ClientError::InvalidAuth) => { + last_err = Some(Error::InvalidAuth); + break; + } + _ => {} + } let _ = tx.send(Ok(client)); #[cfg(feature = "discord")] diff --git a/voxygen/src/menu/main/mod.rs b/voxygen/src/menu/main/mod.rs index 33f7d22493..8ed9b98357 100644 --- a/voxygen/src/menu/main/mod.rs +++ b/voxygen/src/menu/main/mod.rs @@ -106,6 +106,7 @@ impl PlayState for MainMenuState { client_init = client_init.or(Some(ClientInit::new( (server_address, DEFAULT_PORT, false), player, + password, false, ))); } else { diff --git a/voxygen/src/menu/main/start_singleplayer.rs b/voxygen/src/menu/main/start_singleplayer.rs index f497767088..1f97cde4e2 100644 --- a/voxygen/src/menu/main/start_singleplayer.rs +++ b/voxygen/src/menu/main/start_singleplayer.rs @@ -38,6 +38,7 @@ impl PlayState for StartSingleplayerState { username.clone(), Some(global_state.settings.graphics.view_distance), ), + String::default(), true, ); From f2ed2870c64ce147de615f3e90bc1e8a52ed61a0 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 11:23:58 -0400 Subject: [PATCH 03/11] 90% there --- chat-cli/src/main.rs | 2 +- client/src/lib.rs | 22 +++++++++++++++++----- server/src/lib.rs | 6 ++++++ voxygen/src/menu/main/client_init.rs | 1 + 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index dea1044b75..f8d114c7fc 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -48,7 +48,7 @@ fn main() { println!("Players online: {:?}", client.get_players()); - client.register(comp::Player::new(username, None), password); + client.register(comp::Player::new(username, None), password).unwrap(); let (tx, rx) = mpsc::channel(); thread::spawn(move || loop { diff --git a/client/src/lib.rs b/client/src/lib.rs index 82e162d1d3..96f269bb68 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -8,7 +8,7 @@ pub use specs::{join::Join, saveload::Marker, Entity as EcsEntity, ReadStorage}; use common::{ comp, - msg::{ClientMsg, ClientState, ServerError, ServerInfo, ServerMsg}, + msg::{ClientMsg, ClientState, ServerError, ServerInfo, ServerMsg, RequestStateError}, net::PostBox, state::{State, Uid}, terrain::{block::Block, chonk::ChonkMetrics, TerrainChunk, TerrainChunkSize}, @@ -134,11 +134,23 @@ impl Client { /// Request a state transition to `ClientState::Registered`. pub fn register(&mut self, player: comp::Player, password: String) -> Result<(), Error> { self.postbox.send_message(ClientMsg::Register { player, password }); - /*match self.postbox.next_message() { - + loop { + match self.postbox.next_message() { + Some(ServerMsg::StateAnswer(Err((RequestStateError::Denied, _)))) => { + println!("Got a bad"); + break Err(Error::InvalidAuth) + }, + Some(ServerMsg::StateAnswer(Ok(ClientState::Registered))) => { + println!("Got a good"); + break Ok(()) + } + Some(x) => { + println!("Got unusual message: {:?}", x); + } + None => { println!("Got nothing?"); }, + + } } - self.client_state = ClientState::Pending;*/ - Err(Error::InvalidAuth) } /// Request a state transition to `ClientState::Character`. diff --git a/server/src/lib.rs b/server/src/lib.rs index e4d2de309b..94ae8a221d 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -530,11 +530,16 @@ impl Server { // Valid player ClientMsg::Register { player, password } if player.is_valid() => { if let Some(pass) = accounts.get(&player.alias) { + println!("Verifying {} with {} against {}", + player.alias, password, pass); if pass != &password { client.error_state(RequestStateError::Denied); break; } } else { + + println!("Adding user {} with password {}", + player.alias, password); accounts.insert(player.alias.clone(), password); } println!("{:?}", accounts); @@ -545,6 +550,7 @@ impl Server { // Use RequestState instead (No need to send `player` again). _ => client.error_state(RequestStateError::Impossible), } + //client.allow_state(ClientState::Registered); } // Invalid player ClientMsg::Register { .. } => { diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 3b43d5a3d6..a0a0362d8d 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -64,6 +64,7 @@ impl ClientInit { } _ => {} } + println!("Auth success"); let _ = tx.send(Ok(client)); #[cfg(feature = "discord")] From de2082469d6a4c9ecd290ffe3cc37ffaecc97f66 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 12:01:15 -0400 Subject: [PATCH 04/11] auth done, no popup yet --- client/src/lib.rs | 11 ++++++++--- common/src/msg/server.rs | 2 +- voxygen/src/menu/main/client_init.rs | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 96f269bb68..1fb2903464 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -132,9 +132,10 @@ impl Client { } /// Request a state transition to `ClientState::Registered`. - pub fn register(&mut self, player: comp::Player, password: String) -> Result<(), Error> { + pub fn register(&mut self, player: comp::Player, password: String) /*-> Result<(), Error>*/ { self.postbox.send_message(ClientMsg::Register { player, password }); - loop { + self.client_state = ClientState::Pending; + /*loop { match self.postbox.next_message() { Some(ServerMsg::StateAnswer(Err((RequestStateError::Denied, _)))) => { println!("Got a bad"); @@ -150,7 +151,7 @@ impl Client { None => { println!("Got nothing?"); }, } - } + }*/ } /// Request a state transition to `ClientState::Character`. @@ -470,6 +471,10 @@ impl Client { self.client_state = state; } ServerMsg::StateAnswer(Err((error, state))) => { + if error == RequestStateError::Denied { + warn!("Connection denied!"); + return Err(Error::InvalidAuth); + } warn!( "StateAnswer: {:?}. Server thinks client is in state {:?}.", error, state diff --git a/common/src/msg/server.rs b/common/src/msg/server.rs index 5d5b8a83a3..8e92e0d2be 100644 --- a/common/src/msg/server.rs +++ b/common/src/msg/server.rs @@ -7,7 +7,7 @@ use crate::{ use fxhash::FxHashMap; use vek::*; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub enum RequestStateError { Denied, Already, diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index a0a0362d8d..8896f62095 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -57,14 +57,15 @@ impl ClientInit { for socket_addr in first_addrs.into_iter().chain(second_addrs) { match Client::new(socket_addr, player.view_distance) { Ok(mut client) => { - match client.register(player, password) { + /*match client.register(player, password) { Err(ClientError::InvalidAuth) => { last_err = Some(Error::InvalidAuth); break; } _ => {} } - println!("Auth success"); + println!("Auth success");*/ + client.register(player, password); let _ = tx.send(Ok(client)); #[cfg(feature = "discord")] From 58e8bd4af91c127ae64a18506475049786fe6f3d Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 12:03:12 -0400 Subject: [PATCH 05/11] removed println statements --- server/src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/server/src/lib.rs b/server/src/lib.rs index 94ae8a221d..b6f32c6a40 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -530,19 +530,14 @@ impl Server { // Valid player ClientMsg::Register { player, password } if player.is_valid() => { if let Some(pass) = accounts.get(&player.alias) { - println!("Verifying {} with {} against {}", - player.alias, password, pass); if pass != &password { client.error_state(RequestStateError::Denied); break; } } else { - - println!("Adding user {} with password {}", player.alias, password); accounts.insert(player.alias.clone(), password); } - println!("{:?}", accounts); match client.client_state { ClientState::Connected => { Self::initialize_player(state, entity, client, player); From 35a3f67e19c0c27117eb16fb9d5b9d5e5c7c49a2 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 12:05:38 -0400 Subject: [PATCH 06/11] ran cargo fmt --- chat-cli/src/main.rs | 4 +++- client/src/lib.rs | 12 +++++++----- server/src/lib.rs | 3 +-- voxygen/src/menu/main/client_init.rs | 7 ++++++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index f8d114c7fc..d01c87aa49 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -48,7 +48,9 @@ fn main() { println!("Players online: {:?}", client.get_players()); - client.register(comp::Player::new(username, None), password).unwrap(); + client + .register(comp::Player::new(username, None), password) + .unwrap(); let (tx, rx) = mpsc::channel(); thread::spawn(move || loop { diff --git a/client/src/lib.rs b/client/src/lib.rs index 1fb2903464..b0b4356e4d 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -8,7 +8,7 @@ pub use specs::{join::Join, saveload::Marker, Entity as EcsEntity, ReadStorage}; use common::{ comp, - msg::{ClientMsg, ClientState, ServerError, ServerInfo, ServerMsg, RequestStateError}, + msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg}, net::PostBox, state::{State, Uid}, terrain::{block::Block, chonk::ChonkMetrics, TerrainChunk, TerrainChunkSize}, @@ -132,10 +132,12 @@ impl Client { } /// Request a state transition to `ClientState::Registered`. - pub fn register(&mut self, player: comp::Player, password: String) /*-> Result<(), Error>*/ { - self.postbox.send_message(ClientMsg::Register { player, password }); + pub fn register(&mut self, player: comp::Player, password: String) /*-> Result<(), Error>*/ + { + self.postbox + .send_message(ClientMsg::Register { player, password }); self.client_state = ClientState::Pending; - /*loop { + /*loop { match self.postbox.next_message() { Some(ServerMsg::StateAnswer(Err((RequestStateError::Denied, _)))) => { println!("Got a bad"); @@ -149,7 +151,7 @@ impl Client { println!("Got unusual message: {:?}", x); } None => { println!("Got nothing?"); }, - + } }*/ } diff --git a/server/src/lib.rs b/server/src/lib.rs index b6f32c6a40..8d0c130105 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -26,7 +26,7 @@ use log::debug; use rand::Rng; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; use std::{ - collections::{HashSet, HashMap}, + collections::{HashMap, HashSet}, i32, net::SocketAddr, sync::{mpsc, Arc}, @@ -535,7 +535,6 @@ impl Server { break; } } else { - player.alias, password); accounts.insert(player.alias.clone(), password); } match client.client_state { diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 8896f62095..53db4ab3d1 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -30,7 +30,12 @@ pub struct ClientInit { rx: Receiver>, } impl ClientInit { - pub fn new(connection_args: (String, u16, bool), player: comp::Player, password: String, wait: bool) -> Self { + pub fn new( + connection_args: (String, u16, bool), + player: comp::Player, + password: String, + wait: bool, + ) -> Self { let (server_address, default_port, prefer_ipv6) = connection_args; let (tx, rx) = channel(); From 66e254db7fac51353ca7914124a123b1bd280ce7 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 12:09:14 -0400 Subject: [PATCH 07/11] actually removed printlns --- client/src/lib.rs | 7 +------ voxygen/src/menu/main/client_init.rs | 12 ++++-------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index b0b4356e4d..64c49f4a78 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -140,17 +140,12 @@ impl Client { /*loop { match self.postbox.next_message() { Some(ServerMsg::StateAnswer(Err((RequestStateError::Denied, _)))) => { - println!("Got a bad"); break Err(Error::InvalidAuth) }, Some(ServerMsg::StateAnswer(Ok(ClientState::Registered))) => { - println!("Got a good"); break Ok(()) } - Some(x) => { - println!("Got unusual message: {:?}", x); - } - None => { println!("Got nothing?"); }, + _ => {} } }*/ diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 53db4ab3d1..69ada9fd9a 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -62,14 +62,10 @@ impl ClientInit { for socket_addr in first_addrs.into_iter().chain(second_addrs) { match Client::new(socket_addr, player.view_distance) { Ok(mut client) => { - /*match client.register(player, password) { - Err(ClientError::InvalidAuth) => { - last_err = Some(Error::InvalidAuth); - break; - } - _ => {} - } - println!("Auth success");*/ + /*if let Err(ClientError::InvalidAuth) == client.register(player, password) { + last_err = Some(Error::InvalidAuth); + break; + }*/ client.register(player, password); let _ = tx.send(Ok(client)); From 3a9bbc49273106180d0d93a3acc8ee7ee91548a0 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 13:21:18 -0400 Subject: [PATCH 08/11] fixed chat-cli --- chat-cli/src/main.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index d01c87aa49..dea1044b75 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -48,9 +48,7 @@ fn main() { println!("Players online: {:?}", client.get_players()); - client - .register(comp::Player::new(username, None), password) - .unwrap(); + client.register(comp::Player::new(username, None), password); let (tx, rx) = mpsc::channel(); thread::spawn(move || loop { From 07d3384b011cdd976e424cad692aba4e3ad1f851 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 17:58:36 -0400 Subject: [PATCH 09/11] fixed the silent kick, made it actually proper --- client/src/lib.rs | 12 ++++-------- voxygen/src/menu/main/client_init.rs | 8 +++++--- voxygen/src/menu/main/mod.rs | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 64c49f4a78..80921ec42f 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -132,23 +132,19 @@ impl Client { } /// Request a state transition to `ClientState::Registered`. - pub fn register(&mut self, player: comp::Player, password: String) /*-> Result<(), Error>*/ - { + pub fn register(&mut self, player: comp::Player, password: String) -> Result<(), Error> { self.postbox .send_message(ClientMsg::Register { player, password }); self.client_state = ClientState::Pending; - /*loop { + loop { match self.postbox.next_message() { Some(ServerMsg::StateAnswer(Err((RequestStateError::Denied, _)))) => { break Err(Error::InvalidAuth) - }, - Some(ServerMsg::StateAnswer(Ok(ClientState::Registered))) => { - break Ok(()) } + Some(ServerMsg::StateAnswer(Ok(ClientState::Registered))) => break Ok(()), _ => {} - } - }*/ + } } /// Request a state transition to `ClientState::Character`. diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 69ada9fd9a..a6095e9eb2 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -62,11 +62,13 @@ impl ClientInit { for socket_addr in first_addrs.into_iter().chain(second_addrs) { match Client::new(socket_addr, player.view_distance) { Ok(mut client) => { - /*if let Err(ClientError::InvalidAuth) == client.register(player, password) { + if let Err(ClientError::InvalidAuth) = + client.register(player, password) + { last_err = Some(Error::InvalidAuth); break; - }*/ - client.register(player, password); + } + //client.register(player, password); let _ = tx.send(Ok(client)); #[cfg(feature = "discord")] diff --git a/voxygen/src/menu/main/mod.rs b/voxygen/src/menu/main/mod.rs index 8ed9b98357..eb2ce26018 100644 --- a/voxygen/src/menu/main/mod.rs +++ b/voxygen/src/menu/main/mod.rs @@ -64,7 +64,7 @@ impl PlayState for MainMenuState { self.main_menu_ui.login_error( match err { InitError::BadAddress(_) | InitError::NoAddress => "Server not found", - InitError::InvalidAuth => "Invalid username or password", + InitError::InvalidAuth => "Invalid credentials", InitError::ServerIsFull => "Server is Full!", InitError::ConnectionFailed(_) => "Connection failed", InitError::ClientCrashed => "Client crashed", From eadf3a76710b8019d48b42a7e03886c3e47b83a7 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 18:24:14 -0400 Subject: [PATCH 10/11] created basic AuthProvider which we can change to suit our needs --- chat-cli/src/main.rs | 4 +++- server/src/auth_provider.rs | 31 +++++++++++++++++++++++++++++++ server/src/lib.rs | 18 ++++++++---------- 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 server/src/auth_provider.rs diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index dea1044b75..d01c87aa49 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -48,7 +48,9 @@ fn main() { println!("Players online: {:?}", client.get_players()); - client.register(comp::Player::new(username, None), password); + client + .register(comp::Player::new(username, None), password) + .unwrap(); let (tx, rx) = mpsc::channel(); thread::spawn(move || loop { diff --git a/server/src/auth_provider.rs b/server/src/auth_provider.rs new file mode 100644 index 0000000000..8daaa22c6a --- /dev/null +++ b/server/src/auth_provider.rs @@ -0,0 +1,31 @@ +use log::{info, warn}; +use std::collections::HashMap; + +pub struct AuthProvider { + accounts: HashMap, +} + +impl AuthProvider { + pub fn new() -> Self { + AuthProvider { + accounts: HashMap::new(), + } + } + + pub fn query(&mut self, username: String, password: String) -> bool { + if let Some(pass) = self.accounts.get(&username) { + if pass != &password { + warn!( + "User '{}' attempted to log in with invalid password '{}'!", + username, password + ); + return false; + } + info!("User '{}' successfully authenticated", username); + } else { + info!("Registered new user '{}'", username); + self.accounts.insert(username, password); + } + true + } +} diff --git a/server/src/lib.rs b/server/src/lib.rs index 8d0c130105..804f502620 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,5 +1,6 @@ #![feature(drain_filter, bind_by_move_pattern_guards)] +pub mod auth_provider; pub mod client; pub mod cmd; pub mod error; @@ -10,6 +11,7 @@ pub mod settings; pub use crate::{error::Error, input::Input, settings::ServerSettings}; use crate::{ + auth_provider::AuthProvider, client::{Client, Clients}, cmd::CHAT_COMMANDS, }; @@ -26,7 +28,7 @@ use log::debug; use rand::Rng; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, i32, net::SocketAddr, sync::{mpsc, Arc}, @@ -70,7 +72,7 @@ pub struct Server { server_info: ServerInfo, // TODO: anything but this - accounts: HashMap, + accounts: AuthProvider, } impl Server { @@ -110,7 +112,7 @@ impl Server { description: settings.server_description.clone(), git_hash: common::util::GIT_HASH.to_string(), }, - accounts: HashMap::new(), + accounts: AuthProvider::new(), server_settings: settings, }; @@ -529,13 +531,9 @@ impl Server { }, // Valid player ClientMsg::Register { player, password } if player.is_valid() => { - if let Some(pass) = accounts.get(&player.alias) { - if pass != &password { - client.error_state(RequestStateError::Denied); - break; - } - } else { - accounts.insert(player.alias.clone(), password); + if !accounts.query(player.alias.clone(), password) { + client.error_state(RequestStateError::Denied); + break; } match client.client_state { ClientState::Connected => { From 79b5c177cd5511f5a84f0c2f16d1bcfa3ac72f27 Mon Sep 17 00:00:00 2001 From: telastrus <4415544-telastrus@users.noreply.gitlab.com> Date: Thu, 8 Aug 2019 21:28:25 -0400 Subject: [PATCH 11/11] swapped if for entry --- server/src/auth_provider.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/server/src/auth_provider.rs b/server/src/auth_provider.rs index 8daaa22c6a..46a49d9503 100644 --- a/server/src/auth_provider.rs +++ b/server/src/auth_provider.rs @@ -13,19 +13,20 @@ impl AuthProvider { } pub fn query(&mut self, username: String, password: String) -> bool { - if let Some(pass) = self.accounts.get(&username) { - if pass != &password { - warn!( - "User '{}' attempted to log in with invalid password '{}'!", - username, password - ); - return false; - } + let pwd = password.clone(); + if self.accounts.entry(username.clone()).or_insert_with(|| { + info!("Registered new user '{}'", &username); + pwd + }) == &password + { info!("User '{}' successfully authenticated", username); + true } else { - info!("Registered new user '{}'", username); - self.accounts.insert(username, password); + warn!( + "User '{}' attempted to log in with invalid password '{}'!", + username, password + ); + false } - true } }