Get identity from middleware

This commit is contained in:
Pierre HUBERT 2022-04-02 17:03:51 +02:00
parent ad58d2de7e
commit cb4daa1112
3 changed files with 26 additions and 6 deletions

View File

@ -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;
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";

View File

@ -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)

View File

@ -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<S, B> Transform<S, ServiceRequest> for AuthMiddleware
}
}
#[derive(Debug)]
enum SessionStatus {
SignedOut,
RegularUser,
Admin
}
pub struct AuthInnerMiddleware<S> {
service: Rc<S>,
}
@ -45,11 +57,13 @@ impl<S, B> Service<ServiceRequest> for AuthInnerMiddleware<S>
{
type Response = ServiceResponse<EitherBody<B>>;
type Error = Error;
#[allow(clippy::type_complexity)]
type Future = Pin<Box<dyn Future<Output=Result<Self::Response, Self::Error>>>>;
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<S, B> Service<ServiceRequest> for AuthInnerMiddleware<S>
));
}
let identity = req.get_identity();
println!("identity: {:?}", identity);
service
.call(req)
.await