diff --git a/kitchen/src/web/auth.rs b/kitchen/src/web/auth.rs index 9a30a35..f73af73 100644 --- a/kitchen/src/web/auth.rs +++ b/kitchen/src/web/auth.rs @@ -24,17 +24,17 @@ use cookie::{Cookie, SameSite}; use secrecy::Secret; use tracing::{debug, info, instrument}; -use super::session::{self, AuthStore}; +use super::storage::{self, AuthStore}; #[instrument(skip_all, fields(user=%auth.0.0))] pub async fn handler( auth: AuthBasic, - Extension(session_store): Extension, + Extension(session_store): Extension, ) -> impl IntoResponse { // NOTE(jwall): It is very important that you do **not** log the password // here. We convert the AuthBasic into UserCreds immediately to help prevent // that. Do not circumvent that protection. - let auth = session::UserCreds::from(auth); + let auth = storage::UserCreds::from(auth); info!("Handling authentication request"); if let Ok(true) = session_store.check_user_creds(&auth).await { debug!("successfully authenticated user"); @@ -45,7 +45,7 @@ pub async fn handler( let cookie_value = session_store.store_session(session).await.unwrap().unwrap(); let mut headers = HeaderMap::new(); // 3. Construct the Session Cookie. - let cookie = Cookie::build(session::AXUM_SESSION_COOKIE_NAME, cookie_value) + let cookie = Cookie::build(storage::AXUM_SESSION_COOKIE_NAME, cookie_value) .same_site(SameSite::Strict) .secure(true) .finish(); @@ -63,10 +63,10 @@ pub async fn handler( } } -impl From for session::UserCreds { +impl From for storage::UserCreds { fn from(AuthBasic((id, pass)): AuthBasic) -> Self { Self { - id: session::UserId(id.clone()), + id: storage::UserId(id.clone()), pass: Secret::from_str(pass.clone().unwrap().as_str()).unwrap(), } } diff --git a/kitchen/src/web/mod.rs b/kitchen/src/web/mod.rs index bb1166e..91fad27 100644 --- a/kitchen/src/web/mod.rs +++ b/kitchen/src/web/mod.rs @@ -29,10 +29,10 @@ use tower::ServiceBuilder; use tower_http::trace::TraceLayer; use tracing::{debug, info, instrument}; -use session::AuthStore; +use storage::{APIStore, AuthStore}; mod auth; -mod session; +mod storage; #[derive(RustEmbed)] #[folder = "../web/dist"] @@ -80,11 +80,11 @@ async fn ui_static_assets(Path(path): Path) -> impl IntoResponse { #[instrument] async fn api_recipes( Extension(store): Extension>, - Extension(app_store): Extension>, - session: session::UserIdFromSession, + Extension(app_store): Extension>, + session: storage::UserIdFromSession, ) -> impl IntoResponse { // Select recipes based on the user-id if it exists or serve the default if it does not. - use session::{UserId, UserIdFromSession::*}; + use storage::{UserId, UserIdFromSession::*}; let result = match session { NoUserId => store .get_recipes() @@ -105,11 +105,11 @@ async fn api_recipes( #[instrument] async fn api_categories( Extension(store): Extension>, - Extension(app_store): Extension>, - session: session::UserIdFromSession, + Extension(app_store): Extension>, + session: storage::UserIdFromSession, ) -> impl IntoResponse { // Select Categories based on the user-id if it exists or serve the default if it does not. - use session::{UserId, UserIdFromSession::*}; + use storage::{UserId, UserIdFromSession::*}; let categories_result = match session { NoUserId => store .get_categories() @@ -129,11 +129,11 @@ async fn api_categories( } pub async fn add_user(store_path: PathBuf, username: String, password: String) { - let app_store = session::SqliteStore::new(store_path) + let app_store = storage::SqliteStore::new(store_path) .await .expect("Unable to create app_store"); - let user_creds = session::UserCreds { - id: session::UserId(username), + let user_creds = storage::UserCreds { + id: storage::UserId(username), pass: secrecy::Secret::from(password), }; app_store @@ -146,7 +146,7 @@ pub async fn add_user(store_path: PathBuf, username: String, password: String) { pub async fn ui_main(recipe_dir_path: PathBuf, store_path: PathBuf, listen_socket: SocketAddr) { let store = Arc::new(recipe_store::AsyncFileStore::new(recipe_dir_path.clone())); //let dir_path = (&dir_path).clone(); - let app_store = session::SqliteStore::new(store_path) + let app_store = storage::SqliteStore::new(store_path) .await .expect("Unable to create app_store"); let router = Router::new() diff --git a/kitchen/src/web/session.rs b/kitchen/src/web/storage.rs similarity index 95% rename from kitchen/src/web/session.rs rename to kitchen/src/web/storage.rs index ac5bab7..cfa8f08 100644 --- a/kitchen/src/web/session.rs +++ b/kitchen/src/web/storage.rs @@ -236,8 +236,23 @@ impl AuthStore for SqliteStore { } } -impl SqliteStore { - pub async fn get_categories_for_user( +// TODO(jwall): We need to do some serious error modeling here. +#[async_trait] +pub trait APIStore { + async fn get_categories_for_user( + &self, + user_id: &str, + ) -> Result, recipe_store::Error>; + + async fn get_recipes_for_user( + &self, + user_id: &str, + ) -> Result>, recipe_store::Error>; +} + +#[async_trait] +impl APIStore for SqliteStore { + async fn get_categories_for_user( &self, user_id: &str, ) -> Result, recipe_store::Error> { @@ -257,7 +272,7 @@ impl SqliteStore { } } - pub async fn get_recipes_for_user( + async fn get_recipes_for_user( &self, user_id: &str, ) -> Result>, recipe_store::Error> {