pub struct RTokenManager { /* private fields */ }Expand description
Issues, stores, and revokes authentication tokens.
This type is designed to be stored in actix-web application state
(e.g. web::Data<RTokenManager>). Internally it uses an Arc<Mutex<...>>,
so Clone creates another handle to the same shared store.
Tokens are generated as UUID v4 strings. Each token is associated with:
- a user id (
String) - an expiration timestamp (Unix epoch milliseconds)
§繁體中文
負責簽發、儲存與註銷 token 的管理器。
一般會放在 actix-web 的 application state 中(例如 web::Data<RTokenManager>)。
內部以 Arc<Mutex<...>> 共享狀態,因此 Clone 只是在同一份映射表上增加一個引用。
token 以 UUID v4 字串產生,並會綁定:
- 使用者 id(
String) - 到期時間(Unix epoch 毫秒)
Implementations§
Source§impl RTokenManager
impl RTokenManager
Sourcepub fn login(&self, id: &str, expire_time: u64) -> Result<String, RTokenError>
pub fn login(&self, id: &str, expire_time: u64) -> Result<String, RTokenError>
Issues a new token for the given user id.
expire_time is treated as TTL in seconds. The token will be considered invalid
once the stored expiration timestamp is earlier than the current time.
Returns RTokenError::MutexPoisoned if the internal mutex is poisoned.
§繁體中文
為指定使用者 id 簽發新 token。
expire_time 會被視為 TTL(秒)。當儲存的到期時間早於目前時間時,token 會被視為無效。
若內部 mutex 發生 poisoned,會回傳 RTokenError::MutexPoisoned。
Sourcepub fn logout(&self, token: &str) -> Result<(), RTokenError>
pub fn logout(&self, token: &str) -> Result<(), RTokenError>
Revokes a token by removing it from the in-memory store.
This operation is idempotent: removing a non-existing token is treated as success.
Returns RTokenError::MutexPoisoned if the internal mutex is poisoned.
§繁體中文
從記憶體儲存表中移除 token,以達到註銷效果。
此操作具冪等性:移除不存在的 token 也視為成功。
若內部 mutex 發生 poisoned,會回傳 RTokenError::MutexPoisoned。
Sourcepub fn validate(&self, token: &str) -> Result<Option<String>, RTokenError>
pub fn validate(&self, token: &str) -> Result<Option<String>, RTokenError>
Validates a token and returns the associated user id if present.
Behavior:
- Returns
Ok(Some(user_id))when the token exists and is not expired. - Returns
Ok(None)when the token does not exist or is expired. - Expired tokens are removed from the in-memory store during validation.
§繁體中文
驗證 token,若有效則回傳對應的使用者 id。
行為:
- token 存在且未過期:回傳
Ok(Some(user_id)) - token 不存在或已過期:回傳
Ok(None) - 若 token 已過期,會在驗證時從記憶體儲存表中移除
Trait Implementations§
Source§impl Clone for RTokenManager
impl Clone for RTokenManager
Source§fn clone(&self) -> RTokenManager
fn clone(&self) -> RTokenManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more