-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat[bckend-middleware]:Implemented custom middleware to check for blacklisted access tokens in Redis. #107
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from rest_framework_simplejwt.tokens import AccessToken | ||
from rest_framework import status | ||
from kalvi.api.views.auth import get_redis_connection | ||
from django.http import JsonResponse | ||
|
||
class CustomOutstandingTokenMiddleware: | ||
""" | ||
This middleware checks for blacklisted tokens in Redis. It does not perform any token validation. | ||
""" | ||
def __init__(self, get_response=None): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
try: | ||
auth_header = request.headers.get('Authorization') | ||
if not auth_header: | ||
return self.get_response(request) | ||
token = auth_header.split()[1] | ||
token_obj = str(AccessToken(token)) | ||
redis_conn = get_redis_connection() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a separate class for Redis in a separate file and implement the logic there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should be separate file for redis config too and blacklisting refresh token too, I am thinking raising seperate PR for this , kinda chore/refactor tasks. I will put everything related to redis at one place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is your view on this? |
||
# Check if the token is blacklisted in Redis | ||
if redis_conn.exists(token_obj): | ||
return JsonResponse({'error': "Access token is blacklisted"}, status=status.HTTP_401_UNAUTHORIZED) | ||
else: | ||
return self.get_response(request) | ||
except Exception: | ||
return self.get_response(request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a separate class for Redis in a separate file and implement the logic there.