Start to implement auth middleware

This commit is contained in:
Pierre HUBERT 2022-04-02 15:44:09 +02:00
parent ce220c52f7
commit 3fdb775308
7 changed files with 84 additions and 6 deletions

14
Cargo.lock generated
View File

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

View File

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

View File

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

View File

@ -3,3 +3,4 @@ pub mod utils;
pub mod constants;
pub mod controllers;
pub mod actors;
pub mod middlewares;

View File

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

View File

@ -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<S, B> Transform<S, ServiceRequest> for AuthMiddleware
where
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Transform = SayHiMiddleware<S>;
type InitError = ();
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ready(Ok(SayHiMiddleware { service }))
}
}
pub struct SayHiMiddleware<S> {
service: S,
}
impl<S, B> Service<ServiceRequest> for SayHiMiddleware<S>
where
S: Service<ServiceRequest, Response=ServiceResponse<B>, Error=Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
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 })
}
}

1
src/middlewares/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod auth_middleware;