RTokenManager

Struct RTokenManager 

Source
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

Source

pub fn new() -> Self

§日本語

空のマネージャを作成します。

§English

Creates an empty manager.

Source

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.

Source

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.

Source

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.

Source

pub fn ttl_seconds(&self, token: &str) -> Result<Option<i64>, RTokenError>

§日本語

token の残り TTL(秒)を返します。

返り値:

  • token が存在しない:Ok(None)
  • token がすでに期限切れ:Ok(Some(0))(本メソッドでは削除しません)
§English

Returns the remaining TTL in seconds for a token.

Returns:

  • Ok(None) when the token does not exist
  • Ok(Some(0)) when the token is already expired (it is not removed here)
Source

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 expired
  • Ok(false) if the token does not exist or is expired (expired tokens are removed)
Source

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).

Source

pub fn prune_expired(&self) -> Result<usize, RTokenError>

§日本語

インメモリストアから期限切れの token を削除し、削除した件数を返します。

§English

Removes expired tokens from the in-memory store and returns how many were removed.

Source

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

Source§

fn clone(&self) -> RTokenManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for RTokenManager

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more