Add authentication layer
This commit is contained in:
central_backend
central_frontend
52
central_backend/src/server/web_api/auth_controller.rs
Normal file
52
central_backend/src/server/web_api/auth_controller.rs
Normal file
@ -0,0 +1,52 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::server::custom_error::HttpResult;
|
||||
use actix_identity::Identity;
|
||||
use actix_remote_ip::RemoteIP;
|
||||
use actix_web::{web, HttpMessage, HttpRequest, HttpResponse};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct AuthRequest {
|
||||
user: String,
|
||||
password: String,
|
||||
}
|
||||
|
||||
/// Perform password authentication
|
||||
pub async fn password_auth(
|
||||
r: web::Json<AuthRequest>,
|
||||
request: HttpRequest,
|
||||
remote_ip: RemoteIP,
|
||||
) -> HttpResult {
|
||||
if r.user != AppConfig::get().admin_username || r.password != AppConfig::get().admin_password {
|
||||
log::error!("Failed login attempt from {}!", remote_ip.0.to_string());
|
||||
return Ok(HttpResponse::Unauthorized().json("Invalid credentials!"));
|
||||
}
|
||||
|
||||
log::info!("Successful login attempt from {}!", remote_ip.0.to_string());
|
||||
Identity::login(&request.extensions(), r.user.to_string())?;
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct AuthInfo {
|
||||
id: String,
|
||||
}
|
||||
|
||||
/// Get current user information
|
||||
pub async fn auth_info(id: Option<Identity>) -> HttpResult {
|
||||
if AppConfig::get().unsecure_disable_login {
|
||||
return Ok(HttpResponse::Ok().json(AuthInfo {
|
||||
id: "auto login".to_string(),
|
||||
}));
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(AuthInfo {
|
||||
id: id.unwrap().id()?,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Sign out user
|
||||
pub async fn sign_out(id: Identity) -> HttpResult {
|
||||
id.logout();
|
||||
|
||||
Ok(HttpResponse::NoContent().finish())
|
||||
}
|
23
central_backend/src/server/web_api/energy_controller.rs
Normal file
23
central_backend/src/server/web_api/energy_controller.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use crate::energy::{consumption, energy_actor};
|
||||
use crate::server::custom_error::HttpResult;
|
||||
use crate::server::WebEnergyActor;
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct Consumption {
|
||||
consumption: i32,
|
||||
}
|
||||
|
||||
/// Get current energy consumption
|
||||
pub async fn curr_consumption() -> HttpResult {
|
||||
let consumption = consumption::get_curr_consumption().await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(Consumption { consumption }))
|
||||
}
|
||||
|
||||
/// Get cached energy consumption
|
||||
pub async fn cached_consumption(energy_actor: WebEnergyActor) -> HttpResult {
|
||||
let consumption = energy_actor.send(energy_actor::GetCurrConsumption).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(Consumption { consumption }))
|
||||
}
|
3
central_backend/src/server/web_api/mod.rs
Normal file
3
central_backend/src/server/web_api/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod auth_controller;
|
||||
pub mod energy_controller;
|
||||
pub mod server_controller;
|
25
central_backend/src/server/web_api/server_controller.rs
Normal file
25
central_backend/src/server/web_api/server_controller.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
pub async fn secure_home() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/plain")
|
||||
.body("SolarEnergy secure central backend")
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct ServerConfig {
|
||||
auth_disabled: bool,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
auth_disabled: AppConfig::get().unsecure_disable_login,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn config() -> HttpResponse {
|
||||
HttpResponse::Ok().json(ServerConfig::default())
|
||||
}
|
Reference in New Issue
Block a user