feat: auto-fetch codex login otp from mailbox

This commit is contained in:
gameloader
2026-03-18 22:48:46 +08:00
parent 8ba8b1404b
commit 3ca69eb5d3
2 changed files with 49 additions and 2 deletions

View File

@@ -198,12 +198,14 @@ class CodexOAuthHTTPFlow:
otp: str = "",
workspace_id: str = "",
use_browser: bool = False,
mailbox: dict | None = None,
) -> None:
self.authorize_url = authorize_url
self.email = email
self.password = password
self.otp = otp
self.workspace_id = workspace_id
self.mailbox = mailbox
self.auth_base = "https://auth.openai.com"
self.proxy = load_proxy()
self.http = HTTPClient(self.proxy)
@@ -304,13 +306,36 @@ class CodexOAuthHTTPFlow:
print(f"[otp] sending email code via {send_url}")
return self._api_json("GET", send_url, referer=referer)
def _fetch_otp_from_mailbox(self) -> str:
"""Try to fetch OTP from mailbox automatically."""
if not self.mailbox:
return ""
from vmail_client import MailClient
mail = MailClient()
print("[otp] auto-fetching OTP from mailbox...")
try:
code = mail.wait_for_otp(self.mailbox, timeout=120, poll=3.0)
print(f" OTP: {code}")
return code
except Exception as e:
print(f"[otp] auto-fetch failed: {e}")
return ""
def _validate_email_otp(self, referer: str) -> dict[str, Any]:
validate_url = f"{self.auth_base}/api/accounts/email-otp/validate"
resend_url = f"{self.auth_base}/api/accounts/email-otp/resend"
while True:
code = (self.otp or input("Email OTP (or type 'resend'): ").strip()).strip()
self.otp = ""
if self.otp:
code = self.otp.strip()
self.otp = ""
elif self.mailbox:
code = self._fetch_otp_from_mailbox()
if not code:
raise FlowError("Failed to auto-fetch OTP from mailbox")
else:
code = input("Email OTP (or type 'resend'): ").strip()
if not code:
continue