rails-cookie-parser

Crates.iorails-cookie-parser
lib.rsrails-cookie-parser
version0.1.0
sourcesrc
created_at2024-09-26 21:02:29.516482
updated_at2024-09-26 21:02:29.516482
descriptionA decryption library for Rails cookies
homepage
repositoryhttps://github.com/joshleaves/rails-cookie-parser
max_upload_size
id1388033
size15,711
Arnaud 'red' Rouyer (joshleaves)

documentation

README

rails-cookie-parser

I already wrote something similar in Node.JS, and looking at Loco, I felt like it would be good to write some APIs in Rust and have them read sessions set in Rails.

Usage

Simple plug-n-use, don't forget to set your SECRET_KEY_BASE:

  use rails_cookie_parser::RailsCookieParser;

  let parser = RailsCookieParser::default();
  let Ok(result) = parser.decipher_cookie("my_cookie--is--very--long") else {
    panic!("An error somewhere on the way");
  }

Since Rails 7, the default hash digest is Sha256. If you want to use your Rails5 or Rails 6 sessions:

  use rails_cookie_parser::RailsCookieParser;

  let parser = RailsCookieParser::default_rails6();
  let Ok(result) = parser.decipher_cookie("my_cookie--is--very--long") else {
    panic!("An error somewhere on the way");
  }

For a more complex usage, you can fine-tune things:


  use rails_cookie_parser::{RailsCookieParser, HashDigest};

  let rails_key = RailsCookieParser::new(
    "secret_key_base"                 // Secret key base
    "authenticated encrypted cookie", // Key salt
    1000,                             // Iterations
    HashDigest::Sha256                // HashDigest: Sha1 or Sha256
  );
  let Ok(result) = parser.decipher_cookie("my_cookie--is--very--long") else {
    panic!("An error somewhere on the way");
  }

License

Licensed under MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT).

Commit count: 1

cargo fmt