Refactor some modules for improved naming

This commit is contained in:
Jeremy Wall 2022-09-03 12:16:35 -04:00
parent 1ce8da0294
commit f87196e4c8
3 changed files with 36 additions and 21 deletions

View File

@ -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<session::SqliteStore>,
Extension(session_store): Extension<storage::SqliteStore>,
) -> 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<AuthBasic> for session::UserCreds {
impl From<AuthBasic> 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(),
}
}

View File

@ -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<String>) -> impl IntoResponse {
#[instrument]
async fn api_recipes(
Extension(store): Extension<Arc<recipe_store::AsyncFileStore>>,
Extension(app_store): Extension<Arc<session::SqliteStore>>,
session: session::UserIdFromSession,
Extension(app_store): Extension<Arc<storage::SqliteStore>>,
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<Arc<recipe_store::AsyncFileStore>>,
Extension(app_store): Extension<Arc<session::SqliteStore>>,
session: session::UserIdFromSession,
Extension(app_store): Extension<Arc<storage::SqliteStore>>,
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()

View File

@ -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<Option<String>, recipe_store::Error>;
async fn get_recipes_for_user(
&self,
user_id: &str,
) -> Result<Option<Vec<RecipeEntry>>, recipe_store::Error>;
}
#[async_trait]
impl APIStore for SqliteStore {
async fn get_categories_for_user(
&self,
user_id: &str,
) -> Result<Option<String>, 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<Option<Vec<RecipeEntry>>, recipe_store::Error> {