
Security News
The Nightmare Before Deployment
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.
redis-jwt-auth
Advanced tools
redis-jwt-auth
A FastAPI Middleware for Secure JWT Authentication with Redis Caching
Overview
redis-jwt-auth is a FastAPI middleware that provides JWT authentication with Redis-based token caching. It enhances performance by reducing database queries and ensures single active sessions per user. The package supports token rotation, secure session management, and seamless integration into FastAPI applications.
Key Features
✅ JWT Token Validation – Decodes and verifies JWT tokens efficiently.
✅ Redis Caching – Stores access tokens, refresh tokens, and user objects in Redis for fast retrieval.
✅ Single Active Session – Ensures a user is logged in from only one device at a time.
✅ Token Rotation (Optional) – Enhances security by refreshing tokens on every request.
✅ Soft-Delete Support – Ensures inactive or deleted users cannot authenticate.
✅ Easy Middleware Integration – Plug and play support for FastAPI applications.
Installation
Install redis-jwt-auth using pip:
pip install redis-jwt-auth
Usage
from fastapi import FastAPI from redis_jwt_auth.middleware import TokenMiddleware
app = FastAPI()
app.add_middleware( TokenMiddleware, redis_host="localhost", redis_port=6379, secret_key="your_jwt_secret", enable_token_rotation=True # Set to False if you don't want token rotation )
Use the middleware to protect routes automatically. The validated user will be available in request.state.user.
from fastapi import Depends, Request
@app.get("/protected-route") async def protected_route(request: Request): user = request.state.user # Retrieved from Redis cache return {"message": f"Welcome, {user['username']}!"}
Action
Description
On login
Tokens and user details are stored in Redis.
On request
JWT is validated, checked against Redis, and user details are fetched.
On logout
Tokens are deleted from Redis, ensuring session invalidation.
Configuration Options
Parameter
Type
Default
Description
redis_host
str
"localhost"
Redis server hostname
redis_port
int
6379
Redis server port
secret_key
str
Required
Secret key for JWT validation
enable_token_rotation
bool
False
Enables automatic token refresh
Future Enhancements
🚀 Support multiple active sessions per user
🚀 Role-based access control (RBAC)
🚀 Integration with OAuth & third-party authentication
🚀 Performance monitoring with Redis cache metrics
Contributing
Want to improve redis-jwt-auth? Feel free to submit issues or pull requests in the GitHub repository!
FAQs
A FastAPI Middleware for Secure JWT Authentication with Redis Caching
We found that redis-jwt-auth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Season’s greetings from Socket, and here’s to a calm end of year: clean dependencies, boring pipelines, no surprises.

Research
/Security News
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.