db-removed
This commit is contained in:
@@ -1,32 +1,12 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
from functools import wraps
|
||||
from starlette.authentication import AuthenticationBackend
|
||||
from starlette.requests import HTTPConnection
|
||||
from httpx import AsyncClient
|
||||
from httpx._exceptions import HTTPError
|
||||
from services.db import local_session
|
||||
from settings import AUTH_URL
|
||||
from orm.author import Author
|
||||
|
||||
|
||||
INTERNAL_AUTH_SERVER = "v2.discours" in AUTH_URL
|
||||
|
||||
|
||||
class AuthCredentials(BaseModel):
|
||||
user_id: Optional[int] = None
|
||||
scopes: Optional[dict] = {}
|
||||
logged_in: bool = False
|
||||
error_message: str = ""
|
||||
|
||||
|
||||
class JWTAuthenticate(AuthenticationBackend):
|
||||
async def authenticate(self, request: HTTPConnection):
|
||||
logged_in, user_id = await check_auth(request)
|
||||
return AuthCredentials(user_id=user_id, logged_in=logged_in), user_id
|
||||
|
||||
|
||||
async def check_auth(req):
|
||||
token = req.headers.get("Authorization")
|
||||
gql = (
|
||||
@@ -45,12 +25,6 @@ async def check_auth(req):
|
||||
return is_authenticated, user_id
|
||||
|
||||
|
||||
async def author_id_by_user_id(user_id):
|
||||
async with local_session() as session:
|
||||
author = session(Author).where(Author.user == user_id).first()
|
||||
return author.id
|
||||
|
||||
|
||||
def login_required(f):
|
||||
@wraps(f)
|
||||
async def decorated_function(*args, **kwargs):
|
||||
@@ -62,8 +36,7 @@ def login_required(f):
|
||||
raise Exception("You are not logged in")
|
||||
else:
|
||||
# Добавляем author_id в контекст
|
||||
author_id = await author_id_by_user_id(user_id)
|
||||
context["author_id"] = author_id
|
||||
context["author_id"] = user_id
|
||||
|
||||
# Если пользователь аутентифицирован, выполняем резолвер
|
||||
return await f(*args, **kwargs)
|
||||
@@ -79,9 +52,7 @@ def auth_request(f):
|
||||
if not is_authenticated:
|
||||
raise HTTPError("please, login first")
|
||||
else:
|
||||
req["author_id"] = (
|
||||
user_id if INTERNAL_AUTH_SERVER else await author_id_by_user_id(user_id)
|
||||
)
|
||||
req["author_id"] = user_id
|
||||
return await f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
|
Reference in New Issue
Block a user