BasicOIDC/src/controllers/login_controller.rs

101 lines
2.6 KiB
Rust
Raw Normal View History

2022-03-30 10:41:22 +00:00
use actix::Addr;
2022-03-30 14:58:00 +00:00
use actix_identity::Identity;
2022-03-30 10:41:22 +00:00
use actix_web::{HttpResponse, Responder, web};
2022-03-30 08:29:10 +00:00
use askama::Template;
2022-03-30 10:41:22 +00:00
use crate::actors::users_actor::{LoginResult, UsersActor};
use crate::actors::users_actor;
2022-03-30 08:29:10 +00:00
use crate::constants::APP_NAME;
2022-04-01 16:59:17 +00:00
use crate::controllers::base_controller::redirect_user;
use crate::data::session_identity::SessionIdentity;
2022-03-30 08:29:10 +00:00
2022-03-30 09:00:20 +00:00
#[derive(Template)]
#[template(path = "base_login_page.html")]
struct BaseLoginPage {
danger: String,
success: String,
page_title: &'static str,
app_name: &'static str,
}
2022-03-30 08:29:10 +00:00
#[derive(Template)]
#[template(path = "login.html")]
2022-03-30 09:00:20 +00:00
struct LoginTemplate {
_parent: BaseLoginPage,
2022-03-30 10:41:22 +00:00
login: String,
2022-03-30 08:29:10 +00:00
}
2022-03-30 10:41:22 +00:00
#[derive(serde::Deserialize)]
2022-04-01 17:05:40 +00:00
pub struct LoginRequestBody {
2022-03-30 10:41:22 +00:00
login: String,
password: String,
}
2022-04-01 17:05:40 +00:00
#[derive(serde::Deserialize)]
pub struct LoginRequestQuery {
logout: Option<bool>,
}
2022-03-30 10:41:22 +00:00
/// Authenticate user
pub async fn login_route(users: web::Data<Addr<UsersActor>>,
2022-04-01 17:05:40 +00:00
query: web::Query<LoginRequestQuery>,
req: Option<web::Form<LoginRequestBody>>,
2022-03-30 14:58:00 +00:00
id: Identity) -> impl Responder {
2022-03-30 10:41:22 +00:00
let mut danger = String::new();
2022-04-01 17:05:40 +00:00
let mut success = String::new();
2022-03-30 10:41:22 +00:00
let mut login = String::new();
2022-04-01 17:05:40 +00:00
// Check if user session must be closed
if let Some(true) = query.logout {
id.forget();
success = "Goodbye!".to_string();
}
2022-04-01 16:59:17 +00:00
// Check if user is already authenticated
if SessionIdentity::is_authenticated(&id) {
return redirect_user("/");
}
2022-03-30 10:41:22 +00:00
// Try to authenticate user
if let Some(req) = &req {
2022-04-01 16:59:17 +00:00
// TODO : check request origin
2022-03-30 10:41:22 +00:00
login = req.login.clone();
let response: LoginResult = users.send(users_actor::LoginRequest {
login: login.clone(),
password: req.password.clone(),
}).await.unwrap();
2022-04-01 16:59:17 +00:00
match response {
LoginResult::Success(user) => {
id.remember(SessionIdentity::from_user(&user).serialize());
return redirect_user("/");
}
c => {
// TODO : add bruteforce detection
log::warn!("Failed login for username {} : {:?}", login, c);
danger = "Login failed.".to_string();
}
}
2022-03-30 10:41:22 +00:00
}
2022-03-30 08:29:10 +00:00
2022-03-30 09:00:20 +00:00
HttpResponse::Ok()
.content_type("text/html")
.body(LoginTemplate {
_parent: BaseLoginPage {
page_title: "Login",
2022-03-30 10:41:22 +00:00
danger,
2022-04-01 17:05:40 +00:00
success,
2022-03-30 09:00:20 +00:00
app_name: APP_NAME,
},
2022-03-30 10:41:22 +00:00
login,
2022-03-30 09:00:20 +00:00
}.render().unwrap())
2022-04-01 17:05:40 +00:00
}
/// Sign out user
pub async fn logout_route() -> impl Responder {
redirect_user("/login?logout=true")
2022-03-30 08:29:10 +00:00
}