From 0a94dcf7767d31f0d7d6a2b7ab32cca22ab7ed13 Mon Sep 17 00:00:00 2001
From: Pierre Jarriges <pierre.jarriges@tutanota.com>
Date: Fri, 9 Sep 2022 17:06:19 +0200
Subject: [PATCH] wip impl auth middleware

---
 src/app/args.rs                  |  6 ++++
 src/app/config.rs                |  4 +++
 src/main.rs                      |  1 +
 src/middleware/authentication.rs | 52 ++++++++++++++++++++++++++++++++
 src/middleware/mod.rs            |  2 ++
 5 files changed, 65 insertions(+)
 create mode 100644 src/middleware/authentication.rs
 create mode 100644 src/middleware/mod.rs

diff --git a/src/app/args.rs b/src/app/args.rs
index 5deb639..8bd54ee 100644
--- a/src/app/args.rs
+++ b/src/app/args.rs
@@ -23,4 +23,10 @@ pub struct AppArgs {
 
     #[structopt(long = "certs_dir", default_value = "/etc/letsencrypt/live")]
     pub ssl_certs_dir: PathBuf,
+
+    #[structopt(short = "u", long = "username", default_value = "admin")]
+    pub admin_id: String,
+
+    #[structopt(short = "p", long = "password", default_value = "password")]
+    pub admin_pwd: String,
 }
diff --git a/src/app/config.rs b/src/app/config.rs
index 97e519b..f4c026d 100644
--- a/src/app/config.rs
+++ b/src/app/config.rs
@@ -18,6 +18,8 @@ pub struct AppConfig {
     pub port_tls: u16,
     pub load: Option<PathBuf>,
     pub ssl_certs_dir: PathBuf,
+    pub admin_id: String,
+    pub admin_pwd: String,
 }
 
 impl AppConfig {
@@ -57,6 +59,8 @@ impl AppConfig {
             port_tls: app_args.port_tls,
             load: app_args.load,
             ssl_certs_dir,
+            admin_id: app_args.admin_id,
+            admin_pwd: app_args.admin_pwd,
         }
     }
     pub fn get_log_level(&self) -> String {
diff --git a/src/main.rs b/src/main.rs
index ab89dc2..b1c536e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,5 @@
 mod app;
+mod middleware;
 mod service;
 mod static_files;
 mod testing;
diff --git a/src/middleware/authentication.rs b/src/middleware/authentication.rs
new file mode 100644
index 0000000..b5516b3
--- /dev/null
+++ b/src/middleware/authentication.rs
@@ -0,0 +1,52 @@
+use actix_web::{
+    dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
+    Error,
+};
+use futures::prelude::future::LocalBoxFuture;
+use std::future::{ready, Ready};
+
+pub struct AuthData;
+
+impl<S, B> Transform<S, ServiceRequest> for AuthData
+where
+    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
+    S::Future: 'static,
+    B: 'static,
+{
+    type Response = ServiceResponse<B>;
+    type Error = Error;
+    type InitError = ();
+    type Transform = AuthenticatedMiddleware<S>;
+    type Future = Ready<Result<Self::Transform, Self::InitError>>;
+
+    fn new_transform(&self, service: S) -> Self::Future {
+        ready(Ok(AuthenticatedMiddleware { service }))
+    }
+}
+
+pub struct AuthenticatedMiddleware<S> {
+    service: S,
+}
+
+impl<S, B> Service<ServiceRequest> for AuthenticatedMiddleware<S>
+where
+    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
+    S::Future: 'static,
+    B: 'static,
+{
+    type Response = ServiceResponse<B>;
+    type Error = Error;
+    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
+
+    forward_ready!(service);
+
+    fn call(&self, req: ServiceRequest) -> Self::Future {
+        println!("AUTH MW");
+        let fut = self.service.call(req);
+        Box::pin(async move {
+            let res = fut.await?;
+            println!("RESP");
+            Ok(res)
+        })
+    }
+}
diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs
new file mode 100644
index 0000000..dcdaf33
--- /dev/null
+++ b/src/middleware/mod.rs
@@ -0,0 +1,2 @@
+mod authentication;
+pub use authentication::*;
-- 
GitLab