-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.py
More file actions
25 lines (19 loc) · 774 Bytes
/
middleware.py
File metadata and controls
25 lines (19 loc) · 774 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import django.http
import rest_framework_simplejwt.authentication
class TokenVersionMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
auth = rest_framework_simplejwt.authentication.JWTAuthentication()
auth_result = auth.authenticate(request)
if auth_result is None:
return self.get_response(request)
user, token = auth_result
if user:
token_version = token.payload.get('token_version', 0)
if token_version != user.token_version:
return django.http.JsonResponse(
{'error': 'Token invalid'},
status=401,
)
return self.get_response(request)