0.3.0+sentry
Some checks failed
deploy / deploy (push) Failing after 47s

This commit is contained in:
2024-04-08 09:30:57 +03:00
parent 106f1bfbde
commit c8f65ca0c9
15 changed files with 373 additions and 322 deletions

View File

@@ -8,34 +8,34 @@ from services.core import get_author_by_user
from settings import AUTH_URL
logger = logging.getLogger('[services.auth] ')
logger = logging.getLogger("[services.auth] ")
logger.setLevel(logging.DEBUG)
async def check_auth(req) -> str | None:
logger.debug('checking auth...')
user_id = ''
async def check_auth(req):
logger.debug("checking auth...")
user_id = ""
try:
token = req.headers.get('Authorization')
token = req.headers.get("Authorization")
if token:
# Logging the authentication token
query_name = 'validate_jwt_token'
operation = 'ValidateToken'
query_name = "validate_jwt_token"
operation = "ValidateToken"
headers = {
'Content-Type': 'application/json',
"Content-Type": "application/json",
}
variables = {
'params': {
'token_type': 'access_token',
'token': token,
"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,
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
"variables": variables,
"operationName": operation,
}
# Asynchronous HTTP request to the authentication server
async with ClientSession() as session:
@@ -44,24 +44,24 @@ async def check_auth(req) -> str | None:
) as response:
if response.status == 200:
data = await response.json()
errors = data.get('errors')
errors = data.get("errors")
if errors:
logger.error(f'{errors}')
logger.error(f"{errors}")
else:
user_id = (
data.get('data', {})
data.get("data", {})
.get(query_name, {})
.get('claims', {})
.get('sub')
.get("claims", {})
.get("sub")
)
logger.info(f'got user_id: {user_id}')
logger.info(f"got user_id: {user_id}")
return user_id
except Exception as e:
# Handling and logging exceptions during authentication check
logger.error(e)
if not user_id:
raise HTTPException(status_code=401, detail='Unauthorized')
raise HTTPException(status_code=401, detail="Unauthorized")
def login_required(f):
@@ -69,16 +69,16 @@ def login_required(f):
async def decorated_function(*args, **kwargs):
info = args[1]
context = info.context
req = context.get('request')
req = context.get("request")
user_id = await check_auth(req)
if user_id:
context['user_id'] = user_id.strip()
context["user_id"] = user_id.strip()
author = get_author_by_user(user_id)
if author and 'id' in author:
context['author_id'] = author['id']
if author and "id" in author:
context["author_id"] = author["id"]
else:
logger.debug(author)
HTTPException(status_code=401, detail='Unauthorized')
HTTPException(status_code=401, detail="Unauthorized")
return await f(*args, **kwargs)
return decorated_function