use crate::error::{GarrisonError, GarrisonResult};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenClaims {
pub login_id: String,
pub expire_at: i64,
pub device: Option<String>,
}
pub trait Token: Send + Sync {
fn generate(&self, login_id: &str, timeout: i64) -> GarrisonResult<String>;
fn generate_with_amr(
&self,
login_id: &str,
timeout: i64,
amr: &[String],
auth_time: Option<i64>,
) -> GarrisonResult<String> {
let _ = (amr, auth_time);
self.generate(login_id, timeout)
}
fn verify(&self, token: &str) -> GarrisonResult<Option<String>>;
fn parse(&self, token: &str) -> GarrisonResult<TokenClaims>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct UuidTokenStyle;
#[derive(Debug, Clone, Copy, Default)]
pub struct Random64TokenStyle;
#[derive(Clone, Default)]
pub struct SimpleTokenStyle {
#[cfg_attr(not(feature = "secure-simple-token"), allow(dead_code))]
secret: String,
}
impl std::fmt::Debug for SimpleTokenStyle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SimpleTokenStyle")
.field("secret", &"[REDACTED]")
.finish()
}
}
impl SimpleTokenStyle {
pub fn new(secret: String) -> Self {
Self { secret }
}
}
#[cfg(feature = "protocol-jwt")]
pub struct JwtTokenStyle {
handler: crate::protocol::jwt::JwtHandler,
}
mod style_impl;
pub struct TokenStyleFactory;
#[cfg(test)]
mod tests;