Hussien Haider commited on
Commit
47517ae
·
1 Parent(s): 712475b
Files changed (1) hide show
  1. app/core/auth.py +14 -1
app/core/auth.py CHANGED
@@ -71,13 +71,23 @@ async def get_current_user(request: Request) -> dict:
71
  When SUPABASE_JWT_SECRET is not configured (HF Spaces / dev),
72
  falls back to trusting the user_id from the request body.
73
  """
 
 
 
 
 
 
 
 
74
  token = extract_token_from_request(request)
75
 
76
  if token:
 
77
  payload = _validate_jwt(token)
78
  if payload:
79
  user_id = payload.get("sub", "")
80
  if user_id:
 
81
  return {
82
  "user_id": user_id,
83
  "email": payload.get("email", ""),
@@ -85,7 +95,8 @@ async def get_current_user(request: Request) -> dict:
85
  }
86
 
87
  # Dev/HF fallback: when no JWT secret is configured, trust user_id from body
88
- if not _has_real_jwt_secret():
 
89
  try:
90
  body_bytes = await request.body()
91
  if body_bytes:
@@ -97,6 +108,8 @@ async def get_current_user(request: Request) -> dict:
97
  return {"user_id": user_id, "email": "", "role": "authenticated"}
98
  except Exception as exc:
99
  log.warning("[auth-dev] Body fallback failed: %s", exc)
 
 
100
 
101
  log.warning("Auth failed: no valid token and no dev fallback available")
102
  raise HTTPException(
 
71
  When SUPABASE_JWT_SECRET is not configured (HF Spaces / dev),
72
  falls back to trusting the user_id from the request body.
73
  """
74
+ import os
75
+
76
+ jwt_secret = os.environ.get("SUPABASE_JWT_SECRET", "")
77
+ _placeholder_values = {"", "your_jwt_secret_here", "CHANGE_ME", "placeholder"}
78
+ has_real_secret = jwt_secret not in _placeholder_values
79
+
80
+ log.info("[auth] SUPABASE_JWT_SECRET set=%s, has_real=%s", bool(jwt_secret), has_real_secret)
81
+
82
  token = extract_token_from_request(request)
83
 
84
  if token:
85
+ log.info("[auth] Token received (len=%d, prefix=%s...)", len(token), token[:16])
86
  payload = _validate_jwt(token)
87
  if payload:
88
  user_id = payload.get("sub", "")
89
  if user_id:
90
+ log.info("[auth] JWT validated, user_id=%s", user_id)
91
  return {
92
  "user_id": user_id,
93
  "email": payload.get("email", ""),
 
95
  }
96
 
97
  # Dev/HF fallback: when no JWT secret is configured, trust user_id from body
98
+ if not has_real_secret:
99
+ log.info("[auth-dev] No real JWT secret — trying body fallback")
100
  try:
101
  body_bytes = await request.body()
102
  if body_bytes:
 
108
  return {"user_id": user_id, "email": "", "role": "authenticated"}
109
  except Exception as exc:
110
  log.warning("[auth-dev] Body fallback failed: %s", exc)
111
+ else:
112
+ log.warning("[auth] Real JWT secret IS set but token validation failed")
113
 
114
  log.warning("Auth failed: no valid token and no dev fallback available")
115
  raise HTTPException(