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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Credits to github.com/rawandahmad698/PyChatGPT
|
2 |
+
import re
|
3 |
+
import urllib
|
4 |
+
|
5 |
+
import tls_client
|
6 |
+
|
7 |
+
|
8 |
+
class Debugger:
|
9 |
+
def __init__(self, debug: bool = False):
|
10 |
+
if debug:
|
11 |
+
print("Debugger enabled on OpenAIAuth")
|
12 |
+
self.debug = debug
|
13 |
+
|
14 |
+
def set_debug(self, debug: bool):
|
15 |
+
self.debug = debug
|
16 |
+
|
17 |
+
def log(self, message: str, end: str = "\n"):
|
18 |
+
if self.debug:
|
19 |
+
print(message, end=end)
|
20 |
+
|
21 |
+
|
22 |
+
class OpenAIAuth:
|
23 |
+
def __init__(
|
24 |
+
self,
|
25 |
+
email_address: str,
|
26 |
+
password: str,
|
27 |
+
proxy: str = None,
|
28 |
+
debug: bool = False,
|
29 |
+
):
|
30 |
+
self.session_token = None
|
31 |
+
self.email_address = email_address
|
32 |
+
self.password = password
|
33 |
+
self.proxy = proxy
|
34 |
+
self.session = tls_client.Session(
|
35 |
+
client_identifier="chrome_109",
|
36 |
+
)
|
37 |
+
self.access_token: str = None
|
38 |
+
self.debugger = Debugger(debug)
|
39 |
+
self.user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
|
40 |
+
|
41 |
+
@staticmethod
|
42 |
+
def url_encode(string: str) -> str:
|
43 |
+
"""
|
44 |
+
URL encode a string
|
45 |
+
:param string:
|
46 |
+
:return:
|
47 |
+
"""
|
48 |
+
return urllib.parse.quote(string)
|
49 |
+
|
50 |
+
def begin(self) -> None:
|
51 |
+
"""
|
52 |
+
Begin the auth process
|
53 |
+
"""
|
54 |
+
self.debugger.log("Beginning auth process")
|
55 |
+
if not self.email_address or not self.password:
|
56 |
+
return
|
57 |
+
|
58 |
+
if self.proxy:
|
59 |
+
proxies = {
|
60 |
+
"http": self.proxy,
|
61 |
+
"https": self.proxy,
|
62 |
+
}
|
63 |
+
self.session.proxies = proxies
|
64 |
+
|
65 |
+
# First, make a request to https://explorer.api.openai.com/auth/login
|
66 |
+
url = "https://explorer.api.openai.com/"
|
67 |
+
headers = {
|
68 |
+
"Host": "ask.openai.com",
|
69 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
70 |
+
"User-Agent": self.user_agent,
|
71 |
+
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
|
72 |
+
"Accept-Encoding": "gzip, deflate, br",
|
73 |
+
"Connection": "keep-alive",
|
74 |
+
}
|
75 |
+
|
76 |
+
response = self.session.get(
|
77 |
+
url=url,
|
78 |
+
headers=headers,
|
79 |
+
)
|
80 |
+
if response.status_code == 200:
|
81 |
+
self.__part_two()
|
82 |
+
else:
|
83 |
+
self.debugger.log("Error in part one")
|
84 |
+
self.debugger.log("Response: ", end="")
|
85 |
+
self.debugger.log(response.text)
|
86 |
+
self.debugger.log("Status code: ", end="")
|
87 |
+
self.debugger.log(response.status_code)
|
88 |
+
raise Exception("API error")
|
89 |
+
|
90 |
+
def __part_two(self) -> None:
|
91 |
+
"""
|
92 |
+
In part two, We make a request to https://explorer.api.openai.com/api/auth/csrf and grab a fresh csrf token
|
93 |
+
"""
|
94 |
+
self.debugger.log("Beginning part two")
|
95 |
+
|
96 |
+
url = "https://explorer.api.openai.com/api/auth/csrf"
|
97 |
+
headers = {
|
98 |
+
"Host": "ask.openai.com",
|
99 |
+
"Accept": "*/*",
|
100 |
+
"Connection": "keep-alive",
|
101 |
+
"User-Agent": self.user_agent,
|
102 |
+
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
|
103 |
+
"Referer": "https://explorer.api.openai.com/auth/login",
|
104 |
+
"Accept-Encoding": "gzip, deflate, br",
|
105 |
+
}
|
106 |
+
response = self.session.get(
|
107 |
+
url=url,
|
108 |
+
headers=headers,
|
109 |
+
)
|
110 |
+
if response.status_code == 200 and "json" in response.headers["Content-Type"]:
|
111 |
+
csrf_token = response.json()["csrfToken"]
|
112 |
+
self.__part_three(token=csrf_token)
|
113 |
+
else:
|
114 |
+
self.debugger.log("Error in part two")
|
115 |
+
self.debugger.log("Response: ", end="")
|
116 |
+
self.debugger.log(response.text)
|
117 |
+
self.debugger.log("Status code: ", end="")
|
118 |
+
self.debugger.log(response.status_code)
|
119 |
+
raise Exception("Error logging in")
|
120 |
+
|
121 |
+
def __part_three(self, token: str) -> None:
|
122 |
+
"""
|
123 |
+
We reuse the token from part to make a request to /api/auth/signin/auth0?prompt=login
|
124 |
+
"""
|
125 |
+
self.debugger.log("Beginning part three")
|
126 |
+
url = "https://explorer.api.openai.com/api/auth/signin/auth0?prompt=login"
|
127 |
+
payload = f"callbackUrl=%2F&csrfToken={token}&json=true"
|
128 |
+
headers = {
|
129 |
+
"Host": "explorer.api.openai.com",
|
130 |
+
"User-Agent": self.user_agent,
|
131 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
132 |
+
"Accept": "*/*",
|
133 |
+
"Sec-Gpc": "1",
|
134 |
+
"Accept-Language": "en-US,en;q=0.8",
|
135 |
+
"Origin": "https://explorer.api.openai.com",
|
136 |
+
"Sec-Fetch-Site": "same-origin",
|
137 |
+
"Sec-Fetch-Mode": "cors",
|
138 |
+
"Sec-Fetch-Dest": "empty",
|
139 |
+
"Referer": "https://explorer.api.openai.com/auth/login",
|
140 |
+
"Accept-Encoding": "gzip, deflate",
|
141 |
+
}
|
142 |
+
self.debugger.log("Payload: " + payload)
|
143 |
+
self.debugger.log("Payload length: " + str(len(payload)))
|
144 |
+
response = self.session.post(url=url, headers=headers, data=payload)
|
145 |
+
if response.status_code == 200 and "json" in response.headers["Content-Type"]:
|
146 |
+
url = response.json()["url"]
|
147 |
+
if (
|
148 |
+
url
|
149 |
+
== "https://explorer.api.openai.com/api/auth/error?error=OAuthSignin"
|
150 |
+
or "error" in url
|
151 |
+
):
|
152 |
+
self.debugger.log("You have been rate limited")
|
153 |
+
raise Exception("You have been rate limited.")
|
154 |
+
self.__part_four(url=url)
|
155 |
+
else:
|
156 |
+
self.debugger.log("Error in part three")
|
157 |
+
self.debugger.log("Response: ", end="")
|
158 |
+
self.debugger.log("Status code: ", end="")
|
159 |
+
self.debugger.log(response.status_code)
|
160 |
+
self.debugger.log(response.headers)
|
161 |
+
self.debugger.log(self.session.cookies.get_dict())
|
162 |
+
raise Exception("Unknown error")
|
163 |
+
|
164 |
+
def __part_four(self, url: str) -> None:
|
165 |
+
"""
|
166 |
+
We make a GET request to url
|
167 |
+
:param url:
|
168 |
+
:return:
|
169 |
+
"""
|
170 |
+
self.debugger.log("Beginning part four")
|
171 |
+
headers = {
|
172 |
+
"Host": "auth0.openai.com",
|
173 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
174 |
+
"Connection": "keep-alive",
|
175 |
+
"User-Agent": self.user_agent,
|
176 |
+
"Accept-Language": "en-US,en;q=0.9",
|
177 |
+
"Referer": "https://explorer.api.openai.com/",
|
178 |
+
}
|
179 |
+
response = self.session.get(
|
180 |
+
url=url,
|
181 |
+
headers=headers,
|
182 |
+
)
|
183 |
+
if response.status_code == 302:
|
184 |
+
try:
|
185 |
+
state = re.findall(r"state=(.*)", response.text)[0]
|
186 |
+
state = state.split('"')[0]
|
187 |
+
self.__part_five(state=state)
|
188 |
+
except IndexError as exc:
|
189 |
+
self.debugger.log("Error in part four")
|
190 |
+
self.debugger.log("Status code: ", end="")
|
191 |
+
self.debugger.log(response.status_code)
|
192 |
+
self.debugger.log("Rate limit hit")
|
193 |
+
self.debugger.log("Response: " + str(response.text))
|
194 |
+
raise Exception("Rate limit hit") from exc
|
195 |
+
else:
|
196 |
+
self.debugger.log("Error in part four")
|
197 |
+
self.debugger.log("Response: ", end="")
|
198 |
+
self.debugger.log(response.text)
|
199 |
+
self.debugger.log("Status code: ", end="")
|
200 |
+
self.debugger.log(response.status_code)
|
201 |
+
self.debugger.log("Wrong response code")
|
202 |
+
raise Exception("Unknown error")
|
203 |
+
|
204 |
+
def __part_five(self, state: str) -> None:
|
205 |
+
"""
|
206 |
+
We use the state to get the login page & check for a captcha
|
207 |
+
"""
|
208 |
+
self.debugger.log("Beginning part five")
|
209 |
+
url = f"https://auth0.openai.com/u/login/identifier?state={state}"
|
210 |
+
|
211 |
+
headers = {
|
212 |
+
"Host": "auth0.openai.com",
|
213 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
214 |
+
"Connection": "keep-alive",
|
215 |
+
"User-Agent": self.user_agent,
|
216 |
+
"Accept-Language": "en-US,en;q=0.9",
|
217 |
+
"Referer": "https://explorer.api.openai.com/",
|
218 |
+
}
|
219 |
+
response = self.session.get(url, headers=headers)
|
220 |
+
if response.status_code == 200:
|
221 |
+
self.__part_six(state=state)
|
222 |
+
else:
|
223 |
+
self.debugger.log("Error in part five")
|
224 |
+
self.debugger.log("Response: ", end="")
|
225 |
+
self.debugger.log(response.text)
|
226 |
+
self.debugger.log("Status code: ", end="")
|
227 |
+
self.debugger.log(response.status_code)
|
228 |
+
raise ValueError("Invalid response code")
|
229 |
+
|
230 |
+
def __part_six(self, state: str) -> None:
|
231 |
+
"""
|
232 |
+
We make a POST request to the login page with the captcha, email
|
233 |
+
:param state:
|
234 |
+
:return:
|
235 |
+
"""
|
236 |
+
self.debugger.log("Beginning part six")
|
237 |
+
url = f"https://auth0.openai.com/u/login/identifier?state={state}"
|
238 |
+
email_url_encoded = self.url_encode(self.email_address)
|
239 |
+
|
240 |
+
payload = (
|
241 |
+
f"state={state}&username={email_url_encoded}&js-available=false&webauthn-available=true&is"
|
242 |
+
f"-brave=false&webauthn-platform-available=true&action=default "
|
243 |
+
)
|
244 |
+
|
245 |
+
headers = {
|
246 |
+
"Host": "auth0.openai.com",
|
247 |
+
"Origin": "https://auth0.openai.com",
|
248 |
+
"Connection": "keep-alive",
|
249 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
250 |
+
"User-Agent": self.user_agent,
|
251 |
+
"Referer": f"https://auth0.openai.com/u/login/identifier?state={state}",
|
252 |
+
"Accept-Language": "en-US,en;q=0.9",
|
253 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
254 |
+
}
|
255 |
+
response = self.session.post(
|
256 |
+
url,
|
257 |
+
headers=headers,
|
258 |
+
data=payload,
|
259 |
+
)
|
260 |
+
if response.status_code == 302:
|
261 |
+
self.__part_seven(state=state)
|
262 |
+
else:
|
263 |
+
self.debugger.log("Error in part six")
|
264 |
+
self.debugger.log("Response: ", end="")
|
265 |
+
self.debugger.log(response.text)
|
266 |
+
self.debugger.log("Status code: ", end="")
|
267 |
+
self.debugger.log(response.status_code)
|
268 |
+
raise Exception("Unknown error")
|
269 |
+
|
270 |
+
def __part_seven(self, state: str) -> None:
|
271 |
+
"""
|
272 |
+
We enter the password
|
273 |
+
:param state:
|
274 |
+
:return:
|
275 |
+
"""
|
276 |
+
url = f"https://auth0.openai.com/u/login/password?state={state}"
|
277 |
+
self.debugger.log("Beginning part seven")
|
278 |
+
email_url_encoded = self.url_encode(self.email_address)
|
279 |
+
password_url_encoded = self.url_encode(self.password)
|
280 |
+
payload = f"state={state}&username={email_url_encoded}&password={password_url_encoded}&action=default"
|
281 |
+
headers = {
|
282 |
+
"Host": "auth0.openai.com",
|
283 |
+
"Origin": "https://auth0.openai.com",
|
284 |
+
"Connection": "keep-alive",
|
285 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
286 |
+
"User-Agent": self.user_agent,
|
287 |
+
"Referer": f"https://auth0.openai.com/u/login/password?state={state}",
|
288 |
+
"Accept-Language": "en-US,en;q=0.9",
|
289 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
290 |
+
}
|
291 |
+
try:
|
292 |
+
response = self.session.post(
|
293 |
+
url,
|
294 |
+
headers=headers,
|
295 |
+
data=payload,
|
296 |
+
)
|
297 |
+
self.debugger.log("Request went through")
|
298 |
+
except Exception as exc:
|
299 |
+
self.debugger.log("Error in part seven")
|
300 |
+
self.debugger.log("Exception: ", end="")
|
301 |
+
self.debugger.log(exc)
|
302 |
+
raise Exception("Could not get response") from exc
|
303 |
+
if response.status_code == 302:
|
304 |
+
self.debugger.log("Response code is 302")
|
305 |
+
try:
|
306 |
+
new_state = re.findall(r"state=(.*)", response.text)[0]
|
307 |
+
new_state = new_state.split('"')[0]
|
308 |
+
self.debugger.log("New state found")
|
309 |
+
self.__part_eight(old_state=state, new_state=new_state)
|
310 |
+
except Exception as exc:
|
311 |
+
raise Exception("Could not find new state") from exc
|
312 |
+
else:
|
313 |
+
self.debugger.log("Error in part seven")
|
314 |
+
self.debugger.log("Status code: ", end="")
|
315 |
+
self.debugger.log(response.status_code)
|
316 |
+
raise Exception("Wrong status code")
|
317 |
+
|
318 |
+
def __part_eight(self, old_state: str, new_state) -> None:
|
319 |
+
self.debugger.log("Beginning part eight")
|
320 |
+
url = f"https://auth0.openai.com/authorize/resume?state={new_state}"
|
321 |
+
headers = {
|
322 |
+
"Host": "auth0.openai.com",
|
323 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
324 |
+
"Connection": "keep-alive",
|
325 |
+
"User-Agent": self.user_agent,
|
326 |
+
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8",
|
327 |
+
"Referer": f"https://auth0.openai.com/u/login/password?state={old_state}",
|
328 |
+
}
|
329 |
+
response = self.session.get(
|
330 |
+
url,
|
331 |
+
headers=headers,
|
332 |
+
allow_redirects=True,
|
333 |
+
)
|
334 |
+
if response.status_code == 200:
|
335 |
+
self.session_token = response.cookies.get_dict()[
|
336 |
+
"__Secure-next-auth.session-token"
|
337 |
+
]
|
338 |
+
self.get_access_token()
|
339 |
+
|
340 |
+
def get_access_token(self):
|
341 |
+
"""
|
342 |
+
Gets access token
|
343 |
+
"""
|
344 |
+
self.session.cookies.set(
|
345 |
+
"__Secure-next-auth.session-token",
|
346 |
+
self.session_token,
|
347 |
+
)
|
348 |
+
response = self.session.get(
|
349 |
+
"https://explorer.api.openai.com/api/auth/session",
|
350 |
+
)
|
351 |
+
if response.status_code == 200:
|
352 |
+
self.access_token = response.json()["accessToken"]
|
353 |
+
self.debugger.log("Access token found")
|
354 |
+
return self.access_token
|
355 |
+
else:
|
356 |
+
self.debugger.log("Error in part nine")
|
357 |
+
self.debugger.log("Status code: ", end="")
|
358 |
+
self.debugger.log(response.status_code)
|
359 |
+
raise Exception("Wrong status code")
|
OpenAIAuth/__init__.py
ADDED
File without changes
|
OpenAIAuth/__pycache__/OpenAIAuth.cpython-39.pyc
ADDED
Binary file (9.91 kB). View file
|
|
OpenAIAuth/__pycache__/__init__.cpython-39.pyc
ADDED
Binary file (169 Bytes). View file
|
|