mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-22 21:39:21 +00:00
Ready to implement password check
This commit is contained in:
parent
975c129f7c
commit
d2035a6a3f
@ -1,10 +1,23 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use crate::data::http_request_handler::HttpRequestHandler;
|
use crate::data::http_request_handler::HttpRequestHandler;
|
||||||
use crate::controllers::routes::RequestResult;
|
use crate::controllers::routes::RequestResult;
|
||||||
|
use crate::helpers::account_helper;
|
||||||
|
|
||||||
/// Account controller
|
/// Account controller
|
||||||
///
|
///
|
||||||
/// @author Pierre Hubert
|
/// @author Pierre Hubert
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct LoginTokens {
|
||||||
|
token1: String,
|
||||||
|
token2: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct LoginSuccess {
|
||||||
|
tokens: LoginTokens
|
||||||
|
}
|
||||||
|
|
||||||
/// Sign in user
|
/// Sign in user
|
||||||
pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
|
pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
|
||||||
let email = request.post_email("userMail")?;
|
let email = request.post_email("userMail")?;
|
||||||
@ -13,7 +26,22 @@ pub fn login_user(request: &mut HttpRequestHandler) -> RequestResult {
|
|||||||
// TODO : limit request
|
// TODO : limit request
|
||||||
|
|
||||||
// Authenticate user
|
// Authenticate user
|
||||||
|
let token = account_helper::login_user(
|
||||||
|
&email, &password, request.api_client());
|
||||||
|
|
||||||
|
match token {
|
||||||
request.success("")
|
Ok(t) => {
|
||||||
|
request.set_response(LoginSuccess {
|
||||||
|
tokens: LoginTokens {
|
||||||
|
token1: t,
|
||||||
|
token2: "dummy_data".to_string()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
// TODO : limit request
|
||||||
|
println!("Error on login: {}", e);
|
||||||
|
request.forbidden("Invalid email address / password!".to_string())
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
@ -35,8 +35,18 @@ impl HttpError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate a 401 error
|
/// Generate a 400 error
|
||||||
pub fn bad_request(message: &str) -> HttpError {
|
pub fn bad_request(message: &str) -> HttpError {
|
||||||
|
HttpError {
|
||||||
|
error: InnerHTTPError {
|
||||||
|
code: 400,
|
||||||
|
message: message.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a 401 error
|
||||||
|
pub fn forbidden(message: &str) -> HttpError {
|
||||||
HttpError {
|
HttpError {
|
||||||
error: InnerHTTPError {
|
error: InnerHTTPError {
|
||||||
code: 401,
|
code: 401,
|
||||||
|
@ -78,6 +78,12 @@ impl HttpRequestHandler {
|
|||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set request response
|
||||||
|
pub fn set_response<T: Serialize>(&mut self, data: T) -> RequestResult {
|
||||||
|
self.response = Some(HttpResponse::Ok().json(data));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Success message
|
/// Success message
|
||||||
pub fn success(&mut self, message: &str) -> RequestResult {
|
pub fn success(&mut self, message: &str) -> RequestResult {
|
||||||
self.response = Some(HttpResponse::Ok().json(SuccessMessage {
|
self.response = Some(HttpResponse::Ok().json(SuccessMessage {
|
||||||
@ -86,20 +92,27 @@ impl HttpRequestHandler {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Internal error message
|
/// Internal error response (500)
|
||||||
pub fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult {
|
pub fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult {
|
||||||
self.response = Some(HttpResponse::InternalServerError().json(
|
self.response = Some(HttpResponse::InternalServerError().json(
|
||||||
HttpError::internal_error("Internal server error.")));
|
HttpError::internal_error("Internal server error.")));
|
||||||
Err(error)
|
Err(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bad request
|
/// Bad request (400)
|
||||||
pub fn bad_request(&mut self, message: String) -> RequestResult {
|
pub fn bad_request(&mut self, message: String) -> RequestResult {
|
||||||
self.response = Some(HttpResponse::BadRequest().json(
|
self.response = Some(HttpResponse::BadRequest().json(
|
||||||
HttpError::bad_request(&message)));
|
HttpError::bad_request(&message)));
|
||||||
Err(Box::new(ExecError::new(&message)))
|
Err(Box::new(ExecError::new(&message)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Forbidden (401)
|
||||||
|
pub fn forbidden(&mut self, message: String) -> RequestResult {
|
||||||
|
self.response = Some(HttpResponse::Forbidden().json(
|
||||||
|
HttpError::forbidden(&message)));
|
||||||
|
Err(Box::new(ExecError::new(&message)))
|
||||||
|
}
|
||||||
|
|
||||||
/// If result is not OK, return a bad request
|
/// If result is not OK, return a bad request
|
||||||
pub fn ok_or_bad_request<E>(&mut self, res: ResultBoxError<E>, msg: &str) -> ResultBoxError<E> {
|
pub fn ok_or_bad_request<E>(&mut self, res: ResultBoxError<E>, msg: &str) -> ResultBoxError<E> {
|
||||||
match res {
|
match res {
|
||||||
@ -117,6 +130,11 @@ impl HttpRequestHandler {
|
|||||||
self.request.path().to_string()
|
self.request.path().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get information about the client which made the request
|
||||||
|
pub fn api_client(&self) -> &APIClient {
|
||||||
|
self.client.as_ref().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if a POST parameter was present in the request or not
|
/// Check if a POST parameter was present in the request or not
|
||||||
pub fn has_post_parameter(&self, name: &str) -> bool {
|
pub fn has_post_parameter(&self, name: &str) -> bool {
|
||||||
self.body.contains_key(name)
|
self.body.contains_key(name)
|
||||||
|
@ -4,3 +4,5 @@ pub mod config;
|
|||||||
pub mod http_error;
|
pub mod http_error;
|
||||||
pub mod http_request_handler;
|
pub mod http_request_handler;
|
||||||
pub mod api_client;
|
pub mod api_client;
|
||||||
|
|
||||||
|
pub mod user;
|
11
src/data/user.rs
Normal file
11
src/data/user.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
/// User information
|
||||||
|
///
|
||||||
|
/// @author Pierre Hubert
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct User {
|
||||||
|
pub id: i64,
|
||||||
|
pub email: String,
|
||||||
|
pub password: String,
|
||||||
|
pub first_name: String,
|
||||||
|
pub last_name: String,
|
||||||
|
}
|
@ -4,3 +4,6 @@
|
|||||||
|
|
||||||
/// API services tokens table
|
/// API services tokens table
|
||||||
pub const SERVICES_TABLES : &str = "comunic_api_services_tokens";
|
pub const SERVICES_TABLES : &str = "comunic_api_services_tokens";
|
||||||
|
|
||||||
|
/// User table
|
||||||
|
pub const USERS_TABLE : &str = "utilisateurs";
|
21
src/helpers/account_helper.rs
Normal file
21
src/helpers/account_helper.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use crate::data::api_client::APIClient;
|
||||||
|
use crate::data::error::ResultBoxError;
|
||||||
|
use crate::helpers::user_helper;
|
||||||
|
|
||||||
|
/// Account helper
|
||||||
|
///
|
||||||
|
/// @author Pierre Hubert
|
||||||
|
|
||||||
|
/// Attempt to sign-in user
|
||||||
|
///
|
||||||
|
/// In this version of the api, we consider that there is only one login token required
|
||||||
|
/// This is why I returns just a simple string, the token created for the user in case of success
|
||||||
|
pub fn login_user(email: &str, password: &str, client: &APIClient) -> ResultBoxError<String> {
|
||||||
|
let user = user_helper::find_user_by_email(email)?;
|
||||||
|
|
||||||
|
// TODO : check user password
|
||||||
|
|
||||||
|
println!("{:#?}", user);
|
||||||
|
|
||||||
|
Ok("d".to_string())
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
pub mod database;
|
pub mod database;
|
||||||
|
|
||||||
pub mod api_helper;
|
pub mod api_helper;
|
||||||
|
pub mod account_helper;
|
||||||
|
pub mod user_helper;
|
27
src/helpers/user_helper.rs
Normal file
27
src/helpers/user_helper.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use crate::data::error::ResultBoxError;
|
||||||
|
use crate::data::user::User;
|
||||||
|
use crate::helpers::database;
|
||||||
|
use crate::database_structure::USERS_TABLE;
|
||||||
|
|
||||||
|
/// User helper
|
||||||
|
///
|
||||||
|
/// @author Pierre Hubert
|
||||||
|
|
||||||
|
/// Get & return information about a user based on his email
|
||||||
|
pub fn find_user_by_email(email: &str) -> ResultBoxError<User> {
|
||||||
|
exec_get_user_query(
|
||||||
|
database::QueryInfo::new(USERS_TABLE).cond("mail", email))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Execute query & return result
|
||||||
|
fn exec_get_user_query(query : database::QueryInfo) -> ResultBoxError<User> {
|
||||||
|
database::query_row(query, |res|{
|
||||||
|
Ok(User {
|
||||||
|
id: res.get_int64("ID")?,
|
||||||
|
email: res.get_str("mail")?,
|
||||||
|
password: res.get_str("password")?,
|
||||||
|
first_name: res.get_str("prenom")?,
|
||||||
|
last_name: res.get_str("nom")?
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user