Spaces:
Sleeping
Sleeping
fix: force HTTPS scheme for HF proxy in Google OAuth
Browse files- app/api/routes/auth.py +11 -5
- test_pydantic.py +17 -0
app/api/routes/auth.py
CHANGED
|
@@ -33,8 +33,14 @@ async def login_google(request: Request):
|
|
| 33 |
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
| 34 |
detail="Google OAuth is not configured on the server."
|
| 35 |
)
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
redirect_uri = settings.GOOGLE_REDIRECT_URI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return await oauth.google.authorize_redirect(request, redirect_uri)
|
| 39 |
|
| 40 |
|
|
@@ -74,7 +80,7 @@ async def auth_google(request: Request, db: Session = Depends(get_db)):
|
|
| 74 |
redirect_url = f"{settings.FRONTEND_URL}/auth/callback?token={access_token}"
|
| 75 |
return RedirectResponse(url=redirect_url)
|
| 76 |
except Exception as e:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
)
|
|
|
|
| 33 |
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
| 34 |
detail="Google OAuth is not configured on the server."
|
| 35 |
)
|
| 36 |
+
|
| 37 |
+
# Always use the explicit setting — never derive from request.url
|
| 38 |
+
# (Hugging Face proxies strip https → causes redirect_uri mismatch)
|
| 39 |
redirect_uri = settings.GOOGLE_REDIRECT_URI
|
| 40 |
+
|
| 41 |
+
# Tell Authlib the real scheme so the state URL is correct too
|
| 42 |
+
request.scope["scheme"] = "https"
|
| 43 |
+
|
| 44 |
return await oauth.google.authorize_redirect(request, redirect_uri)
|
| 45 |
|
| 46 |
|
|
|
|
| 80 |
redirect_url = f"{settings.FRONTEND_URL}/auth/callback?token={access_token}"
|
| 81 |
return RedirectResponse(url=redirect_url)
|
| 82 |
except Exception as e:
|
| 83 |
+
# Redirect to frontend with error info instead of showing raw JSON
|
| 84 |
+
error_msg = str(e).replace('"', "'")
|
| 85 |
+
frontend_error_url = f"{settings.FRONTEND_URL}/login?error={error_msg[:200]}"
|
| 86 |
+
return RedirectResponse(url=frontend_error_url)
|
test_pydantic.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pydantic_settings import BaseSettings
|
| 3 |
+
from typing import List, Union
|
| 4 |
+
from pydantic import field_validator
|
| 5 |
+
|
| 6 |
+
class Settings(BaseSettings):
|
| 7 |
+
ALLOWED_ORIGINS: Union[str, List[str]] = ["http://localhost:3000"]
|
| 8 |
+
|
| 9 |
+
@field_validator("ALLOWED_ORIGINS", mode="before")
|
| 10 |
+
@classmethod
|
| 11 |
+
def assemble_cors_origins(cls, v):
|
| 12 |
+
if isinstance(v, str) and not v.startswith("["):
|
| 13 |
+
return [i.strip() for i in v.split(",") if i.strip()]
|
| 14 |
+
return v
|
| 15 |
+
|
| 16 |
+
os.environ["ALLOWED_ORIGINS"] = "http://localhost:3000,http://localhost:8080"
|
| 17 |
+
print(Settings().ALLOWED_ORIGINS)
|