2023-10-03 14:15:17 +00:00
|
|
|
from functools import wraps
|
2024-01-25 09:25:52 +00:00
|
|
|
|
2024-04-18 11:28:10 +00:00
|
|
|
import httpx
|
2023-12-18 07:17:50 +00:00
|
|
|
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
|
|
|
|
2023-10-03 15:29:56 +00:00
|
|
|
|
2024-04-18 11:28:10 +00:00
|
|
|
async def request_data(gql, headers=None):
|
|
|
|
if headers is None:
|
|
|
|
headers = {"Content-Type": "application/json"}
|
2024-01-24 12:26:16 +00:00
|
|
|
try:
|
2024-04-18 11:28:10 +00:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
response = await client.post(AUTH_URL, json=gql, headers=headers)
|
|
|
|
if response.status_code == 200:
|
|
|
|
data = response.json()
|
|
|
|
errors = data.get("errors")
|
|
|
|
if errors:
|
|
|
|
logger.error(f"HTTP Errors: {errors}")
|
|
|
|
else:
|
|
|
|
return data
|
2024-01-24 12:26:16 +00:00
|
|
|
except Exception as e:
|
|
|
|
# Handling and logging exceptions during authentication check
|
2024-04-18 11:28:10 +00:00
|
|
|
logger.error(f"request_data error: {e}")
|
|
|
|
return None
|
|
|
|
|
2023-12-17 23:14:02 +00:00
|
|
|
|
2024-04-18 11:28:10 +00:00
|
|
|
async def check_auth(req):
|
|
|
|
token = req.headers.get("Authorization")
|
|
|
|
user_id = ""
|
|
|
|
user_roles = []
|
|
|
|
if token:
|
|
|
|
# Logging the authentication token
|
|
|
|
logger.debug(f"{token}")
|
|
|
|
query_name = "validate_jwt_token"
|
|
|
|
operation = "ValidateToken"
|
|
|
|
variables = {"params": {"token_type": "access_token", "token": token}}
|
|
|
|
|
|
|
|
gql = {
|
|
|
|
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{"
|
|
|
|
+ f"{query_name}(params: $params) {{ is_valid claims }} "
|
|
|
|
+ "}",
|
|
|
|
"variables": variables,
|
|
|
|
"operationName": operation,
|
|
|
|
}
|
|
|
|
data = await request_data(gql)
|
|
|
|
if data:
|
|
|
|
logger.debug(data)
|
|
|
|
user_data = data.get("data", {}).get(query_name, {}).get("claims", {})
|
|
|
|
user_id = user_data.get("sub", "")
|
|
|
|
user_roles = user_data.get("allowed_roles", [])
|
|
|
|
return user_id, user_roles
|
2023-10-03 14:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
async def decorated_function(*args, **kwargs):
|
|
|
|
info = args[1]
|
2024-04-18 11:28:10 +00:00
|
|
|
req = info.context.get("request")
|
|
|
|
authorized = await check_auth(req)
|
|
|
|
if authorized:
|
|
|
|
logger.info(authorized)
|
|
|
|
user_id, user_roles = authorized
|
|
|
|
if user_id and user_roles:
|
|
|
|
logger.info(f" got {user_id} roles: {user_roles}")
|
|
|
|
info.context["user_id"] = user_id.strip()
|
2023-10-03 14:15:17 +00:00
|
|
|
return await f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|
2024-04-18 11:28:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def auth_request(f):
|
|
|
|
@wraps(f)
|
|
|
|
async def decorated_function(*args, **kwargs):
|
|
|
|
req = args[0]
|
|
|
|
authorized = await check_auth(req)
|
|
|
|
if authorized:
|
|
|
|
user_id, user_roles = authorized
|
|
|
|
if user_id and user_roles:
|
|
|
|
logger.info(f" got {user_id} roles: {user_roles}")
|
|
|
|
req["user_id"] = user_id.strip()
|
|
|
|
author = get_author_by_user(user_id)
|
|
|
|
if author and "id" in author:
|
|
|
|
req["author_id"] = author["id"]
|
|
|
|
else:
|
|
|
|
logger.debug(author)
|
|
|
|
HTTPException(status_code=404, detail="Cannot find author profile")
|
|
|
|
return await f(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
|
|
|
|
|
|
|
return decorated_function
|