Spaces:
Runtime error
Runtime error
Commit ·
c7aa1ef
1
Parent(s): cef93c1
added latest models
Browse files- app/main.py +12 -8
- app/oauth_google.py +7 -16
app/main.py
CHANGED
|
@@ -49,6 +49,18 @@ default_lm = dspy.LM(default_model, max_tokens=3200,api_key=os.getenv(provider+'
|
|
| 49 |
|
| 50 |
dspy.configure(lm=default_lm)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# CORS middleware - allow frontend to access backend
|
| 53 |
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:5173")
|
| 54 |
# Support multiple origins (local dev + production)
|
|
@@ -61,14 +73,6 @@ app.add_middleware(
|
|
| 61 |
allow_headers=["*"],
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# Session support for OAuth (Authlib requires request.session)
|
| 65 |
-
app.add_middleware(
|
| 66 |
-
SessionMiddleware,
|
| 67 |
-
secret_key=os.getenv("SESSION_SECRET", "change-this-session-secret"),
|
| 68 |
-
same_site="lax",
|
| 69 |
-
https_only=bool(int(os.getenv("SESSION_HTTPS_ONLY", "0"))),
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
|
| 73 |
from .schemas.auth import LoginRequest
|
| 74 |
|
|
|
|
| 49 |
|
| 50 |
dspy.configure(lm=default_lm)
|
| 51 |
|
| 52 |
+
# Session support for OAuth (Authlib requires request.session)
|
| 53 |
+
# NOTE: SessionMiddleware must be added BEFORE CORSMiddleware (middlewares execute in reverse order)
|
| 54 |
+
# For production HTTPS with cross-site OAuth, use same_site="none" with https_only=True
|
| 55 |
+
is_production = os.getenv("SESSION_HTTPS_ONLY", "0") == "1"
|
| 56 |
+
app.add_middleware(
|
| 57 |
+
SessionMiddleware,
|
| 58 |
+
secret_key=os.getenv("SESSION_SECRET", "change-this-session-secret"),
|
| 59 |
+
same_site="none" if is_production else "lax", # "none" required for cross-site OAuth on HTTPS
|
| 60 |
+
https_only=is_production,
|
| 61 |
+
max_age=3600, # 1 hour session lifetime
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
# CORS middleware - allow frontend to access backend
|
| 65 |
frontend_url = os.getenv("FRONTEND_URL", "http://localhost:5173")
|
| 66 |
# Support multiple origins (local dev + production)
|
|
|
|
| 73 |
allow_headers=["*"],
|
| 74 |
)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
from .schemas.auth import LoginRequest
|
| 78 |
|
app/oauth_google.py
CHANGED
|
@@ -38,18 +38,14 @@ async def login(request: Request):
|
|
| 38 |
@router.get("/callback")
|
| 39 |
async def callback(request: Request, db: Session = Depends(get_db)):
|
| 40 |
try:
|
| 41 |
-
#
|
| 42 |
-
if not request.session.get("_state_google_"):
|
| 43 |
-
logger.warning("OAuth state missing from session - user may need to retry login")
|
| 44 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?error=session_expired&message=Please try logging in again")
|
| 45 |
-
|
| 46 |
token = await oauth.google.authorize_access_token(request)
|
| 47 |
userinfo = token.get("userinfo") or await oauth.google.parse_id_token(request, token)
|
| 48 |
|
| 49 |
email = userinfo.get("email")
|
| 50 |
if not email:
|
| 51 |
logger.error("No email in Google OAuth response")
|
| 52 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?error=missing_email")
|
| 53 |
|
| 54 |
user = db.query(User).filter(User.email == email).first()
|
| 55 |
is_new_user = not user
|
|
@@ -73,23 +69,18 @@ async def callback(request: Request, db: Session = Depends(get_db)):
|
|
| 73 |
logger.error(f"Failed to assign free tier to user {user.id}: {e}")
|
| 74 |
|
| 75 |
jwt_token = create_access_token(str(user.id), {"email": user.email})
|
| 76 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?{urlencode({'token': jwt_token})}")
|
| 77 |
-
|
| 78 |
-
except KeyError as e:
|
| 79 |
-
# This often happens when OAuth state is missing
|
| 80 |
-
logger.error(f"OAuth state error (session issue): {e}", exc_info=True)
|
| 81 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?error=session_expired&message=Session expired, please try again")
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
error_msg = str(e).lower()
|
| 85 |
logger.error(f"OAuth callback error: {e}", exc_info=True)
|
| 86 |
|
| 87 |
# Provide more specific error messages
|
| 88 |
-
if "state" in error_msg or "mismatch" in error_msg:
|
| 89 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?error=session_expired&message=Please try logging in again")
|
| 90 |
elif "token" in error_msg:
|
| 91 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?error=token_error&message=Authentication failed, please retry")
|
| 92 |
else:
|
| 93 |
-
return RedirectResponse(url=f"{FRONTEND_URL}/?error=login_failed&message=Login failed, please try again")
|
| 94 |
|
| 95 |
|
|
|
|
| 38 |
@router.get("/callback")
|
| 39 |
async def callback(request: Request, db: Session = Depends(get_db)):
|
| 40 |
try:
|
| 41 |
+
# Let authlib handle the OAuth state validation
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
token = await oauth.google.authorize_access_token(request)
|
| 43 |
userinfo = token.get("userinfo") or await oauth.google.parse_id_token(request, token)
|
| 44 |
|
| 45 |
email = userinfo.get("email")
|
| 46 |
if not email:
|
| 47 |
logger.error("No email in Google OAuth response")
|
| 48 |
+
return RedirectResponse(url=f"{FRONTEND_URL}/?error=missing_email", status_code=302)
|
| 49 |
|
| 50 |
user = db.query(User).filter(User.email == email).first()
|
| 51 |
is_new_user = not user
|
|
|
|
| 69 |
logger.error(f"Failed to assign free tier to user {user.id}: {e}")
|
| 70 |
|
| 71 |
jwt_token = create_access_token(str(user.id), {"email": user.email})
|
| 72 |
+
return RedirectResponse(url=f"{FRONTEND_URL}/?{urlencode({'token': jwt_token})}", status_code=302)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
error_msg = str(e).lower()
|
| 76 |
logger.error(f"OAuth callback error: {e}", exc_info=True)
|
| 77 |
|
| 78 |
# Provide more specific error messages
|
| 79 |
+
if "state" in error_msg or "mismatch" in error_msg or "missingstateerror" in error_msg:
|
| 80 |
+
return RedirectResponse(url=f"{FRONTEND_URL}/?error=session_expired&message=Please try logging in again", status_code=302)
|
| 81 |
elif "token" in error_msg:
|
| 82 |
+
return RedirectResponse(url=f"{FRONTEND_URL}/?error=token_error&message=Authentication failed, please retry", status_code=302)
|
| 83 |
else:
|
| 84 |
+
return RedirectResponse(url=f"{FRONTEND_URL}/?error=login_failed&message=Login failed, please try again", status_code=302)
|
| 85 |
|
| 86 |
|