diff --git a/.github/workflows/build-release-binaries.yml b/.github/workflows/build-release-binaries.yml index 6848e3a..ccbcc65 100644 --- a/.github/workflows/build-release-binaries.yml +++ b/.github/workflows/build-release-binaries.yml @@ -6,10 +6,10 @@ on: - '**' - '!website/**' -# pull_request: -# paths: -# - '**' -# - '!website/**' + # pull_request: + # paths: + # - '**' + # - '!website/**' workflow_dispatch: diff --git a/database/src/error.rs b/database/src/error.rs index 14f7eb9..5f24532 100644 --- a/database/src/error.rs +++ b/database/src/error.rs @@ -10,12 +10,14 @@ pub enum Error { Io(std::io::Error), } +/// sqlx::Error impl From for Error { fn from(err: sqlx::Error) -> Self { Error::SQLx(err) } } +/// std::io::Error impl From for Error { fn from(err: std::io::Error) -> Self { Error::Io(err) diff --git a/database/src/sqlite.rs b/database/src/sqlite.rs index 455b0b9..551ccee 100644 --- a/database/src/sqlite.rs +++ b/database/src/sqlite.rs @@ -43,7 +43,7 @@ impl Database { Ok(self.conn.execute(query).await?) } - /// Find user + /// Search for a user /// ```ignore,rust /// use homedisk_database::{Database, User}; /// @@ -52,7 +52,7 @@ impl Database { /// db.find_user(&user.username, &user.password).await?; /// ``` pub async fn find_user(&self, username: &str, password: &str) -> Result { - debug!("Searching for user - {}", username); + debug!("Searching for a user - {}", username); let query = sqlx::query_as::<_, User>("SELECT * FROM user WHERE username = ? AND password = ?") @@ -74,7 +74,7 @@ impl Database { }) } - /// Find user by UUID + /// Search for a user by UUID /// ```ignore,rust /// use homedisk_database::{Database, User}; /// @@ -111,10 +111,12 @@ mod tests { use crate::{Database, User}; + /// Utils to open database in tests async fn open_db() -> Database { Database::open("sqlite::memory:").await.expect("open db") } + /// Utils to create a new user in tests async fn new_user(db: &Database) { // create user table db.conn @@ -129,11 +131,13 @@ mod tests { db.create_user(&user).await.expect("create user"); } + /// Open database in memory #[tokio::test] async fn open_db_in_memory() { open_db().await; } + /// Create a new user #[tokio::test] async fn create_user() { let db = open_db().await; @@ -141,6 +145,7 @@ mod tests { new_user(&db).await; } + /// Search for a user #[tokio::test] async fn find_user() { let db = open_db().await; @@ -157,6 +162,7 @@ mod tests { assert_eq!(res.password, user.password) } + /// Search for a user with an invalid password to see if the user is returned (it shouldn't be) #[tokio::test] async fn find_user_wrong_password() { let db = open_db().await; @@ -173,6 +179,7 @@ mod tests { assert_eq!(err.to_string(), "user not found") } + /// Search for a user who does not exist #[tokio::test] async fn find_user_wrong_username() { let db = open_db().await; @@ -189,6 +196,7 @@ mod tests { assert_eq!(err.to_string(), "user not found") } + /// Search for a user by UUID #[tokio::test] async fn find_user_by_id() { let db = open_db().await; @@ -202,13 +210,14 @@ mod tests { assert_eq!(res.password, user.password) } + /// Search for a user by UUID who does not exist #[tokio::test] async fn find_user_wrong_id() { let db = open_db().await; new_user(&db).await; - let other_user = User::new("other_user", "secret@passphrase123!"); + let other_user = User::new("other_user", "my secret passphrase"); let err = db.find_user_by_id(other_user.id).await.unwrap_err(); diff --git a/server/src/auth/login.rs b/server/src/auth/login.rs index d7967d5..6526942 100644 --- a/server/src/auth/login.rs +++ b/server/src/auth/login.rs @@ -8,6 +8,7 @@ use homedisk_types::{ use crate::middleware::{create_token, validate_json}; +/// Handle `/auth/login` requests pub async fn handle( Extension(db): Extension, Extension(config): Extension, @@ -16,10 +17,13 @@ pub async fn handle( // validate json request let request = validate_json::(request)?; + // create `User` type let user = User::new(&request.username, &request.password); + // search for a user in database let response = match db.find_user(&user.username, &user.password).await { Ok(user) => { + // create user token let token = create_token(&user, config.jwt.secret.as_bytes(), config.jwt.expires)?; Response::LoggedIn { diff --git a/server/src/auth/whoami.rs b/server/src/auth/whoami.rs index 504f771..9527fd0 100644 --- a/server/src/auth/whoami.rs +++ b/server/src/auth/whoami.rs @@ -9,6 +9,7 @@ use homedisk_types::{ use crate::middleware::validate_jwt; +/// Handle `/auth/whoami` requests pub async fn handle( db: Extension, config: Extension, diff --git a/server/src/fs/delete.rs b/server/src/fs/delete.rs index ea06733..79d8070 100644 --- a/server/src/fs/delete.rs +++ b/server/src/fs/delete.rs @@ -13,6 +13,7 @@ use homedisk_types::{ use crate::fs::validate_path; use crate::middleware::{find_user, validate_jwt}; +/// Handle `/fs/delete` requests pub async fn handle( Extension(db): Extension, Extension(config): Extension, diff --git a/server/src/fs/list.rs b/server/src/fs/list.rs index 04af512..086de55 100644 --- a/server/src/fs/list.rs +++ b/server/src/fs/list.rs @@ -31,6 +31,7 @@ fn dir_size(path: impl Into) -> io::Result { dir_size(fs::read_dir(path.into())?) } +/// Handle `/fs/list` requests pub async fn handle( Extension(db): Extension, Extension(config): Extension, diff --git a/server/src/fs/mod.rs b/server/src/fs/mod.rs index 05a984e..06fccb1 100644 --- a/server/src/fs/mod.rs +++ b/server/src/fs/mod.rs @@ -10,7 +10,7 @@ pub fn app() -> axum::Router { axum::Router::new() .route("/list", post(list::handle)) .route("/upload", post(upload::handle)) - .route("/delete", delete(upload::handle)) + .route("/delete", delete(delete::handle)) .route("/download", get(download::handle)) .route("/createdir", post(create_dir::handle)) } diff --git a/server/src/lib.rs b/server/src/lib.rs index 39e6553..f462848 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -10,6 +10,7 @@ use homedisk_types::config::types::Config; use log::{debug, info}; use tower_http::cors::{AllowOrigin, CorsLayer}; +/// Handle `/health-check` requests async fn health_check() -> &'static str { "I'm alive!" } diff --git a/server/src/middleware/auth.rs b/server/src/middleware/auth.rs index 3c1a351..bff8f3b 100644 --- a/server/src/middleware/auth.rs +++ b/server/src/middleware/auth.rs @@ -2,6 +2,7 @@ use homedisk_types::errors::{AuthError, ServerError}; use jsonwebtoken::TokenData; use rust_utilities::crypto::jsonwebtoken::{Claims, Token}; +/// Validate user token pub fn validate_jwt(secret: &[u8], token: &str) -> Result, ServerError> { match Token::decode(secret, token.to_string()) { Ok(claims) => Ok(claims), diff --git a/server/src/middleware/jwt.rs b/server/src/middleware/jwt.rs index bd79529..ddefbe4 100644 --- a/server/src/middleware/jwt.rs +++ b/server/src/middleware/jwt.rs @@ -2,6 +2,7 @@ use homedisk_database::{Database, User}; use homedisk_types::errors::{AuthError, ServerError}; use rust_utilities::crypto::jsonwebtoken::{Claims, Token}; +/// Create user token pub fn create_token(user: &User, secret: &[u8], expires: i64) -> Result { let token = Token::new(secret, Claims::new(user.id.clone(), expires)); @@ -11,6 +12,7 @@ pub fn create_token(user: &User, secret: &[u8], expires: i64) -> Result Result { match db.find_user_by_id(user_id).await { Ok(user) => Ok(user), diff --git a/server/src/middleware/validate_json.rs b/server/src/middleware/validate_json.rs index 0a8255d..07f5204 100644 --- a/server/src/middleware/validate_json.rs +++ b/server/src/middleware/validate_json.rs @@ -1,6 +1,7 @@ use axum::{extract::rejection::JsonRejection, Json}; use homedisk_types::errors::ServerError; +/// Validate json request pub fn validate_json( payload: Result, JsonRejection>, ) -> Result, ServerError> { diff --git a/types/src/config/toml.rs b/types/src/config/toml.rs index b5b7686..3e6876c 100644 --- a/types/src/config/toml.rs +++ b/types/src/config/toml.rs @@ -9,7 +9,7 @@ use super::types::Config; impl Config { /// Parse configuration file pub fn parse() -> Result { - // configuration file path + // config file path let config_dir = option_return!(dirs::config_dir(), "get config dir")?; let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy()); diff --git a/types/src/database/user.rs b/types/src/database/user.rs index f755d4b..22db9e8 100644 --- a/types/src/database/user.rs +++ b/types/src/database/user.rs @@ -23,7 +23,7 @@ impl User { // change username to lowercase let username = username.to_lowercase(); - // create user UUID + // generate a user UUID let sha1_name = CryptographicHash::hash(Algorithm::SHA1, username.as_bytes()); let id = Uuid::new_v5(&Uuid::NAMESPACE_X500, &sha1_name).to_string(); diff --git a/types/src/errors/mod.rs b/types/src/errors/mod.rs index 0e6ba7d..3ce5e9d 100644 --- a/types/src/errors/mod.rs +++ b/types/src/errors/mod.rs @@ -30,7 +30,7 @@ pub enum ServerError { #[error("failed to extract the request body")] BytesRejection, - #[error("unexcepted error")] + #[error("unexpected error - {0}")] Other(String), } diff --git a/types/src/lib.rs b/types/src/lib.rs index 075ea5c..d422234 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -1,8 +1,7 @@ pub mod auth; pub mod config; -pub mod errors; -pub mod macros; - #[cfg(feature = "database")] pub mod database; +pub mod errors; pub mod fs; +pub mod macros;