|
| 1 | +from requests import Session |
| 2 | +from requests import Response, JSONDecodeError |
| 3 | +from urllib.parse import unquote_plus, quote_plus |
| 4 | + |
| 5 | + |
| 6 | +class Authentication: |
| 7 | + |
| 8 | + @staticmethod |
| 9 | + def login(email: str, password: str) -> str: |
| 10 | + |
| 11 | + session: Session = Session() |
| 12 | + |
| 13 | + session.headers["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " \ |
| 14 | + "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" |
| 15 | + |
| 16 | + session.get( |
| 17 | + url="https://chat.openai.com/api/auth/session", |
| 18 | + headers={ |
| 19 | + "accept": "*/*", |
| 20 | + "accept-language": "en-US,en;q=0.9", |
| 21 | + "content-type": "application/x-www-form-urlencoded", |
| 22 | + "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"", |
| 23 | + "sec-ch-ua-mobile": "?0", |
| 24 | + "sec-ch-ua-platform": "\"Windows\"", |
| 25 | + "sec-fetch-dest": "empty", |
| 26 | + "sec-fetch-mode": "cors", |
| 27 | + "sec-fetch-site": "same-origin", |
| 28 | + "Referer": "https://chat.openai.com/auth/login", |
| 29 | + "Referrer-Policy": "strict-origin-when-cross-origin" |
| 30 | + } |
| 31 | + ) |
| 32 | + |
| 33 | + redirect_response: Response = session.post( |
| 34 | + url="https://chat.openai.com/api/auth/signin/auth0?prompt=login", |
| 35 | + headers={ |
| 36 | + "accept": "*/*", |
| 37 | + "accept-language": "en-US,en;q=0.9", |
| 38 | + "content-type": "application/x-www-form-urlencoded", |
| 39 | + "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"", |
| 40 | + "sec-ch-ua-mobile": "?0", |
| 41 | + "sec-ch-ua-platform": "\"Windows\"", |
| 42 | + "sec-fetch-dest": "empty", |
| 43 | + "sec-fetch-mode": "cors", |
| 44 | + "sec-fetch-site": "same-origin", |
| 45 | + "Referer": "https://chat.openai.com/auth/login", |
| 46 | + "Referrer-Policy": "strict-origin-when-cross-origin" |
| 47 | + }, |
| 48 | + data=f'callbackUrl=%2F&csrfToken=' |
| 49 | + f'{unquote_plus(session.cookies.get("__Host-next-auth.csrf-token")).split("|")[0]}' |
| 50 | + f'&json=true' |
| 51 | + ) |
| 52 | + |
| 53 | + try: |
| 54 | + redirect_response_json: dict = redirect_response.json() |
| 55 | + |
| 56 | + if "url" not in redirect_response_json: |
| 57 | + raise Exception("Key 'url' not in response JSON. Most probably it's a CSRF issue with this package.") |
| 58 | + |
| 59 | + # Get State |
| 60 | + state: str = session.get( |
| 61 | + redirect_response_json["url"], |
| 62 | + allow_redirects=False |
| 63 | + ).headers.get("location").split("state=")[-1] |
| 64 | + |
| 65 | + # Load Login Page for Cookies! |
| 66 | + session.get( |
| 67 | + url=f"https://auth0.openai.com/u/login/password?state={state}", |
| 68 | + headers={ |
| 69 | + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9," |
| 70 | + "image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", |
| 71 | + "accept-language": "en-US,en;q=0.9", |
| 72 | + "cache-control": "no-cache", |
| 73 | + "pragma": "no-cache", |
| 74 | + "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"", |
| 75 | + "sec-ch-ua-mobile": "?0", |
| 76 | + "sec-ch-ua-platform": "\"Windows\"", |
| 77 | + "sec-fetch-dest": "document", |
| 78 | + "sec-fetch-mode": "navigate", |
| 79 | + "sec-fetch-site": "same-origin", |
| 80 | + "sec-fetch-user": "?1", |
| 81 | + "upgrade-insecure-requests": "1", |
| 82 | + "Referer": f"https://auth0.openai.com/u/login/identifier?state={state}", |
| 83 | + "Referrer-Policy": "same-origin" |
| 84 | + } |
| 85 | + ) |
| 86 | + |
| 87 | + # Authenticate |
| 88 | + auth_response: Response = session.post( |
| 89 | + url=f"https://auth0.openai.com/u/login/password?state={state}", |
| 90 | + headers={ |
| 91 | + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp," |
| 92 | + "image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", |
| 93 | + "accept-language": "en-US,en;q=0.9", |
| 94 | + "cache-control": "max-age=0", |
| 95 | + "content-type": "application/x-www-form-urlencoded", |
| 96 | + "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"", |
| 97 | + "sec-ch-ua-mobile": "?0", |
| 98 | + "sec-ch-ua-platform": "\"Windows\"", |
| 99 | + "sec-fetch-dest": "document", |
| 100 | + "sec-fetch-mode": "navigate", |
| 101 | + "sec-fetch-site": "same-origin", |
| 102 | + "sec-fetch-user": "?1", |
| 103 | + "upgrade-insecure-requests": "1", |
| 104 | + "Referer": f"https://auth0.openai.com/u/login/password?state={state}", |
| 105 | + "Referrer-Policy": "same-origin" |
| 106 | + }, |
| 107 | + data=f"state={state}&username={quote_plus(email)}&password={quote_plus(password)}", |
| 108 | + allow_redirects=False |
| 109 | + # data={ |
| 110 | + # "state": state, |
| 111 | + # "username": email, |
| 112 | + # "password": password |
| 113 | + # } |
| 114 | + ) |
| 115 | + |
| 116 | + return auth_response.status_code |
| 117 | + |
| 118 | + except JSONDecodeError: |
| 119 | + raise Exception("HTTP response is not a valid JSON. Most probably it's a CSRF issue with this package.") |
0 commit comments