OAuth callback JWT token fix

This commit is contained in:
saidsurucu
2025-07-09 22:59:30 +03:00
parent de9e337163
commit b0d7151ba1
+38 -38
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 = {
"user_id": user_id,
"session_id": session_id,
"real_jwt_token": real_jwt_token,
"user_authenticated": user_authenticated,
"created_at": time.time(),
"expires_at": time.time() + 300 # 5 minutes expiry
}
if not user_authenticated: # Store in module-level dict (in production, use Redis or database)
return JSONResponse( if not hasattr(oauth_callback, '_code_storage'):
status_code=401, oauth_callback._code_storage = {}
content={"error": "access_denied", "error_description": "User not authenticated"} oauth_callback._code_storage[auth_code] = code_data
)
# Generate authorization code logger.info(f"Stored authorization code with real JWT token")
auth_code = f"clerk_auth_{os.urandom(16).hex()}"
# Store code with JWT token mapping (in production, use proper storage) # Redirect back to client with authorization code
# For now, we'll use a simple in-memory storage redirect_params = {
import time "code": auth_code,
code_data = { "state": state or ""
"user_id": user_id, }
"session_id": session_id,
"real_jwt_token": real_jwt_token,
"user_authenticated": user_authenticated,
"created_at": time.time(),
"expires_at": time.time() + 300 # 5 minutes expiry
}
# 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
logger.info(f"Stored authorization code with JWT token: {bool(real_jwt_token)}") return RedirectResponse(url=final_redirect_url)
else:
# 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 # Keep the same redirect URL so the flow continues
redirect_params = { sign_in_params = {
"code": auth_code, "redirect_url": f"{request.url._url}" # Current callback URL with all params
"state": state or "" }
}
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}")