diff --git a/pyproject.toml b/pyproject.toml index e6d4437d..64a51fa6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,7 @@ use_parentheses = true ensure_newline_before_comments = true line_length = 120 -[tool.ruff] +[lint] select = ["E4", "E7", "E9", "F"] ignore = [] line-length = 120 diff --git a/services/auth.py b/services/auth.py index d0649723..4498b122 100644 --- a/services/auth.py +++ b/services/auth.py @@ -1,13 +1,12 @@ import logging from functools import wraps -from typing import List import httpx from dogpile.cache import make_region from settings import ADMIN_SECRET, AUTH_URL -logger = logging.getLogger('\t[services.auth]\t') +logger = logging.getLogger('[services.auth]') logger.setLevel(logging.DEBUG) @@ -54,40 +53,42 @@ def cache_auth_request(f): # Измененная функция проверки аутентификации с кэшированием @cache_auth_request -async def check_auth(req) -> [str, List[str]]: +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, + try: + 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!) {{ {query_name}(params: $params) {{ is_valid claims }} }}', - 'variables': variables, - 'operationName': operation, - } - data = await request_data(gql) - if data: - user_data = data.get('data', {}).get(query_name, {}).get('claims', {}) - user_id = user_data.get('sub') - user_roles = user_data.get('allowed_roles') + gql = { + 'query': f'query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}', + 'variables': variables, + 'operationName': operation, + } + data = await request_data(gql) + if data: + user_data = data.get('data', {}).get(query_name, {}).get('claims', {}) + user_id = user_data.get('sub') + user_roles = user_data.get('allowed_roles') + except Exception as e: + logger.error(e) # Возвращаем пустые значения, если не удалось получить user_id и user_roles return [user_id, user_roles] async def add_user_role(user_id): - logger.info(f'[services.auth] add author role for user_id: {user_id}') + logger.info(f'add author role for user_id: {user_id}') query_name = '_update_user' operation = 'UpdateUserRoles' headers = {