Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: verify email address validity on signup #64

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]"))
}
}