2021-04-18 18:37:49 +08:00
|
|
|
import logging
|
2022-03-10 14:38:30 +08:00
|
|
|
from typing import Optional, Tuple, Union
|
2022-01-23 07:15:53 +08:00
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
import strawberry # noqa
|
2023-04-10 20:23:31 +08:00
|
|
|
from apps.user.models import User, UserAuth
|
|
|
|
from core.uid_gen import FelicityID
|
2021-09-06 02:05:20 +08:00
|
|
|
|
2021-04-18 18:37:49 +08:00
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-09-06 02:05:20 +08:00
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
@strawberry.type
|
|
|
|
class PageInfo:
|
|
|
|
has_next_page: bool
|
|
|
|
has_previous_page: bool
|
2023-04-25 22:42:39 +08:00
|
|
|
start_cursor: str | None
|
|
|
|
end_cursor: str | None
|
2021-12-30 02:33:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class DeletedItem:
|
2023-03-19 23:21:32 +08:00
|
|
|
uid: FelicityID
|
2021-12-30 02:33:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
2023-04-25 22:20:28 +08:00
|
|
|
class MessagesType:
|
2021-12-30 02:33:08 +08:00
|
|
|
message: str
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class OperationError:
|
|
|
|
error: str
|
2023-04-25 22:42:39 +08:00
|
|
|
suggestion: str | None = ""
|
2022-11-06 20:09:44 +08:00
|
|
|
|
2022-04-14 03:01:13 +08:00
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class OperationSuccess:
|
|
|
|
message: str
|
2021-12-30 02:33:08 +08:00
|
|
|
|
|
|
|
|
2022-01-09 23:59:53 +08:00
|
|
|
DeleteResponse = strawberry.union(
|
|
|
|
"DeleteResponse",
|
|
|
|
(DeletedItem, OperationError),
|
|
|
|
description="Union of possible outcomes when deleting some object",
|
|
|
|
)
|
2021-12-30 02:33:08 +08:00
|
|
|
|
2022-01-09 23:59:53 +08:00
|
|
|
MessageResponse = strawberry.union(
|
|
|
|
"MessageResponse",
|
2023-04-25 22:20:28 +08:00
|
|
|
(MessagesType, OperationError),
|
2022-01-09 23:59:53 +08:00
|
|
|
description="Union of possible outcomes when deleting some object",
|
|
|
|
)
|
2021-12-30 02:33:08 +08:00
|
|
|
|
2023-02-24 08:44:14 +08:00
|
|
|
SuccessErrorResponse = strawberry.union(
|
|
|
|
"SuccessErrorResponse",
|
|
|
|
(OperationSuccess, OperationError),
|
|
|
|
description="Union of possible outcomes when deleting some object",
|
|
|
|
)
|
|
|
|
|
2021-12-30 02:33:08 +08:00
|
|
|
|
2021-04-18 18:37:49 +08:00
|
|
|
def is_authenticated(request):
|
|
|
|
return request.user.is_authenticated
|
|
|
|
|
|
|
|
|
|
|
|
def same_origin(request):
|
2022-01-09 23:59:53 +08:00
|
|
|
return request.headers.get("sec-fetch-site", "unknown") == "same-origin"
|
2021-04-19 03:20:22 +08:00
|
|
|
|
|
|
|
|
2022-11-06 20:09:44 +08:00
|
|
|
async def auth_from_info(info) -> Tuple[bool, Optional[User]]:
|
2022-01-09 23:59:53 +08:00
|
|
|
is_auth = is_authenticated(info.context["request"])
|
2021-09-06 02:05:20 +08:00
|
|
|
|
|
|
|
try:
|
2022-01-09 23:59:53 +08:00
|
|
|
username = info.context["request"].user.username
|
2021-09-06 02:05:20 +08:00
|
|
|
except AttributeError:
|
|
|
|
username = None
|
|
|
|
|
|
|
|
if not username:
|
|
|
|
return False, None
|
|
|
|
|
2021-09-26 16:38:08 +08:00
|
|
|
auth = await UserAuth.get_by_username(username)
|
2021-09-06 02:05:20 +08:00
|
|
|
if not auth:
|
|
|
|
return False, None
|
|
|
|
|
2021-09-26 16:38:08 +08:00
|
|
|
user = await User.get(auth_uid=auth.uid)
|
2021-09-06 02:05:20 +08:00
|
|
|
if not user:
|
|
|
|
return False, None
|
|
|
|
|
|
|
|
return is_auth, user
|
|
|
|
|
|
|
|
|
2023-03-19 23:21:32 +08:00
|
|
|
def verify_user_auth(
|
|
|
|
is_auth: bool = False, user=None, err_msg: str = None
|
|
|
|
) -> Tuple[bool, Optional[OperationError]]:
|
2021-09-06 02:05:20 +08:00
|
|
|
if not is_auth:
|
2023-03-19 23:21:32 +08:00
|
|
|
return False, OperationError(
|
|
|
|
error=f"{err_msg}", suggestion="Try to login again"
|
|
|
|
)
|
2021-09-06 02:05:20 +08:00
|
|
|
|
|
|
|
if not user:
|
2022-11-20 21:20:41 +08:00
|
|
|
return False, OperationError(
|
2021-12-30 02:33:08 +08:00
|
|
|
error="Failed to acquire authenticated user",
|
2022-01-09 23:59:53 +08:00
|
|
|
suggestion="refresh your page. If error persists, logout and login again",
|
2021-12-30 02:33:08 +08:00
|
|
|
)
|
2022-11-20 21:20:41 +08:00
|
|
|
return True, None
|