use axum::headers::authorization::Bearer; use migration::sea_orm::prelude::Uuid; use serde::Serialize; use std::str::FromStr; use ulid::Ulid; /// Key used for caching the user's login #[derive(Clone, Hash, PartialEq, Eq, Serialize)] #[serde(transparent)] pub struct UserBearerToken(pub Ulid); impl UserBearerToken { pub fn redis_key(&self) -> String { format!("bearer:{}", self.0) } pub fn uuid(&self) -> Uuid { Uuid::from_u128(self.0.into()) } } impl Default for UserBearerToken { fn default() -> Self { Self(Ulid::new()) } } impl FromStr for UserBearerToken { type Err = ulid::DecodeError; fn from_str(s: &str) -> Result { let ulid = Ulid::from_str(s)?; Ok(Self(ulid)) } } impl From for UserBearerToken { fn from(x: Ulid) -> Self { Self(x) } } impl From for Uuid { fn from(x: UserBearerToken) -> Self { x.uuid() } } impl TryFrom for UserBearerToken { type Error = ulid::DecodeError; fn try_from(b: Bearer) -> Result { let u = Ulid::from_string(b.token())?; Ok(UserBearerToken(u)) } }