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
impl InMemoryRefreshTokenStore
Sourcepub async fn get_all(&self) -> HashMap<String, RefreshTokenData>
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);Sourcepub async fn clear(&self)
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
impl Default for InMemoryRefreshTokenStore
Source§impl TokenStore for InMemoryRefreshTokenStore
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,
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,
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
JwtError::TokenEmpty- empty token string.JwtError::RefreshTokenNotFound- token absent or expired.
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,
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.