Password hashing
Let the server hash and check passwords with Argon2id, off your script's time and off the game tick.
Why the server hashes
A good password hash is slow on purpose. Your script runs one call at a time, so a hash computed inside the script would hold up every other handler of the script. With the password module the server does the work instead: your script starts a job and polls it.
The server uses Argon2id with OWASP's parameters: 19 MiB of memory, 2 passes, 1 lane, a 16-byte random salt and a 32-byte hash. The result is a standard PHC string such as $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>. The server keeps no password: a job forgets it once the hash is done.
Start a job and poll it
password::hash_password(password)starts hashing a password of at most 1024 bytes with a new random salt and returns aPasswordHashJob.password::verify_password(password, phc)starts checking a password against a PHC string and returns aPasswordVerifyJob.job.poll()returnsNonewhile the server is working, then the result at every later call: the PHC string, or aPasswordCheckwithmatchesandneeds_rehash, or the reason it failed.
The example below is a small login script. To stay short it keeps accounts in memory and keys players by name; a real login script stores the PHC strings in its data folder and tracks sessions per connection (see Login and security scripts).
enum Job {
Register(PasswordHashJob),
Login(PasswordVerifyJob),
}
#[derive(Default)]
struct State {
/// Player name -> PHC string made by the server.
accounts: HashMap<String, String>,
logged_in: HashSet<String>,
/// Player name -> the job the server is working on.
jobs: HashMap<String, Job>,
}
static STATE: Mutex<Option<State>> = Mutex::new(None);
/// Runs `f` on the state. Never call the server while holding the lock: a call can deliver
/// an event back into this plugin.
fn with<T>(f: impl FnOnce(&mut State) -> T) -> T {
let mut guard = STATE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
f(guard.get_or_insert_with(State::default))
}struct Register;
impl CommandHandler for Register {
fn handle(&self, sender: CommandSender, _: Server, args: ConsumedArgs) -> Result<i32, CommandError> {
let Arg::Simple(password) = args.get_value("password") else {
return Err(CommandError::InvalidConsumption(Some("password".into())));
};
let name = sender.get_name();
if with(|s| s.accounts.contains_key(&name) || s.jobs.contains_key(&name)) {
sender.send_message(TextComponent::text("You are registered already: /login <password>"));
return Ok(0);
}
// The server hashes the password on its own threads; the plugin polls the job.
let job = hash_password(&password);
with(|s| s.jobs.insert(name, Job::Register(job)));
Ok(1)
}
}
struct Login;
impl CommandHandler for Login {
fn handle(&self, sender: CommandSender, _: Server, args: ConsumedArgs) -> Result<i32, CommandError> {
let Arg::Simple(password) = args.get_value("password") else {
return Err(CommandError::InvalidConsumption(Some("password".into())));
};
let name = sender.get_name();
let Some(phc) = with(|s| s.accounts.get(&name).cloned()) else {
sender.send_message(TextComponent::text("Register first: /register <password>"));
return Ok(0);
};
let job = verify_password(&password, &phc);
with(|s| s.jobs.insert(name, Job::Login(job)));
Ok(1)
}
}/// Runs every tick off the game tick: finishes the jobs the server is done with.
fn poll_jobs(server: Server) {
let names: Vec<String> = with(|s| s.jobs.keys().cloned().collect());
for name in names {
let done = with(|s| {
let finished = match s.jobs.get(&name)? {
Job::Register(job) => match job.poll()? {
Ok(phc) => {
s.accounts.insert(name.clone(), phc);
s.logged_in.insert(name.clone());
"Registered and logged in."
}
Err(_) => "The server is busy. Try again in a moment.",
},
Job::Login(job) => match job.poll()? {
Ok(check) if check.matches => {
s.logged_in.insert(name.clone());
"Logged in."
}
Ok(_) => "Wrong password.",
Err(_) => "The server is busy. Try again in a moment.",
},
};
s.jobs.remove(&name);
Some(finished)
});
if let Some(message) = done {
tell(&server, &name, message);
if let Some(player) = server.get_player_by_name(&name) {
// Sends the command list again: the player now sees every command.
player.update_commands();
}
}
}
}Answers you must handle
- Busy: when the server has too many jobs at once, a new job fails at once. Tell the player to try again.
- needs_rehash: the password matched, but the stored string was made with other parameters than the server's current ones. Hash the password again and store the new string.
- Failure on verify: the string is not an Argon2id PHC string, or its parameters are beyond what the server accepts.
Keep passwords out of logs
Declare every command that carries a password sensitive (.sensitive(), see Commands and suggestions), never log a password or a hash, and store only the PHC string. The full set of protections a login script needs is in Login and security scripts.