This commit is contained in:
parent
64b571fccd
commit
8830908307
|
@ -6,13 +6,14 @@ from aiohttp.web import HTTPUnauthorized
|
||||||
from settings import AUTH_URL
|
from settings import AUTH_URL
|
||||||
|
|
||||||
|
|
||||||
async def check_auth(req) -> (bool, int | None):
|
async def check_auth(req) -> str | None:
|
||||||
token = req.headers.get("Authorization")
|
token = req.headers.get("Authorization")
|
||||||
|
user_id = ""
|
||||||
if token:
|
if token:
|
||||||
# Logging the authentication token
|
# Logging the authentication token
|
||||||
print(f"[services.auth] checking auth token: {token}")
|
print(f"[services.auth] checking auth token: {token}")
|
||||||
query_name = "validate_jwt_token"
|
query_name = "validate_jwt_token"
|
||||||
opeation = "ValidateToken"
|
operation = "ValidateToken"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
@ -25,46 +26,28 @@ async def check_auth(req) -> (bool, int | None):
|
||||||
}
|
}
|
||||||
|
|
||||||
gql = {
|
gql = {
|
||||||
"query": f"query {opeation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
"query": f"query {operation}($params: ValidateJWTTokenInput!) {{ {query_name}(params: $params) {{ is_valid claims }} }}",
|
||||||
"variables": variables,
|
"variables": variables,
|
||||||
"operationName": opeation,
|
"operationName": operation,
|
||||||
}
|
}
|
||||||
# print(f"[services.auth] Graphql: {gql}")
|
|
||||||
try:
|
try:
|
||||||
# Asynchronous HTTP request to the authentication server
|
# Asynchronous HTTP request to the authentication server
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
async with session.post(AUTH_URL, json=gql, headers=headers) as response:
|
||||||
# Logging the GraphQL response
|
|
||||||
# response_text = await response.text()
|
|
||||||
# print(f"[services.auth] GraphQL Response: {response_text}")
|
|
||||||
|
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
# Parsing JSON response
|
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
errors = data.get("errors")
|
errors = data.get("errors")
|
||||||
if errors:
|
if errors:
|
||||||
print(f"[services.auth] errors: {errors}")
|
print(f"[services.auth] errors: {errors}")
|
||||||
else:
|
else:
|
||||||
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
user_id = data.get("data", {}).get(query_name, {}).get("claims", {}).get("sub")
|
||||||
|
return user_id
|
||||||
if user_id:
|
|
||||||
# Logging the retrieved user ID
|
|
||||||
# print(f"[services.auth] User ID retrieved: {user_id}")
|
|
||||||
return True, user_id
|
|
||||||
else:
|
|
||||||
# Logging when no user ID is found in the response
|
|
||||||
# print("[services.auth] No user ID found in the response")
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# Logging when the request to the authentication server fails
|
|
||||||
# print(f"[services.auth] Request failed with status: {response.status}")
|
|
||||||
pass
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Handling and logging exceptions during authentication check
|
# Handling and logging exceptions during authentication check
|
||||||
print(f"[services.auth] {e}")
|
print(f"[services.auth] {e}")
|
||||||
|
|
||||||
return False, None
|
if not user_id:
|
||||||
|
raise HTTPUnauthorized(text="Please, login first")
|
||||||
|
|
||||||
|
|
||||||
def login_required(f):
|
def login_required(f):
|
||||||
|
@ -72,19 +55,10 @@ def login_required(f):
|
||||||
async def decorated_function(*args, **kwargs):
|
async def decorated_function(*args, **kwargs):
|
||||||
info = args[1]
|
info = args[1]
|
||||||
context = info.context
|
context = info.context
|
||||||
# print(context)
|
|
||||||
req = context.get("request")
|
req = context.get("request")
|
||||||
# print(f"[services.auth] login_required request headers: {req.headers}")
|
user_id = await check_auth(req)
|
||||||
# Performing authentication check
|
if user_id:
|
||||||
is_authenticated, user_id = await check_auth(req)
|
|
||||||
if not is_authenticated:
|
|
||||||
# Raising an exception if the user is not authenticated
|
|
||||||
raise HTTPUnauthorized(text="Please, login first")
|
|
||||||
else:
|
|
||||||
# Adding user_id to the context
|
|
||||||
context["user_id"] = user_id
|
context["user_id"] = user_id
|
||||||
|
|
||||||
# If the user is authenticated, execute the resolver
|
|
||||||
return await f(*args, **kwargs)
|
return await f(*args, **kwargs)
|
||||||
|
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
@ -94,13 +68,8 @@ def auth_request(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
async def decorated_function(*args, **kwargs):
|
async def decorated_function(*args, **kwargs):
|
||||||
req = args[0]
|
req = args[0]
|
||||||
# Performing authentication check
|
user_id = await check_auth(req)
|
||||||
is_authenticated, user_id = await check_auth(req)
|
if user_id:
|
||||||
if not is_authenticated:
|
|
||||||
# Raising HTTPUnauthorized exception if the user is not authenticated
|
|
||||||
raise HTTPUnauthorized(text="Please, login first")
|
|
||||||
else:
|
|
||||||
# Modifying the req with the author_id
|
|
||||||
req["user_id"] = user_id
|
req["user_id"] = user_id
|
||||||
return await f(*args, **kwargs)
|
return await f(*args, **kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user