/* * Copyright (C) 2021 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}; pub const AUTH: &str = "/login"; //crate::PAGES.auth.login; 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, AUTH)) .finish(), ))) } } }