created basic AuthProvider which we can change to suit our needs

This commit is contained in:
telastrus 2019-08-08 18:24:14 -04:00
parent 07d3384b01
commit eadf3a7671
3 changed files with 42 additions and 11 deletions

View File

@ -48,7 +48,9 @@ fn main() {
println!("Players online: {:?}", client.get_players()); 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(); let (tx, rx) = mpsc::channel();
thread::spawn(move || loop { thread::spawn(move || loop {

View File

@ -0,0 +1,31 @@
use log::{info, warn};
use std::collections::HashMap;
pub struct AuthProvider {
accounts: HashMap<String, String>,
}
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
}
}

View File

@ -1,5 +1,6 @@
#![feature(drain_filter, bind_by_move_pattern_guards)] #![feature(drain_filter, bind_by_move_pattern_guards)]
pub mod auth_provider;
pub mod client; pub mod client;
pub mod cmd; pub mod cmd;
pub mod error; pub mod error;
@ -10,6 +11,7 @@ pub mod settings;
pub use crate::{error::Error, input::Input, settings::ServerSettings}; pub use crate::{error::Error, input::Input, settings::ServerSettings};
use crate::{ use crate::{
auth_provider::AuthProvider,
client::{Client, Clients}, client::{Client, Clients},
cmd::CHAT_COMMANDS, cmd::CHAT_COMMANDS,
}; };
@ -26,7 +28,7 @@ use log::debug;
use rand::Rng; use rand::Rng;
use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity}; use specs::{join::Join, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity};
use std::{ use std::{
collections::{HashMap, HashSet}, collections::HashSet,
i32, i32,
net::SocketAddr, net::SocketAddr,
sync::{mpsc, Arc}, sync::{mpsc, Arc},
@ -70,7 +72,7 @@ pub struct Server {
server_info: ServerInfo, server_info: ServerInfo,
// TODO: anything but this // TODO: anything but this
accounts: HashMap<String, String>, accounts: AuthProvider,
} }
impl Server { impl Server {
@ -110,7 +112,7 @@ impl Server {
description: settings.server_description.clone(), description: settings.server_description.clone(),
git_hash: common::util::GIT_HASH.to_string(), git_hash: common::util::GIT_HASH.to_string(),
}, },
accounts: HashMap::new(), accounts: AuthProvider::new(),
server_settings: settings, server_settings: settings,
}; };
@ -529,14 +531,10 @@ impl Server {
}, },
// Valid player // Valid player
ClientMsg::Register { player, password } if player.is_valid() => { ClientMsg::Register { player, password } if player.is_valid() => {
if let Some(pass) = accounts.get(&player.alias) { if !accounts.query(player.alias.clone(), password) {
if pass != &password {
client.error_state(RequestStateError::Denied); client.error_state(RequestStateError::Denied);
break; break;
} }
} else {
accounts.insert(player.alias.clone(), password);
}
match client.client_state { match client.client_state {
ClientState::Connected => { ClientState::Connected => {
Self::initialize_player(state, entity, client, player); Self::initialize_player(state, entity, client, player);