1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +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",
]
[[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"

View File

@ -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"
percent-encoding = "2.1.0"
mailchecker = "3.3.6"

View File

@ -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("")
}

View File

@ -133,12 +133,26 @@ impl HttpRequestHandler {
/// Get a post 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)?;
match &param.string {
Some(s) => Ok(s.to_string()),
None => {
Err(self.bad_request(format!("'{}' is not a string!", name)).unwrap_err())
match (&param.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<String> {
let mail = self.post_string(name)?;
if !mailchecker::is_valid(&mail) {
self.bad_request("Invalid email address!".to_string())?;
}
Ok(mail)
}
}