Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
[package]
name = "async-session"
version = "3.0.0"
name = "saysion"
version = "0.1.0"
license = "MIT OR Apache-2.0"
repository = "https://github.com/http-rs/async-session"
documentation = "https://docs.rs/async-session"
repository = "https://github.com/salvo-rs/saysion"
documentation = "https://docs.rs/saysion"
description = "Async session support with pluggable stores"
readme = "README.md"
edition = "2021"
edition = "2024"
rust-version = "1.89"
keywords = []
categories = []
authors = [
"Yoshua Wuyts <yoshuawuyts@gmail.com>",
"Jacob Rothstein <hi@jbr.me>"
"Jacob Rothstein <hi@jbr.me>",
"chrislearn <chris@acroidea.com>"
]

[dependencies]
async-trait = "0.1.59"
rand = "0.8.5"
base64 = "0.20.0"
sha2 = "0.10.6"
rand = "0.9.2"
base64 = "0.22.1"
sha2 = "0.10.9"
hmac = "0.12.1"
serde = { version = "1.0.150", features = ["derive", "rc"] }
serde_json = "1.0.89"
bincode = "1.3.3"
anyhow = "1.0.66"
blake3 = "1.3.3"
async-lock = "2.6.0"
log = "0.4.17"
postcard = { version = "1.1.3", default-features = false, features = ["use-std"] }
anyhow = "1.0.100"
blake3 = "1.8.2"
async-lock = "3.4.2"
time = { version = "0.3.17", features = ["serde"] }
tracing = "0.1.44"

[dependencies.serde]
version = "1.0.150"
features = ["rc", "derive"]

[dependencies.time]
version = "0.3.17"
features = ["serde"]

[dev-dependencies.async-std]
version = "1.12.0"
features = ["attributes"]
[dev-dependencies]
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread", "time"] }
49 changes: 6 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
<h1 align="center">async-session</h1>
<h1 align="center">saysion</h1>
<div align="center">
<strong>
Async session support with pluggable middleware
Session support with pluggable middleware
</strong>
</div>

<br />

<div align="center">
<!-- Crates version -->
<a href="https://crates.io/crates/async-session">
<img src="https://img.shields.io/crates/v/async-session.svg?style=flat-square"
alt="Crates.io version" />
</a>
<!-- Downloads -->
<a href="https://crates.io/crates/async-session">
<img src="https://img.shields.io/crates/d/async-session.svg?style=flat-square"
alt="Download" />
</a>
<!-- docs.rs docs -->
<a href="https://docs.rs/async-session">
<img src="https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square"
alt="docs.rs docs" />
</a>
</div>

<div align="center">
<h3>
<a href="https://docs.rs/async-session">
<a href="https://docs.rs/saysion">
API Docs
</a>
<span> | </span>
<a href="https://github.com/http-rs/async-session/releases">
<a href="https://github.com/salvo-rs/saysion/releases">
Releases
</a>
<span> | </span>
<a href="https://github.com/http-rs/async-session/blob/main/.github/CONTRIBUTING.md">
<a href="https://github.com/salvo-rs/saysion/blob/main/.github/CONTRIBUTING.md">
Contributing
</a>
</h3>
</div>

## Available session stores

* [async-sqlx-session](https://crates.io/crates/async-sqlx-session) postgres, mysql & sqlite
* [async-redis-session](https://crates.io/crates/async-redis-session)
* [async-mongodb-session](https://crates.io/crates/async-mongodb-session)
* [async-session-r2d2](https://crates.io/crates/async-session-r2d2) - sqlite only

## Framework implementations

* [`tide::sessions`](https://docs.rs/tide/latest/tide/sessions/index.html)
* [warp-sessions](https://docs.rs/warp-sessions/latest/warp_sessions/)
* [trillium-sessions](https://docs.trillium.rs/trillium_sessions)
* [axum-sessions](https://docs.rs/axum_sessions)
* [salvo-sessions](https://docs.rs/salvo_extra/latest/salvo_extra/session/index.html)
*Fork from: https://github.com/http-rs/async-session*

## Safety
This crate uses ``#![deny(unsafe_code)]`` to ensure everything is implemented in
Expand All @@ -64,13 +34,6 @@ This crate uses ``#![deny(unsafe_code)]`` to ensure everything is implemented in
Want to join us? Check out our ["Contributing" guide][contributing] and take a
look at some of these issues:

- [Issues labeled "good first issue"][good-first-issue]
- [Issues labeled "help wanted"][help-wanted]

[contributing]: https://github.com/http-rs/async-session/blob/main/.github/CONTRIBUTING.md
[good-first-issue]: https://github.com/http-rs/async-session/labels/good%20first%20issue
[help-wanted]: https://github.com/http-rs/async-session/labels/help%20wanted

## Acknowledgements

This work is based on the work initiated by
Expand Down
25 changes: 13 additions & 12 deletions src/cookie_store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{async_trait, Result, Session, SessionStore};
use crate::{Result, Session, SessionStore, async_trait};
use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64_STANDARD};

/// A session store that serializes the entire session into a Cookie.
///
Expand Down Expand Up @@ -32,14 +33,14 @@ impl CookieStore {
#[async_trait]
impl SessionStore for CookieStore {
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
let serialized = base64::decode(cookie_value)?;
let session: Session = bincode::deserialize(&serialized)?;
let serialized = BASE64_STANDARD.decode(cookie_value)?;
let session: Session = postcard::from_bytes(&serialized)?;
Ok(session.validate())
}

async fn store_session(&self, session: Session) -> Result<Option<String>> {
let serialized = bincode::serialize(&session)?;
Ok(Some(base64::encode(serialized)))
let serialized = postcard::to_stdvec(&session)?;
Ok(Some(BASE64_STANDARD.encode(serialized)))
}

async fn destroy_session(&self, _session: Session) -> Result {
Expand All @@ -54,9 +55,9 @@ impl SessionStore for CookieStore {
#[cfg(test)]
mod tests {
use super::*;
use async_std::task;
use std::time::Duration;
#[async_std::test]

#[tokio::test]
async fn creating_a_new_session_with_no_expiry() -> Result {
let store = CookieStore::new();
let mut session = Session::new();
Expand All @@ -71,7 +72,7 @@ mod tests {
Ok(())
}

#[async_std::test]
#[tokio::test]
async fn updating_a_session() -> Result {
let store = CookieStore::new();
let mut session = Session::new();
Expand All @@ -89,7 +90,7 @@ mod tests {
Ok(())
}

#[async_std::test]
#[tokio::test]
async fn updating_a_session_extending_expiry() -> Result {
let store = CookieStore::new();
let mut session = Session::new();
Expand All @@ -107,13 +108,13 @@ mod tests {
let session = store.load_session(cookie_value.clone()).await?.unwrap();
assert_eq!(session.expiry().unwrap(), &new_expires);

task::sleep(Duration::from_secs(3)).await;
tokio::time::sleep(Duration::from_secs(3)).await;
assert_eq!(None, store.load_session(cookie_value).await?);

Ok(())
}

#[async_std::test]
#[tokio::test]
async fn creating_a_new_session_with_expiry() -> Result {
let store = CookieStore::new();
let mut session = Session::new();
Expand All @@ -129,7 +130,7 @@ mod tests {

assert!(!loaded_session.is_expired());

task::sleep(Duration::from_secs(3)).await;
tokio::time::sleep(Duration::from_secs(3)).await;
assert_eq!(None, store.load_session(cookie_value).await?);

Ok(())
Expand Down
19 changes: 5 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
//! # Example
//!
//! ```
//! use async_session::{Session, SessionStore, MemoryStore};
//! use saysion::{Session, SessionStore, MemoryStore};
//!
//! # fn main() -> async_session::Result {
//! # async_std::task::block_on(async {
//! # #[tokio::main]
//! # async fn main() -> saysion::Result {
//! #
//! // Init a new session store we can persist sessions to.
//! let mut store = MemoryStore::new();
Expand All @@ -29,7 +29,7 @@
//! assert_eq!(session.get::<usize>("user_id").unwrap(), 1);
//! assert!(!session.data_changed());
//! #
//! # Ok(()) }) }
//! # Ok(()) }
//! ```

// #![forbid(unsafe_code, future_incompatible)]
Expand All @@ -47,6 +47,7 @@
)]

pub use anyhow::Error;
pub use async_trait::async_trait;
/// An anyhow::Result with default return type of ()
pub type Result<T = ()> = std::result::Result<T, Error>;

Expand All @@ -59,13 +60,3 @@ pub use cookie_store::CookieStore;
pub use memory_store::MemoryStore;
pub use session::Session;
pub use session_store::SessionStore;

pub use async_trait::async_trait;
pub use base64;
pub use blake3;
pub use hmac;
pub use log;
pub use serde;
pub use serde_json;
pub use sha2;
pub use time;
Loading
Loading