diff --git a/src/constants.rs b/src/constants.rs index db06e90..69ae281 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -18,4 +18,7 @@ pub const MIN_ACTIVITY_RECORD_TIME: u64 = 10; pub const MIN_PASS_LEN: usize = 4; /// Maximum session duration (6 hours) -pub const MAX_SESSION_DURATION: u64 = 3600 * 6; \ No newline at end of file +pub const MAX_SESSION_DURATION: u64 = 3600 * 6; + +/// The name of the cookie used to store session information +pub const SESSION_COOKIE_NAME: &str = "auth-cookie"; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c9e348d..260f123 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ 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::constants::{DEFAULT_ADMIN_PASSWORD, DEFAULT_ADMIN_USERNAME, SESSION_COOKIE_NAME}; 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; @@ -63,7 +63,7 @@ async fn main() -> std::io::Result<()> { HttpServer::new(move || { let policy = CookieIdentityPolicy::new(config.token_key.as_bytes()) - .name("auth-cookie") + .name(SESSION_COOKIE_NAME) .secure(config.secure_auth_cookie); @@ -71,8 +71,8 @@ async fn main() -> std::io::Result<()> { .app_data(web::Data::new(users_actor.clone())) .wrap(Logger::default()) - .wrap(IdentityService::new(policy)) .wrap(AuthMiddleware {}) + .wrap(IdentityService::new(policy)) // /health route .service(health) diff --git a/src/middlewares/auth_middleware.rs b/src/middlewares/auth_middleware.rs index fcfa623..a1a2592 100644 --- a/src/middlewares/auth_middleware.rs +++ b/src/middlewares/auth_middleware.rs @@ -3,9 +3,14 @@ use std::future::{Future, ready, Ready}; use std::pin::Pin; use std::rc::Rc; +use actix_identity::RequestIdentity; -use actix_web::{dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, Error, HttpResponse}; +use actix_web::{dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, Error, FromRequest, HttpMessage, HttpResponse}; use actix_web::body::EitherBody; +use actix_web::cookie::{Cookie, CookieJar}; +use actix_web::http::header::http_percent_encode; + +use crate::constants::SESSION_COOKIE_NAME; // There are two steps in middleware processing. // 1. Middleware initialization, middleware factory gets called with @@ -33,6 +38,13 @@ impl Transform for AuthMiddleware } } +#[derive(Debug)] +enum SessionStatus { + SignedOut, + RegularUser, + Admin +} + pub struct AuthInnerMiddleware { service: Rc, } @@ -45,11 +57,13 @@ impl Service for AuthInnerMiddleware { type Response = ServiceResponse>; type Error = Error; + + #[allow(clippy::type_complexity)] type Future = Pin>>>; forward_ready!(service); - fn call(&self, req: ServiceRequest) -> Self::Future { + fn call(&self, mut req: ServiceRequest) -> Self::Future { println!("Hi from start. You requested: {}", req.path()); let service = Rc::clone(&self.service); @@ -64,6 +78,9 @@ impl Service for AuthInnerMiddleware )); } + let identity = req.get_identity(); + println!("identity: {:?}", identity); + service .call(req) .await