From b391286e434b816bdc59e838f13f1ce0128e56de Mon Sep 17 00:00:00 2001 From: zutto Date: Fri, 14 Jul 2023 19:24:20 +0300 Subject: [PATCH] temporary hotfix to ios getting new preauth --- src/OpenAIAuth.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/OpenAIAuth.py b/src/OpenAIAuth.py index 1e0e8b9..77e4ccf 100644 --- a/src/OpenAIAuth.py +++ b/src/OpenAIAuth.py @@ -6,7 +6,6 @@ from datetime import datetime as dt from os import getenv from urllib.parse import urlparse, parse_qs - import requests from certifi import where @@ -66,9 +65,26 @@ def get_access_token(self) -> str: if not self.__check_email(self.email) or not self.password: raise Exception("invalid email or password.") - return self.__part_two() + return self.__part_one() + + # temporary fix with loaned preauth cookie. + def __part_one(self): + #pandora api https://github.com/pengzhile/pandora/blob/f663d6ecce862e0bd1d8be49893c996ddde521dd/src/pandora/exts/config.py + preauth_api = getenv("PREAUTH_API_BASE", 'https://ai-{}.fakeopen.com'.format((dt.now() - datetime.timedelta(days=1)).strftime('%Y%m%d'))) + url = '{}/auth/preauth'.format(preauth_api) + resp = self.session.get(url, allow_redirects=False, **self.req_kwargs) + + if resp.status_code == 200: + json = resp.json() + if 'preauth_cookie' not in json or not json['preauth_cookie']: + raise Exception('Get preauth cookie failed.') + + return self.__part_two(json['preauth_cookie']) + else: + raise Exception('Error request preauth.') + - def __part_two(self) -> str: + def __part_two(self, preauth: str) -> str: code_challenge = "w6n3Ix420Xhhu-Q5-mOOEyuPZmAsJHUbBpO8Ub7xBCY" code_verifier = "yGrXROHx_VazA0uovsxKfE263LMFcrSrdm4SlC-rob8" @@ -77,7 +93,7 @@ def __part_two(self) -> str: "%2Fapi.openai.com%2Fv1&redirect_uri=com.openai.chat%3A%2F%2Fauth0.openai.com%2Fios%2Fcom.openai.chat" "%2Fcallback&scope=openid%20email%20profile%20offline_access%20model.request%20model.read" "%20organization.read%20offline&response_type=code&code_challenge={}" - "&code_challenge_method=S256&prompt=login".format(code_challenge) + "&code_challenge_method=S256&prompt=login&preauth_cookie={}".format(code_challenge, preauth) ) return self.__part_three(code_verifier, url)