2023-10-26 20:38:31 +00:00
|
|
|
from functools import wraps
|
2023-10-26 21:07:35 +00:00
|
|
|
from typing import Optional, Tuple
|
|
|
|
|
2023-10-26 20:38:31 +00:00
|
|
|
from graphql.type import GraphQLResolveInfo
|
2023-10-30 21:00:55 +00:00
|
|
|
from sqlalchemy.orm import exc, joinedload
|
2023-10-26 20:38:31 +00:00
|
|
|
from starlette.authentication import AuthenticationBackend
|
|
|
|
from starlette.requests import HTTPConnection
|
2023-10-26 21:07:35 +00:00
|
|
|
|
|
|
|
from auth.credentials import AuthCredentials, AuthUser
|
2024-11-01 12:06:21 +00:00
|
|
|
from auth.exceptions import OperationNotAllowed
|
2023-10-26 21:07:35 +00:00
|
|
|
from auth.tokenstorage import SessionToken
|
2024-11-01 12:06:21 +00:00
|
|
|
from auth.usermodel import Role, User
|
|
|
|
from services.db import local_session
|
2023-10-30 21:00:55 +00:00
|
|
|
from settings import SESSION_TOKEN_HEADER
|
2022-09-14 04:42:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JWTAuthenticate(AuthenticationBackend):
|
2024-11-01 12:06:21 +00:00
|
|
|
async def authenticate(self, request: HTTPConnection) -> Optional[Tuple[AuthCredentials, AuthUser]]:
|
2022-11-22 03:11:26 +00:00
|
|
|
if SESSION_TOKEN_HEADER not in request.headers:
|
2023-10-30 21:00:55 +00:00
|
|
|
return AuthCredentials(scopes={}), AuthUser(user_id=None, username="")
|
2022-09-14 04:42:31 +00:00
|
|
|
|
2022-11-24 14:31:52 +00:00
|
|
|
token = request.headers.get(SESSION_TOKEN_HEADER)
|
|
|
|
if not token:
|
|
|
|
print("[auth.authenticate] no token in header %s" % SESSION_TOKEN_HEADER)
|
2024-11-01 12:06:21 +00:00
|
|
|
return AuthCredentials(scopes={}, error_message=str("no token")), AuthUser(user_id=None, username="")
|
2022-11-22 03:11:26 +00:00
|
|
|
|
2023-10-30 21:00:55 +00:00
|
|
|
if len(token.split(".")) > 1:
|
2023-02-20 16:09:55 +00:00
|
|
|
payload = await SessionToken.verify(token)
|
2023-07-13 14:58:22 +00:00
|
|
|
|
2023-02-20 16:09:55 +00:00
|
|
|
with local_session() as session:
|
|
|
|
try:
|
|
|
|
user = (
|
2023-10-30 21:00:55 +00:00
|
|
|
session.query(User)
|
|
|
|
.options(
|
2023-02-20 16:09:55 +00:00
|
|
|
joinedload(User.roles).options(joinedload(Role.permissions)),
|
2023-10-30 21:00:55 +00:00
|
|
|
joinedload(User.ratings),
|
|
|
|
)
|
|
|
|
.filter(User.id == payload.user_id)
|
|
|
|
.one()
|
2023-02-20 16:09:55 +00:00
|
|
|
)
|
2022-11-28 22:58:23 +00:00
|
|
|
|
2023-02-20 16:09:55 +00:00
|
|
|
scopes = {} # TODO: integrate await user.get_permission()
|
2022-11-28 22:58:23 +00:00
|
|
|
|
2023-02-20 16:09:55 +00:00
|
|
|
return (
|
2023-10-30 21:00:55 +00:00
|
|
|
AuthCredentials(user_id=payload.user_id, scopes=scopes, logged_in=True),
|
|
|
|
AuthUser(user_id=user.id, username=""),
|
2023-02-20 16:09:55 +00:00
|
|
|
)
|
|
|
|
except exc.NoResultFound:
|
|
|
|
pass
|
2022-11-28 22:58:23 +00:00
|
|
|
|
2024-11-01 12:06:21 +00:00
|
|
|
return AuthCredentials(scopes={}, error_message=str("Invalid token")), AuthUser(user_id=None, username="")
|
2022-09-14 04:42:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login_required(func):
|
|
|
|
@wraps(func)
|
|
|
|
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-11-28 14:15:54 +00:00
|
|
|
if not auth or not auth.logged_in:
|
2023-10-30 21:00:55 +00:00
|
|
|
return {"error": "Please login first"}
|
2022-09-14 04:42:31 +00:00
|
|
|
return await func(parent, info, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrap
|
2022-11-24 08:27:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def permission_required(resource, operation, func):
|
|
|
|
@wraps(func)
|
|
|
|
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
2024-11-01 12:06:21 +00:00
|
|
|
print("[auth.authenticate] permission_required for %r with info %r" % (func, info)) # debug only
|
2022-11-24 08:27:01 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
if not auth.logged_in:
|
2022-12-01 08:12:48 +00:00
|
|
|
raise OperationNotAllowed(auth.error_message or "Please login")
|
2022-11-24 08:27:01 +00:00
|
|
|
|
2022-11-25 22:35:42 +00:00
|
|
|
# TODO: add actual check permission logix here
|
2022-11-24 08:27:01 +00:00
|
|
|
|
|
|
|
return await func(parent, info, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrap
|
2024-11-12 14:56:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login_accepted(func):
|
|
|
|
@wraps(func)
|
|
|
|
async def wrap(parent, info: GraphQLResolveInfo, *args, **kwargs):
|
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
|
|
|
|
# Если есть авторизация, добавляем данные автора в контекст
|
|
|
|
if auth and auth.logged_in:
|
|
|
|
# Существующие данные auth остаются
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Очищаем данные автора из контекста если авторизация отсутствует
|
|
|
|
info.context["author"] = None
|
|
|
|
info.context["user_id"] = None
|
|
|
|
|
|
|
|
return await func(parent, info, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrap
|