-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[bckend-middleware]:Implemented custom middleware to check for bl…
…acklisted access tokens in Redis.
- Loading branch information
Showing
4 changed files
with
41 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters