Skip to content

Commit be46a1d

Browse files
version 0.3
1 parent e4aa56c commit be46a1d

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/http_client.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
from galaxy.http import HttpClient
3-
3+
from galaxy.api.errors import InvalidCredentials
44
import aiohttp
55
import logging as log
66
from yarl import URL
@@ -42,7 +42,6 @@ def update_cookies(self, cookies):
4242
def set_auth_lost_callback(self, callback):
4343
self._auth_lost_callback = callback
4444

45-
4645
def get_credentials(self):
4746
creds = {}
4847
creds['cookie_jar'] = pickle.dumps([c for c in self._cookie_jar]).hex()
@@ -53,8 +52,13 @@ async def do_request(self, method, *args, **kwargs):
5352
return await self.request(method, *args, **kwargs)
5453
except Exception as e:
5554
log.warning(f"Request failed with {repr(e)}, attempting to refresh credentials")
56-
#await self.refresh_credentials()
55+
try:
56+
await self.refresh_credentials()
5757
return await self.request(method, *args, **kwargs)
58+
except Exception as e:
59+
log.error(f"Refresh workflow failed with {repr(e)}")
60+
self._auth_lost_callback()
61+
raise InvalidCredentials()
5862

5963
def authenticate_with_cookies(self, cookies):
6064
cookiez = {}
@@ -67,6 +71,15 @@ def authenticate_with_cookies(self, cookies):
6771
self.token = cookiez['SESSION_TOKEN']
6872
self._store_credentials(self.get_credentials())
6973

74+
async def refresh_credentials(self):
75+
data = {'Authorization': f'{{"session":{{"token":"{self.token}"}}}}',
76+
'content-type': 'application/json'}
77+
payload = {}
78+
await self.request('PUT', 'https://api.paradox-interactive.com/accounts/sessions/accounts', headers=data, json=payload)
79+
for cookie in self._cookie_jar:
80+
if cookie['key'] == 'SESSION_TOKEN':
81+
self.token = cookie['value']
82+
self._store_credentials(self.get_credentials())
7083

7184

7285

src/plugin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ async def authenticate(self, stored_credentials=None):
4747
if stored_credentials:
4848
stored_cookies = pickle.loads(bytes.fromhex(stored_credentials['cookie_jar']))
4949
self._http_client.authenticate_with_cookies(stored_cookies)
50-
self.prepare_sku = asyncio.create_task(self.paradox_client.prepare_sku())
50+
self._http_client.set_auth_lost_callback(self.lost_authentication)
5151
acc_id = await self.paradox_client.get_account_id()
52+
self.prepare_sku = asyncio.create_task(self.paradox_client.prepare_sku())
5253
return Authentication(str(acc_id), 'Paradox')
5354
if not stored_credentials:
5455
return NextStep("web_session", AUTH_PARAMS)
5556

5657
async def pass_login_credentials(self, step, credentials, cookies):
5758
self._http_client.authenticate_with_cookies(cookies)
58-
self.prepare_sku = asyncio.create_task(self.paradox_client.prepare_sku())
59+
self._http_client.set_auth_lost_callback(self.lost_authentication)
5960
acc_id = await self.paradox_client.get_account_id()
61+
self.prepare_sku = asyncio.create_task(self.paradox_client.prepare_sku())
6062
return Authentication(str(acc_id), 'Paradox')
6163

6264
async def get_owned_games(self):
@@ -176,10 +178,10 @@ def tick(self):
176178
def shutdown(self):
177179
asyncio.create_task(self._http_client.close())
178180

179-
def prepare_os_compatibility_context(self, game_ids: List[str]) -> Any:
181+
async def prepare_os_compatibility_context(self, game_ids: List[str]) -> Any:
180182
return None
181183

182-
def get_os_compatibility(self, game_id: str, context: Any) -> Optional[OSCompatibility]:
184+
async def get_os_compatibility(self, game_id: str, context: Any) -> Optional[OSCompatibility]:
183185
return OSCompatibility.Windows
184186

185187

src/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1"
1+
__version__ = "0.3"

0 commit comments

Comments
 (0)