wu981526092 commited on
Commit
b9161fb
·
1 Parent(s): fd80df3

Fix OAuth according to HF official documentation

Browse files

Based on https://huggingface.co/docs/hub/en/spaces-oauth:

1. Fix OAuth scopes:
- Include 'openid profile' as required by HF
- These are automatically included but we specify them explicitly

2. Use SPACE_HOST environment variable:
- Official recommended way to get redirect URI
- Fallback to manual construction if not available

3. Update README.md with proper HF OAuth configuration

This should fix the session/cookie issues we've been experiencing.

Files changed (3) hide show
  1. README.md +1 -0
  2. backend/routers/auth.py +31 -18
  3. utils/environment.py +2 -1
README.md CHANGED
@@ -8,6 +8,7 @@ pinned: false
8
  license: mit
9
  app_port: 7860
10
  hf_oauth: true
 
11
  hf_oauth_scopes:
12
  - read-repos
13
  hf_oauth_expiration_minutes: 480
 
8
  license: mit
9
  app_port: 7860
10
  hf_oauth: true
11
+ # HF automatically includes 'openid profile', we add specific scopes we need
12
  hf_oauth_scopes:
13
  - read-repos
14
  hf_oauth_expiration_minutes: 480
backend/routers/auth.py CHANGED
@@ -71,17 +71,22 @@ async def login(request: Request):
71
  logger.error(f"Failed to save OAuth state: {e}")
72
  raise HTTPException(status_code=500, detail="Session configuration error")
73
 
74
- # Get the current host for redirect URI
75
- base_url = str(request.base_url).rstrip('/')
76
-
77
- # Fix for HF Spaces domain format
78
  if is_huggingface_space():
79
- # HF Spaces uses holistic-ai-agentgraph.hf.space format
80
- space_id = os.getenv("SPACE_ID", "")
81
- if space_id:
82
- # Convert "holistic-ai/AgentGraph" to "holistic-ai-agentgraph.hf.space"
83
- space_domain = space_id.replace("/", "-").lower()
84
- base_url = f"https://{space_domain}.hf.space"
 
 
 
 
 
 
 
 
85
 
86
  redirect_uri = f"{base_url}/auth/callback"
87
 
@@ -130,15 +135,23 @@ async def oauth_callback(request: Request, code: str, state: str):
130
  logger.info("✅ OAuth state verification successful")
131
 
132
  # Exchange code for tokens
133
- base_url = str(request.base_url).rstrip('/')
134
-
135
- # Fix for HF Spaces domain format
136
  if is_huggingface_space():
137
- space_id = os.getenv("SPACE_ID", "")
138
- if space_id:
139
- space_domain = space_id.replace("/", "-").lower()
140
- base_url = f"https://{space_domain}.hf.space"
141
-
 
 
 
 
 
 
 
 
 
 
142
  redirect_uri = f"{base_url}/auth/callback"
143
 
144
  try:
 
71
  logger.error(f"Failed to save OAuth state: {e}")
72
  raise HTTPException(status_code=500, detail="Session configuration error")
73
 
74
+ # Get the current host for redirect URI (HF official way)
 
 
 
75
  if is_huggingface_space():
76
+ # Use SPACE_HOST as recommended by HF docs
77
+ space_host = os.getenv("SPACE_HOST")
78
+ if space_host:
79
+ base_url = f"https://{space_host}"
80
+ else:
81
+ # Fallback to manual construction
82
+ space_id = os.getenv("SPACE_ID", "")
83
+ if space_id:
84
+ space_domain = space_id.replace("/", "-").lower()
85
+ base_url = f"https://{space_domain}.hf.space"
86
+ else:
87
+ base_url = str(request.base_url).rstrip('/')
88
+ else:
89
+ base_url = str(request.base_url).rstrip('/')
90
 
91
  redirect_uri = f"{base_url}/auth/callback"
92
 
 
135
  logger.info("✅ OAuth state verification successful")
136
 
137
  # Exchange code for tokens
138
+ # Get the current host for redirect URI (HF official way)
 
 
139
  if is_huggingface_space():
140
+ # Use SPACE_HOST as recommended by HF docs
141
+ space_host = os.getenv("SPACE_HOST")
142
+ if space_host:
143
+ base_url = f"https://{space_host}"
144
+ else:
145
+ # Fallback to manual construction
146
+ space_id = os.getenv("SPACE_ID", "")
147
+ if space_id:
148
+ space_domain = space_id.replace("/", "-").lower()
149
+ base_url = f"https://{space_domain}.hf.space"
150
+ else:
151
+ base_url = str(request.base_url).rstrip('/')
152
+ else:
153
+ base_url = str(request.base_url).rstrip('/')
154
+
155
  redirect_uri = f"{base_url}/auth/callback"
156
 
157
  try:
utils/environment.py CHANGED
@@ -80,7 +80,8 @@ def get_oauth_config() -> Optional[Dict[str, str]]:
80
 
81
  # Force HF-compatible scope for HF Spaces, ignore environment variable
82
  if is_huggingface_space():
83
- scopes = "read-repos" # Only use supported scope
 
84
  else:
85
  scopes = os.getenv("OAUTH_SCOPES", "read-repos")
86
 
 
80
 
81
  # Force HF-compatible scope for HF Spaces, ignore environment variable
82
  if is_huggingface_space():
83
+ # HF automatically includes 'openid profile', we specify additional scopes
84
+ scopes = "openid profile read-repos"
85
  else:
86
  scopes = os.getenv("OAUTH_SCOPES", "read-repos")
87