Added auth

This commit is contained in:
2022-05-29 00:31:32 +02:00
parent 36bb9eeefa
commit 23d514050d
19 changed files with 815 additions and 59 deletions

27
api/schema/permissions.py Normal file
View File

@@ -0,0 +1,27 @@
import typing
from fastapi import Request
from strawberry.permission import BasePermission
from api.token import decode_user_token
from api.token import token_from_headers
if typing.TYPE_CHECKING:
from strawberry.types import Info
class IsAuthenticated(BasePermission):
message = "User is not authenticated"
async def has_permission(self, source: typing.Any, info: "Info", **kwargs) -> bool:
req: Request = info.context["request"]
_, auth_token = token_from_headers(req.headers)
if len(auth_token) == 0:
return False
try:
decode_user_token(auth_token)
return True
except Exception:
return False