1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 08:25:16 +00:00

Create new check functions

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

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)
}
}