OAuth callback JWT token fix

This commit is contained in:
saidsurucu
2025-07-09 22:59:30 +03:00
parent de9e337163
commit b0d7151ba1
+46 -46
View File
@@ -160,53 +160,53 @@ async def oauth_callback(
except Exception as e: except Exception as e:
logger.warning(f"Failed to generate JWT from cookie: {e}") logger.warning(f"Failed to generate JWT from cookie: {e}")
# Last resort - trust Clerk redirect # Only generate authorization code if we have a real JWT token
if not user_authenticated: if user_authenticated and real_jwt_token:
user_authenticated = True # Generate authorization code
logger.info("User authenticated via trusted redirect") auth_code = f"clerk_auth_{os.urandom(16).hex()}"
# For trusted redirect, we can't generate real JWT without session # Store code with JWT token mapping (in production, use proper storage)
# This is expected behavior - real JWT only from JWT token flow # For now, we'll use a simple in-memory storage
logger.warning("Trusted redirect authentication - no real JWT token available") import time
code_data = {
if not user_authenticated: "user_id": user_id,
return JSONResponse( "session_id": session_id,
status_code=401, "real_jwt_token": real_jwt_token,
content={"error": "access_denied", "error_description": "User not authenticated"} "user_authenticated": user_authenticated,
) "created_at": time.time(),
"expires_at": time.time() + 300 # 5 minutes expiry
# Generate authorization code }
auth_code = f"clerk_auth_{os.urandom(16).hex()}"
# Store in module-level dict (in production, use Redis or database)
# Store code with JWT token mapping (in production, use proper storage) if not hasattr(oauth_callback, '_code_storage'):
# For now, we'll use a simple in-memory storage oauth_callback._code_storage = {}
import time oauth_callback._code_storage[auth_code] = code_data
code_data = {
"user_id": user_id, logger.info(f"Stored authorization code with real JWT token")
"session_id": session_id,
"real_jwt_token": real_jwt_token, # Redirect back to client with authorization code
"user_authenticated": user_authenticated, redirect_params = {
"created_at": time.time(), "code": auth_code,
"expires_at": time.time() + 300 # 5 minutes expiry "state": state or ""
} }
# Store in module-level dict (in production, use Redis or database) final_redirect_url = f"{redirect_uri}?{urlencode(redirect_params)}"
if not hasattr(oauth_callback, '_code_storage'): logger.info(f"Redirecting back to client: {final_redirect_url}")
oauth_callback._code_storage = {}
oauth_callback._code_storage[auth_code] = code_data return RedirectResponse(url=final_redirect_url)
else:
logger.info(f"Stored authorization code with JWT token: {bool(real_jwt_token)}") # No JWT token yet - redirect back to sign-in page to wait for authentication
logger.info("No JWT token provided - redirecting back to sign-in to complete authentication")
# Redirect back to client with authorization code
redirect_params = { # Keep the same redirect URL so the flow continues
"code": auth_code, sign_in_params = {
"state": state or "" "redirect_url": f"{request.url._url}" # Current callback URL with all params
} }
final_redirect_url = f"{redirect_uri}?{urlencode(redirect_params)}" sign_in_url = f"https://yargimcp.com/sign-in?{urlencode(sign_in_params)}"
logger.info(f"Redirecting back to client: {final_redirect_url}") logger.info(f"Redirecting back to sign-in: {sign_in_url}")
return RedirectResponse(url=final_redirect_url) return RedirectResponse(url=sign_in_url)
except Exception as e: except Exception as e:
logger.exception(f"Callback processing failed: {e}") logger.exception(f"Callback processing failed: {e}")