From f43e51c9311f870e5385e76ffb4bd84f52258b14 Mon Sep 17 00:00:00 2001 From: oldnapalm <38410858+oldnapalm@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:07:51 -0300 Subject: [PATCH] Update zwift_offline.py --- zwift_offline.py | 55 +++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/zwift_offline.py b/zwift_offline.py index 3659922c..b9635ab8 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -768,25 +768,22 @@ def encrypt_credentials(file, cred): try: credentials = (cred[0] + '\n' + cred[1]).encode('UTF-8') cipher_suite = AES.new(credentials_key, AES.MODE_CFB) - ciphered_text = cipher_suite.encrypt(credentials) with open(file, 'wb') as f: f.write(cipher_suite.iv) - f.write(ciphered_text) + f.write(cipher_suite.encrypt(credentials)) flash("Credentials saved.") except Exception as exc: logger.warning('encrypt_credentials: %s' % repr(exc)) flash("Error saving %s" % file) def decrypt_credentials(file): - cred = ('', '') - if os.path.isfile(file): + try: with open(file, 'rb') as f: - iv = f.read(16) - ciphered_text = f.read() - cipher_suite = AES.new(credentials_key, AES.MODE_CFB, iv=iv) - unciphered_text = cipher_suite.decrypt(ciphered_text).decode('UTF-8') - cred = unciphered_text.splitlines() - return((cred[0], cred[1])) + cipher_suite = AES.new(credentials_key, AES.MODE_CFB, iv=f.read(16)) + cred = cipher_suite.decrypt(f.read()).decode('UTF-8').splitlines() + return (cred[0], cred[1]) + except: + return ('', '') @app.route("/profile//", methods=["GET", "POST"]) @@ -2135,18 +2132,16 @@ def garmin_upload(player_id, activity): else: logger.info("garmin_credentials missing, skip Garmin activity update") return - try: - if garmin_credentials.endswith('.bin'): - cred = decrypt_credentials(garmin_credentials) - username = cred[0] - password = cred[1] - else: - with open(garmin_credentials) as f: - username = f.readline().rstrip('\r\n') - password = f.readline().rstrip('\r\n') - except Exception as exc: - logger.warning("Failed to read %s. Skipping Garmin upload attempt: %s" % (garmin_credentials, repr(exc))) - return + if garmin_credentials.endswith('.bin'): + username, password = decrypt_credentials(garmin_credentials) + else: + with open(garmin_credentials) as f: + username = f.readline().rstrip('\r\n') + password = f.readline().rstrip('\r\n') + garmin_domain = '%s/garmin_domain.txt' % STORAGE_DIR + if os.path.exists(garmin_domain): + with open(garmin_domain) as f: + garth.configure(domain=f.readline().rstrip('\r\n')) tokens_dir = '%s/garth' % profile_dir try: garth.resume(tokens_dir) @@ -2187,13 +2182,7 @@ def intervals_upload(player_id, activity): if not os.path.exists(intervals_credentials): logger.info("intervals_credentials.bin missing, skip Intervals.icu activity update") return - try: - cred = decrypt_credentials(intervals_credentials) - athlete_id = cred[0] - api_key = cred[1] - except Exception as exc: - logger.warning("Failed to read %s. Skipping Intervals.icu upload attempt: %s" % (intervals_credentials, repr(exc))) - return + athlete_id, api_key = decrypt_credentials(intervals_credentials) try: from requests.auth import HTTPBasicAuth url = 'http://intervals.icu/api/v1/athlete/%s/activities?name=%s' % (athlete_id, activity.name) @@ -2207,13 +2196,7 @@ def zwift_upload(player_id, activity): if not os.path.exists(zwift_credentials): logger.info("zwift_credentials.bin missing, skip Zwift activity update") return - try: - cred = decrypt_credentials(zwift_credentials) - username = cred[0] - password = cred[1] - except Exception as exc: - logger.warning("Failed to read %s. Skipping Zwift upload attempt: %s" % (zwift_credentials, repr(exc))) - return + username, password = decrypt_credentials(zwift_credentials) try: session = requests.session() access_token, refresh_token = online_sync.login(session, username, password)