beginnings of SqliteDatabase

This commit is contained in:
deneb 2024-02-10 12:06:53 +01:00
parent 0006004db1
commit d4639c3152
4 changed files with 66 additions and 6 deletions

View file

@ -19,7 +19,7 @@ color-eyre = { version = "0.6.2" }
ldap3 = { version = "0.11.3", default-features = false, features = ["tls-rustls"] } ldap3 = { version = "0.11.3", default-features = false, features = ["tls-rustls"] }
secrecy = { version = "0.8.0", features = ["serde"] } secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.196", features = ["derive"] } serde = { version = "1.0.196", features = ["derive"] }
sqlx = { version = "0.7.3", features = ["runtime-tokio", "tls-rustls"] } sqlx = { version = "0.7.3", features = ["runtime-tokio", "tls-rustls", "sqlite"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.40" tracing = "0.1.40"
tracing-error = "0.2.0" tracing-error = "0.2.0"

View file

@ -1,7 +1,11 @@
use secrecy::SecretString; use secrecy::SecretString;
use serde::Deserialize; use serde::Deserialize;
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::instrument; use tracing::instrument;
use crate::db::SqliteDatabase;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct Password(pub SecretString); pub struct Password(pub SecretString);
@ -10,21 +14,21 @@ pub struct UserToken(pub SecretString);
#[derive(Debug)] #[derive(Debug)]
pub struct Authenticator { pub struct Authenticator {
// TODO db: Arc<Mutex<SqliteDatabase>>,
} }
impl Authenticator { impl Authenticator {
pub fn new(/* database credentials ? */) -> Self { pub fn new(db: Arc<Mutex<SqliteDatabase>>) -> Self {
Self {} Self { db }
} }
#[instrument] #[instrument]
pub fn create_user_token(&mut self, username: &str, password: &Password) -> UserToken { pub async fn create_user_token(&mut self, username: &str, password: &Password) -> UserToken {
todo!() todo!()
} }
#[instrument] #[instrument]
pub fn verify_user_token(&self, token: &UserToken) -> bool { pub async fn verify_user_token(&self, token: &UserToken) -> bool {
todo!() todo!()
} }
} }

55
src/db.rs Normal file
View file

@ -0,0 +1,55 @@
use sqlx::{query, Connection, SqliteConnection};
use crate::auth::UserToken;
// TODO: allow configuring this via envar
const DB_URI_DEFAULT: &str = "sqlite://sqlite.db";
pub trait Database {
async fn get_token(&self, username: &str) -> UserToken;
}
#[derive(Debug)]
pub struct SqliteDatabase {
conn: SqliteConnection,
}
impl SqliteDatabase {
pub async fn open() -> Self {
let mut db = Self {
conn: SqliteConnection::connect(DB_URI_DEFAULT)
.await
.expect("Failed to open SQLite database"),
};
db.init().await;
db
}
pub async fn init(&mut self) {
query(
"CREATE TABLE IF NOT EXISTS user_tokens (
id INTEGER PRIMARY KEY NOT NULL,
username VARCHAR(255),
token NCHAR(30),
created DATETIME,
valid BOOLEAN,
);",
)
.execute(&mut self.conn)
.await
.expect("Failed to initialize table user_tokens");
}
}
impl Database for SqliteDatabase {
async fn get_token(&self, username: &str) -> UserToken {
todo!()
// query(
// "
// SELECT token, created, valid
// ",
// )
}
}

View file

@ -20,6 +20,7 @@
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
mod auth; mod auth;
mod db;
mod server; mod server;
use color_eyre::Result; use color_eyre::Result;