Added auth
This commit is contained in:
27
api/schema/permissions.py
Normal file
27
api/schema/permissions.py
Normal 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
|
||||
Reference in New Issue
Block a user