|
| 1 | +import urllib |
| 2 | + |
| 3 | +from urllib.error import HTTPError as HTTPTLSError |
| 4 | +from chatgpt.sessions import HTTPTLSSession |
| 5 | +from chatgpt.utils import random_sleep_time |
| 6 | +from .errors import ChatgptError, ChatgptErrorCodes |
| 7 | + |
| 8 | +class OpenAIAuthentication: |
| 9 | + |
| 10 | + def __init__(self, session: HTTPTLSSession): |
| 11 | + self.session = session |
| 12 | + |
| 13 | + |
| 14 | + def _request_login(self): |
| 15 | + response = self.session.request( |
| 16 | + "GET", "https://chat.openai.com/auth/login") |
| 17 | + |
| 18 | + return response |
| 19 | + |
| 20 | + def _request_providers(self): |
| 21 | + response = self.session.request( |
| 22 | + "GET", "https://chat.openai.com/api/auth/providers") |
| 23 | + return response.json() |
| 24 | + |
| 25 | + def _request_csrf(self): |
| 26 | + response = self.session.request( |
| 27 | + "GET", "https://chat.openai.com/api/auth/csrf",) |
| 28 | + return response.json() |
| 29 | + |
| 30 | + def _request_signin(self, url, csrfToken: dict): |
| 31 | + payload = "callbackUrl=%2F&csrfToken={}&json=true".format(csrfToken) |
| 32 | + response = self.session.request("POST", "{}?prompt=login".format(url), |
| 33 | + headers={ |
| 34 | + "Content-Type": "application/x-www-form-urlencoded", |
| 35 | + "Accept": "*/*" |
| 36 | + }, |
| 37 | + data=payload) |
| 38 | + return response.json() |
| 39 | + |
| 40 | + def _request_authorize(self, url: str): |
| 41 | + try: |
| 42 | + |
| 43 | + self.session._headers = { |
| 44 | + "Upgrade-Insecure-Requests": "1" |
| 45 | + } |
| 46 | + response = self.session.request("GET", url) |
| 47 | + except HTTPTLSError as e: |
| 48 | + if e.code == 302: |
| 49 | + if "state" in str(e): |
| 50 | + return str(e).split("state=")[1].split("\">")[0] |
| 51 | + raise e |
| 52 | + raise HTTPTLSError(url, 200, "Bad authorize request", |
| 53 | + response.headers, None) |
| 54 | + |
| 55 | + |
| 56 | + def _request_login_identifier(self, state: str): |
| 57 | + url = "https://auth0.openai.com/u/login/identifier?state={}".format( |
| 58 | + state) |
| 59 | + response = self.session.request("GET", url) |
| 60 | + if "alt=\"captcha\"" in response.text: |
| 61 | + raise HTTPTLSError( |
| 62 | + url, 400, "Captcha needed", response.headers, None) |
| 63 | + return state |
| 64 | + |
| 65 | + def _request_login_identifier_post(self, state, email): |
| 66 | + try: |
| 67 | + url = "https://auth0.openai.com/u/login/identifier?state={}".format( |
| 68 | + state) |
| 69 | + |
| 70 | + email = urllib.parse.quote_plus(email) |
| 71 | + payload = "action=default&is-brave=false&js-available=false&state={}&username={}&webauthn-available=false&webauthn-platform-available=false".format( |
| 72 | + state, email) |
| 73 | + response = self.session.request("POST", url, data=payload, headers={ |
| 74 | + "Content-Type": "application/x-www-form-urlencoded", |
| 75 | + }) |
| 76 | + |
| 77 | + except HTTPTLSError as e: |
| 78 | + if e.code == 302: |
| 79 | + return state |
| 80 | + else: |
| 81 | + raise e |
| 82 | + raise HTTPTLSError(url, 200, "Bad authorize request", |
| 83 | + response.headers, None) |
| 84 | + |
| 85 | + def _request_login_password(self, state): |
| 86 | + url = "https://auth0.openai.com/u/login/password?state={}".format( |
| 87 | + state) |
| 88 | + self.session.request("GET", url) |
| 89 | + return state |
| 90 | + |
| 91 | + def _request_login_password_post(self, state, email: str, password: str): |
| 92 | + url = "https://auth0.openai.com/u/login/password?state={}".format( |
| 93 | + state) |
| 94 | + email = urllib.parse.quote_plus(email) |
| 95 | + password = urllib.parse.quote_plus(password) |
| 96 | + payload = "action=default&password={}&state={}&username={}".format( |
| 97 | + password, state, email) |
| 98 | + try: |
| 99 | + response = self.session.request("POST", url, data=payload, headers={ |
| 100 | + "Content-Type": "application/x-www-form-urlencoded", |
| 101 | + "Accept": "*/*" |
| 102 | + }) |
| 103 | + except HTTPTLSError as e: |
| 104 | + if e.code == 302: |
| 105 | + return str(e).split("state=")[1].split("\">")[0] |
| 106 | + else: |
| 107 | + raise e |
| 108 | + raise HTTPTLSError(url, 200, "Bad authorize request", |
| 109 | + response.headers, None) |
| 110 | + |
| 111 | + def _request_authorize_access_token(self, state): |
| 112 | + response = self.session.request("GET", "https://auth0.openai.com/authorize/resume?state={}".format( |
| 113 | + state), allow_redirects=True) |
| 114 | + return response.cookies.get("__Secure-next-auth.session-token") |
| 115 | + |
| 116 | + def get_session(self): |
| 117 | + """Get the actual gpt session. |
| 118 | + """ |
| 119 | + try: |
| 120 | + response = self.session.request( |
| 121 | + "GET", "https://chat.openai.com/api/auth/session") |
| 122 | + return response.json() |
| 123 | + except HTTPTLSError as e: |
| 124 | + raise ChatgptError( |
| 125 | + "Error getting the session. You may have a wrong access token; try to login again or insert an access_token yourself.", |
| 126 | + ChatgptErrorCodes.LOGIN_ERROR) from e |
| 127 | + |
| 128 | + def login(self, username, password): |
| 129 | + """Get the access token for chatgpt |
| 130 | +
|
| 131 | + Args: |
| 132 | + username (str): Email for chatgpt |
| 133 | + password (str): Password for chatgpt |
| 134 | + """ |
| 135 | + try: |
| 136 | + self._request_login() |
| 137 | + try: |
| 138 | + chatgpt_session = self.get_session() |
| 139 | + if chatgpt_session: |
| 140 | + return chatgpt_session |
| 141 | + except Exception: |
| 142 | + pass |
| 143 | + providers = self._request_providers() |
| 144 | + csrf = self._request_csrf() |
| 145 | + sigin = self._request_signin( |
| 146 | + providers["auth0"]["signinUrl"], csrf["csrfToken"]) |
| 147 | + state = self._request_authorize(sigin["url"]) |
| 148 | + state = self._request_login_identifier(state) |
| 149 | + state = self._request_login_identifier_post(state, username) |
| 150 | + state = self._request_login_password(state) |
| 151 | + state = self._request_login_password_post(state, username, password) |
| 152 | + self._request_authorize_access_token(state) |
| 153 | + random_sleep_time(1, 2) |
| 154 | + return self.get_session() |
| 155 | + except HTTPTLSError as err: |
| 156 | + raise ChatgptError("Login error. Try again or use interceptor.js as explained in the documentation.", |
| 157 | + ChatgptErrorCodes.LOGIN_ERROR) from err |
0 commit comments