|
| 1 | +import os |
| 2 | +import base64 |
| 3 | +import pickle |
| 4 | +import logging |
| 5 | +from bs4 import BeautifulSoup |
| 6 | + |
| 7 | + |
| 8 | +class Auth: |
| 9 | + CAPTCHA_FILENAME = "captcha.svg" |
| 10 | + COOKIES_FILENAME = "cookies.pk" |
| 11 | + |
| 12 | + def __init__(self, username, password): |
| 13 | + self.username = username |
| 14 | + self.password = password |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def get_token(cls, session): |
| 18 | + req = session.get("https://chat.openai.com/api/auth/session") |
| 19 | + return req.json().get("accessToken") |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def save_cookies(cls, session): |
| 23 | + with open(cls.COOKIES_FILENAME, "wb") as f: |
| 24 | + pickle.dump(session.cookies.jar._cookies, f) |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def load_cookies(cls, cookies): |
| 28 | + if not os.path.isfile(cls.COOKIES_FILENAME): |
| 29 | + return None |
| 30 | + with open(cls.COOKIES_FILENAME, "rb") as f: |
| 31 | + jar_cookies = pickle.load(f) |
| 32 | + for domain, pc in jar_cookies.items(): |
| 33 | + for path, c in pc.items(): |
| 34 | + for k, v in c.items(): |
| 35 | + cookies.set(k, v.value, domain=domain, path=path) |
| 36 | + return cookies |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def check_auth(cls, session): |
| 40 | + token = cls.get_token(session) |
| 41 | + return token |
| 42 | + |
| 43 | + def _get_state(self, resp): |
| 44 | + return resp.find("input", {"name": "state", "type": "hidden"}).get("value") |
| 45 | + |
| 46 | + def _captcha_handler(self, resp): |
| 47 | + captcha = resp.find("img", {"alt": "captcha"}) |
| 48 | + if captcha: |
| 49 | + src = captcha.get("src").split(",")[1] |
| 50 | + with open(self.CAPTCHA_FILENAME, "wb") as fh: |
| 51 | + fh.write(base64.urlsafe_b64decode(src)) |
| 52 | + logging.info("Please enter captcha code: ") |
| 53 | + code = input() |
| 54 | + if os.path.isfile(self.CAPTCHA_FILENAME): |
| 55 | + os.remove(self.CAPTCHA_FILENAME) |
| 56 | + return code |
| 57 | + |
| 58 | + def _block_handler(self, resp): |
| 59 | + s = "We have detected a potential security issue with this account" |
| 60 | + return s in str(resp) |
| 61 | + |
| 62 | + def perform_login(self, session): |
| 63 | + req = session.get("https://chat.openai.com/api/auth/csrf") |
| 64 | + csrf = req.json().get("csrfToken") |
| 65 | + |
| 66 | + session.headers.update({"content-type": "application/x-www-form-urlencoded"}) |
| 67 | + params = { |
| 68 | + "callbackUrl": "/", |
| 69 | + "csrfToken": csrf, |
| 70 | + "json": "true" |
| 71 | + } |
| 72 | + req = session.post( |
| 73 | + "https://chat.openai.com/api/auth/signin/auth0?prompt=login", data=params |
| 74 | + ) |
| 75 | + login_link = req.json().get("url") |
| 76 | + |
| 77 | + req = session.get(login_link) |
| 78 | + response = BeautifulSoup(req.text, features="html.parser") |
| 79 | + |
| 80 | + while True: # try to auth |
| 81 | + state = self._get_state(response) |
| 82 | + code = self._captcha_handler(response) |
| 83 | + |
| 84 | + session.headers.update({ |
| 85 | + "content-type": "application/x-www-form-urlencoded" |
| 86 | + }) |
| 87 | + params = { |
| 88 | + "state": state, |
| 89 | + "username": self.username, |
| 90 | + "captcha": code, |
| 91 | + "js-available": "true", |
| 92 | + "webauthn-available": "true", |
| 93 | + "is-brave": "false", |
| 94 | + "webauthn-platform-available": "false", |
| 95 | + "action": "default" |
| 96 | + } |
| 97 | + url = f"https://auth0.openai.com/u/login/identifier?state={state}" |
| 98 | + req = session.post(url, data=params) |
| 99 | + response = BeautifulSoup(req.text, features="html.parser") |
| 100 | + |
| 101 | + # break if password field is found on page |
| 102 | + if response.find("input", {"name": "password", "id": "password"}): |
| 103 | + break |
| 104 | + logging.info("Incorrect captcha or another error, trying again...") |
| 105 | + |
| 106 | + state = self._get_state(response) |
| 107 | + |
| 108 | + is_blocked = self._block_handler(response) |
| 109 | + |
| 110 | + if is_blocked: |
| 111 | + logging.warning("We are blocked") |
| 112 | + return |
| 113 | + |
| 114 | + req = session.get(f"https://auth0.openai.com/u/login/password?state={state}") |
| 115 | + response = BeautifulSoup(req.text, features="html.parser") |
| 116 | + state = self._get_state(response) |
| 117 | + |
| 118 | + session.headers.update({ |
| 119 | + "content-type": "application/x-www-form-urlencoded" |
| 120 | + }) |
| 121 | + params = { |
| 122 | + "state": state, |
| 123 | + "username": self.username, |
| 124 | + "password": self.password, |
| 125 | + "action": "default" |
| 126 | + } |
| 127 | + url = f"https://auth0.openai.com/u/login/password?state={state}" |
| 128 | + req = session.post(url, data=params) |
| 129 | + return req.url == "https://chat.openai.com/chat" |
0 commit comments