Add AuthenticationError enum

This commit is contained in:
Xiretza 2024-02-10 12:08:45 +00:00
parent e9ae0468b3
commit ac7f03db15

View file

@ -1,6 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use rand::{seq::IteratorRandom, thread_rng}; use rand::{seq::IteratorRandom, thread_rng};
use thiserror::Error;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing::{event, instrument, Level}; use tracing::{event, instrument, Level};
@ -9,6 +10,18 @@ use crate::{
secrets::{Password, UserToken}, 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)] #[derive(Debug)]
pub struct Authenticator { pub struct Authenticator {
db: Arc<Mutex<SqliteDatabase>>, db: Arc<Mutex<SqliteDatabase>>,
@ -41,13 +54,16 @@ impl Authenticator {
} }
#[instrument] #[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; let mut db = self.db.lock().await;
// TODO: (in db) distinguish between invalid token and SQLX error // TODO: (in db) distinguish between invalid token and SQLX error
match db.get_token(username).await { db.get_token(username).await?;
Ok(_) => Ok(()),
Err(_) => Err(()), Ok(())
}
} }
} }