core/services/auth.py

104 lines
3.8 KiB
Python
Raw Normal View History

2023-10-23 14:47:11 +00:00
from functools import wraps
2023-11-29 07:23:41 +00:00
import aiohttp
from aiohttp.web import HTTPUnauthorized
2023-12-13 20:39:25 +00:00
2023-10-23 14:47:11 +00:00
from settings import AUTH_URL
2023-12-13 20:42:52 +00:00
async def check_auth(req) -> (bool, int | None):
2023-10-23 14:47:11 +00:00
token = req.headers.get("Authorization")
2023-12-12 05:00:46 +00:00
if token:
2023-12-13 20:39:25 +00:00
# Logging the authentication token
2023-12-12 05:00:46 +00:00
print(f"[services.auth] checking auth token: {token}")
2023-12-13 20:39:25 +00:00
query_name = "validate_jwt_token"
2023-12-13 21:47:02 +00:00
opeation = "ValidateTokenå"
2023-12-13 20:39:25 +00:00
headers = {
"Content-Type": "application/json",
}
variables = {
"params": {
"token_type": "access_token",
"token": token,
}
}
2023-12-11 19:12:18 +00:00
2023-12-12 05:00:46 +00:00
gql = {
2023-12-13 21:47:02 +00:00
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
2023-12-13 20:39:25 +00:00
"variables": variables,
2023-12-13 21:47:02 +00:00
"operationName": opeation,
2023-12-12 05:00:46 +00:00
}
2023-12-13 21:10:34 +00:00
print(f"[services.auth] Graphql: {gql}")
2023-12-13 20:39:25 +00:00
try:
# Asynchronous HTTP request to the authentication server
async with aiohttp.ClientSession() as session:
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
# Logging the GraphQL response
response_text = await response.text()
2023-12-13 21:17:20 +00:00
print(f"[services.auth] GraphQL Response: {response_text}")
2023-12-13 20:39:25 +00:00
if response.status == 200:
# Parsing JSON response
data = await response.json()
2023-12-13 20:54:38 +00:00
errors = data.get("errors")
if errors:
2023-12-13 21:17:20 +00:00
print(f"[services.auth] errors: {errors}")
2023-12-13 20:39:25 +00:00
else:
2023-12-13 20:54:38 +00:00
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
if user_id:
# Logging the retrieved user ID
2023-12-13 21:17:20 +00:00
print(f"[services.auth] User ID retrieved: {user_id}")
2023-12-13 20:54:38 +00:00
return True, user_id
else:
# Logging when no user ID is found in the response
2023-12-13 21:17:20 +00:00
print("[services.auth] No user ID found in the response")
2023-12-13 20:39:25 +00:00
else:
# Logging when the request to the authentication server fails
2023-12-13 21:17:20 +00:00
print(f"[services.auth] Request failed with status: {response.status}")
2023-12-13 20:39:25 +00:00
except Exception as e:
# Handling and logging exceptions during authentication check
2023-12-13 21:17:20 +00:00
print(f"[services.auth] {e}")
2023-12-13 20:39:25 +00:00
2023-12-12 05:00:46 +00:00
return False, None
2023-10-23 14:47:11 +00:00
def login_required(f):
@wraps(f)
async def decorated_function(*args, **kwargs):
info = args[1]
context = info.context
2023-12-13 21:10:34 +00:00
# print(context)
2023-10-23 14:47:11 +00:00
req = context.get("request")
2023-12-13 20:39:25 +00:00
# Performing authentication check
2023-12-13 20:42:19 +00:00
is_authenticated, user_id = await check_auth(req)
2023-10-23 14:47:11 +00:00
if not is_authenticated:
2023-12-13 20:39:25 +00:00
# Raising an exception if the user is not authenticated
raise HTTPUnauthorized(text="Please, login first")
2023-10-23 14:47:11 +00:00
else:
2023-12-13 20:39:25 +00:00
# Adding user_id to the context
2023-12-13 20:42:19 +00:00
context["user_id"] = user_id
2023-10-23 14:47:11 +00:00
2023-11-29 07:23:41 +00:00
# If the user is authenticated, execute the resolver
2023-10-23 14:47:11 +00:00
return await f(*args, **kwargs)
return decorated_function
def auth_request(f):
@wraps(f)
async def decorated_function(*args, **kwargs):
req = args[0]
2023-12-13 20:39:25 +00:00
# Performing authentication check
2023-12-13 20:42:19 +00:00
is_authenticated, user_id = await check_auth(req)
2023-10-23 14:47:11 +00:00
if not is_authenticated:
2023-12-13 20:39:25 +00:00
# Raising HTTPUnauthorized exception if the user is not authenticated
2023-11-29 07:23:41 +00:00
raise HTTPUnauthorized(text="Please, login first")
2023-10-23 14:47:11 +00:00
else:
2023-12-13 20:39:25 +00:00
# Modifying the req with the author_id
2023-12-13 20:42:19 +00:00
req["user_id"] = user_id
2023-10-23 14:47:11 +00:00
return await f(*args, **kwargs)
return decorated_function