From bb31954b22d6e94ea0bb7677bb5c9eb7eb7005f6 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 2 Sep 2023 10:28:08 +0200 Subject: [PATCH] Add basic server config --- virtweb_backend/Cargo.lock | 1 + virtweb_backend/Cargo.toml | 3 +- .../src/controllers/server_controller.rs | 15 +++++++++ virtweb_backend/src/extractors.rs | 1 + .../src/extractors/local_auth_extractor.rs | 33 +++++++++++++++++++ virtweb_backend/src/lib.rs | 1 + virtweb_backend/src/main.rs | 4 +++ 7 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 virtweb_backend/src/extractors.rs create mode 100644 virtweb_backend/src/extractors/local_auth_extractor.rs diff --git a/virtweb_backend/Cargo.lock b/virtweb_backend/Cargo.lock index ba6f0e3..ca53923 100644 --- a/virtweb_backend/Cargo.lock +++ b/virtweb_backend/Cargo.lock @@ -1856,6 +1856,7 @@ dependencies = [ "actix-web", "clap", "env_logger", + "futures-util", "lazy_static", "light-openid", "log", diff --git a/virtweb_backend/Cargo.toml b/virtweb_backend/Cargo.toml index 7f7cf77..fc0d71d 100644 --- a/virtweb_backend/Cargo.toml +++ b/virtweb_backend/Cargo.toml @@ -15,4 +15,5 @@ actix-web = "4" actix-remote-ip = "0.1.0" actix-session = { version = "0.7.2", features = ["cookie-session"] } actix-identity = "0.5.2" -serde = { version = "1.0.175", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0.175", features = ["derive"] } +futures-util = "0.3.28" \ No newline at end of file diff --git a/virtweb_backend/src/controllers/server_controller.rs b/virtweb_backend/src/controllers/server_controller.rs index 8f565ea..ea2aad8 100644 --- a/virtweb_backend/src/controllers/server_controller.rs +++ b/virtweb_backend/src/controllers/server_controller.rs @@ -1,5 +1,20 @@ +use crate::app_config::AppConfig; +use crate::extractors::local_auth_extractor::LocalAuthEnabled; use actix_web::{HttpResponse, Responder}; pub async fn root_index() -> impl Responder { HttpResponse::Ok().body("Hello world!") } + +#[derive(serde::Serialize)] +struct StaticConfig { + local_auth_enabled: bool, + oidc_auth_enabled: bool, +} + +pub async fn static_config(local_auth: LocalAuthEnabled) -> impl Responder { + HttpResponse::Ok().json(StaticConfig { + local_auth_enabled: *local_auth, + oidc_auth_enabled: !AppConfig::get().disable_oidc, + }) +} diff --git a/virtweb_backend/src/extractors.rs b/virtweb_backend/src/extractors.rs new file mode 100644 index 0000000..c472199 --- /dev/null +++ b/virtweb_backend/src/extractors.rs @@ -0,0 +1 @@ +pub mod local_auth_extractor; diff --git a/virtweb_backend/src/extractors/local_auth_extractor.rs b/virtweb_backend/src/extractors/local_auth_extractor.rs new file mode 100644 index 0000000..cc3fbbb --- /dev/null +++ b/virtweb_backend/src/extractors/local_auth_extractor.rs @@ -0,0 +1,33 @@ +use crate::app_config::AppConfig; +use actix_web::dev::Payload; +use actix_web::{Error, FromRequest, HttpRequest}; +use futures_util::future::{ready, Ready}; +use std::ops::Deref; + +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct LocalAuthEnabled(bool); + +impl Deref for LocalAuthEnabled { + type Target = bool; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl FromRequest for LocalAuthEnabled { + type Error = Error; + type Future = Ready>; + + fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future { + if AppConfig::get().disable_local_auth { + return ready(Ok(Self(false))); + } + + let has_disable_local_auth_header = req + .headers() + .get(&AppConfig::get().disable_auth_header_token) + .is_some(); + return ready(Ok(Self(!has_disable_local_auth_header))); + } +} diff --git a/virtweb_backend/src/lib.rs b/virtweb_backend/src/lib.rs index 11c3b98..fd23042 100644 --- a/virtweb_backend/src/lib.rs +++ b/virtweb_backend/src/lib.rs @@ -1,3 +1,4 @@ pub mod app_config; pub mod constants; pub mod controllers; +pub mod extractors; diff --git a/virtweb_backend/src/main.rs b/virtweb_backend/src/main.rs index 4c2df23..12c9378 100644 --- a/virtweb_backend/src/main.rs +++ b/virtweb_backend/src/main.rs @@ -43,6 +43,10 @@ async fn main() -> std::io::Result<()> { proxy: AppConfig::get().proxy_ip.clone(), })) .route("/", web::get().to(server_controller::root_index)) + .route( + "/api/server/static_config", + web::get().to(server_controller::static_config), + ) }) .bind(&AppConfig::get().listen_address)? .run()