[cli/utils] Skip logout when using Qt/GTK, faster logout on Windows

This commit is contained in:
derrod 2021-10-07 05:10:14 +02:00
parent 20c08aa9a4
commit 96ff42f05a
2 changed files with 47 additions and 33 deletions

View file

@ -163,11 +163,7 @@ class LegendaryCLI:
sid = sid.strip('"') sid = sid.strip('"')
exchange_token = self.core.auth_sid(sid) exchange_token = self.core.auth_sid(sid)
else: else:
def callback(web_sid): if do_webview_login(callback_sid=self.core.auth_sid, callback_code=self.core.auth_code):
exchange_code = self.core.auth_sid(web_sid)
return exchange_code and self.core.auth_code(exchange_code)
if do_webview_login(callback=callback):
logger.info(f'Successfully logged in as "{self.core.lgd.userdata["displayName"]}" via WebView') logger.info(f'Successfully logged in as "{self.core.lgd.userdata["displayName"]}" via WebView')
else: else:
logger.error('WebView login attempt failed, please see log for details.') logger.error('WebView login attempt failed, please see log for details.')

View file

@ -2,6 +2,8 @@ import logging
import json import json
import webbrowser import webbrowser
from legendary import __version__
logger = logging.getLogger('WebViewHelper') logger = logging.getLogger('WebViewHelper')
webview_available = True webview_available = True
@ -17,7 +19,8 @@ except Exception as e:
login_url = 'https://www.epicgames.com/id/login' login_url = 'https://www.epicgames.com/id/login'
sid_url = 'https://www.epicgames.com/id/api/redirect?' sid_url = 'https://www.epicgames.com/id/api/redirect?'
logout_url = 'https://www.epicgames.com/id/logout?productName=epic-games&redirectUrl=https://www.epicgames.com/site/' logout_url = 'https://www.epicgames.com/id/logout?productName=epic-games&redirectUrl=' + login_url
goodbye_url = 'https://legendary.gl/goodbye'
window_js = ''' window_js = '''
window.ue = { window.ue = {
signinprompt: { signinprompt: {
@ -47,11 +50,10 @@ sid_req.send();
class MockLauncher: class MockLauncher:
def __init__(self, callback=None): def __init__(self, callback_sid, callback_code):
self.callback = callback self.callback_sid = callback_sid
self.callback_code = callback_code
self.window = None self.window = None
self.exchange_code = None
self.sid = None
self.inject_js = True self.inject_js = True
self.destroy_on_load = False self.destroy_on_load = False
self.callback_result = None self.callback_result = None
@ -59,17 +61,19 @@ class MockLauncher:
def on_loaded(self): def on_loaded(self):
url = self.window.get_current_url() url = self.window.get_current_url()
logger.debug(f'Loaded url: {url.partition("?")[0]}') logger.debug(f'Loaded url: {url.partition("?")[0]}')
# make sure JS necessary window. stuff is available
if self.destroy_on_load:
logger.info('Closing login window...')
self.window.destroy()
return
# Inject JS so required window.ue stuff is available
if self.inject_js: if self.inject_js:
self.window.evaluate_js(window_js) self.window.evaluate_js(window_js)
if 'logout' in url: if 'logout' in url:
# prepare to close browser after logout redirect # prepare to close browser after logout redirect
self.destroy_on_load = True self.destroy_on_load = True
elif self.destroy_on_load:
# close browser after logout
logger.info('Closing web view...')
self.window.destroy()
def nop(self, *args, **kwargs): def nop(self, *args, **kwargs):
return return
@ -78,37 +82,51 @@ class MockLauncher:
webbrowser.open(url) webbrowser.open(url)
def set_exchange_code(self, exchange_code): def set_exchange_code(self, exchange_code):
self.inject_js = False
logger.debug('Got exchange code (stage 1)!') logger.debug('Got exchange code (stage 1)!')
# we cannot use this exchange code as our login would be invalidated # The default Windows webview retains cookies, GTK/Qt do not. Therefore we can
# after logging out on the website. Hence we do the dance of using # skip logging out on those platforms and directly use the exchange code we're given.
# the SID to create *another* exchange code which will create a session # On windows we have to do a little dance with the SID to create a session that
# that remains valid after logging out. # remains valid after logging out in the embedded browser.
self.exchange_code = exchange_code if self.window.gui.renderer in ('gtkwebkit2', 'qtwebengine', 'qtwebkit'):
self.destroy_on_load = True
try:
self.callback_result = self.callback_code(exchange_code)
except Exception as e:
logger.error(f'Logging in via exchange-code failed with {e!r}')
finally:
# We cannot destroy the browser from here,
# so we'll load a small goodbye site first.
self.window.load_url(goodbye_url)
def trigger_sid_exchange(self, *args, **kwargs): def trigger_sid_exchange(self, *args, **kwargs):
self.inject_js = False # check if code-based login hasn't already set the destroy flag
# first obtain SID, then log out if not self.destroy_on_load:
logger.debug(f'Injecting SID JS')
# inject JS to get SID API response and call our API
self.window.evaluate_js(get_sid_js) self.window.evaluate_js(get_sid_js)
def login_sid(self, sid_json): def login_sid(self, sid_json):
# Try SID login, then log out
try: try:
j = json.loads(sid_json) j = json.loads(sid_json)
self.sid = j['sid'] sid = j['sid']
logger.debug(f'Got SID (stage 2)!') logger.debug(f'Got SID (stage 2)! Executing sid login callback...')
if self.callback: exchange_code = self.callback_sid(sid)
logger.debug(f'Calling login callback...') if exchange_code:
self.callback_result = self.callback(self.sid) self.callback_result = self.callback_code(exchange_code)
except Exception as e: except Exception as e:
logger.error(f'Loading SID response failed with {e!r}') logger.error(f'SID login failed with {e!r}')
finally: finally:
logger.debug('Starting browser logout...') logger.debug('Starting browser logout...')
self.window.load_url(logout_url) self.window.load_url(logout_url)
def do_webview_login(callback=None): def do_webview_login(callback_sid=None, callback_code=None):
api = MockLauncher(callback=callback) api = MockLauncher(callback_sid=callback_sid, callback_code=callback_code)
logger.info('Opening web view with Epic Games Login...') logger.info('Opening Epic Games login window...')
window = webview.create_window('Epic Login', url=login_url, width=1024, height=1024, js_api=api) window = webview.create_window(f'Legendary {__version__} - Epic Games Account Login',
url=login_url, width=768, height=1024, js_api=api)
api.window = window api.window = window
window.loaded += api.on_loaded window.loaded += api.on_loaded
webview.start() webview.start()