1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Create new check functions

This commit is contained in:
Pierre HUBERT 2020-05-23 17:09:28 +02:00
parent 2807dcbffa
commit 975c129f7c
4 changed files with 68 additions and 9 deletions

25
Cargo.lock generated
View File

@ -298,6 +298,12 @@ dependencies = [
"nodrop", "nodrop",
] ]
[[package]]
name = "ascii_utils"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71938f30533e4d95a6d17aa530939da3842c2ab6f4f84b9dae68447e4129f74a"
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.31" version = "0.1.31"
@ -505,6 +511,7 @@ dependencies = [
"actix-web", "actix-web",
"encoding_rs", "encoding_rs",
"futures", "futures",
"mailchecker",
"mysql", "mysql",
"percent-encoding", "percent-encoding",
"serde", "serde",
@ -654,6 +661,15 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" 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]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.14" version = "1.0.14"
@ -1068,6 +1084,15 @@ dependencies = [
"linked-hash-map", "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]] [[package]]
name = "match_cfg" name = "match_cfg"
version = "0.1.0" version = "0.1.0"

View File

@ -15,3 +15,4 @@ serde = "1.0.110"
futures = "0.3.5" futures = "0.3.5"
encoding_rs = "0.8.23" encoding_rs = "0.8.23"
percent-encoding = "2.1.0" percent-encoding = "2.1.0"
mailchecker = "3.3.6"

View File

@ -7,5 +7,13 @@ use crate::controllers::routes::RequestResult;
/// Sign in user /// Sign in user
pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult { pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
request.success("Login user") let email = request.post_email("userMail")?;
let password = request.post_string_opt("userPassword", 3, true)?;
// TODO : limit request
// Authenticate user
request.success("")
} }

View File

@ -133,12 +133,26 @@ impl HttpRequestHandler {
/// Get a post string /// Get a post string
pub fn post_string(&mut self, name: &str) -> ResultBoxError<String> { pub fn post_string(&mut self, name: &str) -> ResultBoxError<String> {
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<String> {
let param = self.post_parameter(name)?; let param = self.post_parameter(name)?;
match &param.string { match (&param.string, required) {
Some(s) => Ok(s.to_string()), (None, true) =>
None => { Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err()),
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())
}
} }
} }
} }
@ -166,7 +180,7 @@ impl HttpRequestHandler {
if !s.to_str()?.starts_with(&allowed_origin) { if !s.to_str()?.starts_with(&allowed_origin) {
self.bad_request("Use of this client is prohibited from this domain!".to_string())?; 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); self.headers.insert("Access-Control-Allow-Origin".to_string(), allowed_origin);
@ -176,4 +190,15 @@ impl HttpRequestHandler {
Ok(()) Ok(())
} }
/// Get an email included in the request
pub fn post_email(&mut self, name: &str) -> ResultBoxError<String> {
let mail = self.post_string(name)?;
if !mailchecker::is_valid(&mail) {
self.bad_request("Invalid email address!".to_string())?;
}
Ok(mail)
}
} }