Add AuthenticationError enum
This commit is contained in:
parent
e9ae0468b3
commit
ac7f03db15
1 changed files with 21 additions and 5 deletions
26
src/auth.rs
26
src/auth.rs
|
@ -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(())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue