Spaces:
Sleeping
Sleeping
fix
Browse files273 unmodified lines274275276277278279280281282102 unmodified lines3853863873883893906 unmodified lines397398399400401402403404405406459 unmodified lines866867868869870871872873273 unmodified lines# If username is missing from session (e.g., old session), try to fetch itif not username and session.get("user_info"):user_info = session["user_info"]username = user_info.get("name") or user_info.get("preferred_username") or "user"# Update the session with the username for future requestssession["username"] = usernamereturn MockAuth(session["access_token"], username)102 unmodified linesuserinfo_response.raise_for_status()user_info = userinfo_response.json()# Calculate token expiration# OAuth tokens typically have expires_in in secondsexpires_in = token_data.get("expires_in", 28800) # Default 8 hours6 unmodified lines"user_info": user_info,"timestamp": datetime.now(),"expires_at": expires_at,"username": user_info.get("name") or user_info.get("preferred_username") or "user","deployed_spaces": [] # Track deployed spaces for follow-up updates}# Redirect to frontend with session tokenfrontend_url = f"{protocol}://{SPACE_HOST}/?session={session_token}"return RedirectResponse(url=frontend_url)459 unmodified linesauthorization: Optional[str] = Header(None)):"""Deploy generated code to HuggingFace Spaces"""auth = get_auth_from_header(authorization)if not auth.is_authenticated():raise HTTPException(status_code=401, detail="Authentication required")273 unmodified lines274275276277278279280281282283284285286287288289290102 unmodified lines3933943953963973983994004014024034044054064074084094106 unmodified lines417418419420421422423424425426427428459 unmodified lines888889890891892893894895896897898899900901902903273 unmodified lines# If username is missing from session (e.g., old session), try to fetch itif not username and session.get("user_info"):user_info = session["user_info"]# Use same order as OAuth callback for consistencyusername = (user_info.get("preferred_username") oruser_info.get("name") oruser_info.get("sub") oruser_info.get("username") or"user")# Update the session with the username for future requestssession["username"] = usernameprint(f"[Auth] Extracted and cached username from user_info: {username}")return MockAuth(session["access_token"], username)102 unmodified linesuserinfo_response.raise_for_status()user_info = userinfo_response.json()# Extract username - try multiple possible fieldsusername = (user_info.get("preferred_username") or # Primary HF fielduser_info.get("name") or # Alternative fielduser_info.get("sub") or # OpenID subjectuser_info.get("username") or # Generic username"user" # Fallback)print(f"[OAuth] User info received: {user_info}")print(f"[OAuth] Extracted username: {username}")# Calculate token expiration# OAuth tokens typically have expires_in in secondsexpires_in = token_data.get("expires_in", 28800) # Default 8 hours6 unmodified lines"user_info": user_info,"timestamp": datetime.now(),"expires_at": expires_at,"username": username,"deployed_spaces": [] # Track deployed spaces for follow-up updates}print(f"[OAuth] Session created: {session_token[:10]}... for user: {username}")# Redirect to frontend with session tokenfrontend_url = f"{protocol}://{SPACE_HOST}/?session={session_token}"return RedirectResponse(url=frontend_url)459 unmodified linesauthorization: Optional[str] = Header(None)):"""Deploy generated code to HuggingFace Spaces"""print(f"[Deploy] ========== NEW DEPLOYMENT REQUEST ==========")print(f"[Deploy] Authorization header present: {authorization is not None}")if authorization:auth_preview = authorization[:20] + "..." if len(authorization) > 20 else authorizationprint(f"[Deploy] Authorization preview: {auth_preview}")auth = get_auth_from_header(authorization)print(f"[Deploy] Auth object - is_authenticated: {auth.is_authenticated()}, username: {auth.username}, has_token: {auth.token is not None}")if not auth.is_authenticated():raise HTTPException(status_code=401, detail="Authentication required")
backend_api.py+32-2