notifier/services/auth.py

89 lines
3.3 KiB
Python
Raw Normal View History

2023-11-23 22:58:55 +00:00
from functools import wraps
2023-11-30 06:42:41 +00:00
import aiohttp
2023-12-15 12:36:43 +00:00
from aiohttp.web import HTTPUnauthorized
2023-12-17 06:43:45 +00:00
2023-11-28 16:04:45 +00:00
from orm.author import Author
from services.db import local_session
2023-11-23 22:58:55 +00:00
from settings import AUTH_URL
2023-12-13 22:05:45 +00:00
async def check_auth(req) -> (bool, int | None):
2023-11-23 22:58:55 +00:00
token = req.headers.get("Authorization")
2023-12-13 22:05:45 +00:00
if token:
2023-12-17 06:43:45 +00:00
# Logging the authentication token
2023-12-13 22:05:45 +00:00
print(f"[services.auth] checking auth token: {token}")
query_name = "validate_jwt_token"
2023-12-17 06:43:45 +00:00
opeation = "ValidateToken"
2023-12-13 22:05:45 +00:00
headers = {
"Content-Type": "application/json",
}
variables = {
"params": {
"token_type": "access_token",
2023-12-13 22:16:54 +00:00
"token": token,
2023-12-13 22:05:45 +00:00
}
}
gql = {
2023-12-17 06:43:45 +00:00
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
2023-12-13 22:05:45 +00:00
"variables": variables,
2023-12-17 06:43:45 +00:00
"operationName": opeation,
2023-12-13 22:05:45 +00:00
}
2023-12-17 06:43:45 +00:00
# print(f"[services.auth] Graphql: {gql}")
2023-12-13 22:05:45 +00:00
try:
2023-12-17 06:43:45 +00:00
# Asynchronous HTTP request to the authentication server
2023-12-13 22:05:45 +00:00
async with aiohttp.ClientSession() as session:
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
2023-12-17 06:43:45 +00:00
# Logging the GraphQL response
2023-12-13 22:05:45 +00:00
response_text = await response.text()
print(f"[services.auth] GraphQL Response: {response_text}")
if response.status == 200:
2023-12-17 06:43:45 +00:00
# Parsing JSON response
2023-12-13 22:05:45 +00:00
data = await response.json()
errors = data.get("errors")
if errors:
print(f"[services.auth] errors: {errors}")
else:
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
if user_id:
2023-12-17 06:43:45 +00:00
# Logging the retrieved user ID
2023-12-13 22:05:45 +00:00
print(f"[services.auth] User ID retrieved: {user_id}")
return True, user_id
else:
2023-12-17 06:43:45 +00:00
# Logging when no user ID is found in the response
2023-12-13 22:05:45 +00:00
print("[services.auth] No user ID found in the response")
else:
2023-12-17 06:43:45 +00:00
# Logging when the request to the authentication server fails
2023-12-13 22:05:45 +00:00
print(f"[services.auth] Request failed with status: {response.status}")
except Exception as e:
2023-12-17 06:43:45 +00:00
# Handling and logging exceptions during authentication check
2023-12-13 22:05:45 +00:00
print(f"[services.auth] {e}")
2023-11-23 22:58:55 +00:00
return False, None
def login_required(f):
@wraps(f)
async def decorated_function(*args, **kwargs):
info = args[1]
context = info.context
req = context.get("request")
is_authenticated, user_id = await check_auth(req)
if not is_authenticated:
2023-12-13 22:05:45 +00:00
raise HTTPUnauthorized(text="Please, login first")
2023-11-23 22:58:55 +00:00
else:
2023-11-28 16:04:45 +00:00
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
context["author_id"] = author.id
if user_id:
context["user_id"] = user_id
2023-11-23 22:58:55 +00:00
return await f(*args, **kwargs)
return decorated_function