#session-middleware #warp #http-framework #filter

warp-sessions

Generic session middleware for the warp HTTP framework

17 stable releases

1.0.19 Mar 31, 2023
1.0.18 Jan 25, 2023
1.0.17 Jul 20, 2022
1.0.16 Nov 30, 2021
1.0.12 Jan 27, 2021

#280 in HTTP server

Download history 17/week @ 2024-06-17 9/week @ 2024-06-24 60/week @ 2024-07-01 1/week @ 2024-07-08 24/week @ 2024-07-15 15/week @ 2024-07-22 138/week @ 2024-07-29 4/week @ 2024-08-05 158/week @ 2024-08-12 53/week @ 2024-08-19 16/week @ 2024-08-26 11/week @ 2024-09-02 12/week @ 2024-09-09 22/week @ 2024-09-16 47/week @ 2024-09-23 99/week @ 2024-09-30

182 downloads per month
Used in redact-client

MIT license

25KB
507 lines

warp-sessions

License: MIT

This repo provides session middleware for the warp framework that:

  • Supports async
  • Provides plug-and-play functionality for multiple session backends
  • Integrates into existing Warp-style filter chains

Get started by checking out the docs.

You can also find more extensive code samples in the examples folder.


lib.rs:

warp-sessions

The warp-sessions crate provides a set of filters and an interface to add session support to your warp handlers.

It operates as such:

  1. A warp filter is created which has access to some SessionStore. This filter will, upon receiving a request, extract the session ID cookie, fetch the matching session, and return it to be used by the route handler. It'll also handle creating a new session in the absence of one.
  2. The route handler operates as normal, fetching and setting information in the session struct it received.
  3. When the route is ready to reply, it creates its reply struct and places it in a tuple with the session struct it received. It then calls the session reply handler inside a .and_then(...) call, using .untuple_one() to unpair the output first.

Example

use warp::{Filter, Rejection};
use warp_sessions::{MemoryStore, SessionWithStore, CookieOptions, SameSiteCookieOption};

#[tokio::main]
async fn main() {
    let session_store = MemoryStore::new();

    let route = warp::get()
        .and(warp::path!("test"))
        .and(warp_sessions::request::with_session(
            session_store,
            Some(CookieOptions {
                cookie_name: "sid",
                cookie_value: None,
                max_age: Some(60),
                domain: None,
                path: None,
                secure: false,
                http_only: true,
                same_site: Some(SameSiteCookieOption::Strict),
            }),
        ))
        .and_then(
            move |session_with_store: SessionWithStore<MemoryStore>| async move {
                Ok::<_, Rejection>((
		        warp::reply::html("<html></html>".to_string()),
                    session_with_store,
                ))
            },
        )
        .untuple_one()
        .and_then(warp_sessions::reply::with_session);

    // Start the server
    let port = 8080;
    println!("starting server listening on ::{}", port);
    // warp::serve(route).run(([0, 0, 0, 0], port)).await;
}

The Some(CookieOptions) provided as the second argument to warp_sessions::request::with_session can optionally be None. This will result in a value of CookieOptions::default(). This option encodes the full set of possible cookie parameters that could be applied to the session ID cookie. Check the CookieOptions for information on fields.

Addresses issue #609 by appending to the header map rather than inserting, allowing for multiple session cookies to be set.

This session middleware is meant to be very light and has no shared state other than the session store. It could be reused multiple times in the same route, and with different session stores.

The backing session logic is provided by the async-session crate. Simply implement the SessionStore trait and pass it on to the provided with_session(...) filter to use it.

Dependencies

~12–22MB
~333K SLoC