You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

redis-jwt-auth

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-jwt-auth

A FastAPI Middleware for Secure JWT Authentication with Redis Caching

0.1.1
pipPyPI
Maintainers
1

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

  • Initialize Middleware in FastAPI

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 )

  • Protect Routes

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']}!"}

  • Redis Caching Structure

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

Did you know?

Socket

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.

Install

Related posts