2023-08-14 02:08:08 +08:00
|
|
|
import logging
|
2023-09-12 18:45:22 +08:00
|
|
|
from dataclasses import dataclass
|
2023-09-11 13:02:05 +08:00
|
|
|
from typing import Any
|
2023-08-14 02:08:08 +08:00
|
|
|
|
2023-09-12 18:45:22 +08:00
|
|
|
from graphql import GraphQLError
|
|
|
|
from jose import jwt
|
|
|
|
from pydantic import ValidationError
|
2023-08-14 02:08:08 +08:00
|
|
|
from sanic.request import Request
|
2023-09-12 18:45:22 +08:00
|
|
|
from strawberry.http.temporal_response import TemporalResponse
|
|
|
|
from strawberry.types.info import Info as StrawberryInfo, RootValueType
|
2023-08-14 02:08:08 +08:00
|
|
|
|
|
|
|
from apps.common import schemas as core_schemas # noqa
|
|
|
|
from apps.user import models # noqa
|
2023-09-12 18:45:22 +08:00
|
|
|
from apps.user.models import User
|
2023-08-14 02:08:08 +08:00
|
|
|
from core import security # noqa
|
|
|
|
from core.config import settings # noqa
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-09-12 18:45:22 +08:00
|
|
|
@dataclass
|
|
|
|
class InfoContext:
|
|
|
|
user: User
|
|
|
|
request: Request
|
|
|
|
response: TemporalResponse
|
2023-09-11 13:02:05 +08:00
|
|
|
|
2023-09-12 18:45:22 +08:00
|
|
|
|
|
|
|
Info = StrawberryInfo[InfoContext, RootValueType]
|
|
|
|
|
|
|
|
|
|
|
|
async def get_current_user(token: str = None) -> models.User | None:
|
2023-08-14 02:08:08 +08:00
|
|
|
if not token:
|
|
|
|
GraphQLError("No auth token")
|
|
|
|
try:
|
|
|
|
payload = jwt.decode(
|
|
|
|
token, settings.SECRET_KEY, algorithms=[security.ALGORITHM]
|
|
|
|
)
|
|
|
|
token_data = core_schemas.TokenPayload(**payload)
|
|
|
|
except (jwt.JWTError, ValidationError) as e:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return await models.User.get(uid=token_data.sub)
|
|
|
|
|
|
|
|
|
2023-09-12 18:45:22 +08:00
|
|
|
async def get_current_active_user(token: str = None) -> models.User | None:
|
2023-08-14 02:08:08 +08:00
|
|
|
current_user = await get_current_user(token=token)
|
|
|
|
if not current_user or not current_user.is_active:
|
|
|
|
return None
|
|
|
|
return current_user
|
|
|
|
|
|
|
|
|
|
|
|
async def get_auth_context(request: Request) -> Any:
|
|
|
|
if "Authorization" in request.headers:
|
|
|
|
authorization = request.headers.get("Authorization", None)
|
|
|
|
if not authorization:
|
|
|
|
return {"user": None}
|
|
|
|
_, credentials = authorization.split()
|
2023-09-11 13:02:05 +08:00
|
|
|
return {"user": await get_current_active_user(credentials)}
|
2023-08-14 02:08:08 +08:00
|
|
|
|
|
|
|
logger.info(f"Context: must authenticate {request}")
|
|
|
|
|
|
|
|
return {"user": None}
|
|
|
|
|
|
|
|
|
|
|
|
async def get_auth_user(request: Request) -> Any:
|
|
|
|
if "Authorization" in request.headers:
|
|
|
|
authorization = request.headers.get("Authorization", None)
|
|
|
|
if not authorization:
|
|
|
|
return {"user": None}
|
|
|
|
_, credentials = authorization.split()
|
|
|
|
return await get_current_active_user(credentials)
|
|
|
|
|
|
|
|
logger.info(f"Context: must authenticate {request}")
|
|
|
|
|
|
|
|
return None
|
2023-09-12 18:45:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
async def get_gql_context(request: Request, response: TemporalResponse) -> InfoContext:
|
|
|
|
auth_ctx = await get_auth_context(request)
|
|
|
|
return InfoContext(**{
|
|
|
|
**auth_ctx,
|
|
|
|
"request": request,
|
|
|
|
"response": response
|
|
|
|
})
|