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.
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");
}
Licensed under MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT).