Skip to content

Commit a08ac17

Browse files
refactor(auth): Move token version check to authentication class.
- Remove TokenVersionMiddleware - Implement CustomJWTAuthentication with same validation logic - No functional changes for end users
1 parent 95d0dbe commit a08ac17

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

promo_code/promo_code/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def load_bool(name, default):
5050

5151
REST_FRAMEWORK = {
5252
'DEFAULT_AUTHENTICATION_CLASSES': [
53-
'rest_framework_simplejwt.authentication.JWTAuthentication',
53+
'user.authentication.CustomJWTAuthentication'
5454
],
5555
}
5656

@@ -108,7 +108,6 @@ def load_bool(name, default):
108108
'django.contrib.auth.middleware.AuthenticationMiddleware',
109109
'django.contrib.messages.middleware.MessageMiddleware',
110110
'django.middleware.clickjacking.XFrameOptionsMiddleware',
111-
'user.middleware.TokenVersionMiddleware',
112111
]
113112

114113
ROOT_URLCONF = 'promo_code.urls'

promo_code/user/authentication.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import rest_framework_simplejwt.exceptions
2+
import rest_framework_simplejwt.authentication
3+
4+
5+
class CustomJWTAuthentication(rest_framework_simplejwt.authentication.JWTAuthentication):
6+
def authenticate(self, request):
7+
try:
8+
user_token = super().authenticate(request)
9+
except rest_framework_simplejwt.exceptions.InvalidToken:
10+
raise rest_framework_simplejwt.exceptions.AuthenticationFailed('Token is invalid or expired')
11+
12+
if user_token:
13+
user, token = user_token
14+
if token.payload.get('token_version') != user.token_version:
15+
raise rest_framework_simplejwt.exceptions.AuthenticationFailed('Token invalid')
16+
17+
return user_token

promo_code/user/middleware.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)