2022-10-28 09:38:21 +03:00
|
|
|
use axum::headers::authorization::Bearer;
|
2022-12-14 05:13:23 +03:00
|
|
|
use migration::sea_orm::prelude::Uuid;
|
|
|
|
use serde::Serialize;
|
2023-06-21 00:22:14 +03:00
|
|
|
use std::str::FromStr;
|
2022-10-28 09:38:21 +03:00
|
|
|
use ulid::Ulid;
|
|
|
|
|
|
|
|
/// Key used for caching the user's login
|
2022-12-14 05:13:23 +03:00
|
|
|
#[derive(Clone, Hash, PartialEq, Eq, Serialize)]
|
|
|
|
#[serde(transparent)]
|
2022-10-28 09:38:21 +03:00
|
|
|
pub struct UserBearerToken(pub Ulid);
|
|
|
|
|
2022-12-14 05:13:23 +03:00
|
|
|
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<Self, Self::Err> {
|
|
|
|
let ulid = Ulid::from_str(s)?;
|
|
|
|
|
|
|
|
Ok(Self(ulid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Ulid> for UserBearerToken {
|
|
|
|
fn from(x: Ulid) -> Self {
|
|
|
|
Self(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UserBearerToken> for Uuid {
|
|
|
|
fn from(x: UserBearerToken) -> Self {
|
|
|
|
x.uuid()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:38:21 +03:00
|
|
|
impl TryFrom<Bearer> for UserBearerToken {
|
|
|
|
type Error = ulid::DecodeError;
|
|
|
|
|
|
|
|
fn try_from(b: Bearer) -> Result<Self, ulid::DecodeError> {
|
|
|
|
let u = Ulid::from_string(b.token())?;
|
|
|
|
|
|
|
|
Ok(UserBearerToken(u))
|
|
|
|
}
|
|
|
|
}
|