pub struct RTokenManager { /* private fields */ }Expand description
§日本語
認証 token の発行・保存・失効を行うマネージャです。
actix-web のアプリケーション state(例:web::Data<RTokenManager>)に保持する想定で、
内部では Arc<Mutex<...>> を使って状態を共有します。そのため Clone は同じストアへの
ハンドルを増やすだけです。
token は UUID v4 文字列として生成され、次と紐づきます:
- ユーザー ID(
String) - 有効期限(Unix epoch ミリ秒)
§English
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)
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>
§日本語
指定ユーザー ID の新しい token を発行します。
expire_time は TTL(秒)として扱います。保存された有効期限が現在時刻より過去であれば、
token は無効とみなされます。
内部 mutex が poisoned の場合は RTokenError::MutexPoisoned を返します。
§English
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.
Sourcepub fn logout(&self, token: &str) -> Result<(), RTokenError>
pub fn logout(&self, token: &str) -> Result<(), RTokenError>
§日本語
token をインメモリストアから削除して失効させます。
この操作は冪等です。存在しない token を削除しても成功として扱います。
内部 mutex が poisoned の場合は RTokenError::MutexPoisoned を返します。
§English
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.
Sourcepub fn expires_at(&self, token: &str) -> Result<Option<u64>, RTokenError>
pub fn expires_at(&self, token: &str) -> Result<Option<u64>, RTokenError>
§日本語
token に保存されている有効期限(Unix epoch ミリ秒)を返します。
token が存在しない場合は Ok(None) を返します。本メソッドは token の期限切れ判定は
行いません。
§English
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.
Sourcepub fn ttl_seconds(&self, token: &str) -> Result<Option<i64>, RTokenError>
pub fn ttl_seconds(&self, token: &str) -> Result<Option<i64>, RTokenError>
Sourcepub fn renew(&self, token: &str, ttl_seconds: u64) -> Result<bool, RTokenError>
pub fn renew(&self, token: &str, ttl_seconds: u64) -> Result<bool, RTokenError>
§日本語
token の有効期限を now + ttl_seconds に延長します。
返り値:
- token が存在し、期限切れでない:
Ok(true) - token が存在しない、または期限切れ:
Ok(false)(期限切れの場合は削除します)
§English
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)
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>
§日本語
同じユーザー(および roles)に対して新しい token を発行し、古い token を失効させます。
新しい token の TTL は「現在から ttl_seconds」になります。
古い token が存在しない、または期限切れの場合は Ok(None) を返します(期限切れの場合は
削除します)。
§English
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).
Sourcepub fn prune_expired(&self) -> Result<usize, RTokenError>
pub fn prune_expired(&self) -> Result<usize, RTokenError>
Sourcepub fn validate(&self, token: &str) -> Result<Option<String>, RTokenError>
pub fn validate(&self, token: &str) -> Result<Option<String>, RTokenError>
§日本語
token を検証し、有効であれば紐づくユーザー ID を返します。
振る舞い:
- token が存在し、期限切れでない:
Ok(Some(user_id)) - token が存在しない、または期限切れ:
Ok(None) - 期限切れ token は検証時にストアから削除されます
§English
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.
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