diff --git a/Cargo.lock b/Cargo.lock index ff495d0..31a0ce8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -298,6 +298,12 @@ dependencies = [ "nodrop", ] +[[package]] +name = "ascii_utils" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71938f30533e4d95a6d17aa530939da3842c2ab6f4f84b9dae68447e4129f74a" + [[package]] name = "async-trait" version = "0.1.31" @@ -505,6 +511,7 @@ dependencies = [ "actix-web", "encoding_rs", "futures", + "mailchecker", "mysql", "percent-encoding", "serde", @@ -654,6 +661,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "fast_chemail" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "495a39d30d624c2caabe6312bfead73e7717692b44e0b32df168c275a2e8e9e4" +dependencies = [ + "ascii_utils", +] + [[package]] name = "flate2" version = "1.0.14" @@ -1068,6 +1084,15 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "mailchecker" +version = "3.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1aeed7b929850f57cbab30c8569b4eb608d9096096a89d815f99ab9fe2f454d" +dependencies = [ + "fast_chemail", +] + [[package]] name = "match_cfg" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index e3bfbb2..5ee8e40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ actix-rt = "1.1.1" serde = "1.0.110" futures = "0.3.5" encoding_rs = "0.8.23" -percent-encoding = "2.1.0" \ No newline at end of file +percent-encoding = "2.1.0" +mailchecker = "3.3.6" \ No newline at end of file diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index b8e4f20..1b7f546 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -6,6 +6,14 @@ use crate::controllers::routes::RequestResult; /// @author Pierre Hubert /// Sign in user -pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { - request.success("Login user") +pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { + let email = request.post_email("userMail")?; + let password = request.post_string_opt("userPassword", 3, true)?; + + // TODO : limit request + + // Authenticate user + + + request.success("") } \ No newline at end of file diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 755760f..ea03ec0 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -133,12 +133,26 @@ impl HttpRequestHandler { /// Get a post string pub fn post_string(&mut self, name: &str) -> ResultBoxError { + self.post_string_opt(name, 1, true) + } + + /// Get a post string, specifying minimum length + pub fn post_string_opt(&mut self, name: &str, min_length: usize, required: bool) + -> ResultBoxError { let param = self.post_parameter(name)?; - match ¶m.string { - Some(s) => Ok(s.to_string()), - None => { - Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err()) + match (¶m.string, required) { + (None, true) => + Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err()), + + (None, false) => Ok(String::new()), + + (Some(s), _) => { + if s.len() >= min_length { + Ok(s.to_string()) + } else { + Err(self.bad_request(format!("'{}' is too short!", name)).unwrap_err()) + } } } } @@ -155,7 +169,7 @@ impl HttpRequestHandler { if let Some(domain) = &client.domain { - let allowed_origin = match conf().force_https { + let allowed_origin = match conf().force_https { true => format!("https://{}", domain), false => format!("http://{}", domain) }; @@ -166,7 +180,7 @@ impl HttpRequestHandler { if !s.to_str()?.starts_with(&allowed_origin) { self.bad_request("Use of this client is prohibited from this domain!".to_string())?; } - }, + } } self.headers.insert("Access-Control-Allow-Origin".to_string(), allowed_origin); @@ -176,4 +190,15 @@ impl HttpRequestHandler { Ok(()) } + + /// Get an email included in the request + pub fn post_email(&mut self, name: &str) -> ResultBoxError { + let mail = self.post_string(name)?; + + if !mailchecker::is_valid(&mail) { + self.bad_request("Invalid email address!".to_string())?; + } + + Ok(mail) + } } \ No newline at end of file