diff --git a/actix-session/CHANGES.md b/actix-session/CHANGES.md index 1cdc19e25..070b4351a 100644 --- a/actix-session/CHANGES.md +++ b/actix-session/CHANGES.md @@ -6,6 +6,7 @@ - Rename `redis-rs-session` crate feature to `redis-session`. - Rename `redis-rs-tls-session` crate feature to `redis-session-native-tls`. - Remove `redis-actor-session` crate feature (and, therefore, the `actix-redis` based storage backend). +- Expose `storage::generate_session_key()`. ## 0.9.0 diff --git a/actix-session/src/storage/mod.rs b/actix-session/src/storage/mod.rs index 5c6022b17..8c0dd5b11 100644 --- a/actix-session/src/storage/mod.rs +++ b/actix-session/src/storage/mod.rs @@ -18,6 +18,8 @@ mod redis_rs; mod utils; #[cfg(feature = "cookie-session")] -pub use cookie::CookieSessionStore; +pub use self::cookie::CookieSessionStore; #[cfg(feature = "redis-session")] -pub use redis_rs::{RedisSessionStore, RedisSessionStoreBuilder}; +pub use self::redis_rs::{RedisSessionStore, RedisSessionStoreBuilder}; +#[cfg(feature = "redis-session")] +pub use self::utils::generate_session_key; diff --git a/actix-session/src/storage/utils.rs b/actix-session/src/storage/utils.rs index bc9198867..4dcfed7bb 100644 --- a/actix-session/src/storage/utils.rs +++ b/actix-session/src/storage/utils.rs @@ -1,17 +1,13 @@ -use rand::{distributions::Alphanumeric, rngs::OsRng, Rng as _}; +use rand::distributions::{Alphanumeric, DistString as _}; use crate::storage::SessionKey; /// Session key generation routine that follows [OWASP recommendations]. /// /// [OWASP recommendations]: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#session-id-entropy -pub(crate) fn generate_session_key() -> SessionKey { - let value = std::iter::repeat(()) - .map(|()| OsRng.sample(Alphanumeric)) - .take(64) - .collect::>(); - - // These unwraps will never panic because pre-conditions are always verified - // (i.e. length and character set) - String::from_utf8(value).unwrap().try_into().unwrap() +pub fn generate_session_key() -> SessionKey { + Alphanumeric + .sample_string(&mut rand::thread_rng(), 64) + .try_into() + .expect("generated string should be within size range for a session key") }