mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
created basic AuthProvider which we can change to suit our needs
This commit is contained in:
parent
07d3384b01
commit
eadf3a7671
@ -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 {
|
||||
|
31
server/src/auth_provider.rs
Normal file
31
server/src/auth_provider.rs
Normal 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
|
||||
}
|
||||
}
|
@ -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<String, String>,
|
||||
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 => {
|
||||
|
Loading…
Reference in New Issue
Block a user