Add communication with user actor

This commit is contained in:
Pierre HUBERT 2022-03-30 12:41:22 +02:00
parent bfe4674f88
commit 6fdac7fbb1
6 changed files with 94 additions and 10 deletions

View File

@ -1,7 +1,21 @@
use actix::{Actor, Context}; use actix::{Actor, Context, Handler, Message, MessageResult};
use crate::data::entity_manager::EntityManager; use crate::data::entity_manager::EntityManager;
use crate::data::user::User; use crate::data::user::{User, verify_password};
#[derive(Debug)]
pub enum LoginResult {
AccountNotFound,
InvalidPassword,
Success(User),
}
#[derive(Message)]
#[rtype(LoginResult)]
pub struct LoginRequest {
pub login: String,
pub password: String,
}
pub struct UsersActor { pub struct UsersActor {
manager: EntityManager<User>, manager: EntityManager<User>,
@ -15,4 +29,21 @@ impl UsersActor {
impl Actor for UsersActor { impl Actor for UsersActor {
type Context = Context<Self>; type Context = Context<Self>;
}
impl Handler<LoginRequest> for UsersActor {
type Result = MessageResult<LoginRequest>;
fn handle(&mut self, msg: LoginRequest, _ctx: &mut Self::Context) -> Self::Result {
match self.manager.find_by_username_or_email(&msg.login) {
None => MessageResult(LoginResult::AccountNotFound),
Some(user) => {
if !verify_password(msg.password, &user.password) {
return MessageResult(LoginResult::InvalidPassword);
}
MessageResult(LoginResult::Success(user))
}
}
}
} }

View File

@ -1,6 +1,9 @@
use actix_web::{HttpResponse, Responder}; use actix::Addr;
use actix_web::{HttpResponse, Responder, web};
use askama::Template; use askama::Template;
use crate::actors::users_actor::{LoginResult, UsersActor};
use crate::actors::users_actor;
use crate::constants::APP_NAME; use crate::constants::APP_NAME;
#[derive(Template)] #[derive(Template)]
@ -16,20 +19,42 @@ struct BaseLoginPage {
#[template(path = "login.html")] #[template(path = "login.html")]
struct LoginTemplate { struct LoginTemplate {
_parent: BaseLoginPage, _parent: BaseLoginPage,
mail: String, login: String,
} }
#[derive(serde::Deserialize)]
pub struct LoginRequest {
login: String,
password: String,
}
/// Authenticate user
pub async fn login_route(users: web::Data<Addr<UsersActor>>,
req: Option<web::Form<LoginRequest>>) -> impl Responder {
let mut danger = String::new();
let mut login = String::new();
// Try to authenticate user
if let Some(req) = &req {
login = req.login.clone();
let response: LoginResult = users.send(users_actor::LoginRequest {
login: login.clone(),
password: req.password.clone(),
}).await.unwrap();
danger = format!("{:?}", response)
}
pub async fn login_route() -> impl Responder {
HttpResponse::Ok() HttpResponse::Ok()
.content_type("text/html") .content_type("text/html")
.body(LoginTemplate { .body(LoginTemplate {
_parent: BaseLoginPage { _parent: BaseLoginPage {
page_title: "Login", page_title: "Login",
danger: "".to_string(), danger,
success: "".to_string(), success: "".to_string(),
app_name: APP_NAME, app_name: APP_NAME,
}, },
mail: "".to_string() login,
}.render().unwrap()) }.render().unwrap())
} }

View File

@ -1,4 +1,5 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::slice::Iter;
use crate::utils::err::Res; use crate::utils::err::Res;
@ -44,4 +45,9 @@ impl<E> EntityManager<E> where E: serde::Serialize + serde::de::DeserializeOwned
self.list.push(el); self.list.push(el);
self.save() self.save()
} }
/// Iterate over the entries of this entity manager
pub fn iter(&self) -> Iter<'_, E> {
self.list.iter()
}
} }

View File

@ -1,3 +1,4 @@
use crate::data::entity_manager::EntityManager;
use crate::data::service::ServiceID; use crate::data::service::ServiceID;
use crate::utils::err::Res; use crate::utils::err::Res;
@ -38,11 +39,32 @@ impl Default for User {
need_reset_password: false, need_reset_password: false,
enabled: true, enabled: true,
admin: false, admin: false,
authorized_services: None authorized_services: None,
} }
} }
} }
pub fn hash_password<P: AsRef<[u8]>>(pwd: P) -> Res<String> { pub fn hash_password<P: AsRef<[u8]>>(pwd: P) -> Res<String> {
Ok(bcrypt::hash(pwd, bcrypt::DEFAULT_COST)?) Ok(bcrypt::hash(pwd, bcrypt::DEFAULT_COST)?)
}
pub fn verify_password<P: AsRef<[u8]>>(pwd: P, hash: &str) -> bool {
match bcrypt::verify(pwd, hash) {
Ok(r) => r,
Err(e) => {
log::warn!("Failed to verify password! {:?}", e);
false
}
}
}
impl EntityManager<User> {
pub fn find_by_username_or_email(&self, u: &str) -> Option<User> {
for entry in self.iter() {
if entry.username.eq(u) || entry.email.eq(u) {
return Some(entry.clone());
}
}
None
}
} }

View File

@ -3,8 +3,8 @@
<form action="/login" method="post"> <form action="/login" method="post">
<div> <div>
<div class="form-floating"> <div class="form-floating">
<input name="mail" type="text" required class="form-control" id="floatingName" placeholder="unsername" <input name="login" type="text" required class="form-control" id="floatingName" placeholder="unsername"
value="{{ mail }}"> value="{{ login }}">
<label for="floatingName">Email address or username</label> <label for="floatingName">Email address or username</label>
</div> </div>