2024-02-16 23:56:15 +00:00
|
|
|
import logging
|
|
|
|
|
2023-12-18 07:30:14 +00:00
|
|
|
from aiohttp import ClientSession
|
2023-12-17 11:47:05 +00:00
|
|
|
from strawberry.extensions import Extension
|
|
|
|
|
2023-12-18 07:30:14 +00:00
|
|
|
from orm.author import Author
|
2024-02-16 23:56:15 +00:00
|
|
|
from services.db import local_session
|
|
|
|
from settings import AUTH_URL
|
2023-11-23 22:58:55 +00:00
|
|
|
|
2024-01-23 14:52:12 +00:00
|
|
|
|
2024-02-16 23:56:15 +00:00
|
|
|
logger = logging.getLogger('\t[services.auth]\t')
|
2024-01-23 14:52:12 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2023-12-22 09:09:03 +00:00
|
|
|
|
2024-02-04 04:58:44 +00:00
|
|
|
|
2023-12-18 07:30:14 +00:00
|
|
|
async def check_auth(req) -> str | None:
|
2024-02-16 23:56:15 +00:00
|
|
|
token = req.headers.get('Authorization')
|
|
|
|
user_id = ''
|
2023-12-13 22:05:45 +00:00
|
|
|
if token:
|
2024-02-16 23:56:15 +00:00
|
|
|
query_name = 'validate_jwt_token'
|
|
|
|
operation = 'ValidateToken'
|
2023-12-13 22:05:45 +00:00
|
|
|
headers = {
|
2024-02-16 23:56:15 +00:00
|
|
|
'Content-Type': 'application/json',
|
2023-12-13 22:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
variables = {
|
2024-02-16 23:56:15 +00:00
|
|
|
'params': {
|
|
|
|
'token_type': 'access_token',
|
|
|
|
'token': token,
|
2023-12-13 22:05:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gql = {
|
2024-02-16 23:56:15 +00:00
|
|
|
'query': f'query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}',
|
|
|
|
'variables': variables,
|
|
|
|
'operationName': operation,
|
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-18 07:30:14 +00:00
|
|
|
async with ClientSession() as session:
|
2023-12-13 22:05:45 +00:00
|
|
|
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
2024-02-16 23:56:15 +00:00
|
|
|
print(f'[services.auth] HTTP Response {response.status} {await response.text()}')
|
2023-12-13 22:05:45 +00:00
|
|
|
if response.status == 200:
|
|
|
|
data = await response.json()
|
2024-02-16 23:56:15 +00:00
|
|
|
errors = data.get('errors')
|
2023-12-13 22:05:45 +00:00
|
|
|
if errors:
|
2024-02-16 23:56:15 +00:00
|
|
|
print(f'[services.auth] errors: {errors}')
|
2023-12-13 22:05:45 +00:00
|
|
|
else:
|
2024-02-16 23:56:15 +00:00
|
|
|
user_id = data.get('data', {}).get(query_name, {}).get('claims', {}).get('sub')
|
2023-12-22 09:09:03 +00:00
|
|
|
if user_id:
|
2024-02-16 23:56:15 +00:00
|
|
|
print(f'[services.auth] got user_id: {user_id}')
|
2023-12-22 09:09:03 +00:00
|
|
|
return user_id
|
2023-12-13 22:05:45 +00:00
|
|
|
except Exception as e:
|
2023-12-22 10:08:35 +00:00
|
|
|
import traceback
|
2024-02-04 04:58:44 +00:00
|
|
|
|
2023-12-22 10:08:35 +00:00
|
|
|
traceback.print_exc()
|
2023-12-17 06:43:45 +00:00
|
|
|
# Handling and logging exceptions during authentication check
|
2024-02-16 23:56:15 +00:00
|
|
|
print(f'[services.auth] Error {e}')
|
2023-12-13 22:05:45 +00:00
|
|
|
|
2023-12-22 09:34:47 +00:00
|
|
|
return None
|
2023-12-22 09:09:03 +00:00
|
|
|
|
2023-12-17 11:47:05 +00:00
|
|
|
|
|
|
|
class LoginRequiredMiddleware(Extension):
|
|
|
|
async def on_request_start(self):
|
|
|
|
context = self.execution_context.context
|
2024-02-16 23:56:15 +00:00
|
|
|
req = context.get('request')
|
2023-12-18 07:30:14 +00:00
|
|
|
user_id = await check_auth(req)
|
|
|
|
if user_id:
|
2024-02-16 23:56:15 +00:00
|
|
|
context['user_id'] = user_id.strip()
|
2023-12-17 11:47:05 +00:00
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
2024-02-16 23:56:15 +00:00
|
|
|
context['author_id'] = author.id
|
|
|
|
context['user_id'] = user_id or None
|
2023-12-22 09:09:03 +00:00
|
|
|
|
|
|
|
self.execution_context.context = context
|