223 lines
7.8 KiB
Python
223 lines
7.8 KiB
Python
"""Composio: browser login -> skip passkey -> cliKey -> project api key."""
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
import time
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
EMAIL = sys.argv[1] if len(sys.argv) > 1 else "0618455607-001@t-online.de"
|
|
PASSWORD = sys.argv[2] if len(sys.argv) > 2 else "pUX@e6ykq3YE"
|
|
IMAP_PASS = sys.argv[3] if len(sys.argv) > 3 else "nurmir11051943"
|
|
PROFILE = "C:/Users/User/tmp/composio/chrome_prof"
|
|
CHROME = "C:/Program Files/Google/Chrome/Application/chrome.exe"
|
|
if not os.path.exists(CHROME):
|
|
CHROME = os.path.expandvars(r"%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe")
|
|
OUT = "C:/Users/User/tmp/composio"
|
|
DASH = "https://dashboard.composio.dev"
|
|
LOGIN = "https://login.composio.dev"
|
|
|
|
|
|
def log(*a):
|
|
print(*a, flush=True)
|
|
|
|
|
|
def shot(pg, name):
|
|
try:
|
|
pg.screenshot(path=f"{OUT}/bshot_{name}.png", full_page=False)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def dump(pg, name):
|
|
try:
|
|
open(f"{OUT}/bhtml_{name}.html", "w", encoding="utf-8").write(pg.content())
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
def wait_any(pg, sels, timeout=45):
|
|
end = time.time() + timeout
|
|
while time.time() < end:
|
|
for s in sels:
|
|
try:
|
|
if pg.locator(s).count() > 0 and pg.locator(s).first.is_visible():
|
|
return s
|
|
except Exception:
|
|
pass
|
|
pg.wait_for_timeout(700)
|
|
return None
|
|
|
|
|
|
def imap_code(mail, pw, since_ts=None, tries=18, delay=8):
|
|
import imaplib, email as emod, re as r2
|
|
from email.utils import parsedate_to_datetime
|
|
for _ in range(tries):
|
|
best = None
|
|
try:
|
|
M = imaplib.IMAP4_SSL("secureimap.t-online.de")
|
|
M.login(mail, pw)
|
|
M.select("INBOX")
|
|
typ, data = M.search(None, '(FROM "login.composio.dev")')
|
|
ids = data[0].split()
|
|
for i in reversed(ids[-5:]):
|
|
typ, msgd = M.fetch(i, "(RFC822)")
|
|
item = msgd[0]
|
|
raw = item[1] if isinstance(item, tuple) else None
|
|
if not raw:
|
|
continue
|
|
m = emod.message_from_bytes(raw)
|
|
try:
|
|
dts = parsedate_to_datetime(m["Date"]).timestamp()
|
|
except Exception:
|
|
dts = 0
|
|
if since_ts and dts < since_ts - 180:
|
|
continue
|
|
body = b""
|
|
if m.is_multipart():
|
|
for part in m.walk():
|
|
if part.get_content_type() == "text/plain":
|
|
body = part.get_payload(decode=True) or b""
|
|
break
|
|
else:
|
|
body = m.get_payload(decode=True) or b""
|
|
txt = (body or b"").decode("utf-8", "ignore")
|
|
codes = r2.findall(r"(\d{6})", txt)
|
|
if codes:
|
|
log(" imap mail date:", m["Date"], "code:", codes[-1])
|
|
M.logout()
|
|
return codes[-1]
|
|
except Exception as e:
|
|
log(" imap err:", e)
|
|
try:
|
|
M.logout()
|
|
except Exception:
|
|
pass
|
|
time.sleep(delay)
|
|
return None
|
|
|
|
|
|
T0 = time.time()
|
|
with sync_playwright() as p:
|
|
ctx = p.chromium.launch_persistent_context(
|
|
PROFILE, executable_path=CHROME, headless=False, viewport={"width": 1280, "height": 900},
|
|
args=["--disable-blink-features=AutomationControlled", "--no-first-run"],
|
|
)
|
|
pg = ctx.pages[0] if ctx.pages else ctx.new_page()
|
|
pg.set_default_timeout(30000)
|
|
log("[1] goto entry")
|
|
pg.goto(DASH + "/f/~/connect/clients", wait_until="domcontentloaded")
|
|
pg.wait_for_timeout(4000)
|
|
log(" url:", pg.url)
|
|
dump(pg, "1_entry")
|
|
# email step
|
|
sel = wait_any(pg, ['input[name="email"]', 'input[type="email"]'])
|
|
if not sel:
|
|
log("no email field; url:", pg.url)
|
|
shot(pg, "no_email")
|
|
ctx.close()
|
|
sys.exit(2)
|
|
log("[2] fill email")
|
|
pg.fill('input[name="email"]', EMAIL)
|
|
pg.wait_for_timeout(400)
|
|
btn = wait_any(pg, ['button[type="submit"]'], 10)
|
|
if btn:
|
|
try:
|
|
pg.locator('button[type="submit"]').first.click()
|
|
except Exception:
|
|
pg.keyboard.press("Enter")
|
|
else:
|
|
pg.keyboard.press("Enter")
|
|
pg.wait_for_timeout(5000)
|
|
log(" url:", pg.url)
|
|
dump(pg, "2_after_email")
|
|
# password step
|
|
sel = wait_any(pg, ['input[name="password"]', 'input[type="password"]'], 25)
|
|
if sel:
|
|
log("[3] fill password")
|
|
pg.fill(sel, PASSWORD)
|
|
pg.wait_for_timeout(300)
|
|
try:
|
|
pg.locator('button[type="submit"]').first.click()
|
|
except Exception:
|
|
pg.keyboard.press("Enter")
|
|
pg.wait_for_timeout(6000)
|
|
log(" url:", pg.url)
|
|
dump(pg, "3_after_pw")
|
|
# OTP radar challenge
|
|
if "radar-challenge" in pg.url or pg.locator('input[autocomplete="one-time-code"]').count() > 0:
|
|
log("[3b] otp challenge")
|
|
dump(pg, "3b_otp")
|
|
code = imap_code(EMAIL, IMAP_PASS, T0)
|
|
if not code:
|
|
log(" no otp code")
|
|
ctx.close()
|
|
sys.exit(3)
|
|
box = pg.locator('[data-test="otp-input"]')
|
|
if box.count() == 0:
|
|
box = pg.locator('input[name="code"]')
|
|
log(" otp inputs:", box.count())
|
|
box.first.click(force=True)
|
|
pg.keyboard.type(code, delay=140)
|
|
pg.wait_for_timeout(7000)
|
|
log(" url:", pg.url)
|
|
dump(pg, "3b_after_otp")
|
|
# passkey interstitial
|
|
if "passkey" in pg.url or pg.locator('button[name="intent"][value="skip"]').count() > 0:
|
|
log("[4] passkey page, clicking skip")
|
|
dump(pg, "4_passkey")
|
|
try:
|
|
pg.locator('button[name="intent"][value="skip"]').first.click()
|
|
except Exception:
|
|
try:
|
|
pg.get_by_text("Vorerst überspringen").click()
|
|
except Exception as e:
|
|
log(" skip click failed:", e)
|
|
pg.wait_for_timeout(7000)
|
|
log(" url:", pg.url)
|
|
dump(pg, "4_after_skip")
|
|
# wait for dashboard
|
|
for i in range(20):
|
|
if "dashboard.composio.dev" in pg.url and "login" not in pg.url:
|
|
break
|
|
pg.wait_for_timeout(1500)
|
|
log("[5] url now:", pg.url)
|
|
dump(pg, "5_final")
|
|
shot(pg, "5_final")
|
|
cookies = ctx.cookies()
|
|
open(f"{OUT}/bctx_cookies.json", "w", encoding="utf-8").write(json.dumps(cookies, indent=1))
|
|
log(" cookies:", len(cookies), [c["name"] for c in cookies][:8])
|
|
# cliKey flow on dashboard origin
|
|
if "dashboard.composio.dev" not in pg.url:
|
|
pg.goto(DASH + "/", wait_until="domcontentloaded")
|
|
pg.wait_for_timeout(3000)
|
|
log("[6] dashboard url:", pg.url)
|
|
r = pg.evaluate(
|
|
"""async () => {
|
|
const res = await fetch('/api/v3.1/cli/create-session', {
|
|
method: 'POST', headers: {'content-type': 'application/json'},
|
|
body: JSON.stringify({scope: 'project'}), credentials: 'include'});
|
|
return res.status + ' ' + (await res.text()).slice(0, 300);
|
|
}"""
|
|
)
|
|
log("[6] create-session:", r)
|
|
m = re.search(r'"id"\s*:\s*"([^"]+)"', r) or re.search(r'\{"id":"([^"]+)"\}', r)
|
|
sid = m.group(1) if m else None
|
|
if not sid:
|
|
m2 = re.search(r'[0-9a-f]{20,40}', r)
|
|
sid = m2.group(0) if m2 else None
|
|
log(" session id:", sid)
|
|
if sid:
|
|
pg.goto(f"{DASH}/?cliKey={sid}", wait_until="domcontentloaded")
|
|
pg.wait_for_timeout(4000)
|
|
dump(pg, "6_approve")
|
|
r2 = pg.evaluate(
|
|
"""async (sid) => {
|
|
const res = await fetch('/api/v3.1/cli/get-session?id=' + encodeURIComponent(sid),
|
|
{credentials: 'include'});
|
|
return res.status + ' ' + (await res.text()).slice(0, 800);
|
|
}""", sid)
|
|
log("[7] get-session:", r2)
|
|
open(f"{OUT}/bsession_keys.txt", "w", encoding="utf-8").write(r2)
|
|
ctx.close() |