use std::sync::Arc;
use crate::{Session, SessionError, SessionStore};
pub const DEFAULT_SESSION_COOKIE: &str = "SERENADE_SESSION";
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CookieSessionOptions {
name: String,
path: String,
http_only: bool,
secure: bool,
same_site: SameSite,
max_age_secs: Option<u64>,
}
impl Default for CookieSessionOptions {
fn default() -> Self {
Self {
name: DEFAULT_SESSION_COOKIE.to_owned(),
path: "/".to_owned(),
http_only: true,
secure: false,
same_site: SameSite::Lax,
max_age_secs: None,
}
}
}
impl CookieSessionOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
#[must_use]
pub fn with_path(mut self, path: impl Into<String>) -> Self {
self.path = path.into();
self
}
#[must_use]
pub const fn with_http_only(mut self, http_only: bool) -> Self {
self.http_only = http_only;
self
}
#[must_use]
pub const fn with_secure(mut self, secure: bool) -> Self {
self.secure = secure;
self
}
#[must_use]
pub const fn with_same_site(mut self, same_site: SameSite) -> Self {
self.same_site = same_site;
self
}
#[must_use]
pub const fn with_max_age_secs(mut self, max_age_secs: Option<u64>) -> Self {
self.max_age_secs = max_age_secs;
self
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SameSite {
Lax,
Strict,
None,
}
impl SameSite {
const fn as_str(self) -> &'static str {
match self {
Self::Lax => "Lax",
Self::Strict => "Strict",
Self::None => "None",
}
}
}
#[derive(Clone)]
pub struct CookieSession {
store: Arc<dyn SessionStore>,
options: CookieSessionOptions,
}
impl CookieSession {
#[must_use]
pub fn new(store: Arc<dyn SessionStore>) -> Self {
Self {
store,
options: CookieSessionOptions::default(),
}
}
#[must_use]
pub fn with_options(store: Arc<dyn SessionStore>, options: CookieSessionOptions) -> Self {
Self { store, options }
}
#[must_use]
pub const fn options(&self) -> &CookieSessionOptions {
&self.options
}
pub fn open(&self, cookie_header: Option<&str>) -> Result<Session, SessionError> {
if let Some(id) =
cookie_header.and_then(|header| parse_cookie_value(header, self.options.name()))
&& let Some(attributes) = self.store.load(&id)?
{
return Ok(Session::existing(id, attributes));
}
Ok(Session::new(generate_session_id()?))
}
pub fn commit(&self, session: &Session) -> Result<Option<String>, SessionError> {
if session.is_invalidated() {
self.store.delete(session.id())?;
return Ok(Some(format_set_cookie(
&self.options,
session.id(),
ClearCookie::Yes,
)));
}
if !session.is_dirty() {
return Ok(None);
}
self.store.save(session.id(), &session.take_for_save())?;
Ok(Some(format_set_cookie(
&self.options,
session.id(),
ClearCookie::No,
)))
}
}
#[derive(Clone, Copy)]
enum ClearCookie {
Yes,
No,
}
pub fn generate_session_id() -> Result<String, SessionError> {
let mut bytes = [0_u8; 32];
getrandom::fill(&mut bytes).map_err(|_| SessionError::Generation)?;
Ok(hex_encode(&bytes))
}
#[must_use]
pub fn parse_cookie_value(cookie_header: &str, name: &str) -> Option<String> {
for part in cookie_header.split(';') {
let part = part.trim();
let Some((key, value)) = part.split_once('=') else {
continue;
};
if key.trim() == name {
return Some(value.trim().to_owned());
}
}
None
}
fn format_set_cookie(options: &CookieSessionOptions, id: &str, clear: ClearCookie) -> String {
let mut out = format!("{}={}", options.name, id);
out.push_str("; Path=");
out.push_str(&options.path);
match clear {
ClearCookie::Yes => out.push_str("; Max-Age=0"),
ClearCookie::No => {
if let Some(max_age) = options.max_age_secs {
out.push_str("; Max-Age=");
out.push_str(&max_age.to_string());
}
}
}
if options.http_only {
out.push_str("; HttpOnly");
}
if options.secure {
out.push_str("; Secure");
}
out.push_str("; SameSite=");
out.push_str(options.same_site.as_str());
out
}
fn hex_encode(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(bytes.len() * 2);
for byte in bytes {
out.push(HEX[usize::from(byte >> 4)] as char);
out.push(HEX[usize::from(byte & 0x0f)] as char);
}
out
}