Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
1861128
1
Parent(s): 6ac96d0
🚀 Auto-deploy backend from GitHub (c024e6d)
Browse files
main.py
CHANGED
|
@@ -531,6 +531,24 @@ def _init_firebase_admin() -> None:
|
|
| 531 |
logger.warning("firebase-admin is not available; protected API endpoints will reject requests.")
|
| 532 |
return
|
| 533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
try:
|
| 535 |
if not firebase_admin._apps: # type: ignore[attr-defined]
|
| 536 |
init_options: Dict[str, Any] = {}
|
|
@@ -538,12 +556,17 @@ def _init_firebase_admin() -> None:
|
|
| 538 |
if FIREBASE_AUTH_PROJECT_ID:
|
| 539 |
init_options["projectId"] = FIREBASE_AUTH_PROJECT_ID
|
| 540 |
|
| 541 |
-
|
| 542 |
-
|
|
|
|
| 543 |
credentials_obj = cast(Any, firebase_admin).credentials.Certificate(service_account_payload)
|
|
|
|
| 544 |
elif FIREBASE_SERVICE_ACCOUNT_FILE:
|
| 545 |
credentials_obj = cast(Any, firebase_admin).credentials.Certificate(FIREBASE_SERVICE_ACCOUNT_FILE)
|
|
|
|
| 546 |
|
|
|
|
|
|
|
| 547 |
if credentials_obj and init_options:
|
| 548 |
firebase_admin.initialize_app(credentials_obj, options=init_options) # type: ignore[union-attr]
|
| 549 |
elif credentials_obj:
|
|
@@ -551,7 +574,15 @@ def _init_firebase_admin() -> None:
|
|
| 551 |
elif init_options:
|
| 552 |
firebase_admin.initialize_app(options=init_options) # type: ignore[union-attr]
|
| 553 |
else:
|
| 554 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
_firebase_ready = True
|
| 556 |
if FIREBASE_AUTH_PROJECT_ID:
|
| 557 |
logger.info(f"Firebase Admin SDK initialized for API auth verification (projectId={FIREBASE_AUTH_PROJECT_ID})")
|
|
|
|
| 531 |
logger.warning("firebase-admin is not available; protected API endpoints will reject requests.")
|
| 532 |
return
|
| 533 |
|
| 534 |
+
# Helper: load Firebase service account JSON from env var OR HF Spaces secret file
|
| 535 |
+
def _load_firebase_sa_json() -> Optional[str]:
|
| 536 |
+
# 1. Environment variable (standard deployment)
|
| 537 |
+
if FIREBASE_SERVICE_ACCOUNT_JSON:
|
| 538 |
+
return FIREBASE_SERVICE_ACCOUNT_JSON
|
| 539 |
+
# 2. HF Spaces secret mount path (secrets mounted as files at /secret/)
|
| 540 |
+
hf_space_secret_path = "/secret/FIREBASE_SERVICE_ACCOUNT_JSON"
|
| 541 |
+
if os.path.exists(hf_space_secret_path):
|
| 542 |
+
try:
|
| 543 |
+
with open(hf_space_secret_path, "r") as f:
|
| 544 |
+
content = f.read().strip()
|
| 545 |
+
if content:
|
| 546 |
+
logger.info(f"Loaded FIREBASE_SERVICE_ACCOUNT_JSON from HF Spaces secret file: {hf_space_secret_path}")
|
| 547 |
+
return content
|
| 548 |
+
except Exception as e:
|
| 549 |
+
logger.warning(f"Failed to read HF Spaces secret file {hf_space_secret_path}: {e}")
|
| 550 |
+
return None
|
| 551 |
+
|
| 552 |
try:
|
| 553 |
if not firebase_admin._apps: # type: ignore[attr-defined]
|
| 554 |
init_options: Dict[str, Any] = {}
|
|
|
|
| 556 |
if FIREBASE_AUTH_PROJECT_ID:
|
| 557 |
init_options["projectId"] = FIREBASE_AUTH_PROJECT_ID
|
| 558 |
|
| 559 |
+
sa_json = _load_firebase_sa_json()
|
| 560 |
+
if sa_json:
|
| 561 |
+
service_account_payload = json.loads(sa_json)
|
| 562 |
credentials_obj = cast(Any, firebase_admin).credentials.Certificate(service_account_payload)
|
| 563 |
+
logger.info("Firebase credentials loaded from FIREBASE_SERVICE_ACCOUNT_JSON")
|
| 564 |
elif FIREBASE_SERVICE_ACCOUNT_FILE:
|
| 565 |
credentials_obj = cast(Any, firebase_admin).credentials.Certificate(FIREBASE_SERVICE_ACCOUNT_FILE)
|
| 566 |
+
logger.info("Firebase credentials loaded from FIREBASE_SERVICE_ACCOUNT_FILE")
|
| 567 |
|
| 568 |
+
# Only initialize if we have credentials or at minimum a project ID
|
| 569 |
+
# Without credentials, Firebase init succeeds but ALL Firestore calls will fail
|
| 570 |
if credentials_obj and init_options:
|
| 571 |
firebase_admin.initialize_app(credentials_obj, options=init_options) # type: ignore[union-attr]
|
| 572 |
elif credentials_obj:
|
|
|
|
| 574 |
elif init_options:
|
| 575 |
firebase_admin.initialize_app(options=init_options) # type: ignore[union-attr]
|
| 576 |
else:
|
| 577 |
+
# No credentials AND no project ID — Firebase will NOT be usable
|
| 578 |
+
logger.error(
|
| 579 |
+
"Firebase Admin SDK could not initialize: no credentials found. "
|
| 580 |
+
"Set FIREBASE_SERVICE_ACCOUNT_JSON env var, FIREBASE_SERVICE_ACCOUNT_FILE path, "
|
| 581 |
+
"or ensure HF Spaces secret is mounted at /secret/FIREBASE_SERVICE_ACCOUNT_JSON. "
|
| 582 |
+
"Firestore operations will fail."
|
| 583 |
+
)
|
| 584 |
+
return
|
| 585 |
+
|
| 586 |
_firebase_ready = True
|
| 587 |
if FIREBASE_AUTH_PROJECT_ID:
|
| 588 |
logger.info(f"Firebase Admin SDK initialized for API auth verification (projectId={FIREBASE_AUTH_PROJECT_ID})")
|