Skip to main content

SessionGuard

Struct SessionGuard 

Source
pub struct SessionGuard<S: SessionStore> { /* private fields */ }

Implementations§

Source§

impl<S: SessionStore> SessionGuard<S>

Source

pub fn new(store: S, config: SessionConfig) -> Self

Source

pub fn config(&self) -> &SessionConfig

Source

pub fn bind( &self, ctx: &RequestContext<'_>, now: u64, ) -> Result<SessionVerdict, SessionError>

登录:建会话 + 绑指纹 + 记位置,并返回异地判定。

登录本身总是成功(除非调用方误用或后端故障)—— 异地只影响 verdict, 由调用方决定是否走二次验证,不应阻断登录。

Source

pub fn verify(&self, ctx: &RequestContext<'_>, now: u64) -> SessionVerdict

每请求校验。

返回 SessionVerdict 而非 Result:认证路径上「拒绝」是正常结果而非错误, 强制调用方在类型层面处理每一种拒绝。

get → recent_logins → touch 是三次独立锁获取,不原子(与 throttle::Throttle::record_failure 的窗口说明同一取舍):并发下 touch 可能落在一条刚被 revoke / purge 掉的记录上(落空,无副作用), 登录历史也可能读到略旧的一份。这不构成绕过 —— 各判定只吃自己那一份 快照(get 返回的是克隆),没有跨字段的复合不变量会被撕开; 要消除这个窗口得把「读记录 + 读历史 + 刷新 last_seen」并成一个 store 操作。

Source

pub fn revoke(&self, token: &str) -> Result<(), StoreError>

吊销单个会话(登出)。

Source

pub fn revoke_all(&self, subject: &str) -> Result<usize, StoreError>

吊销某 subject 的全部会话(改密码 / 踢下线),返回受影响条数。

Source

pub fn rotate( &self, old: &str, new: &str, ctx: &RequestContext<'_>, now: u64, ) -> Result<(), SessionError>

续期换 token:旧 token 吊销,新 token 由调用方提供。

旧会话必须存在、未吊销、未过期,且指纹与本次 ctx 相符 —— 否则等于允许攻击者拿别人的 token 换一个自己的新 token,是提权漏洞。

前置条件:new 必须是一个未在用的 token(不存在,或已过期,或已吊销 —— 死记录可以覆盖)。指向一个在用会话返回 SessionError::TokenInUse 且不改动任何记录:store 的 put 是无条件 upsert,不挡这一下就等于让 攻击者用自己的会话把受害者的记录整个盖掉(受害者下次 verify 读到的是 攻击者的指纹 ⇒ 被动踢下线)。new == old 同样按 TokenInUse 拒绝 —— 它自相矛盾(既要吊销 old 又要 new 可用),静默接受只会得到一个 「rotate 成功但会话已死」的假象。

新记录的身份字段(subject / location / coords / signature)一律 以服务端记录为准,不接受 ctx 覆盖。

TOCTOU:查 new 与随后的 put 是两次独立锁获取,不原子 —— 两个并发 rotate 指向同一个新 token 时仍可能互相覆盖(要攻击者猜到对方 的新 token 并卡进这个窄窗)。本 trait 没有条件写 / 事务,这个检查把 「必然覆盖」降级为「窄窗竞争」,仍然值得;要彻底关窗得加 put_if_absent 一类的原子原语。

Source

pub fn purge_expired(&self, now: u64) -> Result<usize, StoreError>

清除已过期记录,返回清除条数。

内存后端无后台线程,map 条目只增不减;长期运行的进程需按 ttl_secs 量级的间隔定时调用(同时回收休眠 subject 的登录历史,见 LOGIN_HISTORY_KEEP_SECS)。

Auto Trait Implementations§

§

impl<S> Freeze for SessionGuard<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for SessionGuard<S>
where S: RefUnwindSafe,

§

impl<S> Send for SessionGuard<S>

§

impl<S> Sync for SessionGuard<S>

§

impl<S> Unpin for SessionGuard<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for SessionGuard<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for SessionGuard<S>
where S: UnwindSafe,

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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.