Skip to content

Commit

Permalink
Merge pull request #69 from PocketRelay/feat-random-names
Browse files Browse the repository at this point in the history
feat: randomly generate names for in-game account creation
  • Loading branch information
jacobtread authored May 12, 2024
2 parents c926ee7 + 11a8b55 commit 762020e
Show file tree
Hide file tree
Showing 5 changed files with 2,165 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ hashbrown = { version = "0.14.3", default-features = false, features = [
] }

uuid = { version = "^1", features = ["v4", "serde", "fast-rng"] }
rand = "0.8"

# SeaORM
[dependencies.sea-orm]
Expand Down
13 changes: 13 additions & 0 deletions src/database/entities/players.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ impl Model {
Entity::find().filter(Column::Email.eq(email)).one(db)
}

/// Attempts to find a player with the provided username
///
/// `db` The database connection
/// `username` The username to search for
pub fn by_username<'a>(
db: &'a DatabaseConnection,
username: &str,
) -> impl Future<Output = DbResult<Option<Self>>> + Send + 'a {
Entity::find()
.filter(Column::DisplayName.eq(username))
.one(db)
}

/// Determines whether the current player has permission to
/// make actions on behalf of the other player. This can
/// occur when they are both the same player or the role of
Expand Down
22 changes: 18 additions & 4 deletions src/session/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ use crate::{
router::{Blaze, Extension, SessionAuth},
SessionLink,
},
utils::hashing::{hash_password, verify_password},
utils::{
hashing::{hash_password, verify_password},
random_name::generate_random_name,
},
};
use email_address::EmailAddress;
use log::{debug, error};
use rand::{rngs::StdRng, SeedableRng};
use std::{borrow::Cow, sync::Arc};
use tokio::fs::read_to_string;

Expand Down Expand Up @@ -299,14 +303,24 @@ pub async fn handle_create_account(
return Err(AuthenticationError::Exists.into());
}

// Hash the proivded plain text password using Argon2
// Hash the provided plain text password using Argon2
let hashed_password: String = hash_password(&password).map_err(|err| {
error!("Failed to hash password for creating account: {}", err);
GlobalError::System
})?;

// Create a default display name from the first 99 chars of the email
let display_name: String = email.chars().take(99).collect::<String>();
let mut rng = StdRng::from_entropy();
let display_name: String;

loop {
let generated_name = generate_random_name(&mut rng);

// Ensure the generated name is unique
if Player::by_username(&db, &generated_name).await?.is_none() {
display_name = generated_name;
break;
}
}

// Use the super admin role if the email is the super admins
let role: PlayerRole = if config.dashboard.is_super_email(&email) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ pub mod hashing;
pub mod lock;
pub mod logging;
pub mod parsing;
pub mod random_name;
pub mod signing;
pub mod types;
Loading

0 comments on commit 762020e

Please sign in to comment.