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 expires_at(&self, token: &str) -> Result<Option<u64>, RTokenError>
pub fn expires_at(&self, token: &str) -> Result<Option<u64>, RTokenError>
Returns the stored expiration timestamp for a token (milliseconds since Unix epoch).
Returns Ok(None) if the token does not exist. This method does not validate
whether the token has already expired.
§繁體中文
回傳 token 的到期時間戳(Unix epoch 毫秒)。
若 token 不存在,回傳 Ok(None)。本方法不會檢查 token 是否已過期。
Sourcepub fn ttl_seconds(&self, token: &str) -> Result<Option<i64>, RTokenError>
pub fn ttl_seconds(&self, token: &str) -> Result<Option<i64>, RTokenError>
Returns the remaining TTL in seconds for a token.
Returns:
Ok(None)when the token does not existOk(Some(0))when the token is already expired (it is not removed here)
§繁體中文
回傳 token 剩餘 TTL(秒)。
回傳:
- token 不存在:
Ok(None) - token 已過期:
Ok(Some(0))(本方法不會在此移除它)
Sourcepub fn renew(&self, token: &str, ttl_seconds: u64) -> Result<bool, RTokenError>
pub fn renew(&self, token: &str, ttl_seconds: u64) -> Result<bool, RTokenError>
Extends a token’s lifetime to now + ttl_seconds.
Returns:
Ok(true)if the token exists and is not expiredOk(false)if the token does not exist or is expired (expired tokens are removed)
§繁體中文
將 token 續期為 now + ttl_seconds。
回傳:
- token 存在且未過期:
Ok(true) - token 不存在或已過期:
Ok(false)(若已過期會順便移除)
Sourcepub fn rotate(
&self,
token: &str,
ttl_seconds: u64,
) -> Result<Option<String>, RTokenError>
pub fn rotate( &self, token: &str, ttl_seconds: u64, ) -> Result<Option<String>, RTokenError>
Issues a new token for the same user (and roles) and revokes the old token.
The new token will have a lifetime of ttl_seconds from now.
Returns Ok(None) if the old token does not exist or is expired (expired tokens
are removed).
§繁體中文
為同一位使用者(以及角色)換發新 token,並註銷舊 token。
新 token 的 TTL 會以現在起算 ttl_seconds。
若舊 token 不存在或已過期,回傳 Ok(None)(若已過期會順便移除)。
Sourcepub fn prune_expired(&self) -> Result<usize, RTokenError>
pub fn prune_expired(&self) -> Result<usize, RTokenError>
Removes expired tokens from the in-memory store and returns how many were removed.
§繁體中文
從記憶體儲存表中移除已過期的 token,並回傳移除數量。
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