From 926dae57fb9dfcd1bff479f9fea7234313c6acb0 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 22 May 2021 22:25:00 +0000 Subject: [PATCH] Add check password command. --- src/commands/management.rs | 24 +++++++++++++++++++++++- src/commands/parser.rs | 7 ++++++- src/error.rs | 3 +++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/commands/management.rs b/src/commands/management.rs index aaf8dc5..571a75d 100644 --- a/src/commands/management.rs +++ b/src/commands/management.rs @@ -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()), + } + } +} diff --git a/src/commands/parser.rs b/src/commands/parser.rs index 2e6874d..2ab0837 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -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, BotError> { Ok(Box::new(RegisterCommand(input.to_owned()))) } +fn parse_check_command(input: &str) -> Result, BotError> { + Ok(Box::new(CheckCommand(input.to_owned()))) +} + fn parse_get_variable_command(input: &str) -> Result, BotError> { Ok(Box::new(GetVariableCommand(input.to_owned()))) } @@ -146,6 +150,7 @@ pub fn parse_command(input: &str) -> Result, 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. diff --git a/src/error.rs b/src/error.rs index 6ed0be3..65182d1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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)]