use trait object for database

This commit is contained in:
deneb 2024-02-11 16:48:03 +01:00
parent e5b95a1f8e
commit c887378476
5 changed files with 16 additions and 8 deletions

1
Cargo.lock generated
View file

@ -576,6 +576,7 @@ dependencies = [
name = "factoriauth" name = "factoriauth"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"async-trait",
"axum", "axum",
"base64", "base64",
"clap", "clap",

View file

@ -39,3 +39,4 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
rustls = "=0.21.7" rustls = "=0.21.7"
rustls-webpki = "=0.101.6" rustls-webpki = "=0.101.6"
sct = "=0.7.0" sct = "=0.7.0"
async-trait = "0.1.77"

View file

@ -16,7 +16,7 @@ use tracing::{event, instrument, Level};
use crate::{ use crate::{
config::AuthBackendConfig, config::AuthBackendConfig,
db::{Database, SqliteDatabase, UserTokenEntry}, db::{Database, UserTokenEntry},
secrets::{ secrets::{
PadlockGenerationSecret, Password, ServerHash, ServerPadlock, UserServerKey, UserToken, PadlockGenerationSecret, Password, ServerHash, ServerPadlock, UserServerKey, UserToken,
}, },
@ -69,14 +69,17 @@ impl ValidateLogin for AuthenticationBackend {
#[derive(Debug)] #[derive(Debug)]
pub struct UserAuthenticator { pub struct UserAuthenticator {
db: Arc<Mutex<SqliteDatabase>>, db: Arc<Mutex<Box<dyn Database + Send>>>,
backends: Vec<AuthenticationBackend>, backends: Vec<AuthenticationBackend>,
} }
impl UserAuthenticator { impl UserAuthenticator {
const TOKEN_LEN: usize = 30; const TOKEN_LEN: usize = 30;
pub fn new(db: Arc<Mutex<SqliteDatabase>>, backends: Vec<AuthenticationBackend>) -> Self { pub fn new(
db: Arc<Mutex<Box<dyn Database + Send>>>,
backends: Vec<AuthenticationBackend>,
) -> Self {
Self { db, backends } Self { db, backends }
} }

View file

@ -1,5 +1,6 @@
use std::str::FromStr; use std::{fmt::Debug, str::FromStr};
use axum::async_trait;
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use sqlx::{query, query_as, sqlite::SqliteConnectOptions, Connection, SqliteConnection}; use sqlx::{query, query_as, sqlite::SqliteConnectOptions, Connection, SqliteConnection};
use tracing::instrument; use tracing::instrument;
@ -22,7 +23,8 @@ pub enum UserTokenEntry {
), ),
} }
pub trait Database { #[async_trait]
pub trait Database: Debug {
async fn get_token(&mut self, username: &str) -> Result<Option<UserTokenEntry>, sqlx::Error>; async fn get_token(&mut self, username: &str) -> Result<Option<UserTokenEntry>, sqlx::Error>;
async fn save_token(&mut self, username: &str, token: &UserToken) -> Result<(), sqlx::Error>; async fn save_token(&mut self, username: &str, token: &UserToken) -> Result<(), sqlx::Error>;
@ -69,6 +71,7 @@ impl SqliteDatabase {
} }
} }
#[async_trait]
impl Database for SqliteDatabase { impl Database for SqliteDatabase {
#[instrument] #[instrument]
async fn get_token(&mut self, username: &str) -> Result<Option<UserTokenEntry>, sqlx::Error> { async fn get_token(&mut self, username: &str) -> Result<Option<UserTokenEntry>, sqlx::Error> {

View file

@ -33,7 +33,7 @@ use auth::{
use clap::Parser; use clap::Parser;
use color_eyre::Result; use color_eyre::Result;
use config::Config; use config::Config;
use db::SqliteDatabase; use db::{Database, SqliteDatabase};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing::{event, instrument, Level}; use tracing::{event, instrument, Level};
use tracing_error::ErrorLayer; use tracing_error::ErrorLayer;
@ -82,9 +82,9 @@ async fn main() -> Result<()> {
let config = load_config(&args.config).await?; let config = load_config(&args.config).await?;
let database = Arc::new(Mutex::new( let database: Arc<Mutex<Box<dyn Database + Send>>> = Arc::new(Mutex::new(Box::new(
SqliteDatabase::open(&config.database.connection_string).await, SqliteDatabase::open(&config.database.connection_string).await,
)); )));
let mut auth_backends = vec![]; let mut auth_backends = vec![];
for c in config.auth_backends { for c in config.auth_backends {