Hamdy005 commited on
Commit
f3c951d
·
1 Parent(s): 38fc3eb

refactor: decouple Hugging Face token from user authentication by using X-Auth-Token for JWTs

Browse files
Files changed (1) hide show
  1. dependencies.py +16 -19
dependencies.py CHANGED
@@ -12,6 +12,7 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token", auto_error=False)
12
 
13
  async def get_current_user(
14
  token: Optional[str] = Depends(oauth2_scheme),
 
15
  x_user_id: Optional[str] = Header(None),
16
  x_user_name: Optional[str] = Header(None),
17
  x_user_email: Optional[str] = Header(None),
@@ -19,16 +20,6 @@ async def get_current_user(
19
  auth_client = get_auth_supabase()
20
  client = get_supabase()
21
 
22
- # Dev mode: no Supabase configured
23
- if client is None:
24
- if x_user_id:
25
- return {
26
- "id": x_user_id,
27
- "name": x_user_name or "User",
28
- "email": x_user_email or f"user{x_user_id}@studymate.ai",
29
- }
30
- return DEV_USER
31
-
32
  # 1. Fall back to x-user-id header first (High performance, used by frontend)
33
  if x_user_id:
34
  return {
@@ -37,23 +28,29 @@ async def get_current_user(
37
  "email": x_user_email,
38
  }
39
 
40
- # 2. If no header, and a Bearer token is present, verify it with Supabase
41
- if token:
 
 
 
 
 
 
 
 
 
 
 
 
42
  try:
43
  verify_client = auth_client if auth_client is not None else client
44
- response = verify_client.auth.get_user(token)
45
  user = getattr(response, "user", None) or response
46
  if user:
47
  return user
48
  except Exception:
49
  pass
50
 
51
- # 3. Last resort: default dev user if everything is missing
52
- if client is None:
53
- return DEV_USER
54
-
55
- if token:
56
- raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Invalid authentication")
57
  raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Not authenticated")
58
 
59
 
 
12
 
13
  async def get_current_user(
14
  token: Optional[str] = Depends(oauth2_scheme),
15
+ x_auth_token: Optional[str] = Header(None),
16
  x_user_id: Optional[str] = Header(None),
17
  x_user_name: Optional[str] = Header(None),
18
  x_user_email: Optional[str] = Header(None),
 
20
  auth_client = get_auth_supabase()
21
  client = get_supabase()
22
 
 
 
 
 
 
 
 
 
 
 
23
  # 1. Fall back to x-user-id header first (High performance, used by frontend)
24
  if x_user_id:
25
  return {
 
28
  "email": x_user_email,
29
  }
30
 
31
+ # 2. Extract the actual Supabase JWT
32
+ # Prefer X-Auth-Token, otherwise use token from Authorization header
33
+ supabase_token = x_auth_token or token
34
+
35
+ # If the token is a Hugging Face token (starts with hf_), ignore it for user auth
36
+ if supabase_token and supabase_token.startswith("hf_"):
37
+ supabase_token = None
38
+
39
+ # Dev mode: no Supabase configured
40
+ if client is None:
41
+ return DEV_USER
42
+
43
+ # 3. Verify the token with Supabase
44
+ if supabase_token:
45
  try:
46
  verify_client = auth_client if auth_client is not None else client
47
+ response = verify_client.auth.get_user(supabase_token)
48
  user = getattr(response, "user", None) or response
49
  if user:
50
  return user
51
  except Exception:
52
  pass
53
 
 
 
 
 
 
 
54
  raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Not authenticated")
55
 
56