From ac7f03db15fb121a27e269609ee00cd890b6423c Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Feb 2024 12:08:45 +0000 Subject: [PATCH] Add AuthenticationError enum --- src/auth.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 7a57701..fd2c405 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use rand::{seq::IteratorRandom, thread_rng}; +use thiserror::Error; use tokio::sync::Mutex; use tracing::{event, instrument, Level}; @@ -9,6 +10,18 @@ use crate::{ secrets::{Password, UserToken}, }; +#[derive(Debug, Error)] +pub enum AuthenticationError { + #[error("Invalid username or password")] + InvalidUserOrPassword, + #[error("Invalid token")] + InvalidToken, + #[error("Invalid server hash")] + InvalidServerHash, + #[error("Authentication backend error")] + Backend(#[from] sqlx::Error), +} + #[derive(Debug)] pub struct Authenticator { db: Arc>, @@ -41,13 +54,16 @@ impl Authenticator { } #[instrument] - pub async fn verify_user_token(&self, username: &str, token: &UserToken) -> Result<(), ()> { + pub async fn verify_user_token( + &self, + username: &str, + token: &UserToken, + ) -> Result<(), AuthenticationError> { let mut db = self.db.lock().await; // TODO: (in db) distinguish between invalid token and SQLX error - match db.get_token(username).await { - Ok(_) => Ok(()), - Err(_) => Err(()), - } + db.get_token(username).await?; + + Ok(()) } }