#!/usr/bin/env python3 """Haze (usehaze.ai) autoreg: bootstrap -> IMAP code -> verify-code -> token saved. Usage: python reg_one.py Saves token to token.txt, profile JSON to profile.json. No secrets printed. """ import json, sys, time, imaplib, email, re, subprocess API = "https://usehaze.ai/api" UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" def curl_post(path, payload): r = subprocess.run([ "curl", "-s", "-X", "POST", API + path, "-H", "Content-Type: application/json", "-H", "User-Agent: " + UA, "-d", json.dumps(payload), "-w", "\n%{http_code}", ], capture_output=True, text=True, timeout=60) body, _, code = r.stdout.rpartition("\n") try: return int(code), json.loads(body) except Exception: return int(code or 0), {"raw": body[:300]} def get_otp(email_addr, email_pass, imap_host="secureimap.t-online.de", tries=12, delay=5): for t in range(tries): try: m = imaplib.IMAP4_SSL(imap_host, 993, timeout=30) m.login(email_addr, email_pass) m.select("INBOX") typ, data = m.search(None, '(FROM "welcome@workos.dev")') ids = data[0].split() if ids: typ, d = m.fetch(ids[-1], "(RFC822)") raw = d[0][1].decode("utf-8", "ignore") msg = email.message_from_string(raw) body = "" if msg.is_multipart(): for p in msg.walk(): if p.get_content_type() == "text/plain": body = p.get_payload(decode=True).decode("utf-8", "ignore") break else: body = msg.get_payload(decode=True).decode("utf-8", "ignore") codes = re.findall(r"\b(\d{6})\b", body) m.logout() if codes: return codes[-1] m.logout() except Exception as e: print(f" imap try {t}: {e}") time.sleep(delay) return None def main(): mail, password = sys.argv[1], sys.argv[2] print(f"[1] bootstrap create {mail}") code, res = curl_post("/auth/bootstrap", {"email": mail, "password": password, "create": True}) print(f" -> {code} keys={list(res.keys())}") if code != 200: print("FAIL bootstrap:", json.dumps(res)[:300]) return 1 if "accessToken" in res: tok = res["accessToken"] open("token.txt", "w").write(tok) json.dump(res.get("profile", {}), open("profile.json", "w"), indent=1) print(" direct session, plan:", res.get("profile", {}).get("plan")) return 0 if res.get("status") != "verification_required": print("UNEXPECTED:", json.dumps(res)[:300]) return 1 pending = res["pendingToken"] print(f"[2] waiting for OTP on IMAP ...") otp = get_otp(mail, MAIL_PASS) if not otp: print("FAIL: no OTP") return 2 print(f"[3] verify-code (otp len {len(otp)})") code, res = curl_post("/auth/verify-code", {"code": otp, "pending_authentication_token": pending}) print(f" -> {code}") if "accessToken" not in res: print("FAIL verify:", json.dumps(res)[:300]) return 3 open("token.txt", "w").write(res["accessToken"]) open("refresh.txt", "w").write(res.get("refreshToken", "")) prof = res.get("profile", {}) json.dump(prof, open("profile.json", "w"), indent=1) print(f"OK token saved ({len(res['accessToken'])} chars). plan={prof.get('plan')} handle={prof.get('handle')}") return 0 if __name__ == "__main__": MAIL_PASS = sys.argv[3] sys.exit(main())