diff --git a/crates/actix-jwt-session/src/redis_adapter.rs b/crates/actix-jwt-session/src/redis_adapter.rs index 4e72e4b..335c572 100644 --- a/crates/actix-jwt-session/src/redis_adapter.rs +++ b/crates/actix-jwt-session/src/redis_adapter.rs @@ -1,11 +1,12 @@ use super::*; -use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse}; +use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}; use futures_util::future::LocalBoxFuture; use jsonwebtoken::{DecodingKey, Validation}; use redis::AsyncCommands; use std::marker::PhantomData; use std::rc::Rc; use std::sync::Arc; +use std::future::{ready, Ready}; #[derive(Clone)] pub struct RedisStorage { @@ -79,6 +80,37 @@ where } } +#[derive(Debug,Clone)] +pub struct RedisMiddlewareFactory { + jwt_decoding_key: Arc, + jwt_validator: Arc, + storage: Arc, + _claims_type_marker: PhantomData, +} + +impl Transform for RedisMiddlewareFactory +where + S: Service, Error = actix_web::Error> + 'static, + ClaimsType: DeserializeOwned + 'static, +{ + type Response = ServiceResponse; + type Error = actix_web::Error; + type Transform = RedisMiddleware; + type InitError = (); + type Future = Ready>; + + fn new_transform(&self, service: S) -> Self::Future { + ready(Ok(RedisMiddleware { + service: Rc::new(service), + storage: self.storage.clone(), + jwt_decoding_key: self.jwt_decoding_key.clone(), + jwt_validator: self.jwt_validator.clone(), + _claims_type_marker: PhantomData, + })) + } +} + + #[cfg(test)] mod tests { use super::*;