unmiddlewared

This commit is contained in:
2023-10-04 23:42:39 +03:00
parent 2db81462d0
commit e76f924b2d
10 changed files with 75 additions and 96 deletions

View File

@@ -11,9 +11,7 @@ from settings import AUTH_URL
from orm.author import Author
class AuthUser(BaseModel):
user_id: Optional[int]
username: Optional[str]
INTERNAL_AUTH_SERVER = "v2.discours" in AUTH_URL
class AuthCredentials(BaseModel):
@@ -26,17 +24,14 @@ class AuthCredentials(BaseModel):
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),
AuthUser(user_id=user_id),
)
return AuthCredentials(user_id=user_id, logged_in=logged_in), user_id
async def check_auth(req):
token = req.headers.get("Authorization")
gql = (
{"mutation": "{ getSession { user { id } } }"}
if "v2" in AUTH_URL
if INTERNAL_AUTH_SERVER
else {"query": "{ session { user { id } } }"}
)
headers = {"Authorization": token, "Content-Type": "application/json"}
@@ -84,8 +79,9 @@ def auth_request(f):
if not is_authenticated:
raise HTTPError("please, login first")
else:
author_id = await author_id_by_user_id(user_id)
req["author_id"] = author_id
req["author_id"] = (
user_id if INTERNAL_AUTH_SERVER else await author_id_by_user_id(user_id)
)
return await f(*args, **kwargs)
return decorated_function