This commit is contained in:
MedzikUser 2022-06-23 11:52:48 +02:00
parent 03afaa1b49
commit 63250bdf2c
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
13 changed files with 74 additions and 65 deletions

60
Cargo.lock generated
View File

@ -323,12 +323,12 @@ dependencies = [
[[package]]
name = "crossbeam-utils"
version = "0.8.8"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38"
checksum = "8ff1f980957787286a554052d03c7aee98d99cc32e09f6d45f0a814133c87978"
dependencies = [
"cfg-if",
"lazy_static",
"once_cell",
]
[[package]]
@ -343,9 +343,9 @@ dependencies = [
[[package]]
name = "crypto-utils"
version = "0.2.0"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c21850a20f41468aedddf2dcf88124ae8e6959c8c7b610c986f870fd62fbba3c"
checksum = "8d09d7237fa1de6fb962d691ece115c8f9d69e36aad536129e0b03bdaf8bda96"
dependencies = [
"chrono",
"jsonwebtoken",
@ -601,12 +601,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hashbrown"
version = "0.12.1"
@ -622,7 +616,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d452c155cb93fecdfb02a73dd57b5d8e442c2063bd7aac72f1bc5e4263a43086"
dependencies = [
"hashbrown 0.12.1",
"hashbrown",
]
[[package]]
@ -667,13 +661,10 @@ dependencies = [
name = "homedisk-database"
version = "0.0.0"
dependencies = [
"anyhow",
"futures-util",
"homedisk-types",
"log",
"serde",
"sqlx",
"thiserror",
"tokio",
"uuid",
]
@ -691,7 +682,6 @@ dependencies = [
"homedisk-database",
"homedisk-types",
"hyper",
"jsonwebtoken",
"log",
"serde",
"thiserror",
@ -792,12 +782,12 @@ dependencies = [
[[package]]
name = "indexmap"
version = "1.8.2"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"hashbrown 0.11.2",
"hashbrown",
]
[[package]]
@ -949,9 +939,9 @@ dependencies = [
[[package]]
name = "mio"
version = "0.8.3"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "713d550d9b44d89174e066b7a6217ae06234c10cb47819a88290d2b353c31799"
checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf"
dependencies = [
"libc",
"log",
@ -1137,18 +1127,18 @@ checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]]
name = "proc-macro2"
version = "1.0.39"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c54b25569025b7fc9651de43004ae593a75ad88543b17178aa5e1b9c4f15f56f"
checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.18"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804"
dependencies = [
"proc-macro2",
]
@ -1317,7 +1307,7 @@ dependencies = [
"num-bigint",
"num-traits",
"thiserror",
"time 0.3.9",
"time 0.3.11",
]
[[package]]
@ -1328,7 +1318,7 @@ checksum = "48dfff04aade74dd495b007c831cd6f4e0cee19c344dd9dc0884c0289b70a786"
dependencies = [
"log",
"termcolor",
"time 0.3.9",
"time 0.3.11",
]
[[package]]
@ -1476,9 +1466,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.96"
version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf"
checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd"
dependencies = [
"proc-macro2",
"quote",
@ -1555,9 +1545,9 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.9"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
checksum = "72c91f41dcb2f096c05f0873d667dceec1087ce5bcf984ec8ffb19acddbb3217"
dependencies = [
"itoa",
"libc",
@ -1662,9 +1652,9 @@ dependencies = [
[[package]]
name = "tower"
version = "0.4.12"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a89fd63ad6adf737582df5db40d286574513c69a11dac5214dc3b5603d6713e"
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
@ -1714,9 +1704,9 @@ checksum = "343bc9466d3fe6b0f960ef45960509f84480bf4fd96f92901afe7ff3df9d3a62"
[[package]]
name = "tower-service"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6"
checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
[[package]]
name = "tracing"

View File

@ -4,14 +4,11 @@ version = "0.0.0"
edition = "2021"
[dependencies]
thiserror = "1.0.31"
log = "0.4.17"
futures-util = "0.3.21"
serde = { version = "1.0.137", features = ["derive"] }
sqlx = { version = "0.6.0", features = ["runtime-tokio-rustls", "sqlite"] }
uuid = { version = "1.1.2", features = ["v5"] }
homedisk-types = { path = "../types", features = ["database"] }
[dev-dependencies]
anyhow = "1.0.58"
tokio = { version = "1.19.2", features = ["rt-multi-thread", "macros"] }

View File

@ -2,6 +2,10 @@
mod sqlite;
/// Imported from [homedisk_types::database::User].
pub use homedisk_types::database::User;
/// Imported from [homedisk_types::errors::DatabaseError].
pub use homedisk_types::errors::DatabaseError as Error;
/// Imported from [homedisk_types::errors::DatabaseResult].
pub use homedisk_types::errors::DatabaseResult as Result;
pub use sqlite::*;

View File

@ -2,7 +2,7 @@ use futures_util::TryStreamExt;
use log::debug;
use sqlx::{sqlite::SqliteQueryResult, Executor, Row, SqlitePool};
use super::{Error, User};
use super::{Error, Result, User};
/// SQL Database
#[derive(Debug, Clone)]
@ -14,7 +14,7 @@ pub struct Database {
impl Database {
/// Open a SQLite database
/// ```no_run
/// # async fn foo() -> anyhow::Result<()> {
/// # async fn foo() -> homedisk_database::Result<()> {
/// use homedisk_database::Database;
///
/// // open database in memory
@ -25,7 +25,7 @@ impl Database {
///
/// # Ok(()) }
/// ```
pub async fn open(path: &str) -> Result<Self, Error> {
pub async fn open(path: &str) -> Result<Self> {
debug!("Opening SQLite database");
// create a database pool
@ -37,13 +37,13 @@ impl Database {
/// Create all required tabled for HomeDisk
/// ```
/// # async fn foo() -> anyhow::Result<()> {
/// # async fn foo() -> homedisk_database::Result<()> {
/// # let db = homedisk_database::Database::open("sqlite::memory:").await?;
/// db.create_tables().await?;
///
/// # Ok(()) }
/// ```
pub async fn create_tables(&self) -> Result<SqliteQueryResult, Error> {
pub async fn create_tables(&self) -> Result<SqliteQueryResult> {
let query = sqlx::query(include_str!("../../tables.sql"));
Ok(self.conn.execute(query).await?)
@ -51,7 +51,7 @@ impl Database {
/// Create a new User
/// ```
/// # async fn foo() -> anyhow::Result<()> {
/// # async fn foo() -> homedisk_database::Result<()> {
/// # let db = homedisk_database::Database::open("sqlite::memory:").await?;
/// # db.create_tables().await?;
/// use homedisk_database::User;
@ -64,7 +64,7 @@ impl Database {
///
/// # Ok(()) }
/// ```
pub async fn create_user(&self, user: &User) -> Result<SqliteQueryResult, Error> {
pub async fn create_user(&self, user: &User) -> Result<SqliteQueryResult> {
debug!("Creating user - {}", user.username);
// insert user to a database
@ -79,7 +79,7 @@ impl Database {
/// Search for a user
/// ```
/// # async fn foo() -> anyhow::Result<()> {
/// # async fn foo() -> homedisk_database::Result<()> {
/// # let db = homedisk_database::Database::open("sqlite::memory:").await?;
/// # db.create_tables().await?;
/// use homedisk_database::User;
@ -92,7 +92,7 @@ impl Database {
///
/// # Ok(()) }
/// ```
pub async fn find_user(&self, user: &User) -> Result<User, Error> {
pub async fn find_user(&self, user: &User) -> Result<User> {
debug!("Searching for a user - {}", user.username);
// create query request to database
@ -124,7 +124,7 @@ impl Database {
/// Search for a user by UUID
/// ```
/// # async fn foo() -> anyhow::Result<()> {
/// # async fn foo() -> homedisk_database::Result<()> {
/// # let db = homedisk_database::Database::open("sqlite::memory:").await?;
/// # db.create_tables().await?;
/// use homedisk_database::User;
@ -137,7 +137,7 @@ impl Database {
///
/// # Ok(()) }
/// ```
pub async fn find_user_by_id(&self, id: &str) -> Result<User, Error> {
pub async fn find_user_by_id(&self, id: &str) -> Result<User> {
debug!("Searching for a user by UUID - {}", id);
// create query request to database

View File

@ -10,9 +10,8 @@ base64 = "0.13.0"
byte-unit = "4.0.14"
futures = "0.3.21"
hyper = { version = "0.14.19", features = ["full"] }
jsonwebtoken = "8.1.1"
log = "0.4.17"
crypto-utils = { version = "0.2.0", features = ["jwt"] }
crypto-utils = { version = "0.3.0", features = ["jwt"] }
serde = { version = "1.0.137", features = ["derive"] }
thiserror = "1.0.31"
tower-http = { version = "0.3.4", features = ["full"] }

View File

@ -1,9 +1,8 @@
use crypto_utils::jsonwebtoken::{Claims, Token};
use crypto_utils::jsonwebtoken::{Token, TokenData};
use homedisk_types::errors::{AuthError, ServerError};
use jsonwebtoken::TokenData;
/// Validate user token
pub fn validate_jwt(secret: &[u8], token: &str) -> Result<TokenData<Claims>, ServerError> {
pub fn validate_jwt(secret: &[u8], token: &str) -> Result<TokenData, ServerError> {
match Token::decode(secret, token.to_string()) {
// if success return claims
Ok(claims) => Ok(claims),

View File

@ -16,6 +16,6 @@ zeroize = { version = "1.5.5", features = ["derive"] }
axum = { version = "0.5.9", optional = true }
toml = { version = "0.5.9", optional = true }
dirs = { version = "4.0.0", optional = true }
crypto-utils = { version = "0.2.0", features = ["sha"], optional = true }
crypto-utils = { version = "0.3.0", features = ["sha"], optional = true }
hex = { version = "0.4.3", optional = true }
sqlx = { version = "0.6.0", features = ["sqlite"], optional = true }

View File

@ -22,6 +22,8 @@ impl User {
/// use homedisk_types::database::User;
///
/// let user = User::new("medzik", "SuperSecretPassword123!");
///
/// # assert_eq!(user.username, "medzik")
/// ```
pub fn new(username: &str, password: &str) -> Self {
// change username to lowercase
@ -81,6 +83,19 @@ mod tests {
use super::User;
/// Check if the id is reproducable
#[test]
fn check_id_reproducable() {
// example user data
let username = "test";
let password = "password";
let user_a = User::new(username, password);
let user_b = User::new(username, password);
assert_eq!(user_a.id, user_b.id)
}
/// Check if the username is in lowercase
#[test]
fn check_username_is_in_lowercase() {

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// HTTP `/auth/*` Errors
#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)]
#[derive(Debug, Clone, Serialize, Deserialize, Error)]
pub enum Error {
/// Username or Password incorrect.
#[error("user not found")]

View File

@ -1,27 +1,30 @@
use thiserror::Error;
/// Database Error
#[derive(Debug, thiserror::Error)]
#[derive(Debug, Error)]
pub enum Error {
/// Username or Password incorrect.
#[error("user not found")]
UserNotFound,
/// sqlx::Error
/// [sqlx::Error](https://docs.rs/sqlx/latest/sqlx/enum.Error.html)
#[error("sqlx error - {0}")]
SQLx(sqlx::Error),
/// std::io::Error
/// [std::io::Error]
#[error("std::io error - {0}")]
StdIo(std::io::Error),
}
/// sqlx::Error
impl From<sqlx::Error> for Error {
fn from(err: sqlx::Error) -> Self {
Error::SQLx(err)
}
}
/// std::io::Error
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::StdIo(err)
}
}
/// Custom Result alias for a [enum@Error].
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use thiserror::Error;
/// `/fs/*` Error
#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)]
#[derive(Debug, Clone, Serialize, Deserialize, Error)]
pub enum Error {
/// File doesn't exists.
#[error("file doesn't exists")]

View File

@ -8,5 +8,6 @@ mod server;
pub use auth::Error as AuthError;
pub use database::Error as DatabaseError;
pub use database::Result as DatabaseResult;
pub use fs::Error as FsError;
pub use server::Error as ServerError;

View File

@ -1,9 +1,10 @@
use serde::{Deserialize, Serialize};
use thiserror::Error;
use super::{AuthError, FsError};
/// HTTP Server Error
#[derive(Debug, Clone, Serialize, Deserialize, thiserror::Error)]
#[derive(Debug, Clone, Serialize, Deserialize, Error)]
#[serde(tag = "error", content = "error_message", rename_all = "kebab-case")]
pub enum Error {
/// Auth error.