Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 2036f4f

Browse files
authored
Merge pull request #63 from nihar1024/main
fix: update auth checks for stats endpoints
2 parents f8c52ea + 05d4b05 commit 2036f4f

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/deps/auth.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,7 @@ def is_superuser(user_token: dict = Depends(user_token), throw_error: bool = Tru
7373
def clean_path(path: str) -> str:
7474
return path.replace(settings.API_V2_STR + "/", "")
7575

76-
77-
async def auth_z(
78-
request: Request,
79-
user_token: dict = Depends(user_token),
80-
async_session: AsyncSession = Depends(get_db),
81-
) -> bool:
82-
76+
async def _validate_authorization(request: Request, user_token: dict, async_session: AsyncSession):
8377
if settings.AUTH is not False:
8478
try:
8579
user_id = user_token["sub"]
@@ -107,3 +101,36 @@ async def auth_z(
107101
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e))
108102

109103
return True
104+
105+
async def auth_z(
106+
request: Request,
107+
user_token: dict = Depends(user_token),
108+
async_session: AsyncSession = Depends(get_db),
109+
) -> bool:
110+
"""
111+
Authorization function to check if the user has access to the requested resource.
112+
"""
113+
try:
114+
await _validate_authorization(request, user_token, async_session)
115+
except Exception as e:
116+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e))
117+
118+
return True
119+
120+
async def auth_z_lite(request: Request, async_session: AsyncSession):
121+
"""
122+
Authorization function to check if the user has access to the requested resource (without FastAPI dependencies).
123+
"""
124+
125+
try:
126+
token = request.headers.get("Authorization")
127+
if token:
128+
token = token.split(" ")[1]
129+
else:
130+
raise HTTPException(
131+
status_code=status.HTTP_401_UNAUTHORIZED, detail="Missing authorization token"
132+
)
133+
user_token = decode_token(token)
134+
return await _validate_authorization(request, user_token, async_session)
135+
except Exception as e:
136+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(e))

src/endpoints/v2/project.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import List
1+
from typing import List, Annotated
22
from uuid import UUID
33

4-
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, status
4+
from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query, status, Request
55
from fastapi.responses import JSONResponse
66
from fastapi_pagination import Page
77
from fastapi_pagination import Params as PaginationParams
@@ -18,7 +18,7 @@
1818
from src.db.models.scenario import Scenario
1919
from src.db.models.scenario_feature import ScenarioFeature
2020
from src.db.session import AsyncSession
21-
from src.deps.auth import auth_z
21+
from src.deps.auth import auth_z, auth_z_lite
2222
from src.endpoints.deps import get_db, get_scenario, get_user_id
2323
from src.schemas.common import OrderEnum
2424
from src.schemas.error import HTTPErrorHandler
@@ -517,9 +517,9 @@ async def get_chart_data(
517517
summary="Get aggregated statistics for a column",
518518
response_model=dict,
519519
status_code=200,
520-
dependencies=[Depends(auth_z)],
521520
)
522521
async def get_statistic_aggregation(
522+
request: Request,
523523
async_session: AsyncSession = Depends(get_db),
524524
project_id: UUID4 = Path(
525525
...,
@@ -567,6 +567,18 @@ async def get_statistic_aggregation(
567567
):
568568
"""Get aggregated statistics for a numeric column based on the supplied group-by column and CQL-filter."""
569569

570+
# Check authorization status
571+
try:
572+
await auth_z_lite(request, async_session)
573+
except HTTPException as e:
574+
# Check publication status if unauthorized
575+
public_project = await crud_project.get_public_project(
576+
async_session=async_session,
577+
project_id=str(project_id),
578+
)
579+
if not public_project:
580+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized")
581+
570582
# Ensure an operation or expression is specified
571583
if operation is None and expression is None:
572584
raise HTTPException(
@@ -618,9 +630,9 @@ async def get_statistic_aggregation(
618630
summary="Get histogram statistics for a column",
619631
response_model=dict,
620632
status_code=200,
621-
dependencies=[Depends(auth_z)],
622633
)
623634
async def get_statistic_histogram(
635+
request: Request,
624636
async_session: AsyncSession = Depends(get_db),
625637
project_id: UUID4 = Path(
626638
...,
@@ -655,6 +667,18 @@ async def get_statistic_histogram(
655667
):
656668
"""Get histogram statistics for a numeric column based on the specified number of bins and CQL-filter."""
657669

670+
# Check authorization status
671+
try:
672+
await auth_z_lite(request, async_session)
673+
except HTTPException as e:
674+
# Check publication status if unauthorized
675+
public_project = await crud_project.get_public_project(
676+
async_session=async_session,
677+
project_id=str(project_id),
678+
)
679+
if not public_project:
680+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized")
681+
658682
# Ensure the number of bins is not excessively large
659683
if num_bins > 100:
660684
raise HTTPException(

0 commit comments

Comments
 (0)