login trim username, improve validation, social ordering

This commit is contained in:
aljazerzen 2021-04-01 16:37:13 +02:00
parent 3506f754aa
commit 08146f6a3a
5 changed files with 44 additions and 22 deletions

View File

@ -14,15 +14,21 @@ pub struct Player {
impl Player {
pub fn new(alias: String, uuid: Uuid) -> Self { Self { alias, uuid } }
pub fn is_valid(&self) -> bool { Self::alias_is_valid(&self.alias) }
pub fn is_valid(&self) -> bool { Self::alias_validate(&self.alias).is_ok() }
pub fn alias_is_valid(alias: &str) -> bool {
pub fn alias_validate(alias: &str) -> Result<(), AliasError> {
// TODO: Expose auth name validation and use it here.
// See https://gitlab.com/veloren/auth/-/blob/master/server/src/web.rs#L20
alias
if !alias
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
&& alias.len() <= MAX_ALIAS_LEN
{
Err(AliasError::ForbiddenCharacters)
} else if alias.len() > MAX_ALIAS_LEN {
Err(AliasError::TooLong)
} else {
Ok(())
}
}
/// Not to be confused with uid
@ -38,3 +44,18 @@ pub struct Respawn;
impl Component for Respawn {
type Storage = NullStorage<Self>;
}
pub enum AliasError {
ForbiddenCharacters,
TooLong,
}
impl ToString for AliasError {
fn to_string(&self) -> String {
match *self {
AliasError::ForbiddenCharacters => "Alias contains illegal characters.",
AliasError::TooLong => "Alias is too long.",
}
.to_string()
}
}

View File

@ -751,11 +751,11 @@ fn handle_alias(
return;
}
if let Ok(alias) = scan_fmt!(&args, &action.arg_fmt(), String) {
if !comp::Player::alias_is_valid(&alias) {
if let Err(error) = comp::Player::alias_validate(&alias) {
// Prevent silly aliases
server.notify_client(
client,
ServerGeneral::server_msg(ChatType::CommandError, "Invalid alias."),
ServerGeneral::server_msg(ChatType::CommandError, error.to_string()),
);
return;
}

View File

@ -386,8 +386,8 @@ impl<'a> Widget for Social<'a> {
player
.character
.as_ref()
.map(|character| &character.name)
.unwrap_or(&player.player_alias)
.map(|character| character.name.to_lowercase())
.unwrap_or_else(|| player.player_alias.to_string())
});
for (i, (&uid, player_info)) in player_list.into_iter().enumerate() {
let hide_username = true;

View File

@ -340,18 +340,19 @@ fn attempt_login(
client_init: &mut Option<ClientInit>,
runtime: Option<Arc<runtime::Runtime>>,
) {
if comp::Player::alias_is_valid(&username) {
// Don't try to connect if there is already a connection in progress.
if client_init.is_none() {
*client_init = Some(ClientInit::new(
connection_args,
username,
Some(settings.graphics.view_distance),
password,
runtime,
));
}
} else {
*info_message = Some("Invalid username".to_string());
if let Err(err) = comp::Player::alias_validate(&username) {
*info_message = Some(err.to_string());
return;
}
// Don't try to connect if there is already a connection in progress.
if client_init.is_none() {
*client_init = Some(ClientInit::new(
connection_args,
username,
Some(settings.graphics.view_distance),
password,
runtime,
));
}
}

View File

@ -354,7 +354,7 @@ impl Controls {
};
events.push(Event::LoginAttempt {
username: self.login_info.username.clone(),
username: self.login_info.username.trim().to_string(),
password: self.login_info.password.clone(),
server_address: self.login_info.server.clone(),
});