Update app/main.py
Browse files- app/main.py +37 -5
app/main.py
CHANGED
|
@@ -291,10 +291,42 @@ async def verify_request(request: Request):
|
|
| 291 |
# If no valid token, allow (for public endpoints)
|
| 292 |
return True
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
def _resolve_user_id(request: Request, supplied_user_id: Optional[str]) -> Optional[str]:
|
| 300 |
"""Return supplied user_id if provided and not empty, otherwise None (will auto-generate in log_media_click)."""
|
|
@@ -606,7 +638,7 @@ async def colorize(
|
|
| 606 |
import time
|
| 607 |
start_time = time.time()
|
| 608 |
|
| 609 |
-
verify_app_check_token(x_firebase_appcheck)
|
| 610 |
|
| 611 |
ip_address = request.client.host if request.client else None
|
| 612 |
effective_user_id = _resolve_user_id(request, user_id)
|
|
|
|
| 291 |
# If no valid token, allow (for public endpoints)
|
| 292 |
return True
|
| 293 |
|
| 294 |
+
from firebase_admin import app_check
|
| 295 |
+
from fastapi import HTTPException
|
| 296 |
+
import os
|
| 297 |
+
|
| 298 |
+
def verify_app_check_token(
|
| 299 |
+
token: str | None,
|
| 300 |
+
*,
|
| 301 |
+
required: bool = False
|
| 302 |
+
):
|
| 303 |
+
"""
|
| 304 |
+
If required=False:
|
| 305 |
+
- Missing token is allowed
|
| 306 |
+
- Invalid token is rejected
|
| 307 |
+
If required=True:
|
| 308 |
+
- Missing OR invalid token is rejected
|
| 309 |
+
"""
|
| 310 |
+
|
| 311 |
+
# Token missing
|
| 312 |
+
if not token:
|
| 313 |
+
if required:
|
| 314 |
+
raise HTTPException(
|
| 315 |
+
status_code=401,
|
| 316 |
+
detail="Firebase App Check token missing"
|
| 317 |
+
)
|
| 318 |
+
return True # OPTIONAL → allow request
|
| 319 |
+
|
| 320 |
+
# Token present → must be valid
|
| 321 |
+
try:
|
| 322 |
+
app_check.verify_token(token)
|
| 323 |
+
return True
|
| 324 |
+
except Exception as e:
|
| 325 |
+
raise HTTPException(
|
| 326 |
+
status_code=401,
|
| 327 |
+
detail="Invalid Firebase App Check token"
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
|
| 331 |
def _resolve_user_id(request: Request, supplied_user_id: Optional[str]) -> Optional[str]:
|
| 332 |
"""Return supplied user_id if provided and not empty, otherwise None (will auto-generate in log_media_click)."""
|
|
|
|
| 638 |
import time
|
| 639 |
start_time = time.time()
|
| 640 |
|
| 641 |
+
verify_app_check_token(x_firebase_appcheck,required=False)
|
| 642 |
|
| 643 |
ip_address = request.client.host if request.client else None
|
| 644 |
effective_user_id = _resolve_user_id(request, user_id)
|