Skip to main content

InMemoryRefreshTokenStore

Struct InMemoryRefreshTokenStore 

Source
pub struct InMemoryRefreshTokenStore { /* private fields */ }
Expand description

Thread-safe, in-memory implementation of TokenStore.

Internally keeps a HashMap<String, RefreshTokenData> behind a tokio::sync::RwLock, allowing concurrent reads while serializing writes. Suitable for development, testing and single-instance deployments.

§Examples

use chrono::{Duration, Utc};
use actix_jwt::core::TokenStore;
use actix_jwt::store::InMemoryRefreshTokenStore;

let store = InMemoryRefreshTokenStore::new();

let expiry = Utc::now() + Duration::hours(1);
store.set("tok-1", serde_json::json!({"uid": 1}), expiry).await.unwrap();

let data = store.get("tok-1").await.unwrap();
assert_eq!(data["uid"], 1);

store.delete("tok-1").await.unwrap();
assert!(store.get("tok-1").await.is_err());

Implementations§

Source§

impl InMemoryRefreshTokenStore

Source

pub fn new() -> Self

Creates a new empty store.

Source

pub async fn get_all(&self) -> HashMap<String, RefreshTokenData>

Returns a clone of all non-expired tokens in the store.

Expired tokens are filtered out but not removed from the underlying map. Call TokenStore::cleanup to actually evict them.

§Examples
use chrono::{Duration, Utc};
use actix_jwt::store::InMemoryRefreshTokenStore;
use actix_jwt::core::TokenStore;

let store = InMemoryRefreshTokenStore::new();
let expiry = Utc::now() + Duration::hours(1);
store.set("a", serde_json::json!(1), expiry).await.unwrap();

let all = store.get_all().await;
assert_eq!(all.len(), 1);
Source

pub async fn clear(&self)

Removes all tokens from the store (including non-expired ones).

§Examples
use chrono::{Duration, Utc};
use actix_jwt::store::InMemoryRefreshTokenStore;
use actix_jwt::core::TokenStore;

let store = InMemoryRefreshTokenStore::new();
store.set("x", serde_json::json!(1), Utc::now() + Duration::hours(1)).await.unwrap();
store.clear().await;
assert_eq!(store.count().await.unwrap(), 0);

Trait Implementations§

Source§

impl Default for InMemoryRefreshTokenStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl TokenStore for InMemoryRefreshTokenStore

Source§

fn set<'life0, 'life1, 'async_trait>( &'life0 self, token: &'life1 str, user_data: Value, expiry: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<(), JwtError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Stores a refresh token with associated user data and expiration.

§Errors

Returns JwtError::TokenEmpty if token is an empty string.

Source§

fn get<'life0, 'life1, 'async_trait>( &'life0 self, token: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Value, JwtError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Retrieves user data for the given refresh token.

Performs lazy cleanup: if the token exists but is expired it is removed from the store and JwtError::RefreshTokenNotFound is returned.

§Errors
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, token: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), JwtError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Removes a refresh token from storage.

Silently succeeds if the token is empty or does not exist.

Source§

fn cleanup<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<usize, JwtError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Removes all expired tokens from the store.

Returns the number of entries that were evicted.

Source§

fn count<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<usize, JwtError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Returns the total number of tokens in the store including expired ones.

To get only valid tokens use get_all.

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> 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, 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