2024-01-25 09:25:52 +00:00
|
|
|
import logging
|
2023-10-03 14:15:17 +00:00
|
|
|
from functools import wraps
|
2024-01-25 09:25:52 +00:00
|
|
|
|
2023-12-18 07:17:50 +00:00
|
|
|
from aiohttp import ClientSession
|
|
|
|
from starlette.exceptions import HTTPException
|
2023-11-28 09:05:39 +00:00
|
|
|
|
2023-12-19 17:20:37 +00:00
|
|
|
from services.core import get_author_by_user
|
2024-04-18 10:47:01 +00:00
|
|
|
from services.logger import root_logger as logger
|
2023-10-14 12:59:43 +00:00
|
|
|
from settings import AUTH_URL
|
2023-10-03 14:15:17 +00:00
|
|
|
|
2024-01-23 20:13:49 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2023-10-03 15:29:56 +00:00
|
|
|
|
2024-01-23 21:13:14 +00:00
|
|
|
|
2024-04-08 06:30:57 +00:00
|
|
|
async def check_auth(req):
|
|
|
|
logger.debug("checking auth...")
|
|
|
|
user_id = ""
|
2024-01-24 12:26:16 +00:00
|
|
|
try:
|
2024-04-08 06:30:57 +00:00
|
|
|
token = req.headers.get("Authorization")
|
2024-01-24 12:26:16 +00:00
|
|
|
if token:
|
|
|
|
# Logging the authentication token
|
2024-04-08 06:30:57 +00:00
|
|
|
query_name = "validate_jwt_token"
|
|
|
|
operation = "ValidateToken"
|
2024-01-24 12:26:16 +00:00
|
|
|
headers = {
|
2024-04-08 06:30:57 +00:00
|
|
|
"Content-Type": "application/json",
|
2024-01-24 12:26:16 +00:00
|
|
|
}
|
2023-12-13 22:08:47 +00:00
|
|
|
|
2024-01-24 12:26:16 +00:00
|
|
|
variables = {
|
2024-04-08 06:30:57 +00:00
|
|
|
"params": {
|
|
|
|
"token_type": "access_token",
|
|
|
|
"token": token,
|
2024-01-24 12:26:16 +00:00
|
|
|
}
|
2023-12-13 22:08:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 12:26:16 +00:00
|
|
|
gql = {
|
2024-04-08 06:30:57 +00:00
|
|
|
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
|
|
|
"variables": variables,
|
|
|
|
"operationName": operation,
|
2024-01-24 12:26:16 +00:00
|
|
|
}
|
2023-12-18 07:17:50 +00:00
|
|
|
# Asynchronous HTTP request to the authentication server
|
|
|
|
async with ClientSession() as session:
|
2024-02-17 08:52:26 +00:00
|
|
|
async with session.post(
|
|
|
|
AUTH_URL, json=gql, headers=headers
|
|
|
|
) as response:
|
2023-12-13 22:08:47 +00:00
|
|
|
if response.status == 200:
|
|
|
|
data = await response.json()
|
2024-04-08 06:30:57 +00:00
|
|
|
errors = data.get("errors")
|
2023-12-13 22:08:47 +00:00
|
|
|
if errors:
|
2024-04-08 06:30:57 +00:00
|
|
|
logger.error(f"{errors}")
|
2023-12-13 22:08:47 +00:00
|
|
|
else:
|
2024-02-17 08:52:26 +00:00
|
|
|
user_id = (
|
2024-04-08 06:30:57 +00:00
|
|
|
data.get("data", {})
|
2024-02-17 08:52:26 +00:00
|
|
|
.get(query_name, {})
|
2024-04-08 06:30:57 +00:00
|
|
|
.get("claims", {})
|
|
|
|
.get("sub")
|
2024-02-17 08:52:26 +00:00
|
|
|
)
|
2024-04-08 06:30:57 +00:00
|
|
|
logger.info(f"got user_id: {user_id}")
|
2023-12-18 07:17:50 +00:00
|
|
|
return user_id
|
2024-01-24 12:26:16 +00:00
|
|
|
except Exception as e:
|
|
|
|
# Handling and logging exceptions during authentication check
|
|
|
|
logger.error(e)
|
2023-12-17 23:14:02 +00:00
|
|
|
|
2023-12-18 07:17:50 +00:00
|
|
|
if not user_id:
|
2024-04-08 06:30:57 +00:00
|
|
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
async def decorated_function(*args, **kwargs):
|
|
|
|
info = args[1]
|
|
|
|
context = info.context
|
2024-04-08 06:30:57 +00:00
|
|
|
req = context.get("request")
|
2023-12-18 07:17:50 +00:00
|
|
|
user_id = await check_auth(req)
|
|
|
|
if user_id:
|
2024-04-08 06:30:57 +00:00
|
|
|
context["user_id"] = user_id.strip()
|
2023-12-19 17:20:37 +00:00
|
|
|
author = get_author_by_user(user_id)
|
2024-04-08 06:30:57 +00:00
|
|
|
if author and "id" in author:
|
|
|
|
context["author_id"] = author["id"]
|
2024-01-24 12:26:16 +00:00
|
|
|
else:
|
|
|
|
logger.debug(author)
|
2024-04-08 06:30:57 +00:00
|
|
|
HTTPException(status_code=401, detail="Unauthorized")
|
2023-10-03 14:15:17 +00:00
|
|
|
return await f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|