Spaces:
Running
Running
Ahmed Mostafa commited on
Commit ·
2bfdff3
1
Parent(s): b680a06
fix log 1.3
Browse files- src/auth/dependencies.py +28 -14
src/auth/dependencies.py
CHANGED
|
@@ -37,18 +37,25 @@ async def get_current_user(
|
|
| 37 |
# 1. Try Firebase Verification
|
| 38 |
try:
|
| 39 |
firebase_result = verify_token(token)
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if firebase_payload:
|
| 44 |
uid = firebase_payload.get("uid")
|
| 45 |
email = firebase_payload.get("email")
|
| 46 |
if not uid:
|
| 47 |
-
logger.error("Firebase payload missing 'uid' [CP1]")
|
| 48 |
-
raise HTTPException(status_code=401, detail="Invalid Firebase token payload [CP1]")
|
| 49 |
|
| 50 |
if db is None:
|
| 51 |
-
logger.warning(f"Firestore not available, returning transient user for {email} [CP2]")
|
| 52 |
return User(id=uid, email=email or "unknown@example.com", username=firebase_payload.get("name", uid), role="user")
|
| 53 |
|
| 54 |
# Retrieve from Firestore
|
|
@@ -62,7 +69,7 @@ async def get_current_user(
|
|
| 62 |
user_data.setdefault("role", user_data.get("role", "user"))
|
| 63 |
return User(**user_data)
|
| 64 |
else:
|
| 65 |
-
logger.info(f"New Firebase user detected: {email or uid} [CP3]")
|
| 66 |
return User(
|
| 67 |
id=uid,
|
| 68 |
email=email or "unknown@example.com",
|
|
@@ -73,8 +80,8 @@ async def get_current_user(
|
|
| 73 |
except HTTPException:
|
| 74 |
raise
|
| 75 |
except Exception as e:
|
| 76 |
-
logger.error(f"Unexpected error in Firebase auth path: {
|
| 77 |
-
firebase_error =
|
| 78 |
|
| 79 |
# 2. Fallback to Custom JWT Decoding
|
| 80 |
try:
|
|
@@ -82,7 +89,7 @@ async def get_current_user(
|
|
| 82 |
if payload:
|
| 83 |
username: Optional[str] = payload.get("sub")
|
| 84 |
if not username:
|
| 85 |
-
raise HTTPException(status_code=401, detail="Token missing subject claim [CP5]")
|
| 86 |
|
| 87 |
if db is None:
|
| 88 |
return User(id="mock_id", email="mock@example.com", username=username, password_hash="mock", role="user")
|
|
@@ -96,16 +103,23 @@ async def get_current_user(
|
|
| 96 |
user_data["id"] = user_doc.id
|
| 97 |
return User(**user_data)
|
| 98 |
else:
|
| 99 |
-
logger.error(f"User {username} not found in database [CP6]")
|
| 100 |
-
raise HTTPException(status_code=401, detail="User account not found [CP6]")
|
| 101 |
except HTTPException:
|
| 102 |
raise
|
| 103 |
except Exception as e:
|
| 104 |
-
logger.error(f"Unexpected error in custom JWT auth path: {
|
| 105 |
|
| 106 |
# If both failed, then it's a 401
|
| 107 |
-
fb_diag = f"Error: {firebase_error
|
| 108 |
-
error_detail = f"Auth Failure [CP8] | Token: {token_prefix}... |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
logger.error(f"Authentication failed: {error_detail}")
|
| 111 |
raise HTTPException(
|
|
|
|
| 37 |
# 1. Try Firebase Verification
|
| 38 |
try:
|
| 39 |
firebase_result = verify_token(token)
|
| 40 |
+
# Log type for absolute certainty
|
| 41 |
+
logger.info(f"Firebase result (v2) type: {type(firebase_result)}")
|
| 42 |
+
|
| 43 |
+
if not isinstance(firebase_result, dict):
|
| 44 |
+
firebase_error = f"Internal Error [CP0-v2]: verify_token returned {type(firebase_result)}"
|
| 45 |
+
firebase_payload = None
|
| 46 |
+
else:
|
| 47 |
+
firebase_payload = firebase_result.get("payload")
|
| 48 |
+
firebase_error = firebase_result.get("error")
|
| 49 |
|
| 50 |
if firebase_payload:
|
| 51 |
uid = firebase_payload.get("uid")
|
| 52 |
email = firebase_payload.get("email")
|
| 53 |
if not uid:
|
| 54 |
+
logger.error("Firebase payload missing 'uid' [CP1-v2]")
|
| 55 |
+
raise HTTPException(status_code=401, detail="Invalid Firebase token payload [CP1-v2]")
|
| 56 |
|
| 57 |
if db is None:
|
| 58 |
+
logger.warning(f"Firestore not available, returning transient user for {email} [CP2-v2]")
|
| 59 |
return User(id=uid, email=email or "unknown@example.com", username=firebase_payload.get("name", uid), role="user")
|
| 60 |
|
| 61 |
# Retrieve from Firestore
|
|
|
|
| 69 |
user_data.setdefault("role", user_data.get("role", "user"))
|
| 70 |
return User(**user_data)
|
| 71 |
else:
|
| 72 |
+
logger.info(f"New Firebase user detected: {email or uid} [CP3-v2]")
|
| 73 |
return User(
|
| 74 |
id=uid,
|
| 75 |
email=email or "unknown@example.com",
|
|
|
|
| 80 |
except HTTPException:
|
| 81 |
raise
|
| 82 |
except Exception as e:
|
| 83 |
+
logger.error(f"Unexpected error in Firebase auth path (v2): {repr(e)} [CP4-v2]")
|
| 84 |
+
firebase_error = f"Exception [CP4-v2]: {repr(e)}"
|
| 85 |
|
| 86 |
# 2. Fallback to Custom JWT Decoding
|
| 87 |
try:
|
|
|
|
| 89 |
if payload:
|
| 90 |
username: Optional[str] = payload.get("sub")
|
| 91 |
if not username:
|
| 92 |
+
raise HTTPException(status_code=401, detail="Token missing subject claim [CP5-v2]")
|
| 93 |
|
| 94 |
if db is None:
|
| 95 |
return User(id="mock_id", email="mock@example.com", username=username, password_hash="mock", role="user")
|
|
|
|
| 103 |
user_data["id"] = user_doc.id
|
| 104 |
return User(**user_data)
|
| 105 |
else:
|
| 106 |
+
logger.error(f"User {username} not found in database [CP6-v2]")
|
| 107 |
+
raise HTTPException(status_code=401, detail="User account not found [CP6-v2]")
|
| 108 |
except HTTPException:
|
| 109 |
raise
|
| 110 |
except Exception as e:
|
| 111 |
+
logger.error(f"Unexpected error in custom JWT auth path: {repr(e)} [CP7-v2]")
|
| 112 |
|
| 113 |
# If both failed, then it's a 401
|
| 114 |
+
fb_diag = f"Firebase Error (v2): {firebase_error if firebase_error else 'None returned'}"
|
| 115 |
+
error_detail = f"Auth Failure [CP8-v2] | Token: {token_prefix}... | {fb_diag}"
|
| 116 |
+
|
| 117 |
+
logger.error(f"Authentication failed (v2): {error_detail}")
|
| 118 |
+
raise HTTPException(
|
| 119 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 120 |
+
detail=error_detail,
|
| 121 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 122 |
+
)
|
| 123 |
|
| 124 |
logger.error(f"Authentication failed: {error_detail}")
|
| 125 |
raise HTTPException(
|