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;
|
2023-07-12 10:35:07 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-06-24 19:48:28 +03:00
|
|
|
use std::fmt;
|
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
|
2023-07-12 10:35:07 +03:00
|
|
|
#[derive(Copy, Clone, Debug, Deserialize, Hash, PartialEq, Eq, Serialize)]
|
2022-12-14 05:13:23 +03:00
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-24 19:48:28 +03:00
|
|
|
impl fmt::Display for UserBearerToken {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|