Spaces:
Running
Running
fix
Browse files- backend_api.py +32 -2
backend_api.py
CHANGED
|
@@ -274,9 +274,17 @@ def get_auth_from_header(authorization: Optional[str] = None):
|
|
| 274 |
# If username is missing from session (e.g., old session), try to fetch it
|
| 275 |
if not username and session.get("user_info"):
|
| 276 |
user_info = session["user_info"]
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
# Update the session with the username for future requests
|
| 279 |
session["username"] = username
|
|
|
|
| 280 |
|
| 281 |
return MockAuth(session["access_token"], username)
|
| 282 |
|
|
@@ -385,6 +393,18 @@ async def oauth_callback(code: str, state: str, request: Request):
|
|
| 385 |
userinfo_response.raise_for_status()
|
| 386 |
user_info = userinfo_response.json()
|
| 387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
# Calculate token expiration
|
| 389 |
# OAuth tokens typically have expires_in in seconds
|
| 390 |
expires_in = token_data.get("expires_in", 28800) # Default 8 hours
|
|
@@ -397,10 +417,12 @@ async def oauth_callback(code: str, state: str, request: Request):
|
|
| 397 |
"user_info": user_info,
|
| 398 |
"timestamp": datetime.now(),
|
| 399 |
"expires_at": expires_at,
|
| 400 |
-
"username":
|
| 401 |
"deployed_spaces": [] # Track deployed spaces for follow-up updates
|
| 402 |
}
|
| 403 |
|
|
|
|
|
|
|
| 404 |
# Redirect to frontend with session token
|
| 405 |
frontend_url = f"{protocol}://{SPACE_HOST}/?session={session_token}"
|
| 406 |
return RedirectResponse(url=frontend_url)
|
|
@@ -866,8 +888,16 @@ async def deploy(
|
|
| 866 |
authorization: Optional[str] = Header(None)
|
| 867 |
):
|
| 868 |
"""Deploy generated code to HuggingFace Spaces"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
auth = get_auth_from_header(authorization)
|
| 870 |
|
|
|
|
|
|
|
| 871 |
if not auth.is_authenticated():
|
| 872 |
raise HTTPException(status_code=401, detail="Authentication required")
|
| 873 |
|
|
|
|
| 274 |
# If username is missing from session (e.g., old session), try to fetch it
|
| 275 |
if not username and session.get("user_info"):
|
| 276 |
user_info = session["user_info"]
|
| 277 |
+
# Use same order as OAuth callback for consistency
|
| 278 |
+
username = (
|
| 279 |
+
user_info.get("preferred_username") or
|
| 280 |
+
user_info.get("name") or
|
| 281 |
+
user_info.get("sub") or
|
| 282 |
+
user_info.get("username") or
|
| 283 |
+
"user"
|
| 284 |
+
)
|
| 285 |
# Update the session with the username for future requests
|
| 286 |
session["username"] = username
|
| 287 |
+
print(f"[Auth] Extracted and cached username from user_info: {username}")
|
| 288 |
|
| 289 |
return MockAuth(session["access_token"], username)
|
| 290 |
|
|
|
|
| 393 |
userinfo_response.raise_for_status()
|
| 394 |
user_info = userinfo_response.json()
|
| 395 |
|
| 396 |
+
# Extract username - try multiple possible fields
|
| 397 |
+
username = (
|
| 398 |
+
user_info.get("preferred_username") or # Primary HF field
|
| 399 |
+
user_info.get("name") or # Alternative field
|
| 400 |
+
user_info.get("sub") or # OpenID subject
|
| 401 |
+
user_info.get("username") or # Generic username
|
| 402 |
+
"user" # Fallback
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
print(f"[OAuth] User info received: {user_info}")
|
| 406 |
+
print(f"[OAuth] Extracted username: {username}")
|
| 407 |
+
|
| 408 |
# Calculate token expiration
|
| 409 |
# OAuth tokens typically have expires_in in seconds
|
| 410 |
expires_in = token_data.get("expires_in", 28800) # Default 8 hours
|
|
|
|
| 417 |
"user_info": user_info,
|
| 418 |
"timestamp": datetime.now(),
|
| 419 |
"expires_at": expires_at,
|
| 420 |
+
"username": username,
|
| 421 |
"deployed_spaces": [] # Track deployed spaces for follow-up updates
|
| 422 |
}
|
| 423 |
|
| 424 |
+
print(f"[OAuth] Session created: {session_token[:10]}... for user: {username}")
|
| 425 |
+
|
| 426 |
# Redirect to frontend with session token
|
| 427 |
frontend_url = f"{protocol}://{SPACE_HOST}/?session={session_token}"
|
| 428 |
return RedirectResponse(url=frontend_url)
|
|
|
|
| 888 |
authorization: Optional[str] = Header(None)
|
| 889 |
):
|
| 890 |
"""Deploy generated code to HuggingFace Spaces"""
|
| 891 |
+
print(f"[Deploy] ========== NEW DEPLOYMENT REQUEST ==========")
|
| 892 |
+
print(f"[Deploy] Authorization header present: {authorization is not None}")
|
| 893 |
+
if authorization:
|
| 894 |
+
auth_preview = authorization[:20] + "..." if len(authorization) > 20 else authorization
|
| 895 |
+
print(f"[Deploy] Authorization preview: {auth_preview}")
|
| 896 |
+
|
| 897 |
auth = get_auth_from_header(authorization)
|
| 898 |
|
| 899 |
+
print(f"[Deploy] Auth object - is_authenticated: {auth.is_authenticated()}, username: {auth.username}, has_token: {auth.token is not None}")
|
| 900 |
+
|
| 901 |
if not auth.is_authenticated():
|
| 902 |
raise HTTPException(status_code=401, detail="Authentication required")
|
| 903 |
|