From 018862d01246a25adfab88d12a63bc4b79492e4a Mon Sep 17 00:00:00 2001 From: Ross Scroggs Date: Sun, 27 Feb 2022 08:46:41 -0800 Subject: [PATCH] Allow user copy/paste retries in gam oauth create (#1488) --- src/gam/auth/oauth.py | 29 ++++++++++++++++++++++------- src/gam/var.py | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/gam/auth/oauth.py b/src/gam/auth/oauth.py index c3de04cc..24d0638e 100644 --- a/src/gam/auth/oauth.py +++ b/src/gam/auth/oauth.py @@ -20,6 +20,8 @@ import google_auth_oauthlib.flow import google.oauth2.credentials import google.oauth2.id_token +from gam import controlflow +from gam import display from gam import fileutils from gam import transport from gam.var import GM_Globals, GM_WINDOWS @@ -38,6 +40,8 @@ MESSAGE_LOCAL_SERVER_AUTHORIZATION_PROMPT = ('\nYour browser has been opened to' MESSAGE_LOCAL_SERVER_SUCCESS = ('The authentication flow has completed. You may' ' close this browser window and return to GAM.') +MESSAGE_AUTHENTICATION_COMPLETE = ('\nThe authentication flow has completed.\n') + class CredentialsError(Exception): """Base error class.""" @@ -531,7 +535,7 @@ def _localhost_to_ip(): return local_ip def _wait_for_http_client(d): - wsgi_app = google_auth_oauthlib.flow._RedirectWSGIApp('') + wsgi_app = google_auth_oauthlib.flow._RedirectWSGIApp(MESSAGE_LOCAL_SERVER_SUCCESS) wsgiref.simple_server.WSGIServer.allow_reuse_address = False # Convert hostn to IP since apparently binding to the IP # reduces odds of firewall blocking us @@ -606,20 +610,31 @@ class _ShortURLFlow(google_auth_oauthlib.flow.InstalledAppFlow): d['auth_url'], _ = self.authorization_url(**kwargs) print(f"URL is: {d['auth_url']}") user_input.start() + userInput = False while True: sleep(0.1) if not http_client.is_alive(): user_input.terminate() break elif not user_input.is_alive(): + userInput = True http_client.terminate() break - code = d['code'] - if code.startswith('http'): - parsed_url = urlparse(code) - parsed_params = parse_qs(parsed_url.query) - code = parsed_params.get('code', [None])[0] - self.fetch_token(code=code) + while True: + code = d['code'] + if code.startswith('http'): + parsed_url = urlparse(code) + parsed_params = parse_qs(parsed_url.query) + code = parsed_params.get('code', [None])[0] + try: + self.fetch_token(code=code) + break + except Exception as e: + if not userInput: + controlflow.system_error_exit(8, str(e)) + display.print_error(str(e)) + _wait_for_user_input(d) + sys.stdout.write(MESSAGE_AUTHENTICATION_COMPLETE) return self.credentials class _FileLikeThreadLock: diff --git a/src/gam/var.py b/src/gam/var.py index c05845eb..ff921382 100644 --- a/src/gam/var.py +++ b/src/gam/var.py @@ -8,7 +8,7 @@ import platform import re GAM_AUTHOR = 'Jay Lee ' -GAM_VERSION = '6.15' +GAM_VERSION = '6.16' GAM_LICENSE = 'Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)' GAM_URL = 'https://git.io/gam'