added competition endpoints

This commit is contained in:
Андрей Сумин
2025-03-01 01:10:08 +03:00
parent 65c86d2a9c
commit 7791a57b88
25 changed files with 209 additions and 64 deletions
+29
View File
@@ -0,0 +1,29 @@
import datetime
from typing import Any
import jwt
from django.conf import settings
from django.http import HttpRequest
from ninja.security import HttpBearer
from apps.user.models import User
class BearerAuth(HttpBearer):
def authenticate(self, request: HttpRequest, token: str) -> Any | None:
data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
if data["exp"] < datetime.datetime.now().timestamp():
return None
user = User.objects.get(id=data["id"])
return user
@staticmethod
def generate_jwt(user: User) -> str:
data = {
"exp": (
datetime.datetime.now() + datetime.timedelta(days=365)
).timestamp(),
"id": str(user.id),
}
return jwt.encode(data, settings.SECRET_KEY, algorithm="HS256")