r-token
r-token is a small token authentication helper for Rust and actix-web.
It provides two token managers:
- In-memory:
RTokenManagerstores tokens in memory with an expiration timestamp. - Redis/Valkey (optional):
RTokenRedisManagerstores tokens in Redis with TTL.
For actix-web, r-token follows a “parameter-as-authentication” style: add RUser to handler parameters, and the request is authenticated automatically via the Actix extractor mechanism.
Features
- Zero boilerplate: no custom middleware required for basic header auth.
- Extractor-first: declaring
RUserprotects the route. - Thread-safe, shared state:
RTokenManagerisCloneand shares an in-memory store. - TTL support:
- In-memory: tokens expire based on a per-login TTL (seconds).
- Redis/Valkey: expiration is enforced by Redis TTL (seconds).
- Redis/Valkey backend (optional):
RTokenRedisManagerstoresuser_idby token key.
Security notes
- This library implements bearer-token authentication. Always use HTTPS in production.
- Token strings grant access. Treat them like passwords: do not log them, do not store them in plaintext client storage without careful threat modeling.
- The Redis backend stores
user_idas the Redis value. If you need stronger protection against Redis data disclosure, consider storing a hashed token (not currently implemented by this crate).
Status
This project is in active development. Review the source code and tests before adopting it in security-sensitive environments.
This project has not yet released version 1.0.0 and is not recommended for use in any production-level projects. The current state should be considered experimental and potentially unstable.
Please note that this project has not been officially released. Both the documentation on docs.rs and this README file may not always reflect the latest changes in the source code. Always refer to the actual source code for the most accurate information. This documentation inconsistency will be resolved upon official release.
Installation
Add r-token to your Cargo.toml:
[]
= "0.1.4"
Feature flags
r-token uses Cargo features to keep dependencies optional:
actix(default): enables theRUserextractor and actix-web integration.redis: enables Redis/Valkey support via therediscrate.redis-actix: convenience feature =redis+actix.
Examples:
[]
= { = "0.1.4", = false }
[]
= { = "0.1.4", = ["redis-actix"] }
Authorization header
The RUser extractor (and the Redis example server) reads the token from Authorization and supports:
Authorization: <token>
Authorization: Bearer <token>
API overview
Core types:
RTokenManager(always available): issues and revokes tokens in memory.RTokenError(always available): error type used by in-memory manager.
Actix integration (requires actix, enabled by default):
RUser:actix_web::FromRequestextractor that validatesAuthorization.
Redis backend (requires redis):
RTokenRedisManager: issues, validates, and revokes tokens backed by Redis/Valkey.
In-memory usage (actix-web)
1. Add endpoints
No manual header parsing is needed. Inject RUser into protected handlers.
use ;
use ;
async
async
async
2. Register and Run
Initialize RTokenManager and register it with your Actix application.
use ;
use RTokenManager;
async
Behavioral details
In-memory manager:
RTokenManager::login(user_id, ttl_seconds)returns a UUID v4 token string.- Expiration is tracked by storing an absolute expiration timestamp (milliseconds since Unix epoch).
RTokenManager::logout(token)is idempotent: revoking a non-existent token is treated as success.
Actix extractor:
- On success,
RUserprovidesidand the rawtoken. - Failure modes:
401 Unauthorized: missing token, invalid token, or expired token.500 Internal Server Error: token manager missing fromapp_data, or internal mutex poisoned.
Redis manager:
RTokenRedisManager::login(user_id, ttl_seconds)storesprefix + tokenas the key anduser_idas the value, with Redis TTL set tottl_seconds.validate(token)returnsOk(None)when the key is absent (revoked or expired).logout(token)deletes the key and is idempotent.
Redis/Valkey usage
If you want token persistence and Redis-managed TTL expiration, enable redis (or redis-actix) and use RTokenRedisManager.
You also need a Tokio runtime in your application (do not rely on transitive dependencies):
[]
= { = "1", = ["macros", "rt-multi-thread"] }
use RTokenRedisManager;
async
Usage examples (curl)
Login
# Response: 550e8400-e29b-41d4-a716-446655440000
Access Protected Resource
# Without Token -> 401 Unauthorized
# With Token -> 200 OK
Example servers in this repo
In-memory (actix-web)
Redis/Valkey (actix-web)
Environment variables:
REDIS_URL(default:redis://127.0.0.1/)R_TOKEN_PREFIX(default:r_token:token:)
REDIS_URL=redis://127.0.0.1/
Roadmap
- In-memory token management + extractor
- Token expiration (TTL)
- Redis/Valkey backend token storage (optional)
- Role-based access control (RBAC)
- Cookie support
License
MIT