Files
core/services/auth.py

110 lines
3.6 KiB
Python
Raw Normal View History

2024-01-25 22:41:27 +03:00
from functools import wraps
2024-04-08 10:38:58 +03:00
2024-02-19 12:49:33 +03:00
import httpx
2024-02-26 12:52:22 +03:00
from starlette.exceptions import HTTPException
2024-01-23 21:59:46 +03:00
2024-02-27 13:54:47 +03:00
from services.logger import root_logger as logger
2024-02-19 11:10:12 +03:00
from settings import ADMIN_SECRET, AUTH_URL
2024-01-13 11:49:12 +03:00
2024-02-21 10:27:16 +03:00
2024-01-25 22:41:27 +03:00
async def request_data(gql, headers=None):
if headers is None:
2024-02-21 19:14:58 +03:00
headers = {'Content-Type': 'application/json'}
2024-01-10 16:29:49 +03:00
try:
2024-02-19 11:10:12 +03: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()
2024-02-21 19:14:58 +03:00
errors = data.get('errors')
2024-02-19 11:10:12 +03:00
if errors:
2024-02-21 19:14:58 +03:00
logger.error(f'HTTP Errors: {errors}')
2024-02-19 11:10:12 +03:00
else:
return data
2024-01-10 16:29:49 +03:00
except Exception as e:
2024-01-23 22:12:22 +03:00
# Handling and logging exceptions during authentication check
2024-02-23 15:02:14 +03:00
logger.error(f'request_data error: {e}')
2024-02-27 14:16:54 +03:00
return None
2024-01-10 16:29:49 +03:00
2024-02-19 15:51:09 +03:00
async def check_auth(req):
2024-02-21 19:14:58 +03:00
token = req.headers.get('Authorization')
user_id = ''
2024-02-24 00:00:46 +03:00
user_roles = []
2024-02-20 18:20:57 +03:00
if token:
2024-02-23 14:53:14 +03:00
# Logging the authentication token
logger.debug(f'{token}')
query_name = 'validate_jwt_token'
operation = 'ValidateToken'
2024-02-23 19:35:40 +03:00
variables = {'params': {'token_type': 'access_token', 'token': token}}
2024-02-23 14:53:14 +03:00
gql = {
'query': f'query {operation}($params: ValidateJWTTokenInput!) {{'
2024-03-06 12:25:55 +03:00
+ f'{query_name}(params: $params) {{ is_valid claims }} '
+ '}',
2024-02-23 14:53:14 +03:00
'variables': variables,
'operationName': operation,
}
data = await request_data(gql)
if data:
2024-02-26 12:52:22 +03:00
logger.debug(data)
2024-02-23 14:53:14 +03:00
user_data = data.get('data', {}).get(query_name, {}).get('claims', {})
2024-02-27 13:40:56 +03:00
user_id = user_data.get('sub', '')
user_roles = user_data.get('allowed_roles', [])
return user_id, user_roles
2024-01-23 22:12:22 +03:00
2023-12-25 01:42:39 +03:00
2024-01-10 16:29:49 +03:00
async def add_user_role(user_id):
2024-02-23 15:02:14 +03:00
logger.info(f'add author role for user_id: {user_id}')
2024-02-21 19:14:58 +03:00
query_name = '_update_user'
operation = 'UpdateUserRoles'
2024-01-25 22:41:27 +03:00
headers = {
2024-02-21 19:14:58 +03:00
'Content-Type': 'application/json',
'x-authorizer-admin-secret': ADMIN_SECRET,
2024-01-25 22:41:27 +03:00
}
2024-02-21 19:14:58 +03:00
variables = {'params': {'roles': 'author, reader', 'id': user_id}}
2023-12-25 01:42:39 +03:00
gql = {
2024-02-21 19:14:58 +03:00
'query': f'mutation {operation}($params: UpdateUserInput!) {{ {query_name}(params: $params) {{ id roles }} }}',
'variables': variables,
'operationName': operation,
2023-12-25 01:42:39 +03:00
}
2024-01-10 16:29:49 +03:00
data = await request_data(gql, headers)
if data:
2024-02-21 19:14:58 +03:00
user_id = data.get('data', {}).get(query_name, {}).get('id')
2024-01-10 16:29:49 +03:00
return user_id
2023-10-23 17:47:11 +03:00
2024-01-25 22:41:27 +03:00
2023-10-23 17:47:11 +03:00
def login_required(f):
2024-01-23 21:59:46 +03:00
@wraps(f)
2023-10-23 17:47:11 +03:00
async def decorated_function(*args, **kwargs):
info = args[1]
2024-03-05 18:04:47 +03:00
req = info.context.get('request')
2024-02-27 14:06:00 +03:00
authorized = await check_auth(req)
if authorized:
2024-03-11 11:59:20 +03:00
logger.info(authorized)
2024-02-27 14:06:00 +03:00
user_id, user_roles = authorized
if user_id and user_roles:
logger.info(f' got {user_id} roles: {user_roles}')
2024-03-05 18:04:47 +03:00
info.context['user_id'] = user_id.strip()
info.context['roles'] = user_roles
2024-02-27 14:16:54 +03:00
return await f(*args, **kwargs)
2023-10-23 17:47:11 +03:00
return decorated_function
2024-01-23 21:59:46 +03:00
2023-10-23 17:47:11 +03:00
def auth_request(f):
2024-01-23 21:59:46 +03:00
@wraps(f)
2023-10-23 17:47:11 +03:00
async def decorated_function(*args, **kwargs):
2024-02-23 14:53:14 +03:00
req = args[0]
2024-02-27 14:06:00 +03:00
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()
req['roles'] = user_roles
2024-02-27 14:16:54 +03:00
return await f(*args, **kwargs)
2024-02-26 12:52:22 +03:00
else:
raise HTTPException(status_code=401, detail='Unauthorized')
2023-10-23 17:47:11 +03:00
return decorated_function