From 6451055e2b6a71f61fb4f3bca8769786044f96be Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 12 Sep 2022 00:23:57 +0530 Subject: [PATCH] feat: argon2_creds error handlling --- Cargo.lock | 1 + Cargo.toml | 1 + src/errors.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 4 files changed, 67 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 656f575..1a3738f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1380,6 +1380,7 @@ dependencies = [ "num_cpus", "num_enum", "pretty_env_logger", + "rand", "serde", "serde_json", "sqlx", diff --git a/Cargo.toml b/Cargo.toml index 73fbcfd..a0fc585 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ tokio = { version = "1", features=["sync"]} num_enum = "0.5.7" mime_guess = "2.0.4" +rand = "0.8.5" [dev-dependencies] mktemp = "0.4.1" diff --git a/src/errors.rs b/src/errors.rs index 509cc9c..9ee5433 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -25,6 +25,7 @@ use actix_web::{ http::{header, StatusCode}, HttpResponse, HttpResponseBuilder, }; +use argon2_creds::errors::CredsError; use config::ConfigError as ConfigErrorInner; use derive_more::{Display, Error}; use git2::Error as GitError; @@ -137,6 +138,43 @@ pub enum ServiceError { /// Account not found #[display(fmt = "Account not found")] AccountNotFound, + + #[display( + fmt = "This server is is closed for registration. Contact admin if this is unexpecter" + )] + /// registration failure, server is is closed for registration + ClosedForRegistration, + + #[display(fmt = "The value you entered for email is not an email")] //405j + /// The value you entered for email is not an email" + NotAnEmail, + + #[display(fmt = "Wrong password")] + /// wrong password + WrongPassword, + + /// when the value passed contains profanity + #[display(fmt = "Can't allow profanity in usernames")] + ProfanityError, + /// when the value passed contains blacklisted words + /// see [blacklist](https://github.com/shuttlecraft/The-Big-Username-Blacklist) + #[display(fmt = "Username contains blacklisted words")] + BlacklistError, + /// when the value passed contains characters not present + /// in [UsernameCaseMapped](https://tools.ietf.org/html/rfc8265#page-7) + /// profile + #[display(fmt = "username_case_mapped violation")] + UsernameCaseMappedError, + + #[display(fmt = "Passsword too short")] + /// password too short + PasswordTooShort, + #[display(fmt = "password too long")] + /// password too long + PasswordTooLong, + #[display(fmt = "Passwords don't match")] + /// passwords don't match + PasswordsDontMatch, } impl From for ServiceError { @@ -199,6 +237,32 @@ impl ResponseError for ServiceError { ServiceError::EmailTaken => StatusCode::BAD_REQUEST, ServiceError::UsernameTaken => StatusCode::BAD_REQUEST, ServiceError::AccountNotFound => StatusCode::NOT_FOUND, + + ServiceError::ProfanityError => StatusCode::BAD_REQUEST, //BADREQUEST, + ServiceError::BlacklistError => StatusCode::BAD_REQUEST, //BADREQUEST, + ServiceError::UsernameCaseMappedError => StatusCode::BAD_REQUEST, //BADREQUEST, + + ServiceError::PasswordTooShort => StatusCode::BAD_REQUEST, //BADREQUEST, + ServiceError::PasswordTooLong => StatusCode::BAD_REQUEST, //BADREQUEST, + ServiceError::PasswordsDontMatch => StatusCode::BAD_REQUEST, //BADREQUEST, + ServiceError::ClosedForRegistration => StatusCode::FORBIDDEN, //FORBIDDEN, + ServiceError::NotAnEmail => StatusCode::BAD_REQUEST, //BADREQUEST, + ServiceError::WrongPassword => StatusCode::UNAUTHORIZED, //UNAUTHORIZED, + } + } +} + +impl From for ServiceError { + #[cfg(not(tarpaulin_include))] + fn from(e: CredsError) -> ServiceError { + match e { + CredsError::UsernameCaseMappedError => ServiceError::UsernameCaseMappedError, + CredsError::ProfainityError => ServiceError::ProfanityError, + CredsError::BlacklistError => ServiceError::BlacklistError, + CredsError::NotAnEmail => ServiceError::NotAnEmail, + CredsError::Argon2Error(_) => ServiceError::InternalServerError, + CredsError::PasswordTooLong => ServiceError::PasswordTooLong, + CredsError::PasswordTooShort => ServiceError::PasswordTooShort, } } } diff --git a/src/main.rs b/src/main.rs index fedc13f..c2262ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,6 +37,7 @@ mod serve; mod settings; #[cfg(test)] mod tests; +mod utils; use ctx::Ctx; pub use routes::ROUTES as V1_API_ROUTES;