[cli] Add --code and --delete options to auth command

--code allows specifying an exchange code directly, e.g.
       when the web authentication is handled by an external
       application.
--delete simply deletes the current authentication data.
This commit is contained in:
derrod 2020-05-12 20:01:25 +02:00
parent 36c1a9591f
commit 0df80773c0

View file

@ -43,12 +43,15 @@ class LegendaryCLI:
return ql return ql
def auth(self, args): def auth(self, args):
if args.auth_delete:
self.core.lgd.invalidate_userdata()
return
try: try:
logger.info('Testing existing login data if present...') logger.info('Testing existing login data if present...')
if self.core.login(): if self.core.login():
logger.info('Stored credentials are still valid, if you wish to switch to a different ' logger.info('Stored credentials are still valid, if you wish to switch to a different '
'account, delete ~/.config/legendary/user.json and try again.') 'account, run "legendary auth --delete" and try again.')
exit(0)
except ValueError: except ValueError:
pass pass
except InvalidCredentialsError: except InvalidCredentialsError:
@ -69,6 +72,7 @@ class LegendaryCLI:
logger.error('No EGS login session found, please login normally.') logger.error('No EGS login session found, please login normally.')
exit(1) exit(1)
if not args.auth_code:
# unfortunately the captcha stuff makes a complete CLI login flow kinda impossible right now... # unfortunately the captcha stuff makes a complete CLI login flow kinda impossible right now...
print('Please login via the epic web login!') print('Please login via the epic web login!')
webbrowser.open( webbrowser.open(
@ -80,6 +84,8 @@ class LegendaryCLI:
'in your web browser after you have finished logging in.') 'in your web browser after you have finished logging in.')
exchange_code = input('Please enter code from JSON response: ') exchange_code = input('Please enter code from JSON response: ')
exchange_token = exchange_code.strip().strip('"') exchange_token = exchange_code.strip().strip('"')
else:
exchange_token = args.auth_code
if self.core.auth_code(exchange_token): if self.core.auth_code(exchange_token):
logger.info(f'Successfully logged in as "{self.core.lgd.userdata["displayName"]}"') logger.info(f'Successfully logged in as "{self.core.lgd.userdata["displayName"]}"')
@ -479,6 +485,10 @@ def main():
if os.name == 'nt': if os.name == 'nt':
auth_parser.add_argument('--import', dest='import_egs_auth', action='store_true', auth_parser.add_argument('--import', dest='import_egs_auth', action='store_true',
help='Import EGS authentication data') help='Import EGS authentication data')
auth_parser.add_argument('--code', dest='auth_code', action='store', metavar='<exchange code>',
help='Use specified exchange code instead of interactive authentication.')
auth_parser.add_argument('--delete', dest='auth_delete', action='store_true',
help='Remove existing authentication data')
install_parser.add_argument('--base-path', dest='base_path', action='store', metavar='<path>', install_parser.add_argument('--base-path', dest='base_path', action='store', metavar='<path>',
help='Path for game installations (defaults to ~/legendary)') help='Path for game installations (defaults to ~/legendary)')