cleanup login provider

This commit is contained in:
aljazerzen 2021-03-27 11:43:53 +01:00
parent 0c8448517e
commit 0e71af92f1
3 changed files with 13 additions and 41 deletions

View File

@ -1,11 +1,8 @@
use super::Event;
use crate::{
client::Client, login_provider::LoginProvider, persistence, presence::Presence,
state_ext::StateExt, Server,
};
use crate::{client::Client, persistence, presence::Presence, state_ext::StateExt, Server};
use common::{
comp,
comp::{group, Player},
comp::group,
uid::{Uid, UidAllocator},
};
use common_base::span;
@ -143,12 +140,6 @@ pub fn handle_client_disconnect(server: &mut Server, entity: EcsEntity) -> Event
)));
}
// Make sure to remove the player from the logged in list. (See LoginProvider)
if let Some(player) = state.ecs().read_storage::<Player>().get(entity) {
let mut login_provider = state.ecs().write_resource::<LoginProvider>();
login_provider.logout(player.uuid());
}
// Sync the player's character data to the database
let entity = persist_entity(state, entity);

View File

@ -43,9 +43,9 @@ impl PendingLogin {
impl Component for PendingLogin {
type Storage = IdvStorage<Self>;
}
pub struct LoginProvider {
runtime: Arc<Runtime>,
accounts: HashMap<Uuid, String>,
auth_server: Option<Arc<AuthClient>>,
}
@ -68,23 +68,10 @@ impl LoginProvider {
Self {
runtime,
accounts: HashMap::new(),
auth_server,
}
}
fn login(&mut self, uuid: Uuid, username: String) -> Result<(), RegisterError> {
info!(?username, "New User");
self.accounts.insert(uuid, username);
Ok(())
}
pub fn logout(&mut self, uuid: Uuid) {
if self.accounts.remove(&uuid).is_none() {
error!(?uuid, "Attempted to logout user that is not logged in.");
};
}
pub fn verify(&self, username_or_token: &str) -> PendingLogin {
let (pending_s, pending_r) = oneshot::channel();
@ -108,7 +95,7 @@ impl LoginProvider {
PendingLogin { pending_r }
}
pub fn try_login(
pub fn login(
&mut self,
pending: &mut PendingLogin,
#[cfg(feature = "plugins")] world: &EcsWorld,
@ -118,17 +105,17 @@ impl LoginProvider {
banlist: &HashMap<Uuid, BanRecord>,
) -> Option<Result<(String, Uuid), RegisterError>> {
match pending.pending_r.try_recv() {
Ok(Err(e)) => Some(Err(e.into())),
Ok(Err(e)) => Some(Err(e)),
Ok(Ok((username, uuid))) => {
if let Some(ban_record) = banlist.get(&uuid) {
// Pull reason string out of ban record and send a copy of it
return Some(Err(RegisterError::Banned(ban_record.reason.clone()).into()));
return Some(Err(RegisterError::Banned(ban_record.reason.clone())));
}
// user can only join if he is admin, the whitelist is empty (everyone can join)
// or his name is in the whitelist
if !whitelist.is_empty() && !whitelist.contains(&uuid) && !admins.contains(&uuid) {
return Some(Err(RegisterError::NotOnWhitelist.into()));
return Some(Err(RegisterError::NotOnWhitelist));
}
#[cfg(feature = "plugins")]
{
@ -139,7 +126,7 @@ impl LoginProvider {
Ok(e) => {
for i in e.into_iter() {
if let PlayerJoinResult::Kick(a) = i {
return Some(Err(RegisterError::Kicked(a).into()));
return Some(Err(RegisterError::Kicked(a)));
}
}
},
@ -149,17 +136,14 @@ impl LoginProvider {
};
}
// add the user to self.accounts
match self.login(uuid, username.clone()) {
Ok(()) => Some(Ok((username, uuid))),
Err(e) => Some(Err(e)),
}
info!(?username, "New User");
Some(Ok((username, uuid)))
},
Err(tokio::sync::oneshot::error::TryRecvError::Closed) => {
error!("channel got closed to early, this shouldn't happen");
Some(Err(RegisterError::AuthError(
"Internal Error verifying".to_string(),
).into()))
)))
},
Err(tokio::sync::oneshot::error::TryRecvError::Empty) => None,
}

View File

@ -115,7 +115,7 @@ impl<'a> System<'a> for Sys {
uid_allocator: &uid_allocator,
};
let (username, uuid) = match login_provider.try_login(
let (username, uuid) = match login_provider.login(
&mut pending,
#[cfg(feature = "plugins")]
&ecs_world,
@ -156,10 +156,7 @@ impl<'a> System<'a> for Sys {
// Create "fake" successful pending auth and mark it to
// be inserted into pending_logins at the end of this
// run
retries.push((
entity,
PendingLogin::new_success(username.to_string(), uuid),
));
retries.push((entity, PendingLogin::new_success(username, uuid)));
return Ok(());
}