Skip to content

Commit

Permalink
Merge pull request #94 from soundslocke/fix-typos
Browse files Browse the repository at this point in the history
Fix typos in code and comments
  • Loading branch information
genusistimelord authored Jan 7, 2025
2 parents 6624dc4 + f01c48e commit 5237393
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 75 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ Axum Session
## 📑 Overview

<p align="center">
`axum_session` provide's a Session management middleware that stores all session data within a MemoryStore internally.
`axum_session` provide's a Session management middleware that stores all session data within a MemoryStore internally.
Optionally it can save data to a persistent database for long term storage.
Uses Cookie or Header stored UUID's to sync back to the session store.
</p>

- Cookies or Header Store of Generated Session UUID and a Store Boolean.
- Uses a DatabasePool Trait so you can implement your own Sub Storage Layer.
- Convenient API for `Session` no need to mark as Read or Write making Usage Easier.
- Convenient API for `Session` no need to mark as Read or Write making Usage Easier.
- Uses `dashmap` for internal memory lookup and storage to achieve high throughput.
- Uses Serdes for Data Serialization so it can store any Serdes supported type's into the Sessions data.
- Uses Serde for Data Serialization so it can store any Serde supported types into the Sessions data.
- Supports Redis, SurrealDB, MongoDB and SQLx optional Databases out of the Box.
- Supports Memory Only usage. No need to use a persistant database.
- Supports Memory Only usage. No need to use a persistent database.
- Supports Cookie and Header Signing for integrity, and authenticity.
- Supports Database Session Data Encryption for confidentiality, integrity.
- Supports SessionID renewal for enhanced Security.
Expand All @@ -36,7 +36,7 @@ If you need help with this library or have suggestions please go to our [Discord

## 📦 Install

Axum Session uses [`tokio`].
Axum Session uses [`tokio`].
to your cargo include for Axum Session.

[`tokio`]: https://github.com/tokio-rs/tokio
Expand All @@ -50,23 +50,23 @@ axum_session = { version = "0.15.0" }

## 📱 Cargo Feature Flags

| Features | Description |
| ----------------------------- | ---------------------------------------------------------------------------------------------- |
| `advanced` | Enable functions allowing more direct control over the sessions. |
| `rest_mode` | Disables Cookie Handlering In place of Header only usage for Rest API Requests and Responses. |
| `key-store` | Enabled the optional key storage. Will increase ram usage based on Fastbloom settings. |
| Features | Description |
| ----------------------------- | -------------------------------------------------------------------------------------------------- |
| `advanced` | Enables functions that provide more control over sessions. |
| `rest_mode` | Disables cookie handling and instead only uses a header. For rest API requests and responses. |
| `key-store` | Enables optional in-process key storage. This increases RAM usage depending on Fastbloom settings. |


| Database Crate | Persistent | Description |
| ----------------------------------------------------------------------------------- | ---------- | ----------------------------------------------------------- |
| [`axum_session_sqlx`](https://crates.io/crates/axum_session_sqlx) | Yes | Sqlx session store |
| [`axum_session_surreal`](https://crates.io/crates/axum_session_surreal) | Yes | Surreal session store |
| [`axum_session_mongo`](https://crates.io/crates/axum_session_mongo) | Yes | Mongo session store |
| [`axum_session_redispool`](https://crates.io/crates/axum_session_redispool) | Yes | RedisPool session store |
| [`axum_session_redispool`](https://crates.io/crates/axum_session_redispool) | Yes | RedisPool session store |

## 🔎 Example Default Setup

You can find examples within the [`Repository`](https://github.com/AscendingCreations/AxumSession/tree/main/examples)
You can find examples within the [`Repository`](https://github.com/AscendingCreations/AxumSession/tree/main/examples)

```rust ignore
use sqlx::{ConnectOptions, postgres::{PgPoolOptions, PgConnectOptions}};
Expand Down Expand Up @@ -121,15 +121,15 @@ async fn connect_to_database() -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {

## 🔑 Key Store Details

To enable and use fastbloom key storage for less database lookups.
To enable and use fastbloom key storage for less database lookups.
Add the feature `"key-store"` to the crate’s features. This feature will increase the ram usage server side.
but will heavily improve the bandwidth limitations and reduce latency of returns from the server.
but will heavily improve the bandwidth limitations and reduce latency of returns from the server.
This is based on how much the `filter_expected_elements` and `filter_false_positive_probability` are set too.
The higher they are the more ram is used. You will also need to Enable the bloom filter in the config for it to be used. By default,
The higher they are the more ram is used. You will also need to Enable the bloom filter in the config for it to be used. By default,
the `use_bloom_filters` is enabled and these config options exist whither or not the feature is enabled.
Please refer to `with_filter_expected_elements` and `with_filter_false_positive_probability` within the documents to set the options.
Otherwise stick with the default settings which should work in most situations. Just do note these options provide on how many False positives
could possibly occur when comparing a UUID to what currently exists, which means it will keep trying till it finds none that match.
could possibly occur when comparing a UUID to what currently exists, which means it will keep trying till it finds none that match.
Higher values decrease the chance of a false positive but increase ram usage.

## 😎 Session Login and Authentication via `axum_session_auth`
Expand Down
14 changes: 7 additions & 7 deletions databases/redis-bb8-pool/src/redis_bb8_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl DatabasePool for SessionRedisPool {
async fn count(&self, table_name: &str) -> Result<i64, DatabaseError> {
let mut con = match self.pool.get().await {
Ok(v) => v,
Err(err) => return Err(DatabaseError::GenericAquire(err.to_string())),
Err(err) => return Err(DatabaseError::GenericAcquire(err.to_string())),
};

let count: i64 = if table_name.is_empty() {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.get()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
redis::pipe()
.atomic() //makes this a transation.
.set(&id, session)
Expand All @@ -99,7 +99,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.get()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let id = if table_name.is_empty() {
id.to_string()
} else {
Expand All @@ -118,7 +118,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.get()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let id = if table_name.is_empty() {
id.to_string()
} else {
Expand All @@ -137,7 +137,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.get()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let id = if table_name.is_empty() {
id.to_string()
} else {
Expand All @@ -157,7 +157,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.get()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
if table_name.is_empty() {
redis::cmd("FLUSHDB")
.query_async::<()>(&mut *con)
Expand Down Expand Up @@ -187,7 +187,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.get()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let table_name = if table_name.is_empty() {
"*".to_string()
} else {
Expand Down
4 changes: 2 additions & 2 deletions databases/redispool/src/redis_cluster_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl DatabasePool for SessionRedisClusterPool {
async fn count(&self, table_name: &str) -> Result<i64, DatabaseError> {
let mut con = self
.pool
.aquire()
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;

let count: i64 = if table_name.is_empty() {
redis::cmd("DBSIZE")
Expand Down
14 changes: 7 additions & 7 deletions databases/redispool/src/redis_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl DatabasePool for SessionRedisPool {
async fn count(&self, table_name: &str) -> Result<i64, DatabaseError> {
let mut con = match self.pool.acquire().await {
Ok(v) => v,
Err(err) => return Err(DatabaseError::GenericAquire(err.to_string())),
Err(err) => return Err(DatabaseError::GenericAcquire(err.to_string())),
};

let count: i64 = if table_name.is_empty() {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
redis::pipe()
.atomic() //makes this a transation.
.set(&id, session)
Expand All @@ -96,7 +96,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let id = if table_name.is_empty() {
id.to_string()
} else {
Expand All @@ -115,7 +115,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let id = if table_name.is_empty() {
id.to_string()
} else {
Expand All @@ -134,7 +134,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let id = if table_name.is_empty() {
id.to_string()
} else {
Expand All @@ -154,7 +154,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
if table_name.is_empty() {
redis::cmd("FLUSHDB")
.query_async::<()>(&mut con)
Expand Down Expand Up @@ -184,7 +184,7 @@ impl DatabasePool for SessionRedisPool {
.pool
.acquire()
.await
.map_err(|err| DatabaseError::GenericAquire(err.to_string()))?;
.map_err(|err| DatabaseError::GenericAcquire(err.to_string()))?;
let table_name = if table_name.is_empty() {
"*".to_string()
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
let poll = connect_to_database().await;
// A premade saved and loaded Key.
// A pre-made, saved, and loaded Key.
let key = [
0, 6, 244, 144, 182, 219, 119, 30, 186, 208, 221, 180, 0, 206, 248, 7, 135, 27, 241, 0, 43,
32, 128, 232, 76, 0, 40, 46, 1, 3, 220, 0, 42, 165, 66, 0, 36, 193, 19, 251, 196, 145, 38,
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl SessionConfig {

/// Set's the session's cookie's path.
///
/// This is used to deturmine when the cookie takes effect within the website path.
/// This is used to determine when the cookie takes effect within the website path.
/// Leave as default ("/") for cookie to be used site wide.
///
/// # Examples
Expand Down
2 changes: 1 addition & 1 deletion src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait DatabasePool {
#[derive(Error, Debug)]
pub enum DatabaseError {
#[error("Database insert error {0}")]
GenericAquire(String),
GenericAcquire(String),
#[error("Database insert error {0}")]
GenericInsertError(String),
#[error("Database select error {0}")]
Expand Down
2 changes: 1 addition & 1 deletion src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ pub(crate) fn set_headers<T>(
// Add Session Store Boolean
if session.store.config.session_mode.is_opt_in() && storable && !destroy {
let name = NameType::Store.get_name(&session.store.config);
//storable doesnt need signing or encryption.
//storable doesn't need signing or encryption.
let value = storable.to_string();

if let Ok(name) = HeaderName::from_bytes(name.as_bytes()) {
Expand Down
26 changes: 14 additions & 12 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ where
// Check if the session id exists if not lets check if it exists in the database or generate a new session.
// If manual mode is enabled then do not check for a Session unless the UUID is not new.
let check_database: bool = if is_new && !session.store.config.session_mode.is_manual() {
let sess = SessionData::new(session.id.0, storable, &session.store.config);
session.store.inner.insert(session.id.inner(), sess);
let session_data = SessionData::new(session.id.0, storable, &session.store.config);
session.store.inner.insert(session.id.inner(), session_data);
false
} else if !is_new || !session.store.config.session_mode.is_manual() {
!session.store.service_session_data(&session)
Expand Down Expand Up @@ -234,7 +234,7 @@ where
);

if !destroy && (!session.store.config.session_mode.is_manual() || loaded) && renew {
// Lets change the Session ID and destory the old Session from the database.
// Lets change the Session ID and destroy the old Session from the database.
let session_id = match Session::generate_uuid(&session.store).await {
Ok(v) => v,
Err(err) => {
Expand Down Expand Up @@ -275,30 +275,32 @@ where
&& session.store.is_persistent()
&& !destroy
{
let clone_session = if let Some(mut sess) =
let clone_session = if let Some(mut session_inner) =
session.store.inner.get_mut(&session.id.inner())
{
// Check if Database needs to be updated or not. TODO: Make updatable based on a timer for in memory only.
if session.store.config.database.always_save || sess.update || !sess.validate()
if session.store.config.database.always_save
|| session_inner.update
|| !session_inner.validate()
{
if sess.longterm {
sess.expires = Utc::now() + session.store.config.max_lifespan;
if session_inner.longterm {
session_inner.expires = Utc::now() + session.store.config.max_lifespan;
} else {
sess.expires = Utc::now() + session.store.config.lifespan;
session_inner.expires = Utc::now() + session.store.config.lifespan;
};

sess.update = false;
session_inner.update = false;

Some(sess.clone())
Some(session_inner.clone())
} else {
None
}
} else {
None
};

if let Some(sess) = clone_session {
if let Err(err) = session.store.store_session(&sess).await {
if let Some(store_session) = clone_session {
if let Err(err) = session.store.store_session(&store_session).await {
return trace_error(err, "failed to save session to database");
} else {
tracing::info!("Session id {}: was saved to the database.", session.id);
Expand Down
Loading

0 comments on commit 5237393

Please sign in to comment.