cookiebox/
lib.rs

1//! A type safe cookie management crate for the Actix Web framework.
2//!
3//! Cookiebox provides a type safe and flexible approach to managing cookies in Actix web based applications.
4//! It allows you to define, configure, and manage cookies with minimal boilerplate.
5//!
6//! # Features
7//! - This crate uses [biscotti](https://docs.rs/biscotti/latest/biscotti/) under the hood, which inherit most of it's features.
8//! - Offers the ability to configure settings on a per cookie basis.
9//! - Enforces type definitions for deserializing cookie values upon retrieval.
10//! - Allows customization of both the data type and data serialization.
11//! - Provides a straightforward and type safe interface for managing cookies.
12//!
13//! # Usage
14//! To start using the cookiebox in your web application, you must register [CookieMiddleware] in your App.
15//!```no_run
16//!use actix_web::{web, App, HttpServer, HttpResponse, Error};
17//!use cookiebox::{Processor, ProcessorConfig, CookieMiddleware};
18//!
19//!#[actix_web::main]
20//!async fn main() -> std::io::Result<()> {
21//!    // Start by creating a `Processor` from the `ProcessorConfig`
22//!    // This decides which cookie needs to decrypted or verified.
23//!    let processor: Processor = ProcessorConfig::default().into();
24//!   
25//!    HttpServer::new(move ||
26//!            App::new()
27//!            // Add cookie middleware
28//!            .wrap(CookieMiddleware::new(processor.clone()))
29//!            .default_service(web::to(|| HttpResponse::Ok())))
30//!        .bind(("127.0.0.1", 8080))?
31//!        .run()
32//!        .await
33//!}
34//!```
35//! Define your desired cookie types with customizable configurations.
36//!```no_run
37//!use actix_web::HttpMessage;
38//!use cookiebox::cookiebox_macros::{cookie, FromRequest};
39//!use cookiebox::cookies::{Cookie, CookieName, IncomingConfig, OutgoingConfig};
40//!use cookiebox::{Attributes, SameSite};
41//!use serde_json::json;
42//!
43//!// Define a cookie type
44//!#[cookie(name = "__my-cookie")]
45//!pub struct MyCookie;
46//!
47//!// IncomingConfig gives the cookie type get and get_all cookies with similar name
48//!// You may opt out if don't want read cookie data
49//!impl IncomingConfig for MyCookie {
50//!    // Configure the get return to any custom type
51//!    type Get = String;
52//!}
53//!// OutgoingConfig gives the cookie type insert and remove cookies
54//!// You may opt out if don't want insert or remove a cookie
55//!impl OutgoingConfig for MyCookie {
56//!    // Configure the insert to any custom type
57//!    type Insert = (String, i32);
58//!    
59//!    // In most cases, the default serialization should be sufficient. However, if needed,
60//!    // you can customize the way the cookie value is serialized by implementing this method.
61//!    fn serialize(values: Self::Insert) -> serde_json::Value {
62//!        json!(
63//!             format!("String: {} - i32: {}", values.0, values.1)
64//!        )
65//!    }
66//!    
67//!    // Set the appropriate attribute for the cookie, check `Attributes` for more details
68//!    fn attributes<'c>() -> Attributes<'c> {
69//!        Attributes::new().same_site(SameSite::Lax).http_only(false)
70//!    }
71//!}
72//!// Once defined, you need to add these cookies in a collection struct and use derive macro to implement FromRequest
73//!// Note: The macro only allows struct with either a single unnamed field or multiple named fields
74//!#[derive(FromRequest)]
75//!pub struct CookieCollection<'c>(Cookie<'c, MyCookie>);
76//!
77//!```
78//! Now, your cookies can be accessed in request handlers by using `CookieCollection` as a parameter.
79//!
80//! If you would like to see an example, click [here](https://github.com/MSalah73/cookiebox/tree/master/examples).
81
82mod attributes;
83pub mod cookies;
84mod middleware;
85mod storage;
86
87pub use attributes::Attributes;
88pub use biscotti::{Expiration, Key, Processor, ProcessorConfig, SameSite, config, time};
89pub use cookiebox_macros;
90pub use middleware::CookieMiddleware;
91pub use storage::Storage;