Skip to content

Commit

Permalink
Merge pull request #64 from torrust/bug-63-email-addresses-arent-prop…
Browse files Browse the repository at this point in the history
…erly-checked-for-validity-on-signup

fix: verify email address validity on signup
  • Loading branch information
mickvandijke authored Aug 29, 2022
2 parents 8312543 + d30fd46 commit 513debf
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ tokio = {version = "1.13", features = ["macros", "io-util", "net", "time", "rt-m

lettre = { version = "0.10.0-rc.3", features = ["builder", "tokio1", "tokio1-rustls-tls", "smtp-transport"]}
sailfish = "0.3.3"

regex = "1.6.0"
6 changes: 3 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub enum ServiceError {

#[display(fmt = "Email is required")] //405j
EmailMissing,
#[display(fmt = "The value you entered for email is not an email")] //405j
NotAnEmail,
#[display(fmt = "Please enter a valid email address")] //405j
EmailInvalid,

#[display(fmt = "The value you entered for URL is not a URL")] //405j
NotAUrl,
Expand Down Expand Up @@ -130,7 +130,7 @@ impl ResponseError for ServiceError {
fn status_code(&self) -> StatusCode {
match self {
ServiceError::ClosedForRegistration => StatusCode::FORBIDDEN,
ServiceError::NotAnEmail => StatusCode::BAD_REQUEST,
ServiceError::EmailInvalid => StatusCode::BAD_REQUEST,
ServiceError::NotAUrl => StatusCode::BAD_REQUEST,
ServiceError::WrongPasswordOrUsername => StatusCode::FORBIDDEN,
ServiceError::UsernameNotFound => StatusCode::NOT_FOUND,
Expand Down
8 changes: 8 additions & 0 deletions src/handlers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::config::EmailOnSignup;
use crate::models::response::OkResponse;
use crate::models::response::TokenResponse;
use crate::mailer::VerifyClaims;
use crate::utils::regex::validate_email_address;
use crate::utils::time::current_time;

pub fn init_routes(cfg: &mut web::ServiceConfig) {
Expand Down Expand Up @@ -63,6 +64,13 @@ pub async fn register(req: HttpRequest, mut payload: web::Json<Register>, app_da
_ => {}
}

if let Some(email) = &payload.email {
// check if email address is valid
if !validate_email_address(email) {
return Err(ServiceError::EmailInvalid)
}
}

if payload.password != payload.confirm_password {
return Err(ServiceError::PasswordsDontMatch)
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod parse_torrent;
pub mod time;
pub mod hex;
pub mod regex;
31 changes: 31 additions & 0 deletions src/utils/regex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use regex::Regex;

pub fn validate_email_address(email_address_to_be_checked: &str) -> bool {
let email_regex = Regex::new(r"^([a-z\d_+]([a-z\d_+.]*[a-z\d_+])?)@([a-z\d]+([\-.][a-z\d]+)*\.[a-z]{2,6})").unwrap();

email_regex.is_match(email_address_to_be_checked)
}

#[cfg(test)]
mod tests {
use crate::utils::regex::validate_email_address;

#[test]
fn validate_email_address_test() {
assert!(!validate_email_address("test"));

assert!(!validate_email_address("test@"));

assert!(!validate_email_address("test@torrust"));

assert!(!validate_email_address("test@torrust."));

assert!(!validate_email_address("test@."));

assert!(!validate_email_address("[email protected]"));

assert!(validate_email_address("[email protected]"));

assert!(validate_email_address("[email protected]"))
}
}

0 comments on commit 513debf

Please sign in to comment.