Can perform local authentication
This commit is contained in:
31
virtweb_backend/src/controllers/auth_controller.rs
Normal file
31
virtweb_backend/src/controllers/auth_controller.rs
Normal file
@ -0,0 +1,31 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::extractors::auth_extractor::AuthChecker;
|
||||
use crate::extractors::local_auth_extractor::LocalAuthEnabled;
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct LocalAuthReq {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
/// Perform local authentication
|
||||
pub async fn local_auth(
|
||||
local_auth_enabled: LocalAuthEnabled,
|
||||
req: web::Json<LocalAuthReq>,
|
||||
auth: AuthChecker,
|
||||
) -> impl Responder {
|
||||
if !*local_auth_enabled {
|
||||
log::error!("Local auth attempt while this authentication method is disabled!");
|
||||
return HttpResponse::UnprocessableEntity().json("Local authentication is disabled!");
|
||||
}
|
||||
|
||||
if !AppConfig::get().check_local_login(&req.username, &req.password) {
|
||||
log::error!("Local auth attempt with invalid credentials!");
|
||||
return HttpResponse::Unauthorized().json("Invalid credentials!");
|
||||
}
|
||||
|
||||
auth.authenticate(&req.username);
|
||||
|
||||
HttpResponse::Accepted().json("Welcome")
|
||||
}
|
@ -1 +1,2 @@
|
||||
pub mod auth_controller;
|
||||
pub mod server_controller;
|
||||
|
Reference in New Issue
Block a user