axum-security 0.0.2

A security toolbox for the Axum library
Documentation
use std::borrow::Cow;

use cookie_monster::Cookie;
use uuid::Uuid;

/// Unique identifier for a cookie session, stored as the cookie value.
///
/// Generated as a UUID v7 by default.
#[derive(Debug, Hash, Clone, PartialEq, Eq)]
pub struct SessionId(Box<str>);

impl SessionId {
    pub fn new() -> Self {
        SessionId(Uuid::now_v7().to_string().into_boxed_str())
    }

    pub fn from_cookie(cookie: &Cookie) -> Self {
        SessionId(cookie.value().into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<SessionId> for Cow<'static, str> {
    fn from(value: SessionId) -> Self {
        Cow::Owned(value.0.into_string())
    }
}

impl From<String> for SessionId {
    fn from(value: String) -> Self {
        Self(value.into_boxed_str())
    }
}

impl Default for SessionId {
    fn default() -> Self {
        Self::new()
    }
}