44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
|
|
use crate::{limited::FixedLenString, user::Username};
|
||
|
|
use chrono::{DateTime, Utc};
|
||
|
|
use serde::{Deserialize, Serialize};
|
||
|
|
|
||
|
|
pub const TOKEN_LEN: usize = 0x20;
|
||
|
|
|
||
|
|
pub type TokenString = FixedLenString<TOKEN_LEN>;
|
||
|
|
|
||
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||
|
|
pub struct Token {
|
||
|
|
pub token: TokenString,
|
||
|
|
pub username: Username,
|
||
|
|
pub display_name: Option<String>,
|
||
|
|
pub pronouns: Option<String>,
|
||
|
|
pub created_at: DateTime<Utc>,
|
||
|
|
pub expires_at: DateTime<Utc>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Token {
|
||
|
|
pub fn login_token(&self) -> TokenLogin {
|
||
|
|
TokenLogin(self.token.clone())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||
|
|
pub struct TokenLogin(pub FixedLenString<TOKEN_LEN>);
|
||
|
|
|
||
|
|
#[cfg(feature = "ssr")]
|
||
|
|
impl axum_extra::headers::authorization::Credentials for TokenLogin {
|
||
|
|
const SCHEME: &'static str = "Bearer";
|
||
|
|
|
||
|
|
fn decode(value: &axum::http::HeaderValue) -> Option<Self> {
|
||
|
|
value
|
||
|
|
.to_str()
|
||
|
|
.ok()
|
||
|
|
.and_then(|v| FixedLenString::new(v.strip_prefix("Bearer ").unwrap_or(v).to_string()))
|
||
|
|
.map(Self)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn encode(&self) -> axum::http::HeaderValue {
|
||
|
|
axum::http::HeaderValue::from_str(self.0.as_str()).expect("bearer token encode")
|
||
|
|
}
|
||
|
|
}
|