completed authentication serializers

This commit is contained in:
2024-03-06 00:38:41 +03:00
parent 7cf8cc4a6b
commit 23c3918697
13 changed files with 118 additions and 2 deletions
+26
View File
@@ -0,0 +1,26 @@
import jwt
from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import authentication, exceptions
class JWTAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
auth_data = authentication.get_authorization_header(request)
if not auth_data:
return None
prefix, token = auth_data.decode("utf-8").split(" ")
try:
payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms="HS256")
user = User.objects.get(username=payload["username"])
return (user, token)
except jwt.DecodeError as err:
raise exceptions.AuthenticationFailed("Your token is invalid, please login")
except jwt.ExpiredSignatureError as err:
raise exceptions.AuthenticationFailed("Your token is expired, login")
return super().authenticate(request)