Ruts: Rust Tower Session for HTTP Applications
Ruts is a robust, flexible session management library for Rust web applications. It provides a seamless way to handle user sessions in tower-based web frameworks, with a focus on security, performance, and ease of use.
Features
- Compatible with tower-based web frameworks (e.g., axum, warp)
- Flexible session storage with built-in support for Redis
- Customizable cookie options for session management
- Support for both cookie-based and URL-based sessions
- Asynchronous API for efficient session handling
- Type-safe session data management
- Automatic session expiration and regeneration
Installation
Add the following to your Cargo.toml:
[]
= "0.1.11"
Quick Start
Here's a basic example of how to use Ruse with axum:
use ;
use ;
use RedisStore;
use RedisClient;
use Arc;
use ClientLike;
use CookieManagerLayer;
async
async
Usage
Setting Up the Session Layer
To use Ruse in your application, you need to set up the SessionLayer:
let store = new;
let session_layer = new
.with_cookie_options;
Using Sessions in Request Handlers
Ruse provides an extractor for axum that allows you to easily access the session in your request handlers:
async
Session Methods
Ruse provides several methods for working with session data:
insert: Add a new key-value pair to the sessionget: Retrieve a value from the sessionupdate: Update an existing value in the sessionremove: Remove a key-value pair from the sessiondelete: Delete the entire sessionregenerate: Generate a new session IDexpire: Set or update the session expiration time
Configuration
You can customize various aspects of session management using CookieOptions:
let cookie_options = build
.name
.http_only
.same_site
.secure
.max_age // 2 hours
.path;
Security Considerations
- Always use HTTPS in production to protect session cookies.
- Set appropriate
SameSiteandSecureflags for cookies. - Regularly regenerate session IDs to prevent session fixation attacks.
- Be cautious about what data you store in sessions and how long sessions last.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.