feat: auth.py total recomp
All checks were successful
Deploy on Push / deploy (push) Successful in 1m29s
All checks were successful
Deploy on Push / deploy (push) Successful in 1m29s
This commit is contained in:
parent
2e6678c657
commit
2964b72635
34
auth.py
34
auth.py
|
@ -1,22 +1,18 @@
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import web
|
|
||||||
|
|
||||||
AUTH_URL = 'https://auth.discours.io'
|
AUTH_URL = 'https://auth.discours.io'
|
||||||
|
|
||||||
|
|
||||||
async def check_auth(req):
|
async def check_auth(req):
|
||||||
token = req.headers.get("Authorization")
|
token = req.headers.get("Authorization")
|
||||||
headers = {"Authorization": token, "Content-Type": "application/json"} # "Bearer " + removed
|
headers = {"Authorization": token, "Content-Type": "application/json"}
|
||||||
|
|
||||||
print(f"[services.auth] checking auth token: {token}")
|
print(f"[services.auth] checking auth token: {token}")
|
||||||
|
|
||||||
query_name = "session"
|
|
||||||
query_type = "query"
|
|
||||||
operation = "GetUserId"
|
|
||||||
|
|
||||||
gql = {
|
gql = {
|
||||||
"query": query_type + " " + operation + " { " + query_name + " { user { id } } }",
|
"query": "query GetUserId { session { user { id } } }",
|
||||||
"operationName": operation,
|
"operationName": "GetUserId",
|
||||||
"variables": None,
|
"variables": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,25 +23,19 @@ async def check_auth(req):
|
||||||
return False, None
|
return False, None
|
||||||
r = await response.json()
|
r = await response.json()
|
||||||
if r:
|
if r:
|
||||||
user_id = r.get("data", {}).get(query_name, {}).get("user", {}).get("id", None)
|
user_id = r.get("data", {}).get("session", {}).get("user", {}).get("id", None)
|
||||||
is_authenticated = user_id is not None
|
is_authenticated = user_id is not None
|
||||||
return is_authenticated, user_id
|
return is_authenticated, user_id
|
||||||
return False, None
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
def login_required(f):
|
def login_required(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
async def decorated_function(*args, **kwargs):
|
async def decorated_function(request, *args, **kwargs):
|
||||||
info = args[1]
|
is_authenticated, user_id = await check_auth(request)
|
||||||
context = info.context
|
|
||||||
req = context.get("request")
|
|
||||||
is_authenticated, user_id = await check_auth(req)
|
|
||||||
if not is_authenticated:
|
if not is_authenticated:
|
||||||
raise web.HTTPUnauthorized(text="You are not logged in") # Return HTTP 401 Unauthorized
|
return JSONResponse({'error': 'Unauthorized'}, status_code=401)
|
||||||
else:
|
|
||||||
context["user_id"] = user_id
|
|
||||||
|
|
||||||
# If the user is authenticated, execute the resolver
|
|
||||||
return await f(*args, **kwargs)
|
|
||||||
|
|
||||||
|
# Make user_id available to the route handler, if needed
|
||||||
|
request.state.user_id = user_id
|
||||||
|
return await f(request, *args, **kwargs)
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
|
2
main.py
2
main.py
|
@ -18,7 +18,7 @@ STORJ_END_POINT = os.environ.get('STORJ_END_POINT')
|
||||||
STORJ_BUCKET_NAME = os.environ.get('STORJ_BUCKET_NAME')
|
STORJ_BUCKET_NAME = os.environ.get('STORJ_BUCKET_NAME')
|
||||||
CDN_DOMAIN = os.environ.get('CDN_DOMAIN')
|
CDN_DOMAIN = os.environ.get('CDN_DOMAIN')
|
||||||
|
|
||||||
# @check_auth
|
@check_auth
|
||||||
async def upload_handler(request: Request):
|
async def upload_handler(request: Request):
|
||||||
logging.debug("Received upload request")
|
logging.debug("Received upload request")
|
||||||
form = await request.form()
|
form = await request.form()
|
||||||
|
|
|
@ -12,6 +12,7 @@ aiohttp = "^3.9.1"
|
||||||
uvicorn = "^0.24.0.post1"
|
uvicorn = "^0.24.0.post1"
|
||||||
starlette = "^0.33.0"
|
starlette = "^0.33.0"
|
||||||
aioboto3 = "^9.0.0"
|
aioboto3 = "^9.0.0"
|
||||||
|
python-multipart = "^0.0.5"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
black = "^23.10.1"
|
black = "^23.10.1"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user