From 6d6b494c6fda4477d502876609b4a051d6a1378c Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 7 May 2022 12:30:00 +0530 Subject: [PATCH] feat: rm middleware mod and migrate codebase to use actix_auth_middleware --- src/main.rs | 2 -- src/middleware/auth.rs | 81 ------------------------------------------ src/middleware/mod.rs | 18 ---------- 3 files changed, 101 deletions(-) delete mode 100644 src/middleware/auth.rs delete mode 100644 src/middleware/mod.rs diff --git a/src/main.rs b/src/main.rs index b8b06725..67fcea0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,6 @@ mod demo; mod docs; mod email; mod errors; -mod middleware; #[macro_use] mod pages; #[macro_use] @@ -55,7 +54,6 @@ use static_assets::FileMap; pub use widget::WIDGET_ROUTES; use crate::demo::DemoUser; -pub use crate::middleware::auth::CheckLogin; lazy_static! { pub static ref SETTINGS: Settings = Settings::new().unwrap(); diff --git a/src/middleware/auth.rs b/src/middleware/auth.rs deleted file mode 100644 index 2aa3ff8b..00000000 --- a/src/middleware/auth.rs +++ /dev/null @@ -1,81 +0,0 @@ -/* -* Copyright (C) 2022 Aravinth Manivannan -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Affero General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with this program. If not, see . -*/ -#![allow(clippy::type_complexity)] - -use actix_http::body::AnyBody; -use actix_identity::Identity; -use actix_service::{Service, Transform}; -use actix_web::dev::{ServiceRequest, ServiceResponse}; -use actix_web::{http, Error, FromRequest, HttpResponse}; - -use futures::future::{ok, Either, Ready}; - -use crate::PAGES; - -pub struct CheckLogin; - -impl Transform for CheckLogin -where - S: Service, Error = Error>, - S::Future: 'static, -{ - type Response = ServiceResponse; - type Error = Error; - type Transform = CheckLoginMiddleware; - type InitError = (); - type Future = Ready>; - - fn new_transform(&self, service: S) -> Self::Future { - ok(CheckLoginMiddleware { service }) - } -} -pub struct CheckLoginMiddleware { - service: S, -} - -impl Service for CheckLoginMiddleware -where - S: Service, Error = Error>, - S::Future: 'static, -{ - type Response = ServiceResponse; - type Error = Error; - type Future = Either>>; - - actix_service::forward_ready!(service); - - fn call(&self, req: ServiceRequest) -> Self::Future { - let (r, mut pl) = req.into_parts(); - - // TODO investigate when the bellow statement will - // return error - if let Ok(Some(_)) = Identity::from_request(&r, &mut pl) - .into_inner() - .map(|x| x.identity()) - { - let req = ServiceRequest::from_parts(r, pl); - Either::Left(self.service.call(req)) - } else { - let req = ServiceRequest::from_parts(r, pl); //.ok().unwrap(); - Either::Right(ok(req.into_response( - HttpResponse::Found() - .insert_header((http::header::LOCATION, PAGES.auth.login)) - .finish(), - ))) - } - } -} diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs deleted file mode 100644 index 9a54b1cd..00000000 --- a/src/middleware/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -/* -* Copyright (C) 2022 Aravinth Manivannan -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Affero General Public License as -* published by the Free Software Foundation, either version 3 of the -* License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Affero General Public License for more details. -* -* You should have received a copy of the GNU Affero General Public License -* along with this program. If not, see . -*/ - -pub mod auth;