From 3fdb775308d71e3163e7c7d8b2e3e9526b54233d Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 2 Apr 2022 15:44:09 +0200 Subject: [PATCH] Start to implement auth middleware --- Cargo.lock | 14 +++++++ Cargo.toml | 1 + src/data/session_identity.rs | 2 - src/lib.rs | 3 +- src/main.rs | 8 ++-- src/middlewares/auth_middleware.rs | 61 ++++++++++++++++++++++++++++++ src/middlewares/mod.rs | 1 + 7 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/middlewares/auth_middleware.rs create mode 100644 src/middlewares/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 6ef80d8..9ed6598 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,6 +389,7 @@ dependencies = [ "bcrypt", "clap", "env_logger", + "futures-util", "include_dir", "log", "mime_guess", @@ -707,6 +708,17 @@ version = "0.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" +[[package]] +name = "futures-macro" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "futures-sink" version = "0.3.21" @@ -726,9 +738,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" dependencies = [ "futures-core", + "futures-macro", "futures-task", "pin-project-lite", "pin-utils", + "slab", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index dd99b6d..a80712c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ bcrypt = "0.12.1" uuid = { version = "0.8.2", features = ["v4"] } mime_guess = "2.0.4" askama = "0.11.1" +futures-util = "0.3.21" \ No newline at end of file diff --git a/src/data/session_identity.rs b/src/data/session_identity.rs index be9d86e..13bb8d1 100644 --- a/src/data/session_identity.rs +++ b/src/data/session_identity.rs @@ -27,8 +27,6 @@ struct SessionIdentityData { login_time: u64, last_access: u64, pub status: SessionStatus, - - // TODO : add session max duration (1 day) } pub struct SessionIdentity<'a>(pub &'a Identity); diff --git a/src/lib.rs b/src/lib.rs index 41122ae..955e194 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,5 @@ pub mod data; pub mod utils; pub mod constants; pub mod controllers; -pub mod actors; \ No newline at end of file +pub mod actors; +pub mod middlewares; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1ae47b4..c9e348d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,17 @@ +use actix::Actor; +use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_web::{App, get, HttpServer, web}; use actix_web::middleware::Logger; use clap::Parser; +use basic_oidc::actors::users_actor::UsersActor; use basic_oidc::constants::{DEFAULT_ADMIN_PASSWORD, DEFAULT_ADMIN_USERNAME}; use basic_oidc::controllers::assets_controller::assets_route; use basic_oidc::controllers::login_controller::{login_route, logout_route}; use basic_oidc::data::app_config::AppConfig; use basic_oidc::data::entity_manager::EntityManager; use basic_oidc::data::user::{hash_password, User}; -use basic_oidc::actors::users_actor::UsersActor; -use actix::Actor; -use actix_identity::{IdentityService, CookieIdentityPolicy}; +use basic_oidc::middlewares::auth_middleware::AuthMiddleware; #[get("/health")] async fn health() -> &'static str { @@ -71,6 +72,7 @@ async fn main() -> std::io::Result<()> { .wrap(Logger::default()) .wrap(IdentityService::new(policy)) + .wrap(AuthMiddleware {}) // /health route .service(health) diff --git a/src/middlewares/auth_middleware.rs b/src/middlewares/auth_middleware.rs new file mode 100644 index 0000000..08c42f2 --- /dev/null +++ b/src/middlewares/auth_middleware.rs @@ -0,0 +1,61 @@ +//! # Authentication middleware + +use std::future::{ready, Ready}; + +use actix_web::{ + dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, + Error, +}; +use futures_util::future::LocalBoxFuture; + +// There are two steps in middleware processing. +// 1. Middleware initialization, middleware factory gets called with +// next service in chain as parameter. +// 2. Middleware's call method gets called with normal request. +pub struct AuthMiddleware; + +// Middleware factory is `Transform` trait +// `S` - type of the next service +// `B` - type of response's body +impl Transform for AuthMiddleware + where + S: Service, Error=Error>, + S::Future: 'static, + B: 'static, +{ + type Response = ServiceResponse; + type Error = Error; + type Transform = SayHiMiddleware; + type InitError = (); + type Future = Ready>; + + fn new_transform(&self, service: S) -> Self::Future { + ready(Ok(SayHiMiddleware { service })) + } +} + +pub struct SayHiMiddleware { + service: S, +} + +impl Service for SayHiMiddleware + where + S: Service, Error=Error>, + S::Future: 'static, + B: 'static, +{ + type Response = ServiceResponse; + type Error = Error; + type Future = LocalBoxFuture<'static, Result>; + + forward_ready!(service); + + fn call(&self, req: ServiceRequest) -> Self::Future { + println!("Hi from start. You requested: {}", req.path()); + + let fut = self.service.call(req); + + // Forward request + Box::pin(async move { fut.await }) + } +} diff --git a/src/middlewares/mod.rs b/src/middlewares/mod.rs new file mode 100644 index 0000000..e12d527 --- /dev/null +++ b/src/middlewares/mod.rs @@ -0,0 +1 @@ +pub mod auth_middleware; \ No newline at end of file