Skip to main content

TokenStore

Struct TokenStore 

Source
pub struct TokenStore { /* private fields */ }
Expand description

刷新令牌存储

管理刷新令牌的生命周期,支持轮换、撤销和重放检测。 使用 Mutex<HashMap> 进行线程安全存储。

Implementations§

Source§

impl TokenStore

Source

pub fn new() -> Self

创建新的令牌存储,刷新令牌默认有效期 7 天

Source

pub fn with_refresh_lifetime(self, seconds: i64) -> Self

配置刷新令牌默认有效期(秒)

Source

pub fn issue_family( &self, refresh_token: impl Into<String>, user_id: i64, ) -> Result<StoredToken, AuthError>

签发新的令牌家族(用户登录时调用)

创建一个新的令牌家族,并存储初始刷新令牌。 返回创建的 StoredToken 供调用方返回给客户端。

Source

pub fn refresh( &self, old_refresh_token: &str, new_refresh_token: impl Into<String>, ) -> Result<StoredToken, TokenFamilyError>

刷新令牌(轮换)

验证旧刷新令牌有效后,标记其为已使用,并签发新的刷新令牌(同一家族)。

§安全机制
  1. 如果旧令牌已被使用 -> 返回 ReplayDetected,撤销整个家族
  2. 如果旧令牌已被撤销 -> 返回 NotFound
  3. 如果旧令牌已过期 -> 返回 Expired
  4. 如果家族已被撤销 -> 返回 FamilyRevoked
Source

pub fn revoke_token(&self, token: &str) -> Result<(), TokenFamilyError>

撤销单个令牌

标记令牌为已撤销,但不影响家族中的其他令牌。 适用于用户登出单个设备的场景。

Source

pub fn revoke_family(&self, family_id: &str) -> Result<usize, TokenFamilyError>

撤销整个令牌家族

撤销家族中的所有令牌。适用于:

  • 用户修改密码
  • 检测到重放攻击
  • 管理员强制下线
Source

pub fn revoke_user(&self, user_id: i64) -> usize

撤销用户的所有令牌

撤销属于指定用户的所有令牌家族。 适用于用户修改密码、账户被禁用等场景。

Source

pub fn is_valid(&self, token: &str) -> bool

验证令牌是否有效

Source

pub fn get_token(&self, token: &str) -> Option<StoredToken>

获取令牌信息

Source

pub fn family_tokens(&self, family_id: &str) -> Vec<StoredToken>

获取家族中的所有令牌

Source

pub fn is_family_revoked(&self, family_id: &str) -> bool

检查家族是否已被撤销

Source

pub fn cleanup(&self) -> usize

清理已过期和已撤销的令牌

返回清理的令牌数量。

Source

pub fn token_count(&self) -> usize

返回当前存储的令牌数量

Source

pub fn family_count(&self) -> usize

返回当前存储的家族数量

Trait Implementations§

Source§

impl Default for TokenStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V