Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
temporary hotfix to ios getting new preauth
Browse files Browse the repository at this point in the history
  • Loading branch information
zutto committed Jul 14, 2023
1 parent 5d5759e commit b391286
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/OpenAIAuth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"

Expand All @@ -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)

Expand Down

0 comments on commit b391286

Please sign in to comment.