Add check password command.

This commit is contained in:
projectmoon 2021-05-22 22:25:00 +00:00
parent 4557498ac6
commit 926dae57fb
3 changed files with 32 additions and 2 deletions

View File

@ -1,7 +1,7 @@
use super::{Command, Execution, ExecutionResult};
use crate::context::Context;
use crate::db::Users;
use crate::error::BotError::PasswordCreationError;
use crate::error::BotError::{AuthenticationError, PasswordCreationError};
use crate::logic::{hash_password, record_room_information};
use crate::models::User;
use async_trait::async_trait;
@ -60,3 +60,25 @@ impl Command for RegisterCommand {
Execution::success("User account registered/updated".to_string())
}
}
pub struct CheckCommand(pub String);
#[async_trait]
impl Command for CheckCommand {
fn name(&self) -> &'static str {
"check user password"
}
fn is_secure(&self) -> bool {
true
}
async fn execute(&self, ctx: &Context<'_>) -> ExecutionResult {
let user = ctx.db.authenticate_user(&ctx.username, &self.0).await?;
match user {
Some(_) => Execution::success("Password is correct!".to_string()),
None => Err(AuthenticationError.into()),
}
}
}

View File

@ -9,7 +9,7 @@ use crate::commands::{
basic_rolling::RollCommand,
cofd::PoolRollCommand,
cthulhu::{CthAdvanceRoll, CthRoll},
management::{RegisterCommand, ResyncCommand},
management::{CheckCommand, RegisterCommand, ResyncCommand},
misc::HelpCommand,
variables::{
DeleteVariableCommand, GetAllVariablesCommand, GetVariableCommand, SetVariableCommand,
@ -51,6 +51,10 @@ fn parse_register_command(input: &str) -> Result<Box<dyn Command>, BotError> {
Ok(Box::new(RegisterCommand(input.to_owned())))
}
fn parse_check_command(input: &str) -> Result<Box<dyn Command>, BotError> {
Ok(Box::new(CheckCommand(input.to_owned())))
}
fn parse_get_variable_command(input: &str) -> Result<Box<dyn Command>, BotError> {
Ok(Box::new(GetVariableCommand(input.to_owned())))
}
@ -146,6 +150,7 @@ pub fn parse_command(input: &str) -> Result<Box<dyn Command>, BotError> {
"chance" => chance_die(),
"help" => help(&cmd_input),
"register" => parse_register_command(&cmd_input),
"check" => parse_check_command(&cmd_input),
_ => Err(CommandParsingError::UnrecognizedCommand(cmd).into()),
},
//All other errors passed up.

View File

@ -81,6 +81,9 @@ pub enum BotError {
#[error("password creation error: {0}")]
PasswordCreationError(argon2::Error),
#[error("account does not exist, or password incorrect")]
AuthenticationError,
}
#[derive(Error, Debug)]