mirror of
https://gitlab.com/megazordpobeda/DataRush.git
synced 2026-05-23 13:17:10 +00:00
fix: incorrect token 500
This commit is contained in:
@@ -5,15 +5,19 @@ import jwt
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from ninja.security import HttpBearer
|
from ninja.security import HttpBearer
|
||||||
|
from redis.exceptions import AuthorizationError
|
||||||
|
|
||||||
from apps.user.models import User
|
from apps.user.models import User
|
||||||
|
|
||||||
|
|
||||||
class BearerAuth(HttpBearer):
|
class BearerAuth(HttpBearer):
|
||||||
def authenticate(self, request: HttpRequest, token: str) -> Any | None:
|
def authenticate(self, request: HttpRequest, token: str) -> Any | None:
|
||||||
|
try:
|
||||||
data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
|
data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
|
||||||
if data["exp"] < datetime.datetime.now().timestamp():
|
if data["exp"] < datetime.datetime.now().timestamp():
|
||||||
return None
|
return None
|
||||||
|
except Exception:
|
||||||
|
raise AuthorizationError
|
||||||
|
|
||||||
user = User.objects.get(id=data["id"])
|
user = User.objects.get(id=data["id"])
|
||||||
return user
|
return user
|
||||||
|
|||||||
Reference in New Issue
Block a user