From f03f126d4f58b380a640d26c99d0a09b35064e31 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 25 May 2024 03:58:03 +0100 Subject: [PATCH] chore: use redis-rs in session features --- Cargo.lock | 51 +--------------------------------- auth/redis-session/Cargo.toml | 2 +- auth/redis-session/src/main.rs | 24 ++++++---------- 3 files changed, 11 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b453ff0..9e60d71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -292,27 +292,6 @@ dependencies = [ "prost", ] -[[package]] -name = "actix-redis" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38446dc11c743f4f0023b1067c7cfb8f2548f24418e31a193b324e35fa059279" -dependencies = [ - "actix", - "actix-rt", - "actix-service", - "actix-tls", - "actix-web", - "backoff", - "derive_more", - "futures-core", - "log", - "redis-async", - "time", - "tokio 1.37.0", - "tokio-util 0.7.10", -] - [[package]] name = "actix-router" version = "0.5.3" @@ -373,15 +352,13 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b671404ec72194d8af58c2bdaf51e3c477a0595056bd5010148405870dda8df2" dependencies = [ - "actix", - "actix-redis", "actix-service", "actix-utils", "actix-web", "anyhow", "derive_more", - "futures-core", "rand 0.8.5", + "redis", "serde", "serde_json", "tracing", @@ -1586,17 +1563,6 @@ dependencies = [ "tokio-util 0.7.10", ] -[[package]] -name = "backoff" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ddb9cb1ec0a098ad4bbf9344d0713fa193ae1a80af55febcff2627b6a00c1" -dependencies = [ - "getrandom 0.2.12", - "instant", - "rand 0.8.5", -] - [[package]] name = "backtrace" version = "0.3.69" @@ -6031,21 +5997,6 @@ dependencies = [ "url", ] -[[package]] -name = "redis-async" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2777130e406c74c28b6cddc0194fcdc2553b5a8795eef9f6384bd3b70a07ba3f" -dependencies = [ - "bytes 1.6.0", - "futures-channel", - "futures-sink", - "futures-util", - "log", - "tokio 1.37.0", - "tokio-util 0.7.10", -] - [[package]] name = "redis-session-example" version = "1.0.0" diff --git a/auth/redis-session/Cargo.toml b/auth/redis-session/Cargo.toml index 74c6ae6..507952e 100644 --- a/auth/redis-session/Cargo.toml +++ b/auth/redis-session/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [dependencies] actix-web.workspace = true -actix-session = { workspace = true, features = ["redis-actor-session"] } +actix-session = { workspace = true, features = ["redis-rs-session"] } env_logger.workspace = true log.workspace = true diff --git a/auth/redis-session/src/main.rs b/auth/redis-session/src/main.rs index 923048a..7d2e117 100644 --- a/auth/redis-session/src/main.rs +++ b/auth/redis-session/src/main.rs @@ -3,8 +3,8 @@ //! Every request gets a session, corresponding to a cache entry and cookie. //! At login, the session key changes and session state in cache re-assigns. //! At logout, session state in cache is removed and cookie is invalidated. -//! -use actix_session::{storage::RedisActorSessionStore, Session, SessionMiddleware}; + +use actix_session::{storage::RedisSessionStore, Session, SessionMiddleware}; use actix_web::{ middleware, web, web::{get, post, resource}, @@ -81,16 +81,12 @@ async fn main() -> std::io::Result<()> { // authentication cookies for any user! let private_key = actix_web::cookie::Key::generate(); + let store = RedisSessionStore::new("127.0.0.1:6379").await.unwrap(); + HttpServer::new(move || { App::new() // redis session middleware - .wrap( - SessionMiddleware::builder( - RedisActorSessionStore::new("127.0.0.1:6379"), - private_key.clone(), - ) - .build(), - ) + .wrap(SessionMiddleware::builder(store.clone(), private_key.clone()).build()) // enable logger - always register Actix Web Logger middleware last .wrap(middleware::Logger::default()) .service(resource("/").route(get().to(index))) @@ -112,15 +108,13 @@ mod test { #[actix_web::test] async fn test_workflow() { let private_key = actix_web::cookie::Key::generate(); + let store = RedisSessionStore::new("127.0.0.1:6379").await.unwrap(); let srv = actix_test::start(move || { App::new() .wrap( - SessionMiddleware::builder( - RedisActorSessionStore::new("127.0.0.1:6379"), - private_key.clone(), - ) - .cookie_name("test-session".to_owned()) - .build(), + SessionMiddleware::builder(store.clone(), private_key.clone()) + .cookie_name("test-session".to_owned()) + .build(), ) .wrap(middleware::Logger::default()) .service(resource("/").route(get().to(index)))